Skip to content

Commit 6e15a24

Browse files
wait for cli output
1 parent a27ce96 commit 6e15a24

11 files changed

Lines changed: 112 additions & 52 deletions

File tree

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/Cli.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2020 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,7 +18,6 @@
1718
package org.testingisdocumenting.webtau.cli;
1819

1920
import org.testingisdocumenting.webtau.cli.expectation.CliExitCode;
20-
import org.testingisdocumenting.webtau.cli.expectation.CliOutput;
2121
import org.testingisdocumenting.webtau.cli.expectation.CliValidationExitCodeOutputHandler;
2222
import org.testingisdocumenting.webtau.cli.expectation.CliValidationOutputOnlyHandler;
2323
import org.testingisdocumenting.webtau.expectation.ActualPath;
@@ -83,6 +83,17 @@ public void run(String command, ProcessEnv env, CliValidationExitCodeOutputHandl
8383

8484
}
8585

86+
public CliBackgroundCommand runBackground(String command, ProcessEnv env) {
87+
CliBackgroundCommand backgroundCommand = backgroundCommand(command, env);
88+
backgroundCommand.start();
89+
90+
return backgroundCommand;
91+
}
92+
93+
public CliBackgroundCommand runBackground(String command) {
94+
return runBackground(command, ProcessEnv.EMPTY);
95+
}
96+
8697
public CliBackgroundCommand backgroundCommand(String command, ProcessEnv env) {
8798
return new CliBackgroundCommand(command, env);
8899
}
@@ -129,8 +140,8 @@ private void runAndValidate(CliValidationResult validationResult,
129140
}
130141

131142
validationResult.setExitCode(exitCode(runResult.getExitCode()));
132-
validationResult.setOut(cliOutput(runResult));
133-
validationResult.setErr(cliError(runResult));
143+
validationResult.setOut(runResult.getOutput());
144+
validationResult.setErr(runResult.getError());
134145
validationResult.setStartTime(startTime);
135146
validationResult.setElapsedTime(endTime - startTime);
136147

@@ -166,12 +177,4 @@ private static void validateExitCode(CliValidationResult validationResult) {
166177
private CliExitCode exitCode(int exitCode) {
167178
return new CliExitCode(exitCode);
168179
}
169-
170-
private static CliOutput cliOutput(ProcessRunResult runResult) {
171-
return new CliOutput("process output", runResult.getOutput());
172-
}
173-
174-
private static CliOutput cliError(ProcessRunResult runResult) {
175-
return new CliOutput("process error output", runResult.getError());
176-
}
177180
}

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/CliBackgroundCommand.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.testingisdocumenting.webtau.cli;
1818

19+
import org.testingisdocumenting.webtau.cli.expectation.CliOutput;
20+
1921
import java.io.IOException;
2022

2123
public class CliBackgroundCommand {
@@ -40,6 +42,14 @@ public void start() {
4042
}
4143
}
4244

