Skip to content

Commit 8f0d205

Browse files
committed
test: verify interrupted process cleanup and permit reuse
1 parent 5e2cd13 commit 8f0d205

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

‎src/test/java/io/github/easy4j/opencli/core/support/SubprocessCompletionRaceTest.java‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import java.io.InputStream;
99
import java.io.OutputStream;
1010
import java.nio.charset.StandardCharsets;
11+
import java.util.concurrent.CountDownLatch;
12+
import java.util.concurrent.TimeUnit;
13+
import java.util.concurrent.atomic.AtomicBoolean;
1114
import java.util.concurrent.atomic.AtomicInteger;
1215
import java.util.concurrent.atomic.AtomicReference;
1316
import org.apache.commons.exec.CommandLine;
@@ -17,6 +20,7 @@
1720

1821
import static org.junit.jupiter.api.Assertions.assertEquals;
1922
import static org.junit.jupiter.api.Assertions.assertFalse;
23+
import static org.junit.jupiter.api.Assertions.assertNull;
2024
import static org.junit.jupiter.api.Assertions.assertTrue;
2125

2226
/** Verifies terminal evidence when cancellation, watchdog expiry and natural exit coincide. */
@@ -55,6 +59,68 @@ void simultaneousSignalsKeepOneResultAndReuseThePermit() throws Exception {
5559
assertEquals(32, starts.get());
5660
}
5761

62+
@Test
63+
void interruptedRunnerCleansOwnedChildRestoresFlagAndReusesPermit() throws Exception {
64+
CountDownLatch childRunning = new CountDownLatch(1);
65+
AtomicInteger starts = new AtomicInteger();
66+
AtomicReference<OpenCliCancellationToken> currentToken = new AtomicReference<>();
67+
AtomicReference<LiveProcess> firstProcess = new AtomicReference<>();
68+
AtomicReference<SubprocessExecutionSupport.RunSession> firstResult = new AtomicReference<>();
69+
AtomicReference<Throwable> failure = new AtomicReference<>();
70+
AtomicBoolean interruptRestored = new AtomicBoolean();
71+
SubprocessExecutionSupport.Runtime runtime = new SubprocessExecutionSupport.Runtime(1,
72+
builder -> {
73+
if (starts.incrementAndGet() == 1) {
74+
LiveProcess process = new LiveProcess();
75+
firstProcess.set(process);
76+
return process;
77+
}
78+
currentToken.get().cancel();
79+
return new ExitedProcess("second");
80+
}, timeout -> new ExecuteWatchdog(timeout) {
81+
@Override
82+
public synchronized void start(Process process) { childRunning.countDown(); }
83+
84+
@Override
85+
public synchronized boolean killedProcess() { return false; }
86+
});
87+
OpenCliCancellationToken firstToken = new OpenCliCancellationToken();
88+
currentToken.set(firstToken);
89+
Thread runner = new Thread(() -> {
90+
try {
91+
firstResult.set(runtime.execute(new SubprocessExecutionSupport.ExecutionRequest(
92+
CommandLine.parse("fake"), null, null, 2_000L, 32, 32, 100L,
93+
System.nanoTime(), firstToken)));
94+
interruptRestored.set(Thread.currentThread().isInterrupted());
95+
} catch (Throwable ex) {
96+
failure.set(ex);
97+
}
98+
});
99+
100+
runner.start();
101+
assertTrue(childRunning.await(2, TimeUnit.SECONDS));
102+
runner.interrupt();
103+
runner.join(2_000L);
104+
105+
assertFalse(runner.isAlive());
106+
assertNull(failure.get());
107+
assertEquals(TerminationReason.CANCELLED,
108+
firstResult.get().getExecutionDetails().getTerminationReason());
109+
assertEquals(CleanupState.ROOT_EXIT_CONFIRMED,
110+
firstResult.get().getExecutionDetails().getCleanupState());
111+
assertFalse(firstProcess.get().isAlive());
112+
assertTrue(interruptRestored.get());
113+
114+
OpenCliCancellationToken nextToken = new OpenCliCancellationToken();
115+
currentToken.set(nextToken);
116+
SubprocessExecutionSupport.RunSession next = runtime.execute(new SubprocessExecutionSupport.ExecutionRequest(
117+
CommandLine.parse("fake"), null, null, 500L, 32, 32, 100L,
118+
System.nanoTime(), nextToken));
119+
assertEquals(2, starts.get());
120+
assertEquals(TerminationReason.CANCELLED,
121+
next.getExecutionDetails().getTerminationReason());
122+
}
123+
58124
private static final class ExpiredWatchdog extends ExecuteWatchdog {
59125
ExpiredWatchdog() { super(1_000L); }
60126

@@ -93,4 +159,32 @@ public void destroy() { }
93159
@Override
94160
public boolean isAlive() { return false; }
95161
}
162+
163+
private static final class LiveProcess extends Process {
164+
private volatile boolean alive = true;
165+
166+
@Override
167+
public OutputStream getOutputStream() { return new ByteArrayOutputStream(); }
168+
169+
@Override
170+
public InputStream getInputStream() { return new ByteArrayInputStream(new byte[0]); }
171+
172+
@Override
173+
public InputStream getErrorStream() { return new ByteArrayInputStream(new byte[0]); }
174+
175+
@Override
176+
public int waitFor() { return 143; }
177+
178+
@Override
179+
public int exitValue() {
180+
if (alive) { throw new IllegalThreadStateException(); }
181+
return 143;
182+
}
183+
184+
@Override
185+
public void destroy() { alive = false; }
186+
187+
@Override
188+
public boolean isAlive() { return alive; }
189+
}
96190
}

0 commit comments

Comments
 (0)