fix(worker): send the Write2/WriteSecured2 time argument as box-local time (#228)
MXAccess reads the VT_DATE time argument of Write2, WriteSecured2 and their bulk forms as box-local time and converts it to UTC itself. The worker passed the proto Timestamp's UTC clock fields (the COM marshaler ignores DateTime.Kind), so on vd03 (UTC-5) a Write2 sent at 01:33:00Z was stored at 06:33:00Z. VariantConverter.ConvertToComTimestamp converts a timestamp_value instant with TimeZoneInfo.ConvertTimeFromUtc into the worker's zone (TimeZoneInfo.Local by default, DST included; an injectable zone for tests). The executor uses it for the time argument only: value conversion is unchanged, and the read direction (MXAccess reports UTC; ToTimestamp treats it as UTC) is unchanged. The one residual loss is MXAccess-side: the repeated fall-back hour cannot be told apart in an offset-less VT_DATE. Tests: VariantConverterTimestampTests (UTC-5, US-Eastern DST both edges, UTC+05:30, UTC, fall-back ambiguity, value path unchanged); the four executor forwarding tests inject a fixed UTC-5 zone; the windev live Write2 smoke now sends a past whole-second timestamp and asserts the stored one when applied. Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
@@ -319,11 +319,21 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
|
||||
LogEvent(initialValue);
|
||||
originalValue = initialValue.Value;
|
||||
|
||||
// A whole-second instant 30 s in the past, so a stored source timestamp
|
||||
// can be compared exactly and cannot be confused with the write time.
|
||||
DateTime sentTimestampUtc = DateTime.UtcNow.AddSeconds(-30);
|
||||
sentTimestampUtc = new DateTime(
|
||||
sentTimestampUtc.Ticks - (sentTimestampUtc.Ticks % TimeSpan.TicksPerSecond),
|
||||
DateTimeKind.Utc);
|
||||
output.WriteLine(
|
||||
$"Write2 timestamp sent={sentTimestampUtc:O} box zone={TimeZoneInfo.Local.Id} offset={TimeZoneInfo.Local.GetUtcOffset(sentTimestampUtc)}");
|
||||
|
||||
MxCommandReply writeReply = await fixture.Service.Invoke(
|
||||
CreateWrite2Request(
|
||||
sessionId,
|
||||
registerReply.Register.ServerHandle,
|
||||
addItemReply.AddItem.ItemHandle),
|
||||
addItemReply.AddItem.ItemHandle,
|
||||
sentTimestampUtc),
|
||||
new TestServerCallContext()).ConfigureAwait(false);
|
||||
LogReply("Write2", writeReply);
|
||||
|
||||
@@ -362,6 +372,34 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
|
||||
output.WriteLine($"Write2 completion category={status.Category} detail={status.Detail}");
|
||||
}
|
||||
|
||||
// #228: MXAccess reads the Write2 time argument as box-local time, so
|
||||
// the worker must convert the UTC instant before the COM call. When the
|
||||
// provider applied the write, the source timestamp it reports must be
|
||||
// the instant sent. On a UTC box this cannot fail either way, so the
|
||||
// zone is logged above. A refused write (no data change) is logged,
|
||||
// not asserted, as before.
|
||||
bool applied = writeComplete.Statuses.Count > 0
|
||||
&& writeComplete.Statuses.All(status => status.Category == MxStatusCategory.Ok);
|
||||
if (applied)
|
||||
{
|
||||
MxEvent writtenValue = await eventWriter
|
||||
.WaitForMessageAsync(
|
||||
candidate => candidate.Family == MxEventFamily.OnDataChange
|
||||
&& candidate.ItemHandle == addItemReply.AddItem.ItemHandle
|
||||
&& candidate.WorkerSequence > initialValue.WorkerSequence,
|
||||
IntegrationTestEnvironment.LiveMxAccessEventTimeout,
|
||||
streamCancellation.Token)
|
||||
.ConfigureAwait(false);
|
||||
LogEvent(writtenValue);
|
||||
DateTime? storedTimestampUtc = writtenValue.SourceTimestamp?.ToDateTime();
|
||||
output.WriteLine($"Write2 timestamp stored={storedTimestampUtc:O}");
|
||||
Assert.Equal(sentTimestampUtc, storedTimestampUtc);
|
||||
}
|
||||
else
|
||||
{
|
||||
output.WriteLine("Write2 timestamp not checked: the completion was not Ok, so nothing was applied.");
|
||||
}
|
||||
|
||||
// Restore the original value with a plain Write on the same supervisory advise.
|
||||
MxCommandReply restoreReply = await fixture.Service.Invoke(
|
||||
CreateWriteRequest(
|
||||
@@ -1666,7 +1704,8 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
|
||||
private static MxCommandRequest CreateWrite2Request(
|
||||
string sessionId,
|
||||
int serverHandle,
|
||||
int itemHandle)
|
||||
int itemHandle,
|
||||
DateTime timestampUtc)
|
||||
{
|
||||
return new MxCommandRequest
|
||||
{
|
||||
@@ -1688,7 +1727,7 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
|
||||
{
|
||||
DataType = MxDataType.Time,
|
||||
VariantType = "VT_DATE",
|
||||
TimestampValue = Timestamp.FromDateTime(DateTime.UtcNow),
|
||||
TimestampValue = Timestamp.FromDateTime(timestampUtc),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
||||
using ZB.MOM.WW.MxGateway.Worker.Conversion;
|
||||
using ZB.MOM.WW.MxGateway.Worker.Tests.TestSupport;
|
||||
using ProtobufTimestamp = Google.Protobuf.WellKnownTypes.Timestamp;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Tests.Conversion;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for the Write2/WriteSecured2 time-argument conversion (#228): MXAccess
|
||||
/// reads the <c>VT_DATE</c> time argument as box-local time, so the worker must
|
||||
/// hand it the UTC instant as local wall-clock time. Every zone is injected, so
|
||||
/// the results do not depend on the zone of the machine running the tests.
|
||||
/// </summary>
|
||||
public sealed class VariantConverterTimestampTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verifies the UTC-to-local conversion across zones and DST boundaries.
|
||||
/// The first row is the live vd03 case from #228: sent 01:33:00Z, which
|
||||
/// MXAccess stored as 06:33:00Z before the fix.
|
||||
/// </summary>
|
||||
/// <param name="zone">Zone name in <see cref="ResolveZone(string)"/>.</param>
|
||||
/// <param name="utc">UTC instant sent on the wire (ISO 8601).</param>
|
||||
/// <param name="expectedLocal">Expected local wall-clock time handed to MXAccess.</param>
|
||||
[Theory]
|
||||
// UTC-5 in November (EST): the #228 live case.
|
||||
[InlineData("Eastern", "2026-11-13T01:33:00", "2026-11-12T20:33:00")]
|
||||
// UTC-4 in July (EDT).
|
||||
[InlineData("Eastern", "2026-07-15T12:00:00", "2026-07-15T08:00:00")]
|
||||
// Spring forward (2026-03-08 07:00Z): the last EST second, then the first EDT
|
||||
// instant. Local 02:xx does not exist that night and is never produced.
|
||||
[InlineData("Eastern", "2026-03-08T06:59:59", "2026-03-08T01:59:59")]
|
||||
[InlineData("Eastern", "2026-03-08T07:00:00", "2026-03-08T03:00:00")]
|
||||
// Fall back (2026-11-01 06:00Z): the last EDT second, then the first EST instant.
|
||||
[InlineData("Eastern", "2026-11-01T05:59:59", "2026-11-01T01:59:59")]
|
||||
[InlineData("Eastern", "2026-11-01T06:00:00", "2026-11-01T01:00:00")]
|
||||
// Fixed negative and positive (non-whole-hour) offsets.
|
||||
[InlineData("UTC-5", "2026-11-13T01:33:00", "2026-11-12T20:33:00")]
|
||||
[InlineData("UTC+05:30", "2026-11-13T01:33:00", "2026-11-13T07:03:00")]
|
||||
[InlineData("UTC+05:30", "2026-12-31T20:00:00", "2027-01-01T01:30:00")]
|
||||
// A UTC box: the conversion is the identity, which is why #228 cannot be seen there.
|
||||
[InlineData("UTC", "2026-11-13T01:33:00", "2026-11-13T01:33:00")]
|
||||
public void ConvertToComTimestamp_WithTimestampValue_ReturnsBoxLocalWallClockTime(
|
||||
string zone,
|
||||
string utc,
|
||||
string expectedLocal)
|
||||
{
|
||||
VariantConverter converter = new(ResolveZone(zone));
|
||||
|
||||
object? result = converter.ConvertToComTimestamp(CreateTimestampValue(ParseUtc(utc)));
|
||||
|
||||
DateTime local = Assert.IsType<DateTime>(result);
|
||||
Assert.Equal(ParseUnspecified(expectedLocal), DateTime.SpecifyKind(local, DateTimeKind.Unspecified));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the documented fall-back ambiguity: two UTC instants an hour
|
||||
/// apart become the same local wall-clock time, which the offset-less
|
||||
/// <c>VT_DATE</c> cannot tell apart. The conversion itself is exact; the
|
||||
/// 1 h loss is on the MXAccess side.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ConvertToComTimestamp_InRepeatedFallBackHour_MapsTwoInstantsToOneWallClockTime()
|
||||
{
|
||||
VariantConverter converter = new(TestTimeZones.Eastern);
|
||||
|
||||
DateTime daylight = Assert.IsType<DateTime>(
|
||||
converter.ConvertToComTimestamp(CreateTimestampValue(ParseUtc("2026-11-01T05:30:00"))));
|
||||
DateTime standard = Assert.IsType<DateTime>(
|
||||
converter.ConvertToComTimestamp(CreateTimestampValue(ParseUtc("2026-11-01T06:30:00"))));
|
||||
|
||||
Assert.Equal(ParseUnspecified("2026-11-01T01:30:00"), DateTime.SpecifyKind(daylight, DateTimeKind.Unspecified));
|
||||
Assert.Equal(daylight.Ticks, standard.Ticks);
|
||||
Assert.True(TestTimeZones.Eastern.IsAmbiguousTime(DateTime.SpecifyKind(daylight, DateTimeKind.Unspecified)));
|
||||
}
|
||||
|
||||
/// <summary>Verifies the default converter uses the worker box's own zone.</summary>
|
||||
[Fact]
|
||||
public void ConvertToComTimestamp_WithDefaultConverter_UsesTimeZoneInfoLocal()
|
||||
{
|
||||
DateTime utc = ParseUtc("2026-11-13T01:33:00");
|
||||
|
||||
object? result = new VariantConverter().ConvertToComTimestamp(CreateTimestampValue(utc));
|
||||
|
||||
Assert.Equal(TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local), Assert.IsType<DateTime>(result));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the conversion is limited to the time argument: a timestamp
|
||||
/// written as an attribute value is unchanged, because whether a
|
||||
/// <c>Time</c>-typed value write follows the same local-time rule is unproven.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ConvertToComValue_WithTimestampValue_StaysUtcEvenWithZoneInjected()
|
||||
{
|
||||
DateTime utc = ParseUtc("2026-11-13T01:33:00");
|
||||
VariantConverter converter = new(TestTimeZones.FixedUtcMinus5);
|
||||
|
||||
object? result = converter.ConvertToComValue(CreateTimestampValue(utc));
|
||||
|
||||
Assert.Equal(utc, Assert.IsType<DateTime>(result));
|
||||
}
|
||||
|
||||
/// <summary>Verifies a non-timestamp time argument carries no zone and passes through unchanged.</summary>
|
||||
[Fact]
|
||||
public void ConvertToComTimestamp_WithStringValue_PassesThroughUnchanged()
|
||||
{
|
||||
VariantConverter converter = new(TestTimeZones.FixedUtcMinus5);
|
||||
|
||||
object? result = converter.ConvertToComTimestamp(new MxValue { StringValue = "2026-11-13 01:33:00" });
|
||||
|
||||
Assert.Equal("2026-11-13 01:33:00", Assert.IsType<string>(result));
|
||||
}
|
||||
|
||||
/// <summary>Verifies an MXAccess-null time argument stays a CLR null.</summary>
|
||||
[Fact]
|
||||
public void ConvertToComTimestamp_WithNull_ReturnsNull()
|
||||
{
|
||||
VariantConverter converter = new(TestTimeZones.FixedUtcMinus5);
|
||||
|
||||
Assert.Null(converter.ConvertToComTimestamp(new MxValue { IsNull = true }));
|
||||
}
|
||||
|
||||
/// <summary>Verifies an unspecified-kind input is treated as UTC, the rule the proto timestamp follows.</summary>
|
||||
[Fact]
|
||||
public void ToMxAccessLocalTime_WithUnspecifiedKind_TreatsInputAsUtc()
|
||||
{
|
||||
VariantConverter converter = new(TestTimeZones.FixedUtcMinus5);
|
||||
|
||||
DateTime result = converter.ToMxAccessLocalTime(ParseUnspecified("2026-11-13T01:33:00"));
|
||||
|
||||
Assert.Equal(ParseUnspecified("2026-11-12T20:33:00"), DateTime.SpecifyKind(result, DateTimeKind.Unspecified));
|
||||
}
|
||||
|
||||
private static TimeZoneInfo ResolveZone(string zone) => zone switch
|
||||
{
|
||||
"Eastern" => TestTimeZones.Eastern,
|
||||
"UTC-5" => TestTimeZones.FixedUtcMinus5,
|
||||
"UTC+05:30" => TestTimeZones.FixedUtcPlus0530,
|
||||
"UTC" => TimeZoneInfo.Utc,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(zone), zone, "Unknown test zone."),
|
||||
};
|
||||
|
||||
private static MxValue CreateTimestampValue(DateTime utc) => new()
|
||||
{
|
||||
DataType = MxDataType.Time,
|
||||
VariantType = "VT_DATE",
|
||||
TimestampValue = ProtobufTimestamp.FromDateTime(utc),
|
||||
};
|
||||
|
||||
private static DateTime ParseUtc(string value) => DateTime.SpecifyKind(ParseUnspecified(value), DateTimeKind.Utc);
|
||||
|
||||
private static DateTime ParseUnspecified(string value) =>
|
||||
DateTime.ParseExact(value, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None);
|
||||
}
|
||||
@@ -554,6 +554,9 @@ public sealed class MxAccessCommandExecutorTests
|
||||
FakeMxAccessComObjectFactory factory = new(fakeComObject);
|
||||
using StaRuntime runtime = CreateRuntime();
|
||||
using MxAccessStaSession session = new(runtime, factory, new NoopEventSink());
|
||||
// #228: MXAccess reads the time argument as box-local time. Inject a
|
||||
// fixed UTC-5 zone so the expected shift does not depend on this machine.
|
||||
session.MxAccessLocalTimeZone = TestTimeZones.FixedUtcMinus5;
|
||||
await session.StartAsync(workerProcessId: 1234);
|
||||
DateTime timestamp = new(2026, 5, 19, 12, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
@@ -567,7 +570,7 @@ public sealed class MxAccessCommandExecutorTests
|
||||
Assert.True(result.WasSuccessful);
|
||||
Assert.Equal(811, result.ItemHandle);
|
||||
Assert.Equal(100, fakeComObject.WriteValue);
|
||||
Assert.Equal(timestamp, fakeComObject.WriteTimestamp);
|
||||
Assert.Equal(timestamp.AddHours(-5).Ticks, Assert.IsType<DateTime>(fakeComObject.WriteTimestamp).Ticks);
|
||||
Assert.Equal(7, fakeComObject.WriteUserId);
|
||||
Assert.Contains("Write2:81:811", fakeComObject.OperationNames);
|
||||
}
|
||||
@@ -606,6 +609,9 @@ public sealed class MxAccessCommandExecutorTests
|
||||
FakeMxAccessComObjectFactory factory = new(fakeComObject);
|
||||
using StaRuntime runtime = CreateRuntime();
|
||||
using MxAccessStaSession session = new(runtime, factory, new NoopEventSink());
|
||||
// #228: MXAccess reads the time argument as box-local time. Inject a
|
||||
// fixed UTC-5 zone so the expected shift does not depend on this machine.
|
||||
session.MxAccessLocalTimeZone = TestTimeZones.FixedUtcMinus5;
|
||||
await session.StartAsync(workerProcessId: 1234);
|
||||
DateTime timestamp = new(2026, 5, 19, 13, 30, 0, DateTimeKind.Utc);
|
||||
|
||||
@@ -620,7 +626,7 @@ public sealed class MxAccessCommandExecutorTests
|
||||
Assert.Equal(33, fakeComObject.WriteCurrentUserId);
|
||||
Assert.Equal(44, fakeComObject.WriteVerifierUserId);
|
||||
Assert.Equal(999, fakeComObject.WriteValue);
|
||||
Assert.Equal(timestamp, fakeComObject.WriteTimestamp);
|
||||
Assert.Equal(timestamp.AddHours(-5).Ticks, Assert.IsType<DateTime>(fakeComObject.WriteTimestamp).Ticks);
|
||||
Assert.Contains("WriteSecured2:83:831", fakeComObject.OperationNames);
|
||||
}
|
||||
|
||||
@@ -1027,6 +1033,9 @@ public sealed class MxAccessCommandExecutorTests
|
||||
using MxAccessStaSession session = new(runtime, factory, new NoopEventSink());
|
||||
// Same rationale as the Write forwarding test above.
|
||||
session.WriteCompletionTimeout = TimeSpan.Zero;
|
||||
// #228: MXAccess reads the time argument as box-local time. Inject a
|
||||
// fixed UTC-5 zone so the expected shift does not depend on this machine.
|
||||
session.MxAccessLocalTimeZone = TestTimeZones.FixedUtcMinus5;
|
||||
await session.StartAsync(workerProcessId: 1234);
|
||||
DateTime timestamp = new(2026, 5, 19, 12, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
@@ -1037,7 +1046,7 @@ public sealed class MxAccessCommandExecutorTests
|
||||
Assert.Equal(MxCommandKind.Write2, reply.Kind);
|
||||
Assert.Equal(710, fakeComObject.WriteItemHandle);
|
||||
Assert.Equal(456, fakeComObject.WriteValue);
|
||||
Assert.Equal(timestamp, fakeComObject.WriteTimestamp);
|
||||
Assert.Equal(timestamp.AddHours(-5).Ticks, Assert.IsType<DateTime>(fakeComObject.WriteTimestamp).Ticks);
|
||||
Assert.Equal(6, fakeComObject.WriteUserId);
|
||||
}
|
||||
|
||||
@@ -1077,6 +1086,9 @@ public sealed class MxAccessCommandExecutorTests
|
||||
using MxAccessStaSession session = new(runtime, factory, new NoopEventSink());
|
||||
// Same rationale as the WriteSecured forwarding test above.
|
||||
session.WriteCompletionTimeout = TimeSpan.Zero;
|
||||
// #228: MXAccess reads the time argument as box-local time. Inject a
|
||||
// fixed UTC-5 zone so the expected shift does not depend on this machine.
|
||||
session.MxAccessLocalTimeZone = TestTimeZones.FixedUtcMinus5;
|
||||
await session.StartAsync(workerProcessId: 1234);
|
||||
DateTime timestamp = new(2026, 5, 19, 13, 30, 0, DateTimeKind.Utc);
|
||||
|
||||
@@ -1087,7 +1099,7 @@ public sealed class MxAccessCommandExecutorTests
|
||||
Assert.Equal(ProtocolStatusCode.Ok, reply.ProtocolStatus.Code);
|
||||
Assert.Equal(MxCommandKind.WriteSecured2, reply.Kind);
|
||||
Assert.Equal(1011, fakeComObject.WriteValue);
|
||||
Assert.Equal(timestamp, fakeComObject.WriteTimestamp);
|
||||
Assert.Equal(timestamp.AddHours(-5).Ticks, Assert.IsType<DateTime>(fakeComObject.WriteTimestamp).Ticks);
|
||||
Assert.Equal(33, fakeComObject.WriteCurrentUserId);
|
||||
Assert.Equal(44, fakeComObject.WriteVerifierUserId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Worker.Tests.TestSupport;
|
||||
|
||||
/// <summary>
|
||||
/// Custom time zones built in code, so tests of the #228 Write2 time-argument
|
||||
/// conversion behave the same on every machine whatever its own zone or its
|
||||
/// registry time-zone data.
|
||||
/// </summary>
|
||||
internal static class TestTimeZones
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a US-Eastern-shaped zone: UTC-5 standard, UTC-4 daylight, DST from
|
||||
/// the second Sunday of March 02:00 to the first Sunday of November 02:00
|
||||
/// (the vd03 zone). In 2026 DST starts at 2026-03-08T07:00Z and ends at
|
||||
/// 2026-11-01T06:00Z.
|
||||
/// </summary>
|
||||
public static TimeZoneInfo Eastern { get; } = CreateEastern();
|
||||
|
||||
/// <summary>Gets a fixed UTC-5 zone with no DST.</summary>
|
||||
public static TimeZoneInfo FixedUtcMinus5 { get; } = TimeZoneInfo.CreateCustomTimeZone(
|
||||
"Test UTC-5", TimeSpan.FromHours(-5), "Test UTC-5", "Test UTC-5");
|
||||
|
||||
/// <summary>Gets a fixed UTC+05:30 zone with no DST (a positive, non-whole-hour offset).</summary>
|
||||
public static TimeZoneInfo FixedUtcPlus0530 { get; } = TimeZoneInfo.CreateCustomTimeZone(
|
||||
"Test UTC+05:30", new TimeSpan(5, 30, 0), "Test UTC+05:30", "Test UTC+05:30");
|
||||
|
||||
private static TimeZoneInfo CreateEastern()
|
||||
{
|
||||
TimeZoneInfo.TransitionTime dstStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(
|
||||
new DateTime(1, 1, 1, 2, 0, 0), month: 3, week: 2, DayOfWeek.Sunday);
|
||||
TimeZoneInfo.TransitionTime dstEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(
|
||||
new DateTime(1, 1, 1, 2, 0, 0), month: 11, week: 1, DayOfWeek.Sunday);
|
||||
TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(
|
||||
new DateTime(2007, 1, 1), DateTime.MaxValue.Date, TimeSpan.FromHours(1), dstStart, dstEnd);
|
||||
return TimeZoneInfo.CreateCustomTimeZone(
|
||||
"Test Eastern",
|
||||
TimeSpan.FromHours(-5),
|
||||
"Test Eastern",
|
||||
"Test Eastern Standard",
|
||||
"Test Eastern Daylight",
|
||||
new[] { rule });
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,31 @@ namespace ZB.MOM.WW.MxGateway.Worker.Conversion;
|
||||
|
||||
public sealed class VariantConverter
|
||||
{
|
||||
private readonly TimeZoneInfo? mxAccessLocalTimeZone;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a converter that renders timestamped-write time arguments in
|
||||
/// the worker box's local zone (<see cref="TimeZoneInfo.Local"/>).
|
||||
/// </summary>
|
||||
public VariantConverter()
|
||||
: this(mxAccessLocalTimeZone: null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a converter with an explicit zone for the timestamped-write
|
||||
/// time argument. Tests inject a fixed zone so they do not depend on the
|
||||
/// zone of the machine that runs them.
|
||||
/// </summary>
|
||||
/// <param name="mxAccessLocalTimeZone">
|
||||
/// Zone MXAccess reads the <c>Write2</c>/<c>WriteSecured2</c> time argument in,
|
||||
/// or <see langword="null"/> for <see cref="TimeZoneInfo.Local"/> resolved per call.
|
||||
/// </param>
|
||||
public VariantConverter(TimeZoneInfo? mxAccessLocalTimeZone)
|
||||
{
|
||||
this.mxAccessLocalTimeZone = mxAccessLocalTimeZone;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an object value to an MxValue without a specified data type.
|
||||
/// </summary>
|
||||
@@ -157,6 +182,69 @@ public sealed class VariantConverter
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the time argument of a timestamped write (<c>Write2</c>,
|
||||
/// <c>WriteSecured2</c> and their bulk forms) into the CLR object MXAccess
|
||||
/// expects. Unlike <see cref="ConvertToComValue(MxValue)"/>, a
|
||||
/// <c>timestamp_value</c> instant is rendered as box-local wall-clock time.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// MXAccess reads the <c>VT_DATE</c> time argument as local time on the worker
|
||||
/// box and converts it to UTC itself, while a <c>VT_DATE</c> carries no
|
||||
/// offset and the COM marshaler ignores <see cref="DateTime.Kind"/>. Passing
|
||||
/// the UTC clock fields therefore stored the source timestamp shifted by the
|
||||
/// box's UTC offset (+5 h on a UTC-5 box, #228). The read direction is the
|
||||
/// opposite and stays as it is: MXAccess hands OnDataChange timestamps over
|
||||
/// as UTC, and <c>ToTimestamp</c> treats them as UTC.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The UTC-to-local step uses <see cref="TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo)"/>
|
||||
/// with the zone's DST rules, so it is exact and never yields an invalid
|
||||
/// local time. The one residual loss is on the MXAccess side: in the hour
|
||||
/// that repeats when DST ends, two UTC instants share one local wall-clock
|
||||
/// time, and the offset-less <c>VT_DATE</c> cannot say which one is meant,
|
||||
/// so MXAccess may store an instant from that hour 1 h off. Clients that
|
||||
/// need exact timestamps across the fall-back hour should run the worker
|
||||
/// box on UTC.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Only the <c>timestamp_value</c> kind is converted. Any other kind (a
|
||||
/// string or a number) carries no zone, so it is passed through unchanged
|
||||
/// as before and its meaning is the caller's.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="timestamp">Protobuf time argument of a timestamped write.</param>
|
||||
/// <returns>A COM-marshalable value for the MXAccess time argument.</returns>
|
||||
public object? ConvertToComTimestamp(MxValue timestamp)
|
||||
{
|
||||
if (timestamp is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(timestamp));
|
||||
}
|
||||
|
||||
if (!timestamp.IsNull && timestamp.KindCase == MxValue.KindOneofCase.TimestampValue)
|
||||
{
|
||||
return ToMxAccessLocalTime(timestamp.TimestampValue.ToDateTime());
|
||||
}
|
||||
|
||||
return ConvertToComValue(timestamp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a UTC instant to the wall-clock time MXAccess expects for a
|
||||
/// timestamped-write time argument. See <see cref="ConvertToComTimestamp(MxValue)"/>.
|
||||
/// </summary>
|
||||
/// <param name="utcDateTime">UTC instant (<see cref="DateTimeKind.Utc"/> or unspecified).</param>
|
||||
/// <returns>The same instant as wall-clock time in the MXAccess zone.</returns>
|
||||
public DateTime ToMxAccessLocalTime(DateTime utcDateTime)
|
||||
{
|
||||
DateTime utc = utcDateTime.Kind == DateTimeKind.Local
|
||||
? utcDateTime.ToUniversalTime()
|
||||
: DateTime.SpecifyKind(utcDateTime, DateTimeKind.Utc);
|
||||
return TimeZoneInfo.ConvertTimeFromUtc(utc, mxAccessLocalTimeZone ?? TimeZoneInfo.Local);
|
||||
}
|
||||
|
||||
private static Array ConvertToComArray(MxArray array)
|
||||
{
|
||||
return array.ValuesCase switch
|
||||
|
||||
@@ -497,7 +497,7 @@ public sealed class MxAccessCommandExecutor : IStaCommandExecutor
|
||||
write2Command.ServerHandle,
|
||||
write2Command.ItemHandle,
|
||||
variantConverter.ConvertToComValue(write2Command.Value),
|
||||
variantConverter.ConvertToComValue(write2Command.TimestampValue),
|
||||
variantConverter.ConvertToComTimestamp(write2Command.TimestampValue),
|
||||
write2Command.UserId);
|
||||
|
||||
MxCommandReply reply = CreateOkReply(command);
|
||||
@@ -580,7 +580,7 @@ public sealed class MxAccessCommandExecutor : IStaCommandExecutor
|
||||
writeSecured2Command.CurrentUserId,
|
||||
writeSecured2Command.VerifierUserId,
|
||||
variantConverter.ConvertToComValue(writeSecured2Command.Value),
|
||||
variantConverter.ConvertToComValue(writeSecured2Command.TimestampValue));
|
||||
variantConverter.ConvertToComTimestamp(writeSecured2Command.TimestampValue));
|
||||
|
||||
MxCommandReply reply = CreateOkReply(command);
|
||||
AwaitWriteCompletion(
|
||||
@@ -726,7 +726,8 @@ public sealed class MxAccessCommandExecutor : IStaCommandExecutor
|
||||
session.Write2Bulk(
|
||||
write2BulkCommand.ServerHandle,
|
||||
write2BulkCommand.Entries,
|
||||
value => variantConverter.ConvertToComValue(value)));
|
||||
value => variantConverter.ConvertToComValue(value),
|
||||
timestamp => variantConverter.ConvertToComTimestamp(timestamp)));
|
||||
}
|
||||
|
||||
private MxCommandReply ExecuteWriteSecuredBulk(StaCommand command)
|
||||
@@ -785,7 +786,8 @@ public sealed class MxAccessCommandExecutor : IStaCommandExecutor
|
||||
session.WriteSecured2Bulk(
|
||||
writeSecured2BulkCommand.ServerHandle,
|
||||
writeSecured2BulkCommand.Entries,
|
||||
value => variantConverter.ConvertToComValue(value)));
|
||||
value => variantConverter.ConvertToComValue(value),
|
||||
timestamp => variantConverter.ConvertToComTimestamp(timestamp)));
|
||||
}
|
||||
|
||||
private MxCommandReply ExecuteReadBulk(StaCommand command)
|
||||
|
||||
@@ -793,11 +793,16 @@ public sealed class MxAccessSession : IDisposable
|
||||
/// <param name="serverHandle">The MXAccess server handle.</param>
|
||||
/// <param name="entries">The write2 entries to process.</param>
|
||||
/// <param name="convertValue">Converts protobuf MxValue to COM-compatible variant.</param>
|
||||
/// <param name="convertTimestamp">
|
||||
/// Converts the per-entry time argument; it differs from
|
||||
/// <paramref name="convertValue"/> because MXAccess reads the time argument as box-local time (#228).
|
||||
/// </param>
|
||||
/// <returns>The per-entry write results, in the order the entries were provided.</returns>
|
||||
public IReadOnlyList<BulkWriteResult> Write2Bulk(
|
||||
int serverHandle,
|
||||
IReadOnlyList<Write2BulkEntry> entries,
|
||||
Func<MxValue, object?> convertValue)
|
||||
Func<MxValue, object?> convertValue,
|
||||
Func<MxValue, object?> convertTimestamp)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (entries is null)
|
||||
@@ -810,6 +815,11 @@ public sealed class MxAccessSession : IDisposable
|
||||
throw new ArgumentNullException(nameof(convertValue));
|
||||
}
|
||||
|
||||
if (convertTimestamp is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(convertTimestamp));
|
||||
}
|
||||
|
||||
List<BulkWriteResult> results = new(entries.Count);
|
||||
foreach (Write2BulkEntry entry in entries)
|
||||
{
|
||||
@@ -820,7 +830,7 @@ public sealed class MxAccessSession : IDisposable
|
||||
serverHandle,
|
||||
entry.ItemHandle,
|
||||
convertValue(entry.Value),
|
||||
convertValue(entry.TimestampValue),
|
||||
convertTimestamp(entry.TimestampValue),
|
||||
entry.UserId)));
|
||||
}
|
||||
|
||||
@@ -869,11 +879,16 @@ public sealed class MxAccessSession : IDisposable
|
||||
/// <param name="serverHandle">The MXAccess server handle.</param>
|
||||
/// <param name="entries">The WriteSecured2 entries to process.</param>
|
||||
/// <param name="convertValue">Converts protobuf MxValue to COM-compatible variant.</param>
|
||||
/// <param name="convertTimestamp">
|
||||
/// Converts the per-entry time argument; it differs from
|
||||
/// <paramref name="convertValue"/> because MXAccess reads the time argument as box-local time (#228).
|
||||
/// </param>
|
||||
/// <returns>The per-entry write results, in the order the entries were provided.</returns>
|
||||
public IReadOnlyList<BulkWriteResult> WriteSecured2Bulk(
|
||||
int serverHandle,
|
||||
IReadOnlyList<WriteSecured2BulkEntry> entries,
|
||||
Func<MxValue, object?> convertValue)
|
||||
Func<MxValue, object?> convertValue,
|
||||
Func<MxValue, object?> convertTimestamp)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (entries is null)
|
||||
@@ -886,6 +901,11 @@ public sealed class MxAccessSession : IDisposable
|
||||
throw new ArgumentNullException(nameof(convertValue));
|
||||
}
|
||||
|
||||
if (convertTimestamp is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(convertTimestamp));
|
||||
}
|
||||
|
||||
List<BulkWriteResult> results = new(entries.Count);
|
||||
foreach (WriteSecured2BulkEntry entry in entries)
|
||||
{
|
||||
@@ -898,7 +918,7 @@ public sealed class MxAccessSession : IDisposable
|
||||
entry.CurrentUserId,
|
||||
entry.VerifierUserId,
|
||||
convertValue(entry.Value),
|
||||
convertValue(entry.TimestampValue))));
|
||||
convertTimestamp(entry.TimestampValue))));
|
||||
}
|
||||
|
||||
return results;
|
||||
|
||||
@@ -229,6 +229,16 @@ public sealed class MxAccessStaSession : IWorkerRuntimeSession
|
||||
/// </summary>
|
||||
internal TimeSpan WriteCompletionTimeout { get; set; } = ResolveWriteCompletionTimeout();
|
||||
|
||||
/// <summary>
|
||||
/// Zone the command executor renders the Write2/WriteSecured2 time
|
||||
/// argument in, handed over at <see cref="StartAsync(string, int, CancellationToken)"/>.
|
||||
/// <see langword="null"/> (the production value) means
|
||||
/// <see cref="TimeZoneInfo.Local"/>. Internal-settable as a test seam so
|
||||
/// Worker.Tests assert the #228 conversion independently of the zone of
|
||||
/// the machine that runs them.
|
||||
/// </summary>
|
||||
internal TimeZoneInfo? MxAccessLocalTimeZone { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cadence the alarm poll loop waits between polls. Resolved from the
|
||||
/// launcher environment; internal-settable as a test seam (set before
|
||||
@@ -362,7 +372,7 @@ public sealed class MxAccessStaSession : IWorkerRuntimeSession
|
||||
staRuntime,
|
||||
new MxAccessCommandExecutor(
|
||||
session,
|
||||
new VariantConverter(),
|
||||
new VariantConverter(MxAccessLocalTimeZone),
|
||||
alarmCommandHandler,
|
||||
// ReadBulk and the write-completion wait need to pump
|
||||
// Windows messages while they wait for the inbound COM
|
||||
|
||||
Reference in New Issue
Block a user