Skip to content

Commit a6a114d

Browse files
committed
Fix starting stream consumer
1 parent 7f7546f commit a6a114d

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

product/src/test/java/org/itsallcode/openfasttrace/cli/JarLauncher.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public static JarLauncher start(final Path workingDir, final List<String> args)
4747
try
4848
{
4949
final Process process = processBuilder.start();
50-
final ProcessOutputConsumer consumer = new ProcessOutputConsumer(process);
50+
final ProcessOutputConsumer consumer = new ProcessOutputConsumer(process, Duration.ofSeconds(1));
51+
consumer.start();
5152
return new JarLauncher(process, consumer);
5253
}
5354
catch (final IOException exception)
@@ -79,14 +80,15 @@ private static Path getJavaExecutable()
7980
void waitUntilTerminated(final Duration timeout)
8081
{
8182
waitForProcessTerminated(timeout);
82-
LOG.fine("Process terminated with exit code %d".formatted(exitValue()));
83-
consumer.waitForStreamsClosed(timeout);
83+
LOG.fine("Process %d terminated with exit code %d".formatted(process.pid(), exitValue()));
84+
consumer.waitForStreamsClosed();
8485
}
8586

8687
private void waitForProcessTerminated(final Duration timeout)
8788
{
8889
try
8990
{
91+
LOG.finest("Waiting %s for process %d to terminate...".formatted(timeout, process.pid()));
9092
if (!process.waitFor(timeout.toMillis(), TimeUnit.MILLISECONDS))
9193
{
9294
throw new IllegalStateException(
@@ -96,6 +98,8 @@ private void waitForProcessTerminated(final Duration timeout)
9698
catch (final InterruptedException exception)
9799
{
98100
Thread.currentThread().interrupt();
101+
throw new IllegalStateException("Interrupted while waiting %s for process to finish".formatted(timeout),
102+
exception);
99103
}
100104
}
101105

product/src/test/java/org/itsallcode/openfasttrace/cli/ProcessOutputConsumer.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ class ProcessOutputConsumer
1616
private final ProcessStreamConsumer stdOutConsumer;
1717
private final ProcessStreamConsumer stdErrConsumer;
1818

19-
ProcessOutputConsumer(final Process process)
19+
ProcessOutputConsumer(final Process process, final Duration streamCloseTimeout)
2020
{
21-
this(createThreadExecutor(), process, new ProcessStreamConsumer("stdout"), new ProcessStreamConsumer("stderr"));
21+
this(createThreadExecutor(), process, new ProcessStreamConsumer("stdout", streamCloseTimeout),
22+
new ProcessStreamConsumer("stderr", streamCloseTimeout));
2223
}
2324

2425
ProcessOutputConsumer(final Executor executor, final Process process,
@@ -42,6 +43,7 @@ private static Executor createThreadExecutor()
4243

4344
void start()
4445
{
46+
LOG.finest("Start reading stdout and stderr streams in background...");
4547
executor.execute(() -> {
4648
readStream(process.getInputStream(), stdOutConsumer);
4749
});
@@ -55,10 +57,12 @@ private void readStream(final InputStream stream, final ProcessStreamConsumer co
5557
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)))
5658
{
5759
String line = null;
60+
LOG.finest("Start reading from '%s' stream...".formatted(consumer.name));
5861
while ((line = reader.readLine()) != null)
5962
{
6063
consumer.accept(line);
6164
}
65+
LOG.finest("Stream '%s' finished".formatted(consumer.name));
6266
consumer.streamFinished();
6367
}
6468
catch (final IOException exception)
@@ -78,21 +82,23 @@ String getStdErr()
7882
return stdErrConsumer.getContent();
7983
}
8084

81-
void waitForStreamsClosed(final Duration timeout)
85+
void waitForStreamsClosed()
8286
{
83-
stdOutConsumer.waitUntilStreamClosed(timeout);
84-
stdErrConsumer.waitUntilStreamClosed(timeout);
87+
stdOutConsumer.waitUntilStreamClosed();
88+
stdErrConsumer.waitUntilStreamClosed();
8589
}
8690

8791
private static class ProcessStreamConsumer
8892
{
8993
private final CountDownLatch streamFinished = new CountDownLatch(1);
9094
private final StringBuilder builder = new StringBuilder();
9195
private final String name;
96+
private final Duration streamCloseTimeout;
9297

93-
ProcessStreamConsumer(final String name)
98+
ProcessStreamConsumer(final String name, final Duration streamCloseTimeout)
9499
{
95100
this.name = name;
101+
this.streamCloseTimeout = streamCloseTimeout;
96102
}
97103

98104
String getContent()
@@ -111,12 +117,17 @@ void accept(final String line)
111117
builder.append(line).append("\n");
112118
}
113119

114-
void waitUntilStreamClosed(final Duration timeout)
120+
void waitUntilStreamClosed()
115121
{
116-
if (!await(timeout))
122+
LOG.finest("Waiting %s for stream '%s' to close".formatted(streamCloseTimeout, name));
123+
if (!await(streamCloseTimeout))
117124
{
118125
throw new IllegalStateException(
119-
"Stream '%s' not closed within timeout of %s".formatted(name, timeout));
126+
"Stream '%s' not closed within timeout of %s".formatted(name, streamCloseTimeout));
127+
}
128+
else
129+
{
130+
LOG.finest("Stream '%s' closed".formatted(name));
120131
}
121132
}
122133

@@ -129,7 +140,8 @@ private boolean await(final Duration timeout)
129140
catch (final InterruptedException exception)
130141
{
131142
Thread.currentThread().interrupt();
132-
throw new IllegalStateException("Interrupted while waiting for stream to be closed", exception);
143+
throw new IllegalStateException("Interrupted while waiting for stream '%s' to be closed: %s"
144+
.formatted(name, exception.getMessage()), exception);
133145
}
134146
}
135147
}

0 commit comments

Comments
 (0)