11package io .github .easy4j .opencli .core .support ;
22
33import io .github .easy4j .opencli .core .OpenCliCancellationToken ;
4+ import io .github .easy4j .opencli .core .OpenCliExecutionDetails .CleanupState ;
45import io .github .easy4j .opencli .core .OpenCliExecutionDetails .TerminationReason ;
56import java .io .ByteArrayInputStream ;
67import java .io .ByteArrayOutputStream ;
78import java .io .IOException ;
89import java .io .InputStream ;
910import java .io .OutputStream ;
1011import java .nio .charset .StandardCharsets ;
12+ import java .util .concurrent .CountDownLatch ;
13+ import java .util .concurrent .TimeUnit ;
1114import org .apache .commons .exec .CommandLine ;
1215import org .apache .commons .exec .ExecuteWatchdog ;
1316import org .junit .jupiter .api .Test ;
1417import org .junit .jupiter .api .Timeout ;
1518
1619import static org .junit .jupiter .api .Assertions .assertEquals ;
20+ import static org .junit .jupiter .api .Assertions .assertFalse ;
1721import static org .junit .jupiter .api .Assertions .assertSame ;
1822import static org .junit .jupiter .api .Assertions .assertTrue ;
1923
@@ -39,7 +43,53 @@ void partialOutputAndReadFailureRemainVisibleAfterRootExit() throws Exception {
3943 assertTrue (session .getExecutionDetails ().isStreamsDrained ());
4044 }
4145
42- private static final class IdleWatchdog extends ExecuteWatchdog {
46+ @ Test
47+ void openReaderKeepsCleanupUnconfirmedAfterRootExit () throws Exception {
48+ CountDownLatch releaseReader = new CountDownLatch (1 );
49+ CountDownLatch readerClosed = new CountDownLatch (1 );
50+ InputStream output = new InputStream () {
51+ @ Override
52+ public int read () throws IOException {
53+ try {
54+ releaseReader .await ();
55+ return -1 ;
56+ } catch (InterruptedException ex ) {
57+ Thread .currentThread ().interrupt ();
58+ throw new IOException (ex );
59+ }
60+ }
61+
62+ @ Override
63+ public void close () {
64+ readerClosed .countDown ();
65+ }
66+ };
67+ OpenCliCancellationToken token = new OpenCliCancellationToken ();
68+ SubprocessExecutionSupport .Runtime runtime = new SubprocessExecutionSupport .Runtime (1 ,
69+ builder -> new ExitedProcess (output ), timeout -> new IdleWatchdog () {
70+ @ Override
71+ public synchronized void start (Process process ) {
72+ token .cancel ();
73+ }
74+ });
75+ SubprocessExecutionSupport .ExecutionRequest request = new SubprocessExecutionSupport .ExecutionRequest (
76+ CommandLine .parse ("fake" ), null , null , 1_000L , 32 , 32 , 50L , System .nanoTime (), token );
77+
78+ try {
79+ SubprocessExecutionSupport .RunSession session = runtime .execute (request );
80+
81+ assertEquals (TerminationReason .CANCELLED , session .getExecutionDetails ().getTerminationReason ());
82+ assertEquals (0 , session .getObservedExitCode ());
83+ assertEquals (CleanupState .UNCONFIRMED , session .getExecutionDetails ().getCleanupState ());
84+ assertFalse (session .getExecutionDetails ().isStreamsDrained ());
85+ assertTrue (runtime .isQuarantined ());
86+ } finally {
87+ releaseReader .countDown ();
88+ assertTrue (readerClosed .await (1 , TimeUnit .SECONDS ));
89+ }
90+ }
91+
92+ private static class IdleWatchdog extends ExecuteWatchdog {
4393 IdleWatchdog () { super (1_000L ); }
4494
4595 @ Override
@@ -49,8 +99,12 @@ public synchronized void start(Process process) { }
4999 private static final class ExitedProcess extends Process {
50100 private final InputStream output ;
51101
102+ ExitedProcess (InputStream output ) {
103+ this .output = output ;
104+ }
105+
52106 ExitedProcess (IOException readFailure ) {
53- output = new InputStream () {
107+ this ( new InputStream () {
54108 private boolean emitted ;
55109
56110 @ Override
@@ -66,7 +120,7 @@ public int read(byte[] bytes, int offset, int length) throws IOException {
66120 emitted = true ;
67121 return partial .length ;
68122 }
69- };
123+ }) ;
70124 }
71125
72126 @ Override
0 commit comments