Skip to content

Commit 22423b3

Browse files
committed
Expose exitValue and isAlive methods on the chrome launcher
1 parent 7249401 commit 22423b3

File tree

2 files changed

+107
-9
lines changed

2 files changed

+107
-9
lines changed

cdt-java-client/src/main/java/com/github/kklisura/cdt/launch/ChromeLauncher.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public Path getChromeBinaryPath() {
213213

214214
@Override
215215
public void close() {
216-
if (chromeProcess != null) {
216+
if (chromeProcess != null && chromeProcess.isAlive()) {
217217
LOGGER.info("Closing chrome process...");
218218
chromeProcess.destroy();
219219

@@ -222,14 +222,12 @@ public void close() {
222222
chromeProcess.destroyForcibly();
223223
chromeProcess.waitFor(configuration.getShutdownWaitTime(), TimeUnit.SECONDS);
224224
}
225-
chromeProcess = null;
226225

227226
LOGGER.info("Chrome process closed.");
228227
} catch (InterruptedException e) {
229228
LOGGER.error("Interrupted while waiting for chrome process to shutdown.", e);
230229

231230
chromeProcess.destroyForcibly();
232-
chromeProcess = null;
233231
} finally {
234232
deleteQuietly(userDataDirPath);
235233
}
@@ -242,6 +240,30 @@ public void close() {
242240
}
243241
}
244242

243+
/**
244+
* Returns an exit value. This is just proxy to {@link Process#exitValue}.
245+
*
246+
* @return Exit value of the process if exited.
247+
* @throws {@link IllegalThreadStateException} if the subprocess has not yet terminated. {@link
248+
* IllegalStateException} If the process hasn't even started.
249+
*/
250+
public int exitValue() {
251+
if (chromeProcess == null) {
252+
throw new IllegalStateException("Chrome process has not been started started.");
253+
}
254+
return chromeProcess.exitValue();
255+
}
256+
257+
/**
258+
* Tests whether the subprocess is alive. This is just proxy to {@link Process#isAlive()}.
259+
*
260+
* @return True if the subprocess has not yet terminated.
261+
* @throws IllegalThreadStateException if the subprocess has not yet terminated.
262+
*/
263+
public boolean isAlive() {
264+
return chromeProcess != null && chromeProcess.isAlive();
265+
}
266+
245267
/**
246268
* Launches a chrome process given a chrome binary and its arguments.
247269
*
@@ -254,7 +276,7 @@ public void close() {
254276
*/
255277
private int launchChromeProcess(Path chromeBinary, ChromeArguments chromeArguments)
256278
throws ChromeProcessException {
257-
if (chromeProcess != null) {
279+
if (isAlive()) {
258280
throw new IllegalStateException("Chrome process has already been started started.");
259281
}
260282

cdt-java-client/src/test/java/com/github/kklisura/cdt/launch/ChromeLauncherTest.java

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.easymock.EasyMock.expect;
2727
import static org.easymock.EasyMock.verify;
2828
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertFalse;
2930
import static org.junit.Assert.assertNotNull;
3031
import static org.junit.Assert.assertTrue;
3132
import static org.junit.Assert.fail;
@@ -176,6 +177,7 @@ public void testLaunchWithBinaryAndArgumentsAndDefaultUserDataDir()
176177

177178
final String trigger = "\r\n\r\nDevTools listening on ws://127.0.0.1:9123/";
178179
expect(process.getInputStream()).andReturn(new ByteArrayInputStream(trigger.getBytes()));
180+
expect(process.isAlive()).andReturn(true);
179181

180182
Capture<List<String>> captureArguments = Capture.newInstance();
181183
expect(processLauncher.launch(eq("test-binary-path"), capture(captureArguments)))
@@ -188,7 +190,6 @@ public void testLaunchWithBinaryAndArgumentsAndDefaultUserDataDir()
188190

189191
ChromeService launch = launcher.launch(binaryPath, chromeArguments);
190192

191-
verifyAll();
192193
PowerMock.verify(FilesUtils.class);
193194

194195
assertNotNull(launch);
@@ -213,12 +214,16 @@ public void testLaunchWithBinaryAndArgumentsAndDefaultUserDataDir()
213214
// Ignore this exception.
214215
}
215216

217+
verifyAll();
218+
216219
// Test closing
217220
resetAll();
218221
PowerMock.resetAll(FilesUtils.class);
219222

220223
process.destroy();
221224
expect(process.waitFor(60, TimeUnit.SECONDS)).andReturn(true);
225+
expect(process.isAlive()).andReturn(true);
226+
expect(process.isAlive()).andReturn(false);
222227

223228
shutdownHookRegistry.remove(anyObject());
224229

@@ -260,13 +265,12 @@ public void testLaunchWithBinaryAndArgumentsAndCustomUserDataDir()
260265
Capture<List<String>> captureArguments = Capture.newInstance();
261266
expect(processLauncher.launch(eq("test-binary-path"), capture(captureArguments)))
262267
.andReturn(process);
268+
expect(process.isAlive()).andReturn(true);
263269

264270
replayAll();
265271

266272
ChromeService launch = launcher.launch(binaryPath, chromeArguments);
267273

268-
verifyAll();
269-
270274
assertNotNull(launch);
271275
assertTrue(launch instanceof ChromeServiceImpl);
272276

@@ -289,12 +293,17 @@ public void testLaunchWithBinaryAndArgumentsAndCustomUserDataDir()
289293
// Ignore this exception.
290294
}
291295

296+
verifyAll();
297+
292298
// Test closing
293299
resetAll();
294300

295301
process.destroy();
296302
expect(process.waitFor(60, TimeUnit.SECONDS)).andReturn(true);
297303

304+
expect(process.isAlive()).andReturn(true);
305+
expect(process.isAlive()).andReturn(false);
306+
298307
shutdownHookRegistry.remove(anyObject());
299308

300309
FilesUtils.deleteQuietly(null);
@@ -321,6 +330,7 @@ public void testLaunchWithBinaryAndArgumentsAndCloseWithInterruptedException()
321330

322331
final String trigger = "\r\n\r\nDevTools listening on ws://127.0.0.1:9123/";
323332
expect(process.getInputStream()).andReturn(new ByteArrayInputStream(trigger.getBytes()));
333+
expect(process.isAlive()).andReturn(true);
324334

325335
Capture<List<String>> captureArguments = Capture.newInstance();
326336
expect(processLauncher.launch(eq("test-binary-path"), capture(captureArguments)))
@@ -330,8 +340,6 @@ public void testLaunchWithBinaryAndArgumentsAndCloseWithInterruptedException()
330340

331341
ChromeService launch = launcher.launch(binaryPath, chromeArguments);
332342

333-
verifyAll();
334-
335343
assertNotNull(launch);
336344
assertTrue(launch instanceof ChromeServiceImpl);
337345

@@ -352,6 +360,8 @@ public void testLaunchWithBinaryAndArgumentsAndCloseWithInterruptedException()
352360
// Ignore this exception.
353361
}
354362

363+
verifyAll();
364+
355365
// Test closing
356366
resetAll();
357367

@@ -360,6 +370,8 @@ public void testLaunchWithBinaryAndArgumentsAndCloseWithInterruptedException()
360370

361371
expect(process.destroyForcibly()).andReturn(process);
362372

373+
expect(process.isAlive()).andReturn(true);
374+
363375
shutdownHookRegistry.remove(anyObject());
364376

365377
FilesUtils.deleteQuietly(null);
@@ -446,6 +458,7 @@ public void testLaunchWithBinaryAndArgumentsFailsOnReading()
446458
expect(processLauncher.isExecutable("/test-binary-path")).andReturn(true);
447459

448460
expect(process.getInputStream()).andThrow(new RuntimeException("test exception"));
461+
expect(process.isAlive()).andReturn(true);
449462

450463
Capture<List<String>> captureArguments = Capture.newInstance();
451464
expect(processLauncher.launch(eq("/test-binary-path"), capture(captureArguments)))
@@ -479,6 +492,7 @@ public void testLaunchWithBinaryAndArgumentsThrowsExceptionOnTimeout()
479492

480493
final String trigger = "test\r\ntest";
481494
expect(process.getInputStream()).andReturn(new ByteArrayInputStream(trigger.getBytes()));
495+
expect(process.isAlive()).andReturn(true);
482496

483497
Capture<List<String>> captureArguments = Capture.newInstance();
484498
expect(processLauncher.launch(eq("test-binary-path"), capture(captureArguments)))
@@ -527,6 +541,7 @@ public void testLaunchWithBinaryAndArgumentsThrowsExceptionOnTimeoutForciblyClos
527541

528542
final String trigger = "test\r\n\r\n";
529543
expect(process.getInputStream()).andReturn(new ByteArrayInputStream(trigger.getBytes()));
544+
expect(process.isAlive()).andReturn(true);
530545

531546
Capture<List<String>> captureArguments = Capture.newInstance();
532547
expect(processLauncher.launch(eq("test-binary-path"), capture(captureArguments)))
@@ -559,6 +574,67 @@ public void testLaunchWithBinaryAndArgumentsThrowsExceptionOnTimeoutForciblyClos
559574
assertEquals(removeCaptureShutdownThread.getValue(), addCaptureShutdownThread.getValue());
560575
}
561576

