How to use WaitingConsumer class of org.testcontainers.containers.output package

Best Testcontainers-java code snippet using org.testcontainers.containers.output.WaitingConsumer

Source:IntegrationTest.java Github

copy

Full Screen

...8import org.testcontainers.containers.Network;9import org.testcontainers.containers.output.OutputFrame;10import org.testcontainers.containers.output.Slf4jLogConsumer;11import org.testcontainers.containers.output.ToStringConsumer;12import org.testcontainers.containers.output.WaitingConsumer;13import org.testcontainers.images.builder.ImageFromDockerfile;14import org.testcontainers.junit.jupiter.Container;15import org.testcontainers.junit.jupiter.Testcontainers;16import org.testcontainers.utility.DockerImageName;17import java.util.concurrent.TimeUnit;18import java.util.concurrent.TimeoutException;19import java.util.function.Consumer;20import static io.restassured.RestAssured.*;21@Slf4j22@Testcontainers23public class IntegrationTest {24 public static final String MYSQL_IMAGE = "mysql:5.7.39";25 public static Network network = Network.newNetwork();26 @Container27 public static MySQLContainer mySQLContainer =28 (MySQLContainer)29 new MySQLContainer(DockerImageName.parse(MYSQL_IMAGE))30 .withDatabaseName("dbname")31 .withUsername("admin")32 .withPassword("admin")33 .withNetwork(network)34 .withNetworkAliases("db");35 @Container36 public static GenericContainer phpMyAdmin =37 new GenericContainer(DockerImageName.parse("phpmyadmin:5.2.0"))38 .dependsOn(mySQLContainer)39 .withEnv("PMA_HOST", "db")40 .withEnv("MYSQL_ROOT_PASSWORD", "admin")41 .withNetwork(network)42 .withExposedPorts(80);43 @Container44 public static GenericContainer apiContainer =45 new GenericContainer(46 new ImageFromDockerfile()47 // path -> build context path48 // resourcePath -> the path to the resource based on the classpath49 .withFileFromClasspath("/api.jar", "/api.jar")50 .withDockerfileFromBuilder(51 builder ->52 builder53 .from("adoptopenjdk/openjdk11:ubi")54 .copy("/api.jar", "app.jar")55 .entryPoint("java", "-jar", "/app.jar")56 .build()))57 .dependsOn(mySQLContainer)58 .withNetwork(network)59 .withEnv("spring.datasource.url", "jdbc:mysql://db:3306/dbname")60 .withEnv("spring.datasource.username", "admin")61 .withEnv("spring.datasource.password", "admin")62 .withExposedPorts(8080);63 @BeforeEach64 public void beforeEach() {65 baseURI = "http://localhost";66 port = apiContainer.getFirstMappedPort();67 }68 @AfterEach69 public void afterEach() {70 reset();71 }72 @Test73 public void getAllLogs() {74 final String logs = apiContainer.getLogs();75 log.info("these are the logs {}{}", System.lineSeparator(), logs);76 }77 @Test78 public void getStdOutLogs() {79 final String stdoutLogs = apiContainer.getLogs(OutputFrame.OutputType.STDOUT);80 final String stderrLogs = apiContainer.getLogs(OutputFrame.OutputType.STDERR);81 final String endLogs = apiContainer.getLogs(OutputFrame.OutputType.END);82 log.info("these are stdout logs {}{}", System.lineSeparator(), stdoutLogs);83 log.info("these are stderr logs {}{}", System.lineSeparator(), stderrLogs);84 log.info("these are end logs {}{}", System.lineSeparator(), endLogs);85 }86 @Test87 public void streamingLogs() {88 final Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log).withSeparateOutputStreams();89 apiContainer.followOutput(logConsumer);90 }91 @Test92 public void otherConsumers() throws TimeoutException {93 WaitingConsumer waitingConsumer = new WaitingConsumer();94 ToStringConsumer toStringConsumer = new ToStringConsumer();95 Consumer<OutputFrame> composedConsumer = toStringConsumer.andThen(waitingConsumer);96 apiContainer.followOutput(composedConsumer);97 get("/log");98 waitingConsumer.waitUntil(frame ->99 frame.getUtf8String().contains("task executed"), 30, TimeUnit.SECONDS, 10);100 log.info("my wait ended");101 }102}...

Full Screen

Full Screen

Source:Wait.java Github

copy

Full Screen

...21import com.github.dockerjava.api.DockerClient;22import org.testcontainers.DockerClientFactory;23import org.testcontainers.containers.ContainerLaunchException;24import org.testcontainers.containers.output.OutputFrame;25import org.testcontainers.containers.output.WaitingConsumer;26import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;27import org.testcontainers.containers.wait.strategy.WaitStrategy;28import org.testcontainers.utility.LogUtils;29public class Wait extends org.testcontainers.containers.wait.strategy.Wait {30 /**31 * Convenience method to return a WaitStrategy for log messages using a predicate.32 *33 * @param predicate the predicate to apply to log messages34 * @param times the number of times the pattern is expected35 * @return WaitStrategy36 */37 public static WaitStrategy forLogPredicate(Predicate<OutputFrame> predicate, int times) {38 return new AbstractWaitStrategy() {39 @Override40 protected void waitUntilReady() {41 final DockerClient client = DockerClientFactory.instance().client();42 final WaitingConsumer waitingConsumer = new WaitingConsumer();43 LogUtils.followOutput(client, waitStrategyTarget.getContainerId(), waitingConsumer);44 try {45 waitingConsumer.waitUntil(46 predicate,47 startupTimeout.getSeconds(),48 TimeUnit.SECONDS,49 times50 );51 } catch (TimeoutException e) {52 throw new ContainerLaunchException("Timed out");53 }54 }55 };56 }...

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.WaitingConsumer;3import org.testcontainers.images.builder.ImageFromDockerfile;4import java.io.IOException;5import java.nio.file.Path;6import java.nio.file.Paths;7import java.util.concurrent.TimeUnit;8public class WaitingConsumerTest {9 public static void main(String[] args) throws IOException, InterruptedException {10 Path dockerfile = Paths.get("src/main/resources/Dockerfile");11 ImageFromDockerfile image = new ImageFromDockerfile().withFileFromPath(".", dockerfile);12 WaitingConsumer waitingConsumer = new WaitingConsumer();13 GenericContainer container = new GenericContainer(image)14 .withExposedPorts(8080)15 .waitingFor(waitingConsumer);16 container.start();17 waitingConsumer.waitUntil(frame -> frame.getUtf8String().contains("Started Application in"), 10, TimeUnit.SECONDS);18 container.stop();19 }20}

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.output.WaitingConsumer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import org.testcontainers.containers.output.BaseConsumer;5import org.testcontainers.containers.output.OutputFrame.OutputType;6import org.testcontainers.containers.output.WaitingConsumer;7import org.testcontainers.containers.output.ToStringConsumer;8import org.testcontainers.containers.output.BaseConsumer;9import org.testcontainers.containers.output.OutputFrame.OutputType;10import java.util.concurrent.TimeUnit;11import java.util.concurrent.TimeoutException;12import java.util.concurrent.ExecutionException;13import org.testcontainers.containers.output.OutputFrame;14import org.testcontainers.containers.output.OutputFrame.OutputType;15import java.util.concurrent.TimeUnit;16import java.util.concurrent.TimeoutException;17import java.util.concurrent.ExecutionException;18import org.testcontainers.containers.output.OutputFrame;19import org.testcontainers.containers.output.OutputFrame.OutputType;20import java.util.concurrent.TimeUnit;21import java.util.concurrent.TimeoutException;22import java.util.concurrent.ExecutionException;23import org.testcontainers.containers.output.OutputFrame;24import org.testcontainers.containers.output.OutputFrame.OutputType;25import java.util.concurrent.TimeUnit;26import java.util.concurrent.TimeoutException;27import java.util.concurrent.ExecutionException;28import org.testcontainers.containers.output.OutputFrame;29import org.testcontainers.containers.output.OutputFrame.OutputType;30import java.util.concurrent.TimeUnit;31import java.util.concurrent.TimeoutException;32import java.util.concurrent.ExecutionException;33import org.testcontainers.containers.output.OutputFrame;34import org.testcontainers.containers.output.OutputFrame.OutputType;35import java.util.concurrent.TimeUnit;36import java.util.concurrent.TimeoutException;37import java.util.concurrent.ExecutionException;38import org.testcontainers.containers.output.OutputFrame;39import org.testcontainers.containers.output.OutputFrame.OutputType;40import java.util.concurrent.TimeUnit;41import java.util.concurrent.TimeoutException;42import java.util.concurrent.ExecutionException;43import org.testcontainers.containers.output.OutputFrame;44import org.testcontainers.containers.output.OutputFrame.OutputType;45import java.util.concurrent.TimeUnit;46import java.util.concurrent.TimeoutException;47import java.util.concurrent.ExecutionException;48import org.testcontainers.containers.output.OutputFrame;49import org.testcontainers.containers.output.OutputFrame.OutputType;50import java.util.concurrent.TimeUnit;51import java.util.concurrent.TimeoutException;52import java.util.concurrent.ExecutionException;53import org.testcontainers.containers.output.OutputFrame;54import org.testcontainers.containers.output.OutputFrame.OutputType;55import java.util.concurrent.TimeUnit;56import java.util.concurrent.TimeoutException;57import java.util.concurrent.ExecutionException;58import org.testcontainers

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.output.WaitingConsumer;2import org.testcontainers.containers.KafkaContainer;3import org.testcontainers.containers.GenericContainer;4import org.apache.kafka.clients.consumer.KafkaConsumer;5import java.util.Properties;6import org.apache.kafka.clients.consumer.ConsumerRecords;7import org.apache.kafka.clients.consumer.ConsumerRecord;8import org.apache.kafka.common.serialization.StringDeserializer;9import java.util.Collection;10import java.util.Arrays;11import java.time.Duration;12import java.lang.Thread;13import java.util.concurrent.TimeUnit;14import java.util.ArrayList;15import java.util.List;16import java.lang.String;17public class TestKafka {18 public static void main(String[] args) throws Exception {19 KafkaContainer kafka = new KafkaContainer();20 kafka.start();21 GenericContainer container = new GenericContainer("confluentinc/cp-kafka:latest");22 container.withCommand("tail -f /dev/null");23 container.start();24 WaitingConsumer consumer = new WaitingConsumer();25 KafkaConsumer<String, String> kafkaConsumer = createConsumer(kafka.getBootstrapServers());26 kafkaConsumer.subscribe(Arrays.asList("test"));27 kafkaConsumer.poll(Duration.ofMillis(100));

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.output.WaitingConsumer;2import org.testcontainers.containers.output.OutputFrame;3public class WaitingConsumerExample {4 public static void main(String[] args) throws Exception {5 WaitingConsumer consumer = new WaitingConsumer();6 OutputFrame frame = consumer.getFrame();7 System.out.println(frame);8 }9}

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers.output;2import org.testcontainers.containers.GenericContainer;3public class WaitingConsumerTest {4 public static void main(String[] args) {5 GenericContainer container = new GenericContainer("alpine:3.7")6 .withCommand("sh", "-c", "echo hello");7 container.start();8 WaitingConsumer consumer = new WaitingConsumer();9 container.followOutput(consumer);10 consumer.waitUntil(frame -> frame.getUtf8String().contains("hello"));11 }12}13package org.testcontainers.containers.output;14import org.testcontainers.containers.GenericContainer;15public class OutputFrameTest {16 public static void main(String[] args) {17 GenericContainer container = new GenericContainer("alpine:3.7")18 .withCommand("sh", "-c", "echo hello");19 container.start();20 container.followOutput(new OutputFrame.OutputFrameMatcher() {21 public void accept(OutputFrame outputFrame) {22 System.out.println(outputFrame.getUtf8String());23 }24 public boolean test(OutputFrame outputFrame) {25 return outputFrame.getUtf8String().contains("hello");26 }27 });28 }29}30package org.testcontainers.containers.output;31import org.testcontainers.containers.GenericContainer;32public class OutputFrameTest {33 public static void main(String[] args) {34 GenericContainer container = new GenericContainer("alpine:3.7")35 .withCommand("sh", "-c", "echo hello");36 container.start();37 container.followOutput(new OutputFrame.OutputFrameMatcher() {38 public void accept(OutputFrame outputFrame) {39 System.out.println(outputFrame.getUtf8String());40 }41 public boolean test(OutputFrame outputFrame) {42 return outputFrame.getUtf8String().contains("hello");43 }44 });45 }46}47package org.testcontainers.containers.output;48import org.testcontainers.containers.GenericContainer;49public class OutputFrameTest {50 public static void main(String[] args) {

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.KafkaContainer;2import org.testcontainers.containers.output.WaitingConsumer;3import java.util.concurrent.TimeUnit;4public class TestContainerExample {5 public static void main(String[] args) throws Exception {6 KafkaContainer kafka = new KafkaContainer("5.2.1");7 kafka.start();8 WaitingConsumer waitingConsumer = new WaitingConsumer();9 kafka.followOutput(waitingConsumer);10 waitingConsumer.waitUntil(frame -> frame.getUtf8String().contains("started (kafka.server.KafkaServer)"), 60, TimeUnit.SECONDS);11 System.out.println("Kafka is up and running");12 kafka.stop();13 }14}

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers.output;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.output.WaitingConsumer;4import org.testcontainers.containers.output.OutputFrame;5public class TestWaitingConsumer {6 public static void main(String[] args) throws Exception {7 try (GenericContainer container = new GenericContainer("ubuntu:latest")8 .withCommand("bash", "-c", "echo hello world; sleep 5")9 ) {10 container.start();11 WaitingConsumer consumer = new WaitingConsumer();12 container.followOutput(consumer);13 consumer.waitUntil(frame -> {14 String utf8String = frame.getUtf8String();15 return utf8String.contains("hello world");16 });17 System.out.println("Found 'hello world' in container output");18 }19 }20}

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.output.WaitingConsumer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4WaitingConsumer consumer = new WaitingConsumer();5container.followOutput(consumer);6container.waitingFor(Wait.forLogMessage(".*Server started.*", 1));7List<OutputFrame> output = consumer.toIterable();8output.forEach((frame) -> System.out.println(frame.getUtf8String()));9import org.testcontainers.containers.output.ToStringConsumer;10String output = container.followOutput(new ToStringConsumer()).get();11System.out.println(output);

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.WaitingConsumer;3import java.io.IOException;4public class Main {5 public static void main(String[] args) throws IOException, InterruptedException {6 GenericContainer container = new GenericContainer("ubuntu:latest");7 container.withCommand("sh", "-c", "echo hello && sleep 10 && echo world");8 WaitingConsumer consumer = new WaitingConsumer();9 container.followOutput(consumer);10 container.start();11 consumer.waitUntil(frame -> frame.getUtf8String().contains("hello"));12 System.out.println("container output: " + consumer.getOutput());13 container.stop();14 }15}16import org.testcontainers.containers.GenericContainer;17import org.testcontainers.containers.output.WaitingConsumer;18import java.io.IOException;19public class Main {20 public static void main(String[] args) throws IOException, InterruptedException {21 GenericContainer container = new GenericContainer("ubuntu:latest");22 container.withCommand("sh", "-c", "echo hello && sleep 10 && echo world");23 WaitingConsumer consumer = new WaitingConsumer();24 container.followOutput(consumer);25 container.start();26 consumer.waitUntil(frame -> frame.getUtf8String().contains("world"));27 System.out.println("container output: " + consumer.getOutput());28 container.stop();29 }30}31import org.testcontainers.containers.GenericContainer;32import org.testcontainers.containers.output.OutputFrame;33import java.io.IOException;34import java.util.concurrent.TimeUnit;35public class Main {36 public static void main(String[] args) throws IOException, InterruptedException {37 GenericContainer container = new GenericContainer("ubuntu:latest");38 container.withCommand("sh", "-c", "echo hello && sleep 10 && echo world");39 container.start();40 OutputFrame frame = container.waitingFor(Wait.forLogMessage(".*hello.*", 1))41 .getLogConsumer()

Full Screen

Full Screen

WaitingConsumer

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers.output;2import org.testcontainers.containers.Container;3import java.io.IOException;4import java.io.InputStream;5import java.io.InterruptedIOException;6import java.io.OutputStream;7import java.util.concurrent.CompletableFuture;8import java.util.concurrent.CompletionStage;9import java.util.function.Consumer;10import static java.util.concurrent.TimeUnit.MILLISECONDS;11 * Note that the output is consumed asynchronously, so it is important to call {@link #awaitCompletion()} or12public class WaitingConsumer implements Consumer<OutputFrame> {13 private final OutputStream outputStream;14 private final CompletableFuture<Void> future = new CompletableFuture<>();15 public WaitingConsumer(OutputStream outputStream) {16 this.outputStream = outputStream;17 }18 public static WaitingConsumer waitFor(OutputStream outputStream) {19 return new WaitingConsumer(outputStream);20 }21 public static WaitingConsumer waitFor(InputStream outputStream) {22 return new WaitingConsumer(outputStream);23 }24 public static WaitingConsumer waitFor(OutputStream outputStream, Container container) {25 return new WaitingConsumer(outputStream, container);26 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in WaitingConsumer

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful