Java Code Examples for org.testcontainers.containers.output.WaitingConsumer#waitUntil()

The following examples show how to use org.testcontainers.containers.output.WaitingConsumer#waitUntil() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EtcdContainer.java    From etcd4j with Apache License 2.0 7 votes vote down vote up
private WaitStrategy waitStrategy() {
    return new AbstractWaitStrategy() {
        @Override
        protected void waitUntilReady() {
            final DockerClient client = DockerClientFactory.instance().client();
            final WaitingConsumer waitingConsumer = new WaitingConsumer();

            LogUtils.followOutput(client, waitStrategyTarget.getContainerId(), waitingConsumer);

            try {
                waitingConsumer.waitUntil(
                    f -> f.getUtf8String().contains("etcdserver: published"),
                    startupTimeout.getSeconds(),
                    TimeUnit.SECONDS,
                    1
                );
            } catch (TimeoutException e) {
                throw new ContainerLaunchException("Timed out");
            }
        }
    };
}
 
Example 2
Source File: StartupCheckStrategyTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
private static void waitForHello(GenericContainer container) throws TimeoutException {
    WaitingConsumer consumer = new WaitingConsumer();
    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame ->
        frame.getUtf8String().contains(HELLO_TESTCONTAINERS), 30, TimeUnit.SECONDS);
}
 
Example 3
Source File: LogMessageWaitStrategy.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
@SneakyThrows(IOException.class)
protected void waitUntilReady() {
    WaitingConsumer waitingConsumer = new WaitingConsumer();

    LogContainerCmd cmd = DockerClientFactory.instance().client().logContainerCmd(waitStrategyTarget.getContainerId())
        .withFollowStream(true)
        .withSince(0)
        .withStdOut(true)
        .withStdErr(true);

    try (FrameConsumerResultCallback callback = new FrameConsumerResultCallback()) {
        callback.addConsumer(STDOUT, waitingConsumer);
        callback.addConsumer(STDERR, waitingConsumer);

        cmd.exec(callback);

        Predicate<OutputFrame> waitPredicate = outputFrame ->
            // (?s) enables line terminator matching (equivalent to Pattern.DOTALL)
            outputFrame.getUtf8String().matches("(?s)" + regEx);

        try {
            waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times);
        } catch (TimeoutException e) {
            throw new ContainerLaunchException("Timed out waiting for log output matching '" + regEx + "'");
        }
    }
}
 
Example 4
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test(timeout = 60_000L)
public void testLogConsumer() throws TimeoutException {

    WaitingConsumer waitingConsumer = new WaitingConsumer();
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER);

    Consumer<OutputFrame> composedConsumer = logConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=2"));
}
 
Example 5
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testFetchStdout() throws TimeoutException {
    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home"), 4, TimeUnit.SECONDS);
}
 
Example 6
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testFetchStdoutWithNoLimit() throws TimeoutException {
    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home"));
}
 
Example 7
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testLogConsumer() throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);

    Consumer<OutputFrame> composedConsumer = logConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home"));
}
 
Example 8
Source File: DockerComposeLogConsumerTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testLogConsumer() throws TimeoutException {
    WaitingConsumer logConsumer = new WaitingConsumer();
    DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/v2-compose-test.yml"))
        .withExposedService("redis_1", 6379)
        .withLogConsumer("redis_1", logConsumer);

    try {
        environment.starting(Description.EMPTY);
        logConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("Ready to accept connections"), 5, TimeUnit.SECONDS);
    } finally {
        environment.finished(Description.EMPTY);
    }
}
 
