Skip to content

Commit b5e7437

Browse files
committed
Fixed inheriting of output from stub server
It got broken because of maven-surefire-plugin update, which now prohibits `ProcessBuilder#inheritIO()` and only allows writing to stdout. Output with `#inheritIO()` did not go to the command line but instead got written to a separate temporary file. Warning "Corrupted stdin stream in forked JVM 1" has also been generated. Seems like maven-surefire-plugin prohibits interactions with the output stream because that's how it talks to the forked JVM. By default "forkCount" is 1 so such JVM exists in our case. See: http://maven.apache.org/surefire/maven-surefire-plugin/faq.html#corruptedstream https://issues.apache.org/jira/browse/SUREFIRE-1359
1 parent 8d180ce commit b5e7437

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

driver/src/test/java/org/neo4j/driver/v1/util/StubServer.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,26 @@
1818
*/
1919
package org.neo4j.driver.v1.util;
2020

21+
import java.io.BufferedReader;
2122
import java.io.File;
2223
import java.io.IOException;
24+
import java.io.InputStreamReader;
2325
import java.net.InetSocketAddress;
2426
import java.net.SocketAddress;
2527
import java.nio.channels.SocketChannel;
2628
import java.util.ArrayList;
2729
import java.util.List;
30+
import java.util.concurrent.ExecutorService;
2831

2932
import org.neo4j.driver.v1.Config;
3033

3134
import static java.lang.Thread.sleep;
3235
import static java.util.Arrays.asList;
3336
import static java.util.Collections.singletonList;
37+
import static java.util.concurrent.Executors.newCachedThreadPool;
3438
import static org.junit.Assert.fail;
3539
import static org.junit.Assume.assumeTrue;
40+
import static org.neo4j.driver.v1.util.DaemonThreadFactory.daemon;
3641

3742
public class StubServer
3843
{
@@ -41,6 +46,8 @@ public class StubServer
4146
public static final Config INSECURE_CONFIG = Config.build()
4247
.withoutEncryption().toConfig();
4348

49+
private static final ExecutorService executor = newCachedThreadPool( daemon( "stub-server-output-reader-" ) );
50+
4451
// This may be thrown if the driver has not been closed properly
4552
public static class ForceKilled extends Exception {}
4653

@@ -53,8 +60,9 @@ private StubServer( String script, int port ) throws IOException, InterruptedExc
5360
List<String> command = new ArrayList<>();
5461
command.addAll( singletonList( BOLT_STUB_COMMAND ) );
5562
command.addAll( asList( Integer.toString( port ), script ) );
56-
ProcessBuilder server = new ProcessBuilder().inheritIO().command( command );
63+
ProcessBuilder server = new ProcessBuilder().command( command );
5764
process = server.start();
65+
startReadingOutput( process );
5866
waitForSocket( port );
5967
}
6068

@@ -128,4 +136,34 @@ private static void waitForSocket( int port ) throws InterruptedException
128136
}
129137
throw new AssertionError( "Can't connect to " + address );
130138
}
139+
140+
/**
141+
* Read output of the given process using a separate thread.
142+
* Since maven-surefire-plugin 2.20.0 it is not good to simply inherit IO using {@link ProcessBuilder#inheritIO()}.
143+
* It will result in "Corrupted stdin stream in forked JVM 1" warning being printed and output being redirected to a
144+
* separate temporary file.
145+
* <p>
146+
* Fore more details see:
147+
* <ul>
148+
* <li>http://maven.apache.org/surefire/maven-surefire-plugin/faq.html#corruptedstream</li>
149+
* <li>https://issues.apache.org/jira/browse/SUREFIRE-1359</li>
150+
* </ul>
151+
*
152+
* @param process the process to read output.
153+
*/
154+
private static void startReadingOutput( Process process )
155+
{
156+
executor.submit( () ->
157+
{
158+
try ( BufferedReader reader = new BufferedReader( new InputStreamReader( process.getInputStream() ) ) )
159+
{
160+
String line;
161+
while ( (line = reader.readLine()) != null )
162+
{
163+
System.out.println( line );
164+
}
165+
}
166+
return null;
167+
} );
168+
}
131169
}

0 commit comments

Comments
 (0)