Merge fix/260-followup
ci / windows-x86 (push) Successful in 1m56s
ci / java (push) Failing after 5m50s
ci / portable (push) Failing after 10m33s

This commit is contained in:
Joseph Doherty
2026-12-03 13:14:23 -05:00
2 changed files with 132 additions and 4 deletions
@@ -332,7 +332,11 @@ public final class MxGatewayCli implements Callable<Integer> {
@Override
public Integer call() {
PrintWriter out = spec.commandLine().getOut();
try (BufferedReader reader = new BufferedReader(
// One real client per distinct connection setting for the whole batch; Java
// close() is shutdownNow(), so per-line commands only ever close a non-owning
// handle and this loop closes the real clients once, on every exit path.
try (CachingClientFactory cachingFactory = new CachingClientFactory(clientFactory);
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
@@ -348,7 +352,7 @@ public final class MxGatewayCli implements Callable<Integer> {
PrintWriter cmdOutWriter = new PrintWriter(cmdOut, true);
PrintWriter cmdErrWriter = new PrintWriter(cmdErr, true);
try {
CommandLine cmd = commandLine(clientFactory);
CommandLine cmd = commandLine(cachingFactory);
cmd.setOut(cmdOutWriter);
cmd.setErr(cmdErrWriter);
int exitCode = cmd.execute(args);
@@ -387,6 +391,102 @@ public final class MxGatewayCli implements Callable<Integer> {
}
}
/**
* Batch-mode factory that connects once per distinct connection setting and hands every
* command a non-owning handle. {@link #close()} shuts the real clients down.
*/
static final class CachingClientFactory implements MxGatewayCliClientFactory, AutoCloseable {
private record Key(
String endpoint,
String apiKey,
boolean plaintext,
Path caFile,
String serverNameOverride,
boolean requireCertificateValidation,
Duration timeout) {
}
private final MxGatewayCliClientFactory delegate;
private final Map<Key, MxGatewayCliClient> clients = new LinkedHashMap<>();
CachingClientFactory(MxGatewayCliClientFactory delegate) {
this.delegate = delegate;
}
@Override
public MxGatewayCliClient connect(CommonOptions options) {
Key key = new Key(
options.endpoint,
options.resolvedApiKey,
options.plaintext,
options.caFile,
options.serverNameOverride,
options.requireCertificateValidation,
options.resolvedTimeout);
MxGatewayCliClient shared = clients.get(key);
if (shared == null) {
shared = delegate.connect(options);
clients.put(key, shared);
}
// The shared client's out() is bound to the first line's writer, so each
// command gets a handle that writes to its own line's writer.
return new BorrowedClient(shared, options.spec.commandLine().getOut());
}
@Override
public void close() {
RuntimeException failure = null;
for (MxGatewayCliClient client : clients.values()) {
try {
client.close();
} catch (RuntimeException ex) {
if (failure == null) {
failure = ex;
} else {
failure.addSuppressed(ex);
}
}
}
clients.clear();
if (failure != null) {
throw failure;
}
}
}
private record BorrowedClient(MxGatewayCliClient shared, PrintWriter out) implements MxGatewayCliClient {
@Override
public mxaccess_gateway.v1.MxaccessGateway.OpenSessionReply openSession(OpenSessionRequest request) {
return shared.openSession(request);
}
@Override
public mxaccess_gateway.v1.MxaccessGateway.CloseSessionReply closeSession(CloseSessionRequest request) {
return shared.closeSession(request);
}
@Override
public MxGatewayCliSession session(String sessionId) {
return shared.session(sessionId);
}
@Override
public AcknowledgeAlarmReply acknowledgeAlarm(AcknowledgeAlarmRequest request) {
return shared.acknowledgeAlarm(request);
}
@Override
public MxGatewayAlarmFeedSubscription streamAlarms(
StreamAlarmsRequest request, StreamObserver<AlarmFeedMessage> observer) {
return shared.streamAlarms(request, observer);
}
@Override
public void close() {
// Non-owning: the batch loop closes the shared client.
}
}
abstract static class GalaxyCommand implements Callable<Integer> {
final GalaxyClientFactory galaxyClientFactory;
@@ -1067,6 +1067,18 @@ final class MxGatewayCliTests {
}
}
@Test
void batchReusesOneClientPerDistinctConnectionAndClosesItOnce() {
CountingClientFactory factory = new CountingClientFactory();
String same = "ping --session-id s --plaintext --endpoint a:1\n";
CliRun run = executeBatch(factory, same + same + same + "ping --session-id s --plaintext --endpoint b:2\n");
assertEquals(0, run.exitCode());
assertEquals(2, factory.connects, run.output());
assertEquals(2, factory.closes, run.output());
assertEquals(4, run.output().split(MxGatewayCli.BATCH_EOR, -1).length - 1, run.output());
}
@Test
void batchCommandExecutesVersionAndEmitsEorMarker() {
CliRun run = executeBatch(new FakeClientFactory(), "version --json\n");
@@ -1202,6 +1214,22 @@ final class MxGatewayCliTests {
private record CliRun(int exitCode, String output, String errors) {
}
private static final class CountingClientFactory implements MxGatewayCli.MxGatewayCliClientFactory {
private int connects;
private int closes;
@Override
public MxGatewayCli.MxGatewayCliClient connect(MxGatewayCli.CommonOptions options) {
connects++;
return new FakeClient(options.spec.commandLine().getOut()) {
@Override
public void close() {
closes++;
}
};
}
}
private static final class FakeClientFactory implements MxGatewayCli.MxGatewayCliClientFactory {
private FakeClient client;
@@ -1368,14 +1396,14 @@ final class MxGatewayCliTests {
}
}
private static final class FakeClient implements MxGatewayCli.MxGatewayCliClient {
private static class FakeClient implements MxGatewayCli.MxGatewayCliClient {
private final PrintWriter out;
private final FakeSession session = new FakeSession();
private boolean closeCalled;
private AcknowledgeAlarmRequest lastAcknowledgeAlarmRequest;
private StreamAlarmsRequest lastStreamAlarmsRequest;
private FakeClient(PrintWriter out) {
FakeClient(PrintWriter out) {
this.out = out;
}