577+
@Test
578+
public void testIsAlive() throws IOException {
579+
assertFalse(launcher.isAlive());
580+
581+
final Path binaryPath = Paths.get("test-binary-path");
582+
583+
final ChromeArguments chromeArguments =
584+
ChromeArguments.builder().incognito().userDataDir("user-data-dir-param").build();
585+
586+
shutdownHookRegistry.register(anyObject());
587+
588+
final String trigger = "\r\n\r\nDevTools listening on ws://127.0.0.1:9123/";
589+
expect(process.getInputStream()).andReturn(new ByteArrayInputStream(trigger.getBytes()));
590+
591+
expect(process.isAlive()).andReturn(true);
592+
593+
Capture<List<String>> captureArguments = Capture.newInstance();
594+
expect(processLauncher.launch(eq("test-binary-path"), capture(captureArguments)))
595+
.andReturn(process);
596+
597+
replayAll();
598+
599+
launcher.launch(binaryPath, chromeArguments);
600+
601+
assertTrue(launcher.isAlive());
602+
603+
verifyAll();
604+
}
605+
606+
@Test(expected = IllegalStateException.class)
607+
public void testExitValueThrowsExceptionWhenProcessNotStarted() throws IOException {
608+
launcher.exitValue();
609+
}
610+
611+
@Test
612+
public void testExitValue() throws IOException {
613+
final Path binaryPath = Paths.get("test-binary-path");
614+
615+
final ChromeArguments chromeArguments =
616+
ChromeArguments.builder().incognito().userDataDir("user-data-dir-param").build();
617+
618+
shutdownHookRegistry.register(anyObject());
619+
620+
final String trigger = "\r\n\r\nDevTools listening on ws://127.0.0.1:9123/";
621+
expect(process.getInputStream()).andReturn(new ByteArrayInputStream(trigger.getBytes()));
622+
623+
expect(process.exitValue()).andReturn(123);
624+
625+
Capture<List<String>> captureArguments = Capture.newInstance();
626+
expect(processLauncher.launch(eq("test-binary-path"), capture(captureArguments)))
627+
.andReturn(process);
628+
629+
replayAll();
630+
631+
launcher.launch(binaryPath, chromeArguments);
632+
633+
assertEquals(123, launcher.exitValue());
634+
635+
verifyAll();
636+
}
637+
562638
private static void assertUserDataDir(List<String> arguments) {
563639
boolean hasUserDataDir = false;
564640
for (String argument : arguments) {

0 commit comments

Comments
 (0)