package org.testcontainers.junit;

import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.utility.TestEnvironment;
import redis.clients.jedis.Jedis;

import java.util.concurrent.TimeUnit;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;

/**
 * Created by rnorth on 21/05/2016.
 */
public abstract class BaseDockerComposeTest {

    protected static final int REDIS_PORT = 6379;

    protected abstract DockerComposeContainer getEnvironment();

    @BeforeClass
    public static void checkVersion() {
        Assume.assumeTrue(TestEnvironment.dockerApiAtLeast("1.22"));
    }

    @Test
    public void simpleTest() {
        Jedis jedis = new Jedis(getEnvironment().getServiceHost("redis_1", REDIS_PORT), getEnvironment().getServicePort("redis_1", REDIS_PORT));

        // TODO: remove following resolution of #160
        Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { jedis.connect(); return true; });

        jedis.incr("test");
        jedis.incr("test");
        jedis.incr("test");

        assertEquals("A redis instance defined in compose can be used in isolation", "3", jedis.get("test"));
    }

    @Test
    public void secondTest() {
        // used in manual checking for cleanup in between tests
        Jedis jedis = new Jedis(getEnvironment().getServiceHost("redis_1", REDIS_PORT), getEnvironment().getServicePort("redis_1", REDIS_PORT));

        // TODO: remove following resolution of #160
        Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { jedis.connect(); return true; });

        jedis.incr("test");
        jedis.incr("test");
        jedis.incr("test");

        assertEquals("Tests use fresh container instances", "3", jedis.get("test"));
        // if these end up using the same container one of the test methods will fail.
        // However, @Rule creates a separate DockerComposeContainer instance per test, so this just shouldn't happen
    }
}

@@ -24,24 +24,66 @@
 ers.junit;%0A%0A
+import org.jetbrains.annotations.NotNull;%0A
 import org.j
@@ -95,16 +95,16 @@
 Assume;%0A
-
 import o
@@ -347,16 +347,54 @@
 Jedis;%0A%0A
+import java.util.concurrent.Callable;%0A
 import j
@@ -1144,47 +1144,31 @@
 DS, 
-() -%3E %7B jedis.connect(); return true; %7D
+getLivenessCheck(jedis)
 );%0A%0A
@@ -1737,47 +1737,31 @@
 DS, 
-() -%3E %7B jedis.connect(); return true; %7D
+getLivenessCheck(jedis)
 );%0A%0A
@@ -2017,16 +2017,16 @@
 l fail.%0A
-
         
@@ -2139,11 +2139,207 @@
 en%0A    %7D
+%0A%0A    @NotNull%0A    private Callable%3CBoolean%3E getLivenessCheck(Jedis jedis) %7B%0A        return () -%3E %7B%0A            jedis.connect();%0A            jedis.ping();%0A            return true;%0A        %7D;%0A    %7D
 %0A%7D%0A

Extend pre-test guard on Docker Compose tests to include a Redis ping