2828import java .util .List ;
2929import java .util .Map ;
3030import java .util .concurrent .CountDownLatch ;
31+ import java .util .concurrent .ExecutorService ;
32+ import java .util .concurrent .Executors ;
33+ import java .util .concurrent .Future ;
3134import java .util .concurrent .TimeUnit ;
3235import java .util .regex .Pattern ;
3336import org .eclipse .jetty .client .HttpClient ;
@@ -47,6 +50,8 @@ public class IntegrationTest {
4750
4851 private static final String SERVER_READY_STRING = "Started ServerConnector" ;
4952
53+ private static final ExecutorService EXECUTOR = Executors .newCachedThreadPool ();
54+
5055 private static int serverPort ;
5156
5257 /**
@@ -264,7 +269,7 @@ private void testFunction(
264269 String target ,
265270 ImmutableList <String > extraArgs ,
266271 TestCase ... testCases ) throws Exception {
267- Process server = startServer (signatureType , target , extraArgs );
272+ ServerProcess serverProcess = startServer (signatureType , target , extraArgs );
268273 try {
269274 HttpClient httpClient = new HttpClient ();
270275 httpClient .start ();
@@ -280,9 +285,11 @@ private void testFunction(
280285 assertThat (response .getContentAsString ()).isEqualTo (testCase .expectedResponseText ());
281286 }
282287 } finally {
283- server .destroy ();
284- server .waitFor ();
288+ serverProcess .close ();
285289 }
290+ // Wait for the output monitor task to terminate. If it threw an exception, we will get an
291+ // ExecutionException here.
292+ serverProcess .outputMonitorResult ().get ();
286293 }
287294
288295 private enum SignatureType {
@@ -301,7 +308,22 @@ public String toString() {
301308 }
302309 }
303310
304- private Process startServer (
311+ @ AutoValue
312+ abstract static class ServerProcess implements AutoCloseable {
313+ abstract Process process ();
314+ abstract Future <?> outputMonitorResult ();
315+
316+ static ServerProcess of (Process process , Future <?> outputMonitorResult ) {
317+ return new AutoValue_IntegrationTest_ServerProcess (process , outputMonitorResult );
318+ }
319+
320+ @ Override
321+ public void close () {
322+ process ().destroy ();
323+ }
324+ }
325+
326+ private ServerProcess startServer (
305327 SignatureType signatureType , String target , ImmutableList <String > extraArgs )
306328 throws IOException , InterruptedException {
307329 File javaHome = new File (System .getProperty ("java.home" ));
@@ -325,10 +347,11 @@ private Process startServer(
325347 processBuilder .environment ().putAll (environment );
326348 Process serverProcess = processBuilder .start ();
327349 CountDownLatch ready = new CountDownLatch (1 );
328- new Thread (() -> monitorOutput (serverProcess .getInputStream (), ready )).start ();
350+ Future <?> outputMonitorResult = EXECUTOR .submit (
351+ () -> monitorOutput (serverProcess .getInputStream (), ready ));
329352 boolean serverReady = ready .await (5 , TimeUnit .SECONDS );
330353 assertWithMessage ("Waiting for server to be ready" ).that (serverReady ).isTrue ();
331- return serverProcess ;
354+ return ServerProcess . of ( serverProcess , outputMonitorResult ) ;
332355 }
333356
334357 private void monitorOutput (InputStream processOutput , CountDownLatch ready ) {
@@ -339,6 +362,9 @@ private void monitorOutput(InputStream processOutput, CountDownLatch ready) {
339362 ready .countDown ();
340363 }
341364 System .out .println (line );
365+ if (line .contains ("WARNING" )) {
366+ throw new AssertionError ("Found warning in server output:\n " + line );
367+ }
342368 }
343369 } catch (IOException e ) {
344370 e .printStackTrace ();
0 commit comments