Implement native MXAccess writes in the Rust worker

This commit is contained in:
Joseph Doherty
2026-11-21 09:15:32 -05:00
parent 00a597b08b
commit d1769f81ff
6 changed files with 1050 additions and 87 deletions
@@ -131,13 +131,7 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
string workerPath = IntegrationTestEnvironment.ResolveLiveMxAccessWorkerExecutablePath();
TestWorkerProcessFactory processFactory = new(output);
await using GatewayServiceFixture fixture = new(workerPath, processFactory, output);
WebApplicationBuilder builder = WebApplication.CreateSlimBuilder();
builder.WebHost.ConfigureKestrel(options =>
options.Listen(IPAddress.Loopback, 0, listener => listener.Protocols = HttpProtocols.Http2));
builder.Services.AddGrpc();
builder.Services.AddSingleton(fixture.Service);
await using WebApplication app = builder.Build();
app.MapGrpcService<MxAccessGatewayService>();
await using WebApplication app = BuildLoopbackGateway(fixture.Service);
using CancellationTokenSource deadline = new(TimeSpan.FromSeconds(90));
CancellationToken token = deadline.Token;
await app.StartAsync(token).ConfigureAwait(false);
@@ -229,7 +223,6 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
},
}).ConfigureAwait(false);
// A callback queued before UnAdvise may still cross the pipe.
// Observe the same stream and reject updates generated after acknowledgement.
DateTime unadvisedAt = DateTime.UtcNow;
@@ -267,6 +260,233 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
}
}
/// <summary>Verifies committed Rust worker writes and their callbacks through the gateway.</summary>
/// <returns>A task that represents the live test.</returns>
[RustWorkerLiveFact]
public async Task GatewayGrpc_WithRustWorker_WritesAndReceivesOnWriteComplete()
{
string workerPath = IntegrationTestEnvironment.ResolveLiveMxAccessWorkerExecutablePath();
await using GatewayServiceFixture fixture = new(workerPath, new TestWorkerProcessFactory(output), output);
await using WebApplication app = BuildLoopbackGateway(fixture.Service);
using CancellationTokenSource deadline = new(TimeSpan.FromSeconds(90));
CancellationToken token = deadline.Token;
await app.StartAsync(token).ConfigureAwait(false);
using GrpcChannel channel = GrpcChannel.ForAddress(app.Urls.Single());
MxAccessGateway.MxAccessGatewayClient client = new(channel);
OpenSessionReply opened = await client.OpenSessionAsync(
new OpenSessionRequest { ClientSessionName = "rust-worker-live-write" },
cancellationToken: token).ResponseAsync.ConfigureAwait(false);
Assert.Equal(ProtocolStatusCode.Ok, opened.ProtocolStatus.Code);
string sessionId = opened.SessionId;
MxCommandRequest? restore = null;
try
{
async Task<MxCommandReply> InvokeAsync(MxCommandRequest request)
{
MxCommandReply reply = await client.InvokeAsync(request, cancellationToken: token)
.ResponseAsync.ConfigureAwait(false);
Assert.True(reply.ProtocolStatus.Code == ProtocolStatusCode.Ok,
$"{request.Command.Kind}: {reply.ProtocolStatus.Code} HRESULT={reply.Hresult} {reply.DiagnosticMessage}");
return reply;
}
MxCommandReply worker = await InvokeAsync(new MxCommandRequest
{
SessionId = sessionId,
ClientCorrelationId = "rust-write-worker-info",
Command = new MxCommand { Kind = MxCommandKind.GetWorkerInfo, GetWorkerInfo = new GetWorkerInfoCommand() },
}).ConfigureAwait(false);
Assert.EndsWith("-rust", worker.WorkerInfo.WorkerVersion, StringComparison.Ordinal);
Assert.Equal(opened.WorkerProcessId, worker.WorkerInfo.WorkerProcessId);
MxCommandReply registration = await InvokeAsync(CreateRegisterRequest(sessionId)).ConfigureAwait(false);
int serverHandle = registration.Register.ServerHandle;
MxCommandReply added = await InvokeAsync(CreateAddItemRequest(sessionId, serverHandle)).ConfigureAwait(false);
int itemHandle = added.AddItem.ItemHandle;
using AsyncServerStreamingCall<MxEvent> events = client.StreamEvents(
new StreamEventsRequest { SessionId = sessionId, EmitStreamAttached = true },
cancellationToken: token);
Assert.True(await events.ResponseStream.MoveNext(token).ConfigureAwait(false));
Assert.NotNull(events.ResponseStream.Current.StreamAttached);
await InvokeAsync(CreateAdviseSupervisoryRequest(sessionId, serverHandle, itemHandle)).ConfigureAwait(false);
bool IsTarget(MxEvent update) => update.SessionId == sessionId &&
update.ServerHandle == serverHandle && update.ItemHandle == itemHandle;
bool IsGoodInt(MxEvent update) => IsTarget(update) && update.Family == MxEventFamily.OnDataChange &&
update.Value?.KindCase == MxValue.KindOneofCase.Int32Value && (update.Quality & 0xc0) == 0xc0;
MxEvent? initial = null;
while (await events.ResponseStream.MoveNext(token).ConfigureAwait(false))
{
if (IsGoodInt(events.ResponseStream.Current))
{
initial = events.ResponseStream.Current;
break;
}
}
Assert.NotNull(initial);
int original = initial.Value.Int32Value;
int written = original == 1 ? 2 : 1;
async Task CheckWriteAsync(MxCommandRequest request, int expected, DateTime? expectedSource = null)
{
MxCommandReply reply = await InvokeAsync(request).ConfigureAwait(false);
Assert.Equal(request.Command.Kind, reply.Kind);
Assert.Equal(0, reply.Hresult);
if (request.Command.Kind == MxCommandKind.WriteBulk)
{
Assert.Collection(reply.WriteBulk.Results,
unsupported =>
{
Assert.Equal(itemHandle, unsupported.ItemHandle);
Assert.False(unsupported.WasSuccessful);
Assert.NotEmpty(unsupported.ErrorMessage);
},
accepted =>
{
Assert.Equal(itemHandle, accepted.ItemHandle);
Assert.True(accepted.WasSuccessful, accepted.ErrorMessage);
});
}
else
{
Assert.NotEmpty(reply.Statuses);
Assert.All(reply.Statuses, status => Assert.Equal(MxStatusCategory.Ok, status.Category));
}
MxEvent? completion = null;
MxEvent? changed = null;
while (await events.ResponseStream.MoveNext(token).ConfigureAwait(false))
{
MxEvent update = events.ResponseStream.Current;
if (IsTarget(update) && update.Family == MxEventFamily.OnWriteComplete)
{
completion = update;
}
if (IsGoodInt(update) && update.Value.Int32Value == expected)
{
changed = update;
}
if (completion is not null && changed is not null)
{
break;
}
}
Assert.NotNull(completion);
Assert.NotNull(changed);
Assert.NotEmpty(completion.Statuses);
Assert.All(completion.Statuses, status => Assert.Equal(MxStatusCategory.Ok, status.Category));
if (request.Command.Kind != MxCommandKind.WriteBulk)
{
Assert.Equal(reply.Statuses.ToArray(), completion.Statuses.ToArray());
}
if (expectedSource is DateTime timestamp)
{
Assert.Equal(timestamp, changed.SourceTimestamp?.ToDateTime());
}
output.WriteLine(
$"{request.Command.Kind} committed {IntegrationTestEnvironment.LiveMxAccessItem}={expected}; " +
$"OnWriteComplete sequence={completion.WorkerSequence}, readback sequence={changed.WorkerSequence}.");
}
restore = CreateWriteRequest(sessionId, serverHandle, itemHandle, initial.Value);
MxCommandRequest write = CreateWriteRequest(sessionId, serverHandle, itemHandle,
new MxValue { DataType = MxDataType.Integer, Int32Value = written });
write.ClientCorrelationId = "rust-write-round-trip";
await CheckWriteAsync(write, written).ConfigureAwait(false);
int bulkValue = original == 3 ? 4 : 3;
var bulk = new WriteBulkCommand { ServerHandle = serverHandle };
bulk.Entries.Add(new WriteBulkEntry
{
ItemHandle = itemHandle,
Value = new MxValue { RawValue = Google.Protobuf.ByteString.CopyFrom(new byte[] { 0 }) },
});
bulk.Entries.Add(new WriteBulkEntry
{
ItemHandle = itemHandle,
Value = new MxValue { DataType = MxDataType.Integer, Int32Value = bulkValue },
});
await CheckWriteAsync(new MxCommandRequest
{
SessionId = sessionId,
ClientCorrelationId = "rust-write-bulk-round-trip",
Command = new MxCommand { Kind = MxCommandKind.WriteBulk, WriteBulk = bulk },
}, bulkValue).ConfigureAwait(false);
DateTime timestamp = DateTime.UtcNow.AddSeconds(-30);
timestamp = new DateTime(timestamp.Ticks - timestamp.Ticks % TimeSpan.TicksPerSecond, DateTimeKind.Utc);
MxCommandRequest write2 = CreateWrite2Request(sessionId, serverHandle, itemHandle, timestamp);
write2.Command.Write2.Value = initial.Value;
await CheckWriteAsync(write2, original, timestamp).ConfigureAwait(false);
restore = null;
// The test item is unprotected. Exercise both secured native entry
// points with its current value and surface provider refusal as HRESULT.
foreach (MxCommand command in new MxCommand[]
{
new() { Kind = MxCommandKind.WriteSecured, WriteSecured = new WriteSecuredCommand
{
ServerHandle = serverHandle, ItemHandle = itemHandle, Value = initial.Value,
} },
new() { Kind = MxCommandKind.WriteSecured2, WriteSecured2 = new WriteSecured2Command
{
ServerHandle = serverHandle, ItemHandle = itemHandle, Value = initial.Value,
TimestampValue = write2.Command.Write2.TimestampValue,
} },
})
{
MxCommandReply reply = await client.InvokeAsync(new MxCommandRequest
{
SessionId = sessionId, ClientCorrelationId = $"rust-{command.Kind}", Command = command,
}, cancellationToken: token).ResponseAsync.ConfigureAwait(false);
Assert.Equal(command.Kind, reply.Kind);
Assert.True(reply.ProtocolStatus.Code is ProtocolStatusCode.Ok or ProtocolStatusCode.MxaccessFailure,
$"{command.Kind}: {reply.ProtocolStatus.Code} {reply.DiagnosticMessage}");
if (reply.ProtocolStatus.Code == ProtocolStatusCode.MxaccessFailure)
{
Assert.True(reply.HasHresult && reply.Hresult < 0);
}
output.WriteLine($"{command.Kind} returned {reply.ProtocolStatus.Code}, HRESULT=0x{reply.Hresult:X8}.");
}
}
finally
{
try
{
if (restore is not null)
{
MxCommandReply restored = await client.InvokeAsync(restore, deadline: DateTime.UtcNow.AddSeconds(10))
.ResponseAsync.ConfigureAwait(false);
Assert.Equal(ProtocolStatusCode.Ok, restored.ProtocolStatus.Code);
Assert.NotEmpty(restored.Statuses);
Assert.All(restored.Statuses, status => Assert.Equal(MxStatusCategory.Ok, status.Category));
}
}
finally
{
CloseSessionReply closed = await client.CloseSessionAsync(new CloseSessionRequest { SessionId = sessionId },
deadline: DateTime.UtcNow.AddSeconds(25)).ResponseAsync.ConfigureAwait(false);
Assert.Equal(ProtocolStatusCode.Ok, closed.ProtocolStatus.Code);
}
}
}
private static WebApplication BuildLoopbackGateway(MxAccessGatewayService service)
{
WebApplicationBuilder builder = WebApplication.CreateSlimBuilder();
builder.WebHost.ConfigureKestrel(options =>
options.Listen(IPAddress.Loopback, 0, listener => listener.Protocols = HttpProtocols.Http2));
builder.Services.AddGrpc();
builder.Services.AddSingleton(service);
WebApplication app = builder.Build();
app.MapGrpcService<MxAccessGatewayService>();
return app;
}
private sealed class RustWorkerLiveFactAttribute : FactAttribute
{
public RustWorkerLiveFactAttribute()