Skip to content

Commit

Permalink
test: try to fix what looks like a race condition on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
netmikey committed Feb 3, 2023
1 parent e58008f commit 389b93b
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.concurrent.TimeoutException;

import org.junit.jupiter.api.Assertions;
import org.opentest4j.AssertionFailedError;
Expand All @@ -17,13 +18,17 @@
import io.github.netmikey.testprocesses.TestProcessDefinitionBy;
import io.github.netmikey.testprocesses.TestProcessState;
import io.github.netmikey.testprocesses.TestProcessesRegistry;
import io.github.netmikey.testprocesses.eventdetector.LogPatternEventDetector;

/**
* Utility method for asserting and interacting with those test processes
* specifically created for these framework-tests.
*/
public class TestHelper {

private static final TestProcessDefinitionBy<EchoTestProcess> ECHO_TEST_PROCESS = TestProcessDefinitionBy
.clazz(EchoTestProcess.class);

/**
* Sends a line of test to the {@link EchoTestProcess} currently running in
* the specified registry.
Expand All @@ -35,7 +40,7 @@ public class TestHelper {
*/
public static void sendToEchoProcess(TestProcessesRegistry registry, String line) {
Optional<RunningTestProcess<EchoTestProcess>> runningProcess = registry
.retrieveRunningProcess(TestProcessDefinitionBy.clazz(EchoTestProcess.class));
.retrieveRunningProcess(ECHO_TEST_PROCESS);

if (runningProcess.isPresent()) {
try {
Expand All @@ -56,6 +61,14 @@ public static void sendToEchoProcess(TestProcessesRegistry registry, String line
} catch (IOException e) {
throw new UncheckedIOException(e.getMessage(), e);
}

// Wait for the line to appear on the echo process' stdOut
try {
registry.waitForEventOn(ECHO_TEST_PROCESS, LogPatternEventDetector.onStdOut().withMarker(line));
} catch (TimeoutException e) {
throw new RuntimeException("Timeout while waiting for the line to appear on the EchoProcess' stdOut",
e);
}
} else {
throw new IllegalStateException("Trying to write to " + EchoTestProcess.class.getName()
+ "'s stdIn, but no process is running.");
Expand Down

0 comments on commit 389b93b

Please sign in to comment.