Skip to content

Commit

Permalink
Merge pull request #108 from jstuyts/process-2.x.x
Browse files Browse the repository at this point in the history
#106 Allow clients to indicate that LogWatchStreamProcessor can stop using memory
  • Loading branch information
michaelmosmann committed Apr 23, 2021
2 parents 7ad4442 + ca39d82 commit 54e1ecb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> failures, IStreamProcessor destination) {
this.success = success;
Expand All @@ -48,16 +49,18 @@ public LogWatchStreamProcessor(String success, Set<String> 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));
}
}
}
}
Expand Down Expand Up @@ -94,5 +97,7 @@ public String getOutput() {
return output.toString();
}


public void markResultAsRetrieved() {
hasResultBeenRetrieved = true;
}
}
12 changes: 8 additions & 4 deletions src/main/java/de/flapdoodle/embed/process/runtime/Processes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>(), 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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-<OS identifier>.<archive extension>
.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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)");

Expand Down

0 comments on commit 54e1ecb

Please sign in to comment.