|
8 | 8 | import java.io.InputStream; |
9 | 9 | import java.io.OutputStream; |
10 | 10 | import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.concurrent.CountDownLatch; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | +import java.util.concurrent.atomic.AtomicBoolean; |
11 | 14 | import java.util.concurrent.atomic.AtomicInteger; |
12 | 15 | import java.util.concurrent.atomic.AtomicReference; |
13 | 16 | import org.apache.commons.exec.CommandLine; |
|
17 | 20 |
|
18 | 21 | import static org.junit.jupiter.api.Assertions.assertEquals; |
19 | 22 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNull; |
20 | 24 | import static org.junit.jupiter.api.Assertions.assertTrue; |
21 | 25 |
|
22 | 26 | /** Verifies terminal evidence when cancellation, watchdog expiry and natural exit coincide. */ |
@@ -55,6 +59,68 @@ void simultaneousSignalsKeepOneResultAndReuseThePermit() throws Exception { |
55 | 59 | assertEquals(32, starts.get()); |
56 | 60 | } |
57 | 61 |
|
| 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 | + |
58 | 124 | private static final class ExpiredWatchdog extends ExecuteWatchdog { |
59 | 125 | ExpiredWatchdog() { super(1_000L); } |
60 | 126 |
|
@@ -93,4 +159,32 @@ public void destroy() { } |
93 | 159 | @Override |
94 | 160 | public boolean isAlive() { return false; } |
95 | 161 | } |
| 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 | + } |
96 | 190 | } |
0 commit comments