66import io .github .easy4j .opencli .OpenCliProperties ;
77import io .github .easy4j .opencli .contract .ContractProbe ;
88import io .github .easy4j .opencli .contract .LifecycleProbe ;
9+ import java .io .ByteArrayOutputStream ;
10+ import java .io .PrintStream ;
911import java .io .File ;
1012import tools .jackson .core .JacksonException ;
1113import java .io .InputStream ;
2628import java .util .concurrent .CountDownLatch ;
2729import java .util .concurrent .LinkedBlockingQueue ;
2830import java .util .concurrent .TimeUnit ;
31+ import java .util .concurrent .atomic .AtomicBoolean ;
2932import java .util .concurrent .atomic .AtomicReference ;
3033import java .util .stream .Collectors ;
3134import javax .net .ssl .KeyManagerFactory ;
@@ -106,24 +109,27 @@ void exchangesRegistrationAndCollectResultsAndReconnectsAfterRemoteClose() throw
106109 try (OpenCliWsReverseAgentClient agent = new OpenCliWsReverseAgentClient (probe (ContractProbe .class .getName ()), connection )) {
107110 agent .start ();
108111 WireMessage registration = center .receive ("register" );
112+ assertEquals (5 , registration .body .size ());
109113 assertEquals (AGENT_URL , registration .body .path ("agent_url" ).asText ());
110114 assertEquals ("cdp" , registration .body .path ("mode" ).asText ());
111115 assertEquals ("shell" , registration .body .path ("node_type" ).asText ());
116+ assertTrue (registration .body .path ("label" ).isTextual ());
112117 assertTrue (registration .body .path ("label" ).asText ().isEmpty ());
113118
114119 registration .socket .send ("{\" type\" :\" registered\" }" );
115120 agent .start ();
116121 registration .socket .send ("malformed-json" );
117122 registration .socket .send ("{\" type\" :\" pong\" }" );
118123 registration .socket .send ("{\" type\" :\" ping\" }" );
119- center .receive ("pong" );
124+ assertEquals ( 1 , center .receive ("pong" ). body . size () );
120125
121126 registration .socket .send ("{\" type\" :\" collect\" ,\" request_id\" :\" valid\" ,"
122127 + "\" site\" :\" demo\" ,\" command\" :\" echo\" ,\" format\" :\" text\" ,"
123128 + "\" positional_args\" :[\" kept \" ,null,42],\" args\" :{"
124129 + "\" flag\" :null,\" boolean\" :false,\" count\" :7,\" long\" :2147483648,"
125130 + "\" double\" :1.5,\" text\" :\" a b\" ,\" structured\" :{\" key\" :1}}}" );
126131 JsonNode result = center .receive ("result" ).body ;
132+ assertEquals (5 , result .size ());
127133 assertEquals ("valid" , result .path ("request_id" ).asText ());
128134 assertTrue (result .path ("success" ).asBoolean ());
129135 assertTrue (result .path ("error" ).isNull ());
@@ -137,6 +143,7 @@ void exchangesRegistrationAndCollectResultsAndReconnectsAfterRemoteClose() throw
137143
138144 registration .socket .send ("{\" type\" :\" collect\" ,\" request_id\" :\" invalid\" ,\" command\" :\" echo\" }" );
139145 JsonNode invalid = center .receive ("result" ).body ;
146+ assertEquals (5 , invalid .size ());
140147 assertEquals ("invalid" , invalid .path ("request_id" ).asText ());
141148 assertFalse (invalid .path ("success" ).asBoolean ());
142149 assertTrue (invalid .path ("items" ).isEmpty ());
@@ -471,8 +478,9 @@ void plainWebSocketReconnectDoesNotExposePreviousSessionCache(@TempDir Path temp
471478 verifyReconnectReplay (temporary , null , false );
472479 }
473480
474- @ Test
475- void verifiedTlsReconnectReplaysOnlyAfterAnExplicitRetry (@ TempDir Path temporary ) throws Exception {
481+ @ ParameterizedTest
482+ @ ValueSource (booleans = {false , true })
483+ void verifiedTlsReconnectReplaysOnlyAfterAnExplicitRetry (boolean failDelivery , @ TempDir Path temporary ) throws Exception {
476484 Path storePath = temporary .resolve ("center.p12" );
477485 String keytool = System .getProperty ("os.name" ).startsWith ("Windows" ) ? "keytool.exe" : "keytool" ;
478486 Path tool = Paths .get (System .getProperty ("java.home" ), "bin" , keytool );
@@ -503,12 +511,64 @@ void verifiedTlsReconnectReplaysOnlyAfterAnExplicitRetry(@TempDir Path temporary
503511 SSLContext previous = SSLContext .getDefault ();
504512 try {
505513 SSLContext .setDefault (trustedClient );
506- verifyReconnectReplay (temporary , server , true );
514+ if (failDelivery ) {
515+ verifyFailedDeliveryReplay (temporary , server );
516+ } else {
517+ verifyReconnectReplay (temporary , server , true );
518+ }
507519 } finally {
508520 SSLContext .setDefault (previous );
509521 }
510522 }
511523
524+ private static void verifyFailedDeliveryReplay (Path temporary , SSLContext tls ) throws Exception {
525+ Path marker = temporary .resolve ("delivery-start" );
526+ Path release = Files .createFile (temporary .resolve ("delivery-release" ));
527+ ObjectNode request = collect ("delivery" , "count-hold" , marker );
528+ request .putArray ("positional_args" ).add (release .toString ());
529+ String canary = "delivery-exception-private-canary" ;
530+ AtomicBoolean firstSend = new AtomicBoolean (true );
531+ ByteArrayOutputStream diagnostics = new ByteArrayOutputStream ();
532+ PrintStream previousErr = System .err ;
533+ try (LocalCenter center = new LocalCenter (tls );
534+ OpenCliWsReverseAgentClient agent = new OpenCliWsReverseAgentClient (
535+ probe (LifecycleProbe .class .getName ()), center .agentProperties (), (socket , json ) -> {
536+ if (firstSend .getAndSet (false )) {
537+ throw new IllegalStateException (canary );
538+ }
539+ socket .send (json );
540+ });
541+ PrintStream capture = new PrintStream (diagnostics , true , "UTF-8" )) {
542+ System .setErr (capture );
543+ try {
544+ agent .start ();
545+ WebSocket first = center .receive ("register" ).socket ;
546+ first .send ("{\" type\" :\" registered\" }" );
547+ first .send (request .toString ());
548+
549+ WebSocket second = center .receive ("register" ).socket ;
550+ assertNotSame (first , second );
551+ second .send ("{\" type\" :\" registered\" }" );
552+ second .send ("{\" type\" :\" ping\" }" );
553+ center .receive ("pong" );
554+ assertEquals (Arrays .asList ("started" ), Files .readAllLines (marker , StandardCharsets .UTF_8 ));
555+
556+ second .send (request .toString ());
557+ JsonNode replayed = center .receive ("result" ).body ;
558+ assertEquals ("delivery" , replayed .path ("request_id" ).asText ());
559+ assertTrue (replayed .path ("success" ).asBoolean ());
560+ assertTrue (replayed .path ("cached" ).asBoolean ());
561+ assertTrue (replayed .path ("error" ).isNull ());
562+ assertEquals (Arrays .asList ("started" ), Files .readAllLines (marker , StandardCharsets .UTF_8 ));
563+ } finally {
564+ System .setErr (previousErr );
565+ }
566+ }
567+ String logText = diagnostics .toString (StandardCharsets .UTF_8 .name ());
568+ assertTrue (logText .contains ("failed to send WS result" ));
569+ assertFalse (logText .contains (canary ));
570+ }
571+
512572 private static void verifyReconnectReplay (Path temporary , SSLContext tls , boolean expectCache ) throws Exception {
513573 Path marker = temporary .resolve ("reconnect-start" );
514574 Path release = temporary .resolve ("reconnect-release" );
0 commit comments