Example 9
Source File: KafkaIntegrationTest.java    From kop with Apache License 2.0 4 votes vote down vote up
@Test(timeOut = 3 * 60_000, dataProvider = "integrations")
void simpleProduceAndConsume(final String integration, final Optional<String> topic,
                             final boolean shouldProduce, final boolean shouldConsume) throws Exception {
    String topicName = topic.orElse(integration);
    System.out.println("starting integration " + integration + " with topicName " + topicName);

    admin.topics().createPartitionedTopic(topicName, 1);

    System.out.println("topic created");

    final GenericContainer producer = new GenericContainer<>("streamnative/kop-test-" + integration)
            .withEnv("KOP_BROKER", "host.testcontainers.internal:" + super.kafkaBrokerPort)
            .withEnv("KOP_PRODUCE", "true")
            .withEnv("KOP_TOPIC", topic.orElse(integration))
            .withEnv("KOP_LIMIT", "10")
            .withLogConsumer(new org.testcontainers.containers.output.Slf4jLogConsumer(KafkaIntegrationTest.log))
            .waitingFor(Wait.forLogMessage("starting to produce\\n", 1))
            .withNetworkMode("host");

    final GenericContainer consumer = new GenericContainer<>("streamnative/kop-test-" + integration)
            .withEnv("KOP_BROKER", "host.testcontainers.internal:" + super.kafkaBrokerPort)
            .withEnv("KOP_TOPIC", topic.orElse(integration))
            .withEnv("KOP_CONSUME", "true")
            .withEnv("KOP_LIMIT", "10")
            .withLogConsumer(new org.testcontainers.containers.output.Slf4jLogConsumer(KafkaIntegrationTest.log))
            .waitingFor(Wait.forLogMessage("starting to consume\\n", 1))
            .withNetworkMode("host");

    WaitingConsumer producerWaitingConsumer = null;
    WaitingConsumer consumerWaitingConsumer = null;
    if (shouldProduce) {
        producer.start();
        producerWaitingConsumer = KafkaIntegrationTest.createLogFollower(producer);
        System.out.println("producer started");
    }

    if (shouldConsume) {
        consumer.start();
        consumerWaitingConsumer = KafkaIntegrationTest.createLogFollower(consumer);
        System.out.println("consumer started");
    }

    if (shouldProduce) {
        producerWaitingConsumer.waitUntil(frame ->
                frame.getUtf8String().contains("ExitCode"), 30, TimeUnit.SECONDS);
        KafkaIntegrationTest.checkForErrorsInLogs(producer.getLogs());
    }

    if (shouldConsume) {
        consumerWaitingConsumer.waitUntil(frame ->
                frame.getUtf8String().contains("ExitCode"), 30, TimeUnit.SECONDS);
        KafkaIntegrationTest.checkForErrorsInLogs(consumer.getLogs());
    }
}
 
Example 10
Source File: TekuCLI.java    From teku with Apache License 2.0 4 votes vote down vote up
public void waitForOutput(final String output) throws TimeoutException {
  WaitingConsumer consumer = new WaitingConsumer();
  this.followOutput(consumer, OutputType.STDOUT);
  consumer.waitUntil(frame -> frame.getUtf8String().contains(output), 30, TimeUnit.SECONDS);
}
 
Example 11
Source File: DockerfileTest.java    From testcontainers-java with MIT License 4 votes vote down vote up
@Test
public void filePermissions() throws TimeoutException {

    WaitingConsumer consumer = new WaitingConsumer();

    ImageFromDockerfile image = new ImageFromDockerfile()
            .withFileFromTransferable("/someFile.txt", new Transferable() {
                @Override
                public long getSize() {
                    return 0;
                }

                @Override
                public byte[] getBytes() {
                    return new byte[0];
                }

                @Override
                public String getDescription() {
                    return "test file";
                }

                @Override
                public int getFileMode() {
                    return 0123;
                }


            })
            .withDockerfileFromBuilder(builder -> builder
                    .from("alpine:3.2")
                    .copy("someFile.txt", "/someFile.txt")
                    .cmd("stat -c \"%a\" /someFile.txt")
            );

    GenericContainer container = new GenericContainer(image)
            .withStartupCheckStrategy(new OneShotStartupCheckStrategy())
            .withLogConsumer(consumer);

    try {
        container.start();

        consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("123"), 5, TimeUnit.SECONDS);
    } finally {
        container.stop();
    }
}
 
Example 12
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 3 votes vote down vote up
@Test(timeout = 60_000L)
public void testFetchStdout() throws TimeoutException {

    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=2"),
            30, TimeUnit.SECONDS);
}
 
Example 13
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 3 votes vote down vote up
@Test(timeout = 60_000L)
public void testFetchStdoutWithNoLimit() throws TimeoutException {

    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=2"));
}