Skip to content
This repository was archived by the owner on Sep 6, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/main/java/com/github/junit5docker/DockerExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static java.util.concurrent.CompletableFuture.supplyAsync;
Expand Down Expand Up @@ -56,7 +57,7 @@ private void waitForLogAccordingTo(WaitFor waitFor) {
String expectedLog = waitFor.value();
if (!WaitFor.NOTHING.equals(expectedLog)) {
ExecutorService executor = Executors.newSingleThreadExecutor();
CompletableFuture<Boolean> logFound = supplyAsync(findFirstLogContaining(expectedLog), executor);
CompletableFuture<Boolean> logFound = supplyAsync(findFirstLogContaining(waitFor), executor);
executor.shutdown();
try {
boolean termination = executor.awaitTermination(waitFor.timeoutInMillis(), TimeUnit.MILLISECONDS);
Expand All @@ -72,10 +73,16 @@ private void waitForLogAccordingTo(WaitFor waitFor) {
}
}

private Supplier<Boolean> findFirstLogContaining(String logToFind) {
private Supplier<Boolean> findFirstLogContaining(WaitFor logToFind) {
return () -> {
try (Stream<String> logs = dockerClient.logs(containerId)) {
return logs.anyMatch(log -> log.contains(logToFind));
if (WaitFor.Engine.PLAIN_TEXT.equals(logToFind.engine())) {
return logs.anyMatch(log -> log.contains(logToFind.value()));
}else{
// Regex
Pattern pattern = Pattern.compile(logToFind.value());
return logs.anyMatch(log -> pattern.matcher(log).find());
}
}
};
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/github/junit5docker/WaitFor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,24 @@
*/
String value();

/**
* @return engine use to parse log
*/
Engine engine() default Engine.PLAIN_TEXT ;

/**
* @return the time in milliseconds to wait for the log before giving up.
*/
int timeoutInMillis() default DEFAULT_TIMEOUT;

enum Engine {
/**
* search for simple string
*/
PLAIN_TEXT,
/**
* search for regex pattern
*/
REGEX
}
}
45 changes: 45 additions & 0 deletions src/test/java/com/github/junit5docker/WaitForLogRegexIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.junit5docker;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static com.github.junit5docker.WaitFor.Engine.REGEX;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;


@Docker(image = "faustxvi/open-port-later", ports = @Port(exposed = 8801, inner = 8080),
environments = @Environment(key = "WAITING_TIME", value = "1s"),
waitFor = @WaitFor(value="[a-z]{7}",engine = REGEX))
public class WaitForLogRegexIT {

@BeforeEach
void verifyContainerIsReady() {
checkConnectionToContainer();
}

@Test
void verifyFirstContainerIsStarted() {
checkConnectionToContainer();
}

@AfterEach
void verifyContainerIsStillAlive() {
checkConnectionToContainer();
}

private void checkConnectionToContainer() {
try (CloseableHttpResponse container = HttpClientBuilder.create().build()
.execute(new HttpGet("http://localhost:8801"))) {
assertThat(container).isNotNull();
} catch (IOException e) {
fail("The port 8801 should be listening");
}
}
}