99import java .io .File ;
1010import java .io .InputStream ;
1111import java .nio .charset .StandardCharsets ;
12+ import java .nio .file .Files ;
13+ import java .nio .file .Path ;
14+ import java .nio .file .Paths ;
1215import java .util .Arrays ;
1316import java .util .Collections ;
1417import java .util .Scanner ;
1720import java .util .concurrent .Future ;
1821import java .util .concurrent .TimeUnit ;
1922import org .junit .jupiter .api .Test ;
23+ import org .junit .jupiter .api .io .TempDir ;
2024
2125import static org .junit .jupiter .api .Assertions .*;
2226
@@ -35,20 +39,25 @@ void concurrentInvocationsKeepTheirProfileAndWindow() throws Exception {
3539 }
3640
3741 @ Test
38- void failedInvocationKeepsItsContextWithoutChangingConcurrentOrDefaultCalls () throws Exception {
42+ void failedInvocationKeepsItsContextWithoutChangingConcurrentOrDefaultCalls (@ TempDir Path coordination ) throws Exception {
3943 OpenCliProperties properties = properties ();
4044 properties .setInvocationDefaults (OpenCliInvocationContext .builder ().profile ("profile-default" )
4145 .window (WindowMode .FOREGROUND ).build ());
46+ properties .getEnvironment ().put ("OPENCLI_CONTEXT_TEST_COORDINATION" , coordination .toString ());
4247 ExecutorService pool = Executors .newFixedThreadPool (2 );
4348 try (OpenCliClient client = new OpenCliClient (properties )) {
4449 Future <OpenCliNonZeroExitException > failure = pool .submit (() -> assertThrows (
4550 OpenCliNonZeroExitException .class , () -> state (client , "profile-failure" , WindowMode .BACKGROUND )));
4651 Future <String > success = pool .submit (() -> state (client , "profile-success" , WindowMode .FOREGROUND ));
4752
48- assertEquals ( "--profile|profile-success|browser|session-a|--window|foreground|state" ,
49- success . get ( 12 , TimeUnit . SECONDS ));
53+ OpenCliNonZeroExitException failed = failure . get ( 20 , TimeUnit . SECONDS );
54+ assertEquals ( Integer . valueOf ( 7 ), failed . getPartialResult (). getExitCode ( ));
5055 assertEquals ("--profile|profile-failure|browser|session-a|--window|background|state" ,
51- failure .get (12 , TimeUnit .SECONDS ).getPartialResult ().getStdout ());
56+ failed .getPartialResult ().getStdout ());
57+ assertFalse (success .isDone ());
58+ Files .createFile (coordination .resolve ("release-peer" ));
59+ assertEquals ("--profile|profile-success|browser|session-a|--window|foreground|state" ,
60+ success .get (20 , TimeUnit .SECONDS ));
5261 assertEquals ("--profile|profile-default|browser|session-a|--window|foreground|state" ,
5362 client .invokeDiscovered (OpenCliHelpScope .BROWSER_STATE , "opencli browser <session> state" ,
5463 "session-a" , null , null ).getStdout ());
@@ -124,8 +133,25 @@ public static void main(String[] args) throws Exception {
124133 }
125134 System .out .print (String .join ("|" , args ));
126135 if (argv .startsWith ("--profile profile-failure " )) {
136+ Path coordination = Paths .get (System .getenv ("OPENCLI_CONTEXT_TEST_COORDINATION" ));
137+ awaitFile (coordination .resolve ("peer-ready" ));
127138 System .exit (7 );
128139 }
140+ if (argv .startsWith ("--profile profile-success " )) {
141+ Path coordination = Paths .get (System .getenv ("OPENCLI_CONTEXT_TEST_COORDINATION" ));
142+ Files .createFile (coordination .resolve ("peer-ready" ));
143+ awaitFile (coordination .resolve ("release-peer" ));
144+ }
145+ }
146+ }
147+
148+ private static void awaitFile (Path file ) throws Exception {
149+ long deadline = System .nanoTime () + TimeUnit .SECONDS .toNanos (10 );
150+ while (!Files .exists (file )) {
151+ if (System .nanoTime () >= deadline ) {
152+ throw new IllegalStateException ("Timed out waiting for peer fixture signal" );
153+ }
154+ Thread .sleep (20 );
129155 }
130156 }
131157
0 commit comments