package org.testcontainers.junit;

import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.containers.output.WaitingConsumer;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;

import static org.rnorth.visibleassertions.VisibleAssertions.*;
import static org.testcontainers.containers.output.OutputFrame.OutputType.STDOUT;

/**
 * Simple test for following container output.
 */
public class OutputStreamTest {

    @Rule
    public GenericContainer container = new GenericContainer("alpine:3.2")
            .withCommand("ping -c 5 www.google.com");

    private static final Logger LOGGER = LoggerFactory.getLogger(OutputStreamTest.class);

    @Test
    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);
    }

    @Test
    public void testFetchStdoutWithTimeout() throws TimeoutException {

        WaitingConsumer consumer = new WaitingConsumer();

        container.followOutput(consumer, STDOUT);

        assertThrows("a TimeoutException should be thrown", TimeoutException.class, () -> {
            consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=5"),
                    2, TimeUnit.SECONDS);
            return true;
        });
    }

    @Test
    public void testFetchStdoutWithNoLimit() throws TimeoutException {

        WaitingConsumer consumer = new WaitingConsumer();

        container.followOutput(consumer, STDOUT);

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

    @Test
    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"));
    }

    @Test
    public void testToStringConsumer() throws TimeoutException {

        WaitingConsumer waitingConsumer = new WaitingConsumer();
        ToStringConsumer toStringConsumer = new ToStringConsumer();

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

        waitingConsumer.waitUntilEnd(30, TimeUnit.SECONDS);

        String utf8String = toStringConsumer.toUtf8String();
        assertTrue("the expected first value was found", utf8String.contains("seq=1"));
        assertTrue("the expected last value was found", utf8String.contains("seq=4"));
        assertFalse("a non-expected value was found", utf8String.contains("seq=42"));
    }
}


@@ -910,22 +910,17 @@
 c 5 
-www.google.com
+127.0.0.1
 %22);%0A
@@ -1012,32 +1012,51 @@
 ass);%0A%0A    @Test
+(timeout = 60_000L)
 %0A    public void
@@ -1364,32 +1364,51 @@
     %7D%0A%0A    @Test
+(timeout = 60_000L)
 %0A    public void
@@ -1440,32 +1440,8 @@
 ut()
- throws TimeoutException
  %7B%0A%0A
@@ -1839,32 +1839,51 @@
     %7D%0A%0A    @Test
+(timeout = 60_000L)
 %0A    public void
@@ -2164,32 +2164,51 @@
     %7D%0A%0A    @Test
+(timeout = 60_000L)
 %0A    public void
@@ -2639,32 +2639,32 @@
 eq=2%22));%0A    %7D%0A%0A
-
     @Test%0A    pu
@@ -2656,16 +2656,35 @@
    @Test
+(timeout = 60_000L)
 %0A    pub

Remove internet connectivity dependency from test (#1502)