Skip to content

Commit

Permalink
System Tests > Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gitseti committed Sep 5, 2022
1 parent 3b38928 commit d38f54b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void test_publish_missing_topic() throws Exception {

final Process pub = new ProcessBuilder(publishCommand).start();

cliTestExtension.waitForErrorWithTimeout(pub, "Missing required option: '--topic <topics>'");
cliTestExtension.waitForErrorWithTimeout(pub, "Missing required option: '--topic=<topics>'");
assertEquals(pub.waitFor(), 2);
}

Expand All @@ -113,8 +113,9 @@ void test_publish_missing_message() throws Exception {
final Process pub = new ProcessBuilder(publishCommand).start();

cliTestExtension.waitForErrorWithTimeout(pub, Set.of(
"Error: Missing required argument (specify one of these): (-m <messageFromCommandline> | -m:file <messageFromFile>)",
"Error: Missing required argument (specify one of these): (-m:file <messageFromFile> | -m <messageFromCommandline>)"));
"Error: Missing required argument (specify one of these): (-m=<messageFromCommandline> | -m:file=<messageFromFile>)",
"Error: Missing required argument (specify one of these): (-m:file=<messageFromFile> | -m=<messageFromCommandline>)"));

assertEquals(pub.waitFor(), 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void test_subscribe_missing_topic() throws Exception {
publishCommand.add(String.valueOf(hivemq.getMqttPort()));
final Process sub = new ProcessBuilder(publishCommand).start();

cliTestExtension.waitForErrorWithTimeout(sub, "Missing required option: '--topic <topics>'");
cliTestExtension.waitForErrorWithTimeout(sub, "Missing required option: '--topic=<topics>'");
assertEquals(sub.waitFor(), 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ void test_successful_connect() {
@Test
@Timeout(value = 3, unit = TimeUnit.MINUTES)
void test_unsuccessful_connect() {
cliShellTestExtension.executeCommandWithErrorWithTimeout("con -h localhost -p 22 -i cliTest", Set.of(
"Connection refused: localhost/127.0.0.1:22",
"readAddress(..) failed: Connection reset by peer",
"Connection reset"));
cliShellTestExtension.executeCommandWithErrorWithTimeout("con -h localhost -p 22 -i cliTest",
"Unable to connect. Connection refused");
}
}
13 changes: 5 additions & 8 deletions src/systemTest/java/com/hivemq/cli/utils/CLITestExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import static org.testcontainers.shaded.org.awaitility.Awaitility.await;

public class CLITestExtension {

public static final @NotNull List<String> CLI_EXEC =
Expand Down Expand Up @@ -81,11 +83,6 @@ public void waitForErrorWithTimeout(
waitForErrorWithTimeout(process, Set.of(expectedError));
}

public @NotNull CompletableFuture<Void> waitForError(
final @NotNull Process process, final @NotNull String expectedError) {
return waitForError(process, Set.of(expectedError));
}

public void waitForErrorWithTimeout(
final @NotNull Process process, final @NotNull Set<String> expectedErrors) {
try {
Expand All @@ -101,14 +98,14 @@ public void waitForErrorWithTimeout(
final InputStreamReader errorStreamReader = new InputStreamReader(errorStream, StandardCharsets.UTF_8);
final BufferedReader bufferedErrorReader = new BufferedReader(errorStreamReader);

final ArrayList<CompletableFuture<Void>> commandFinished = new ArrayList<>();
final ArrayList<CompletableFuture<Void>> errorReadFutures = new ArrayList<>();
for (final String expectedError : expectedErrors) {
commandFinished.add(errorConsumer.waitFor(expectedError));
errorReadFutures.add(errorConsumer.waitFor(expectedError));
}
final StringBuilder errorBuilder = new StringBuilder();

return CompletableFuture.runAsync(() -> {
while (commandFinished.stream().filter(CompletableFuture::isDone).findAny().isEmpty()) {
while (errorReadFutures.stream().filter(CompletableFuture::isDone).findAny().isEmpty()) {
try {
final int inputChar = bufferedErrorReader.read();
if (inputChar == -1) {
Expand Down
12 changes: 0 additions & 12 deletions src/systemTest/java/com/hivemq/cli/utils/CommandConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,17 @@

public class CommandConsumer implements Consumer<String> {

private final @NotNull Map<String, CompletableFuture<Void>> patterns = new ConcurrentHashMap<>();
private final @NotNull Map<String, CompletableFuture<Void>> contains = new ConcurrentHashMap<>();

@Override
public void accept(final @NotNull String commandLine) {
patterns.forEach((pattern, future) -> {
if (commandLine.trim().matches(pattern)) {
future.complete(null);
}
});
contains.forEach((command, future) -> {
if (commandLine.contains(command)) {
future.complete(null);
}
});
}

public @NotNull CompletableFuture<Void> waitForPattern(final @NotNull String pattern) {
final CompletableFuture<Void> future = new CompletableFuture<>();
patterns.put(pattern, future);
return future;
}

public @NotNull CompletableFuture<Void> waitFor(final @NotNull String command) {
final CompletableFuture<Void> future = new CompletableFuture<>();
contains.put(command, future);
Expand Down

0 comments on commit d38f54b

Please sign in to comment.