Skip to content

Commit e1a8fad

Browse files
committed
Fix ProcessHelperTest timing issues by using long-running processes
Fixed two failing tests that were using 'echo' commands which complete almost instantly, making it impossible to verify process running state: 1. test_startProcess_replaceExistingProcess: - Changed from 'echo' to 'sleep 10' commands - Added proper wait time before checking process state - Added verification that only one process exists for the session 2. test_destroyProcess_withRunningProcess: - Changed from 'echo' to 'sleep 10' command - Simplified polling logic - just wait 100ms for process to start - Updated exit code assertion (forcibly destroyed processes may have non-zero exit codes) These changes ensure the tests can reliably verify that processes are running before attempting to check their state or destroy them.
1 parent ff70133 commit e1a8fad

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/test/java/org/codelibs/fess/helper/ProcessHelperTest.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ public void test_startProcess_invalidCommand() {
149149

150150
public void test_startProcess_replaceExistingProcess() {
151151
String sessionId = "test_replace";
152-
List<String> cmdList1 = Arrays.asList("echo", "first");
153-
List<String> cmdList2 = Arrays.asList("echo", "second");
152+
// Use sleep commands that run longer so we can verify they're running
153+
List<String> cmdList1 = Arrays.asList("sleep", "10");
154+
List<String> cmdList2 = Arrays.asList("sleep", "10");
154155
Consumer<ProcessBuilder> pbCall = pb -> {
155156
pb.redirectErrorStream(true);
156157
};
@@ -159,15 +160,22 @@ public void test_startProcess_replaceExistingProcess() {
159160
// Start first process
160161
JobProcess jobProcess1 = processHelper.startProcess(sessionId, cmdList1, pbCall);
161162
assertNotNull(jobProcess1);
163+
164+
// Wait for first process to be running
165+
Thread.sleep(100);
162166
assertTrue(processHelper.isProcessRunning(sessionId));
163167

164168
// Start second process with same session ID (should replace first)
165169
JobProcess jobProcess2 = processHelper.startProcess(sessionId, cmdList2, pbCall);
166170
assertNotNull(jobProcess2);
167-
assertTrue(processHelper.isProcessRunning(sessionId));
168171

169-
// Wait a bit for the processes to complete
172+
// Wait for second process to be running
170173
Thread.sleep(100);
174+
assertTrue(processHelper.isProcessRunning(sessionId));
175+
176+
// Verify we still have only one process for this session
177+
Set<String> sessionIds = processHelper.getRunningSessionIdSet();
178+
assertEquals(1, sessionIds.stream().filter(id -> id.equals(sessionId)).count());
171179

172180
// Clean up
173181
processHelper.destroyProcess(sessionId);
@@ -211,7 +219,8 @@ public void test_multipleProcesses() {
211219

212220
public void test_destroyProcess_withRunningProcess() {
213221
String sessionId = "test_destroy";
214-
List<String> cmdList = Arrays.asList("echo", "hello");
222+
// Use sleep command that runs longer so we can verify it's running
223+
List<String> cmdList = Arrays.asList("sleep", "10");
215224
Consumer<ProcessBuilder> pbCall = pb -> {
216225
pb.redirectErrorStream(true);
217226
};
@@ -220,26 +229,18 @@ public void test_destroyProcess_withRunningProcess() {
220229
JobProcess jobProcess = processHelper.startProcess(sessionId, cmdList, pbCall);
221230
assertNotNull(jobProcess);
222231

223-
// Poll for process to be running (max 50 times, 100ms interval)
224-
boolean isRunning = false;
225-
for (int i = 0; i < 50; i++) {
226-
if (processHelper.isProcessRunning(sessionId)) {
227-
isRunning = true;
228-
break;
229-
}
230-
Thread.sleep(100);
231-
}
232-
assertTrue("Process did not become running within timeout", isRunning);
232+
// Wait for process to start
233+
Thread.sleep(100);
233234

234-
// Wait a bit for the process to start
235-
Thread.sleep(50);
235+
// Verify process is running
236+
assertTrue("Process did not become running within timeout", processHelper.isProcessRunning(sessionId));
236237

237238
// Destroy the process
238239
int exitCode = processHelper.destroyProcess(sessionId);
239240
assertFalse(processHelper.isProcessRunning(sessionId));
240241

241-
// Exit code should be 0 or -1 depending on timing
242-
assertTrue(exitCode == 0 || exitCode == -1);
242+
// Exit code should be non-zero for forcibly destroyed process, or -1
243+
assertTrue(exitCode != 0 || exitCode == -1);
243244
} catch (Exception e) {
244245
fail("Unexpected exception: " + e.getMessage());
245246
}

0 commit comments

Comments
 (0)