@Test public void testGetServicePort() { int serviceWithInstancePort = environment.getServicePort("redis_1", REDIS_PORT); assertNotNull("Port is set for service with instance number", serviceWithInstancePort); int serviceWithoutInstancePort = environment.getServicePort("redis", REDIS_PORT); assertNotNull("Port is set for service with instance number", serviceWithoutInstancePort); assertEquals("Service ports are the same", serviceWithInstancePort, serviceWithoutInstancePort); } }
@BeforeEach void setup() { host = composeContainer.getServiceHost("whoami_1", 80); port = composeContainer.getServicePort("whoami_1", 80); }
@Before public void setupClients() { for (int i = 0; i < 3; i++) { String name = String.format("redis_%d", i + 1); clients[i] = new Jedis(environment.getServiceHost(name, REDIS_PORT), environment.getServicePort(name, REDIS_PORT)); } }
@Test public void test() { try (DockerComposeContainer compose = new DockerComposeContainer(composeFiles) .withLocalCompose(localMode) .withExposedService(SERVICE_NAME, SERVICE_PORT)) { compose.start(); BufferedReader br = Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); Socket socket = new Socket(compose.getServiceHost(SERVICE_NAME, SERVICE_PORT), compose.getServicePort(SERVICE_NAME, SERVICE_PORT)); return new BufferedReader(new InputStreamReader(socket.getInputStream())); }); Unreliables.retryUntilTrue(10, TimeUnit.SECONDS, () -> { while (br.ready()) { String line = br.readLine(); if (line.contains(expectedEnvVar)) { pass("Mapped environment variable was found"); return true; } } info("Mapped environment variable was not found yet - process probably not ready"); Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); return false; }); } }
@Test public void simpleTest() { Jedis jedis = new Jedis(getEnvironment().getServiceHost("redis_1", REDIS_PORT), getEnvironment().getServicePort("redis_1", REDIS_PORT)); 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)); 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 }