Skip to content

Commit e58008f

Browse files
committed
fix: use StreamStart.CURRENT_TEST for imperatively started processes
A misleading sanity check was failing when it was tried to retrieve one of the process' streams with `StreamStart.CURRENT_TEST` when the process was started manually / using the API during that very test (so if the process wasn't already running before the start of the test). This has been fixed and is now possible. In this case, the whole stream is returned since the entirety of it took place during the current test.
1 parent 038b636 commit e58008f

File tree

2 files changed

+82
-9
lines changed

2 files changed

+82
-9
lines changed

testprocesses-core/src/main/java/io/github/netmikey/testprocesses/TestProcessesRegistry.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,6 @@ private InputStream streamOf(TestProcessDefinitionBy<?> testProcessDefinitionBy,
250250

251251
Optional<Long> streamFileStartPositionOfCurrentTest = streamPositionRetriever.apply(runningTestProcess);
252252

253-
if (StreamStart.CURRENT_TEST.equals(streamStart)
254-
&& streamFileStartPositionOfCurrentTest.isEmpty()) {
255-
throw new IllegalStateException(
256-
"Test process " + runningTestProcess.getDefinition().getProcessIdentifier()
257-
+ " defined in bean " + runningTestProcess.getDefinition()
258-
+ " seems to be running but does not have the current test's start position on its "
259-
+ streamDescription + " marked. This shouldn't happen.");
260-
}
261-
262253
try {
263254
/*
264255
* Open new channel, seek to the position in it where the current
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package io.github.netmikey.testprocesses.functional;
2+
3+
import static io.github.netmikey.testprocesses.TestProcessDefinitionBy.*;
4+
import static io.github.netmikey.testprocesses.functional.testfixtures.TestHelper.*;
5+
6+
import org.junit.jupiter.api.TestMethodOrder;
7+
import org.assertj.core.api.Assertions;
8+
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
9+
import org.junit.jupiter.api.Order;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
13+
import io.github.netmikey.testprocesses.StartStrategy;
14+
import io.github.netmikey.testprocesses.TestProcessDefinitionBy;
15+
import io.github.netmikey.testprocesses.TestProcessesRegistry;
16+
import io.github.netmikey.testprocesses.functional.testfixtures.EchoTestProcess;
17+
import io.github.netmikey.testprocesses.utils.StreamStart;
18+
19+
/**
20+
* Test the bahvior of the {@link StreamStart} setting when retrieving streams
21+
* from a test process that has been started imperatively using the API (instead
22+
* of the test-method-bound annotation).
23+
* <p>
24+
* To do this, the test method execution is ordered. Beware of the order when
25+
* editing this test. Also, since the order and the state between the running
26+
* test methods is important, running single test methods will fail of course.
27+
*/
28+
@TestProcessesSpringBootTest
29+
@TestMethodOrder(OrderAnnotation.class)
30+
public class StreamStartImperativeTest {
31+
32+
private static final String MARKER_1 = "+++ MARKER_1";
33+
34+
private static final String MARKER_2 = "+++ MARKER_2";
35+
36+
@Autowired
37+
private TestProcessesRegistry registry;
38+
39+
private TestProcessDefinitionBy<EchoTestProcess> echoTestProcess = clazz(EchoTestProcess.class);
40+
41+
/**
42+
* Test starting a test process imperatively / "manually" / using the API
43+
* and then retrieving its stdOut stream.
44+
*/
45+
@Test
46+
@Order(1)
47+
public void testWithProcessStartedViaAPI() {
48+
// Start EchoTestProcess using API
49+
registry.start(echoTestProcess, StartStrategy.REQUIRE_RESTART);
50+
51+
sendToEchoProcess(registry, MARKER_1);
52+
53+
// JUnit Test was running before EchoTestProcess, should return the
54+
// whole stream
55+
String absoluteLog = registry.stdOutAsStringOf(echoTestProcess, StreamStart.ABSOLUTE);
56+
String currentTestLog = registry.stdOutAsStringOf(echoTestProcess, StreamStart.CURRENT_TEST);
57+
58+
// Both should be equal
59+
Assertions.assertThat(absoluteLog).isEqualTo(currentTestLog);
60+
}
61+
62+
/**
63+
* Test that a test process started imperatively / "manually" / using the
64+
* API in a previous test obtains the "current test" markers on its streams
65+
* when the present test starts and behaves as any regular already-running
66+
* process.
67+
*/
68+
@Test
69+
@Order(2)
70+
public void testWithProcessFromPreviousTestStartedViaAPI() {
71+
// EchoTestProcess should still be running
72+
73+
sendToEchoProcess(registry, MARKER_2);
74+
75+
Assertions.assertThat(registry.stdOutAsStringOf(echoTestProcess, StreamStart.ABSOLUTE))
76+
.contains(MARKER_1, MARKER_2);
77+
78+
Assertions.assertThat(registry.stdOutAsStringOf(echoTestProcess, StreamStart.CURRENT_TEST))
79+
.contains(MARKER_2)
80+
.doesNotContain(MARKER_1);
81+
}
82+
}

0 commit comments

Comments
 (0)