45+
public CliOutput getOutput() {
46+
return backgroundRunResult.getOutput();
47+
}
48+
49+
public CliOutput getError() {
50+
return backgroundRunResult.getError();
51+
}
52+
4353
public void stop() {
4454
backgroundRunResult.destroy();
4555
backgroundRunResult = null;

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/ProcessBackgroundRunResult.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.testingisdocumenting.webtau.cli;
1818

19+
import org.testingisdocumenting.webtau.cli.expectation.CliOutput;
20+
1921
import java.io.IOException;
2022
import java.lang.reflect.Field;
2123

@@ -29,6 +31,9 @@ public class ProcessBackgroundRunResult {
2931
private final Thread consumeErrorThread;
3032
private final Thread consumeOutThread;
3133

34+
private final CliOutput output;
35+
private final CliOutput error;
36+
3237
public ProcessBackgroundRunResult(Process process,
3338
StreamGobbler outputGobbler,
3439
StreamGobbler errorGobbler,
@@ -40,6 +45,8 @@ public ProcessBackgroundRunResult(Process process,
4045
this.errorGobbler = errorGobbler;
4146
this.consumeErrorThread = consumeErrorThread;
4247
this.consumeOutThread = consumeOutThread;
48+
this.output = new CliOutput("process output", outputGobbler);
49+
this.error = new CliOutput("process error output", errorGobbler);
4350
}
4451

4552
public Process getProcess() {
@@ -58,12 +65,12 @@ public void destroy() {
5865
}
5966
}
6067

61-
public StreamGobbler getOutputGobbler() {
62-
return outputGobbler;
68+
public CliOutput getOutput() {
69+
return output;
6370
}
6471

65-
public StreamGobbler getErrorGobbler() {
66-
return errorGobbler;
72+
public CliOutput getError() {
73+
return error;
6774
}
6875

6976
public Thread getConsumeErrorThread() {
@@ -75,11 +82,7 @@ public Thread getConsumeOutThread() {
7582
}
7683

7784
public ProcessRunResult createRunResult() {
78-
return new ProcessRunResult(process.exitValue(),
79-
outputGobbler.getLines(),
80-
errorGobbler.getLines(),
81-
outputGobbler.getException(),
82-
errorGobbler.getException());
85+
return new ProcessRunResult(process.exitValue(), output, error);
8386
}
8487

8588
/**
Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
2+
* Copyright 2020 webtau maintainers
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,46 +16,39 @@
1616

1717
package org.testingisdocumenting.webtau.cli;
1818

19+
import org.testingisdocumenting.webtau.cli.expectation.CliOutput;
20+
1921
import java.io.IOException;
2022
import java.util.List;
2123

2224
public class ProcessRunResult {
23-
private int exitCode;
24-
private List<String> output;
25-
private List<String> error;
26-
27-
private IOException outputReadingException;
28-
private IOException errorReadingException;
29-
30-
public ProcessRunResult(int exitCode,
31-
List<String> output,
32-
List<String> error,
33-
IOException outputReadingException,
34-
IOException errorReadingException) {
25+
private final int exitCode;
26+
private final CliOutput output;
27+
private final CliOutput error;
28+
29+
public ProcessRunResult(int exitCode, CliOutput output, CliOutput error) {
3530
this.exitCode = exitCode;
3631
this.output = output;
3732
this.error = error;
38-
this.outputReadingException = outputReadingException;
39-
this.errorReadingException = errorReadingException;
4033
}
4134

4235
public IOException getOutputReadingException() {
43-
return outputReadingException;
36+
return output.getException();
4437
}
4538

4639
public IOException getErrorReadingException() {
47-
return errorReadingException;
40+
return error.getException();
4841
}
4942

5043
public int getExitCode() {
5144
return exitCode;
5245
}
5346

54-
public List<String> getOutput() {
47+
public CliOutput getOutput() {
5548
return output;
5649
}
5750

58-
public List<String> getError() {
51+
public CliOutput getError() {
5952
return error;
6053
}
6154
}

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/StreamGobbler.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2020 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,27 +25,35 @@
2425
import java.io.InputStream;
2526
import java.io.InputStreamReader;
2627
import java.util.ArrayList;
28+
import java.util.Collections;
2729
import java.util.List;
2830

2931
import static org.testingisdocumenting.webtau.cfg.WebTauConfig.getCfg;
3032

3133
public class StreamGobbler implements Runnable {
3234
private final InputStream stream;
35+
36+
private final StringBuffer full;
3337
private final List<String> lines;
3438
private final boolean renderOutput;
3539

3640
private IOException exception;
3741

3842
public StreamGobbler(InputStream stream) {
3943
this.stream = stream;
40-
this.lines = new ArrayList<>();
44+
this.lines = Collections.synchronizedList(new ArrayList<>());
45+
this.full = new StringBuffer();
4146
this.renderOutput = shouldRenderOutput();
4247
}
4348

4449
public List<String> getLines() {
4550
return lines;
4651
}
4752

53+
public String getFull() {
54+
return full.toString();
55+
}
56+
4857
public IOException getException() {
4958
return exception;
5059
}
@@ -81,6 +90,7 @@ private void consume(BufferedReader bufferedReader) throws IOException {
8190
}
8291

8392
lines.add(line);
93+
full.append(line).append('\n');
8494
}
8595
}
8696

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/expectation/CliOutput.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,26 @@
1616

1717
package org.testingisdocumenting.webtau.cli.expectation;
1818

19+
import org.testingisdocumenting.webtau.cli.StreamGobbler;
1920
import org.testingisdocumenting.webtau.expectation.ActualPath;
2021

21-
import java.util.ArrayList;
22+
import java.io.IOException;
2223
import java.util.List;
2324
import java.util.Set;
2425
import java.util.TreeSet;
2526
import java.util.stream.Collectors;
2627

2728
public class CliOutput implements CliResultExpectations {
2829
private final String id;
29-
private final List<String> lines;
30+
private final StreamGobbler streamGobbler;
3031

3132
private final Set<Integer> matchedLinesIdx;
3233

33-
private final String full;
34-
35-
public CliOutput(String id, List<String> lines) {
34+
public CliOutput(String id, StreamGobbler streamGobbler) {
3635
this.id = id;
37-
this.lines = lines;
36+
this.streamGobbler = streamGobbler;
37+
3838
this.matchedLinesIdx = new TreeSet<>();
39-
this.full = String.join("\n", lines);
4039
}
4140

4241
@Override
@@ -45,23 +44,28 @@ public ActualPath actualPath() {
4544
}
4645

4746
public String get() {
48-
return full;
47+
return streamGobbler.getFull();
4948
}
5049

5150
public List<String> getLines() {
52-
return lines;
51+
return streamGobbler.getLines();
52+
}
53+
54+
public IOException getException() {
55+
return streamGobbler.getException();
5356
}
5457

5558
public void registerMatchedLine(Integer idx) {
5659
matchedLinesIdx.add(idx);
5760
}
5861

5962
public List<String> extractMatchedLines() {
63+
List<String> lines = getLines();
6064
return matchedLinesIdx.stream().map(lines::get).collect(Collectors.toList());
6165
}
6266

6367
@Override
6468
public String toString() {
65-
return full;
69+
return streamGobbler.getFull();
6670
}
6771
}

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/expectation/CliResultExpectations.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ default void shouldNot(ValueMatcher valueMatcher) {
4141

4242
@Override
4343
default void waitTo(ValueMatcher valueMatcher, ExpectationTimer expectationTimer, long tickMillis, long timeOutMillis) {
44-
throw new UnsupportedOperationException();
44+
ValueMatcherExpectationSteps.waitStep(null, this, StepReportOptions.REPORT_ALL,
45+
tokenizedMessage(IntegrationTestsMessageBuilder.id(actualPath().getPath())), valueMatcher,
46+
expectationTimer, tickMillis, timeOutMillis);
4547
}
4648

4749
@Override
4850
default void waitToNot(ValueMatcher valueMatcher, ExpectationTimer expectationTimer, long tickMillis, long timeOutMillis) {
49-
throw new UnsupportedOperationException();
51+
ValueMatcherExpectationSteps.waitNotStep(null, this, StepReportOptions.REPORT_ALL,
52+
tokenizedMessage(IntegrationTestsMessageBuilder.id(actualPath().getPath())), valueMatcher,
53+
expectationTimer, tickMillis, timeOutMillis);
5054
}
5155
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package scenarios.cli
2+
3+
import static org.testingisdocumenting.webtau.WebTauGroovyDsl.*
4+
5+
scenario("wait for output") {
6+
def command = cli.runBackground("scripts/sleeps")
7+
command.output.waitTo contain("line two")
8+
9+
command.output.should contain("line one")
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
echo "welcome sleeping script"
3+
sleep 1
4+
echo "line one"
5+
sleep 1
6+
echo "line two"

webtau-feature-testing/src/test/groovy/org/testingisdocumenting/webtau/featuretesting/WebTauCliFeaturesTest.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class WebTauCliFeaturesTest {
5757
}
5858
}
5959

60+
@Test
61+
void "wait for output"() {
62+
supportedPlatformOnly {
63+
runCli('outputWait.groovy', 'webtau.cfg')
64+
}
65+
}
66+
6067
private static void runCli(String restTestName, String configFileName, String... additionalArgs) {
6168
testRunner.runCli("scenarios/cli/$restTestName",
6269
"scenarios/cli/$configFileName", additionalArgs)

0 commit comments

Comments
 (0)