Compare commits
2 Commits
70d2842c16
...
3bb4d5a082
| Author | SHA1 | Date | |
|---|---|---|---|
| 3bb4d5a082 | |||
| 4ab3bd55e5 |
+78
@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.zb.mom.ww.mxgateway.client.MxGatewayAlarmFeedSubscription;
|
||||
import com.zb.mom.ww.mxgateway.client.MxGatewayClient;
|
||||
import com.zb.mom.ww.mxgateway.client.MxGatewayClientOptions;
|
||||
import galaxy_repository.v1.GalaxyRepositoryOuterClass.GalaxyObject;
|
||||
import io.grpc.stub.StreamObserver;
|
||||
@@ -31,6 +32,7 @@ import mxaccess_gateway.v1.MxaccessGateway.CloseSessionRequest;
|
||||
import mxaccess_gateway.v1.MxaccessGateway.MxCommandKind;
|
||||
import mxaccess_gateway.v1.MxaccessGateway.MxCommandReply;
|
||||
import mxaccess_gateway.v1.MxaccessGateway.MxEvent;
|
||||
import mxaccess_gateway.v1.MxaccessGateway.MxEventFamily;
|
||||
import mxaccess_gateway.v1.MxaccessGateway.MxValue;
|
||||
import mxaccess_gateway.v1.MxaccessGateway.OnAlarmTransitionEvent;
|
||||
import mxaccess_gateway.v1.MxaccessGateway.OpenSessionReply;
|
||||
@@ -693,6 +695,60 @@ final class MxGatewayCliTests {
|
||||
+ " out=\n" + run.output() + "\nerr=\n" + run.errors());
|
||||
}
|
||||
|
||||
@Test
|
||||
void streamEventsRendersScriptedEventsIncludingHighUint64Sequence() {
|
||||
// Drive the REAL MxGatewayClient / MxGatewaySession / MxEventStream
|
||||
// path over the in-process harness (Task 4), so the production
|
||||
// stream-events command exercises the real streaming wrapper instead
|
||||
// of a hand-written FakeSession seam.
|
||||
//
|
||||
// The high worker-sequence (-1L == 18446744073709551615 unsigned)
|
||||
// covers the unsigned-rendering regression: worker_sequence is a
|
||||
// proto uint64 carried as a Java long with the top bit set. The CLI's
|
||||
// --json path renders it through protobuf's JsonFormat, which prints
|
||||
// uint64 as an unsigned decimal STRING; a naive %d render would print
|
||||
// a negative number instead.
|
||||
MxEvent dataChange = MxEvent.newBuilder()
|
||||
.setFamily(MxEventFamily.MX_EVENT_FAMILY_ON_DATA_CHANGE)
|
||||
.setSessionId("session-cli")
|
||||
.setServerHandle(7)
|
||||
.setItemHandle(42)
|
||||
.setWorkerSequence(5L)
|
||||
.build();
|
||||
MxEvent highSequence = MxEvent.newBuilder()
|
||||
.setFamily(MxEventFamily.MX_EVENT_FAMILY_OPERATION_COMPLETE)
|
||||
.setSessionId("session-cli")
|
||||
.setServerHandle(9)
|
||||
.setItemHandle(99)
|
||||
// -1L unsigned == 18446744073709551615 (top bit set).
|
||||
.setWorkerSequence(-1L)
|
||||
.build();
|
||||
|
||||
try (InProcessGatewayHarness harness = new InProcessGatewayHarness()) {
|
||||
harness.setScriptedEvents(List.of(dataChange, highSequence));
|
||||
CliRun run = execute(
|
||||
new HarnessClientFactory(harness),
|
||||
"stream-events",
|
||||
"--session-id",
|
||||
"session-cli",
|
||||
"--json");
|
||||
|
||||
assertEquals(0, run.exitCode(), "errors:\n" + run.errors());
|
||||
String out = run.output();
|
||||
// Scripted event fields surface in the JSON render.
|
||||
assertTrue(out.contains("\"family\":\"MX_EVENT_FAMILY_ON_DATA_CHANGE\""), out);
|
||||
assertTrue(out.contains("\"family\":\"MX_EVENT_FAMILY_OPERATION_COMPLETE\""), out);
|
||||
assertTrue(out.contains("\"serverHandle\":7"), out);
|
||||
assertTrue(out.contains("\"itemHandle\":42"), out);
|
||||
// The low sequence renders as the unsigned decimal string "5".
|
||||
assertTrue(out.contains("\"workerSequence\":\"5\""), out);
|
||||
// The high sequence renders as the FULL unsigned decimal, not -1.
|
||||
assertTrue(out.contains("\"workerSequence\":\"18446744073709551615\""), out);
|
||||
assertFalse(out.contains("\"workerSequence\":\"-1\""), out);
|
||||
assertFalse(out.contains("\"workerSequence\":-1"), out);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchCommandExecutesVersionAndEmitsEorMarker() {
|
||||
CliRun run = executeBatch(new FakeClientFactory(), "version --json\n");
|
||||
@@ -849,6 +905,28 @@ final class MxGatewayCliTests {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory that wires the production {@link MxGatewayCli.GrpcMxGatewayCliClient}
|
||||
* adapter around the harness's REAL {@link MxGatewayClient}, so the
|
||||
* stream-events command runs against the in-process scripted gateway over
|
||||
* a real channel (exercising the real {@code MxEventStream}). Mirrors the
|
||||
* production {@code GrpcMxGatewayCliClientFactory}, swapping only the
|
||||
* client construction for the harness-backed client.
|
||||
*/
|
||||
private static final class HarnessClientFactory implements MxGatewayCli.MxGatewayCliClientFactory {
|
||||
private final InProcessGatewayHarness harness;
|
||||
|
||||
private HarnessClientFactory(InProcessGatewayHarness harness) {
|
||||
this.harness = harness;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MxGatewayCli.MxGatewayCliClient connect(MxGatewayCli.CommonOptions options) {
|
||||
return new MxGatewayCli.GrpcMxGatewayCliClient(
|
||||
harness.gatewayClient(), options.spec.commandLine().getOut());
|
||||
}
|
||||
}
|
||||
|
||||
private static final class OverflowingFakeClient implements MxGatewayCli.MxGatewayCliClient {
|
||||
private final PrintWriter out;
|
||||
|
||||
|
||||
@@ -1672,34 +1672,7 @@ public sealed class GatewaySession
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the worker client iff both the gateway-side session state AND
|
||||
/// the worker client's own state are <see cref="SessionState.Ready"/> /
|
||||
/// <see cref="WorkerClientState.Ready"/>. The two states can diverge under
|
||||
/// load: <c>_state</c> only transitions on gateway-driven events (open,
|
||||
/// close, fault), while <see cref="WorkerClient.State"/> can shift on
|
||||
/// worker-side signals (heartbeat watchdog, pipe disconnect) before the
|
||||
/// gateway's session-level reaction observes them. When that happens the
|
||||
/// in-flight RPC fails fast here with both states surfaced in the
|
||||
/// diagnostic (Server-030) so the actual mismatch is actionable instead
|
||||
/// of misleading. The session usually transitions to <c>Faulted</c>
|
||||
/// shortly after.
|
||||
/// </summary>
|
||||
private IWorkerClient GetReadyWorkerClient()
|
||||
{
|
||||
lock (_syncRoot)
|
||||
{
|
||||
IWorkerClient? ready = EvaluateReadyUnderLock(out string? failureMessage);
|
||||
if (ready is not null)
|
||||
{
|
||||
return ready;
|
||||
}
|
||||
|
||||
throw new SessionManagerException(SessionManagerErrorCode.SessionNotReady, failureMessage!);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bounded, opt-in async variant of <see cref="GetReadyWorkerClient"/>. When the
|
||||
/// Bounded, opt-in async variant of the fail-fast readiness check. When the
|
||||
/// session is <see cref="SessionState.Ready"/> but the worker has transiently diverged
|
||||
/// to a non-terminal state (<see cref="WorkerClientState.Handshaking"/>/
|
||||
/// <see cref="WorkerClientState.Created"/>) and the configured worker-ready wait timeout
|
||||
@@ -1742,7 +1715,11 @@ public sealed class GatewaySession
|
||||
DateTimeOffset deadline = _eventStreaming.TimeProvider.GetUtcNow() + _workerReadyWaitTimeout;
|
||||
while (true)
|
||||
{
|
||||
await Task.Delay(pollIntervalMs, cancellationToken).ConfigureAwait(false);
|
||||
await Task.Delay(
|
||||
TimeSpan.FromMilliseconds(pollIntervalMs),
|
||||
_eventStreaming.TimeProvider,
|
||||
cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
lock (_syncRoot)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user