diff --git a/src/main/java/de/flapdoodle/embed/process/io/LogWatchStreamProcessor.java b/src/main/java/de/flapdoodle/embed/process/io/LogWatchStreamProcessor.java index 97491a30..c65f7b7f 100644 --- a/src/main/java/de/flapdoodle/embed/process/io/LogWatchStreamProcessor.java +++ b/src/main/java/de/flapdoodle/embed/process/io/LogWatchStreamProcessor.java @@ -37,6 +37,7 @@ public class LogWatchStreamProcessor implements IStreamProcessor { private String failureFound = null; private final IStreamProcessor destination; + private volatile boolean hasResultBeenRetrieved; public LogWatchStreamProcessor(String success, Set failures, IStreamProcessor destination) { this.success = success; @@ -48,16 +49,18 @@ public LogWatchStreamProcessor(String success, Set failures, IStreamProc public void process(String block) { destination.process(block); - CharSequence line = block; - output.append(line); + if (!hasResultBeenRetrieved) { + CharSequence line = block; + output.append(line); - if (output.indexOf(success) != -1) { - gotResult(true,null); - } else { - for (String failure : failures) { - int failureIndex = output.indexOf(failure); - if (failureIndex != -1) { - gotResult(false,output.substring(failureIndex)); + if (output.indexOf(success) != -1) { + gotResult(true, null); + } else { + for (String failure : failures) { + int failureIndex = output.indexOf(failure); + if (failureIndex != -1) { + gotResult(false, output.substring(failureIndex)); + } } } } @@ -94,5 +97,7 @@ public String getOutput() { return output.toString(); } - + public void markResultAsRetrieved() { + hasResultBeenRetrieved = true; + } } diff --git a/src/main/java/de/flapdoodle/embed/process/runtime/Processes.java b/src/main/java/de/flapdoodle/embed/process/runtime/Processes.java index 4b87286f..905016e7 100644 --- a/src/main/java/de/flapdoodle/embed/process/runtime/Processes.java +++ b/src/main/java/de/flapdoodle/embed/process/runtime/Processes.java @@ -172,10 +172,14 @@ public static boolean isProcessRunning(Platform platform, long pid) { // look for the PID in the output, pass it in for 'success' state LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor(""+pid, new HashSet(), StreamToLineProcessor.wrap(Processors.silent())); - Processors.connect(new InputStreamReader(process.getInputStream()), logWatch); - logWatch.waitForResult(2000); - logger.trace("logWatch output: {}", logWatch.getOutput()); - return logWatch.isInitWithSuccess(); + try { + Processors.connect(new InputStreamReader(process.getInputStream()), logWatch); + logWatch.waitForResult(2000); + logger.trace("logWatch output: {}", logWatch.getOutput()); + return logWatch.isInitWithSuccess(); + } finally { + logWatch.markResultAsRetrieved(); + } } } catch (IOException | InterruptedException e) { diff --git a/src/test/java/de/flapdoodle/embed/process/example/TestExampleReadMeCode.java b/src/test/java/de/flapdoodle/embed/process/example/TestExampleReadMeCode.java index 7c3d77e8..517728f0 100644 --- a/src/test/java/de/flapdoodle/embed/process/example/TestExampleReadMeCode.java +++ b/src/test/java/de/flapdoodle/embed/process/example/TestExampleReadMeCode.java @@ -33,6 +33,7 @@ Ben McCann (benmccann@github) import de.flapdoodle.embed.process.distribution.ArchiveType; import de.flapdoodle.embed.process.distribution.Distribution; import de.flapdoodle.embed.process.distribution.GenericVersion; +import de.flapdoodle.embed.process.distribution.Platform; import de.flapdoodle.embed.process.distribution.IVersion; public class TestExampleReadMeCode { @@ -47,14 +48,49 @@ public void genericProcessStarter() throws IOException { IVersion version=new GenericVersion("2.1.1"); + String phantomjsOsIdentifier; + ArchiveType phantomjsArchiveType; + String phantomjsArchiveTypeExtension; + String phantomjsExecutableName; + + Platform platform = Platform.detect(); + switch (platform) { + case Linux: + String osArchitecture = System.getProperty("os.arch"); + if ("x86_64".equals(osArchitecture) || "amd64".equals(osArchitecture)) { + phantomjsOsIdentifier = "linux-x86_64"; + } else { + phantomjsOsIdentifier = "linux-i686"; + } + phantomjsArchiveType = ArchiveType.TBZ2; + phantomjsArchiveTypeExtension = "tar.bz2"; + phantomjsExecutableName = "phantomjs"; + break; + case OS_X: + phantomjsOsIdentifier = "macosx"; + phantomjsArchiveType = ArchiveType.ZIP; + phantomjsArchiveTypeExtension = "zip"; + phantomjsExecutableName = "phantomjs"; + break; + case Windows: + phantomjsOsIdentifier = "windows"; + phantomjsArchiveType = ArchiveType.ZIP; + phantomjsArchiveTypeExtension = "zip"; + phantomjsExecutableName = "phantomjs.exe"; + break; + default: + throw new IllegalStateException("Unsupported operating system: " + platform); + } + + IRuntimeConfig config = new GenericRuntimeConfigBuilder() .name("phantomjs") - //https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 + //https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-. .downloadPath("https://bitbucket.org/ariya/phantomjs/downloads/") .packageResolver() - .files(Distribution.detectFor(version), FileSet.builder().addEntry(FileType.Executable, "phantomjs").build()) - .archivePath(Distribution.detectFor(version), "phantomjs-"+version.asInDownloadPath()+"-linux-x86_64.tar.bz2") - .archiveType(Distribution.detectFor(version), ArchiveType.TBZ2) + .files(Distribution.detectFor(version), FileSet.builder().addEntry(FileType.Executable, phantomjsExecutableName).build()) + .archivePath(Distribution.detectFor(version), "phantomjs-"+version.asInDownloadPath()+ "-"+phantomjsOsIdentifier+"."+phantomjsArchiveTypeExtension) + .archiveType(Distribution.detectFor(version), phantomjsArchiveType) .build() .build(); diff --git a/src/test/java/de/flapdoodle/embed/process/io/file/TestFileCleaner.java b/src/test/java/de/flapdoodle/embed/process/io/file/TestFileCleaner.java index 38fde244..887ccdbc 100644 --- a/src/test/java/de/flapdoodle/embed/process/io/file/TestFileCleaner.java +++ b/src/test/java/de/flapdoodle/embed/process/io/file/TestFileCleaner.java @@ -96,7 +96,7 @@ public void testCleanup() throws IOException, InterruptedException { logger.info("after release lock (wait a little)"); - Thread.sleep(1000); + Thread.sleep(2000); logger.info("check if temp files there (should NOT be)");