From b1f32485d2c27cf535fb350c0f6478a40b56b5bd Mon Sep 17 00:00:00 2001 From: Vladimir Rogach Date: Thu, 11 Nov 2021 20:04:06 +0300 Subject: [PATCH] Fixes #31. Fix flacky test --- .../TarantoolCartridgeContainer.java | 44 +++++++++++++++---- .../TarantoolCartridgeContainerTest.java | 23 ++++++++-- ...TarantoolCartridgeStaticContainerTest.java | 3 +- src/test/resources/cartridge/instances.yml | 16 +++---- .../cartridge/instances_fixedport.yml | 2 + 5 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/testcontainers/containers/TarantoolCartridgeContainer.java b/src/main/java/org/testcontainers/containers/TarantoolCartridgeContainer.java index fe7e98e..bf7fb2a 100644 --- a/src/main/java/org/testcontainers/containers/TarantoolCartridgeContainer.java +++ b/src/main/java/org/testcontainers/containers/TarantoolCartridgeContainer.java @@ -1,6 +1,7 @@ package org.testcontainers.containers; import com.github.dockerjava.api.command.InspectContainerResponse; +import io.tarantool.driver.exceptions.TarantoolConnectionException; import org.testcontainers.images.builder.ImageFromDockerfile; import java.net.URL; @@ -431,29 +432,56 @@ protected void containerIsStarting(InspectContainerResponse containerInfo) { logger().info("Tarantool Cartridge cluster is starting"); } - @Override - protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) { - super.containerIsStarted(containerInfo, reused); - + private boolean setupTopology() { try { executeScript(topologyConfigurationFile).get(); // The client connection will be closed after that command } catch (Exception e) { - if (e instanceof ExecutionException && e.getCause() instanceof TimeoutException) { - // Do nothing, the cluster is reloading + if (e instanceof ExecutionException) { + if (e.getCause() instanceof TimeoutException) { + return true; + // Do nothing, the cluster is reloading + } else if (e.getCause() instanceof TarantoolConnectionException) { + // Probably cluster is not ready + logger().error("Failed to setup topology: {}", e.getMessage()); + return false; + } } else { - logger().error("Failed to change the app topology", e); + throw new RuntimeException("Failed to change the app topology", e); + } + } + return true; + } + + private void retryingSetupTopology() { + if (!setupTopology()) { + try { + logger().info("Retrying setup topology in 10 seconds"); + Thread.sleep(10000); + } catch (InterruptedException e) { throw new RuntimeException(e); } + if (!setupTopology()) { + throw new RuntimeException("Failed to change the app topology after retry"); + } } + } - // The client must reconnect automatically + private void bootstrapVshard() { try { executeCommand(VSHARD_BOOTSTRAP_COMMAND).get(); } catch (Exception e) { logger().error("Failed to bootstrap vshard cluster", e); throw new RuntimeException(e); } + } + + @Override + protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) { + super.containerIsStarted(containerInfo, reused); + + retryingSetupTopology(); + bootstrapVshard(); logger().info("Tarantool Cartridge cluster is started"); logger().info("Tarantool Cartridge router is listening at {}:{}", getRouterHost(), getRouterPort()); diff --git a/src/test/java/org/testcontainers/containers/TarantoolCartridgeContainerTest.java b/src/test/java/org/testcontainers/containers/TarantoolCartridgeContainerTest.java index 41e5db6..14a79a1 100644 --- a/src/test/java/org/testcontainers/containers/TarantoolCartridgeContainerTest.java +++ b/src/test/java/org/testcontainers/containers/TarantoolCartridgeContainerTest.java @@ -39,28 +39,43 @@ public void test_ClusterContainer_StartsSuccessfully_ifFilesAreCopiedUnderRoot() container.start(); CartridgeContainerTestUtils.executeProfileReplaceSmokeTest(container); + if(container.isRunning()) + container.stop(); } @Test public void test_ClusterContainer_StartsSuccessfully_ifFixedPortsAreConfigured() throws Exception { + Map buildArgs = new HashMap() { + { + put("TARANTOOL_SERVER_USER", "root"); + put("TARANTOOL_SERVER_UID", "0"); + put("TARANTOOL_SERVER_GROUP", "root"); + put("TARANTOOL_SERVER_GID", "0"); + } + }; + TarantoolCartridgeContainer container = new TarantoolCartridgeContainer( "Dockerfile", - "testcontainers-java-tarantool:test", + "testcontainers-java-tarantool-fixport:test", "cartridge/instances_fixedport.yml", - "cartridge/topology_fixedport.lua") - .withDirectoryBinding("cartridge") + "cartridge/topology_fixedport.lua", + buildArgs) + .withCopyFileToContainer(MountableFile.forClasspathResource("cartridge"), "/app") + .withCopyFileToContainer(MountableFile.forClasspathResource("cartridge/instances_fixedport.yml"),"/app/instances.yml") .withStartupTimeout(Duration.ofSeconds(300)) .withUseFixedPorts(true) .withAPIPort(18081) .withRouterPort(13301) .waitingFor( - Wait.forLogMessage(".*Listening HTTP on.*", 1) + Wait.forLogMessage(".*Listening HTTP on.*", 2) ) .withLogConsumer(new Slf4jLogConsumer( LoggerFactory.getLogger(TarantoolCartridgeContainerTest.class))); container.start(); CartridgeContainerTestUtils.executeProfileReplaceSmokeTest(container); + if(container.isRunning()) + container.stop(); } } diff --git a/src/test/java/org/testcontainers/containers/TarantoolCartridgeStaticContainerTest.java b/src/test/java/org/testcontainers/containers/TarantoolCartridgeStaticContainerTest.java index ffc8c4b..0049aad 100644 --- a/src/test/java/org/testcontainers/containers/TarantoolCartridgeStaticContainerTest.java +++ b/src/test/java/org/testcontainers/containers/TarantoolCartridgeStaticContainerTest.java @@ -18,7 +18,6 @@ public class TarantoolCartridgeStaticContainerTest { private static final TarantoolCartridgeContainer container = new TarantoolCartridgeContainer( "Dockerfile", - "testcontainers-java-tarantool:test", "cartridge/instances.yml", "cartridge/topology.lua") .withDirectoryBinding("cartridge") @@ -27,7 +26,7 @@ public class TarantoolCartridgeStaticContainerTest { LoggerFactory.getLogger(TarantoolCartridgeStaticContainerTest.class))); @Test - public void testContainerWithParameters() throws Exception { + public void test_StaticClusterContainer_StartsSuccessfully_ifDirectoryBinndingIsUsed() throws Exception { CartridgeContainerTestUtils.executeProfileReplaceSmokeTest(container); } } diff --git a/src/test/resources/cartridge/instances.yml b/src/test/resources/cartridge/instances.yml index 1119e19..69b19ff 100644 --- a/src/test/resources/cartridge/instances.yml +++ b/src/test/resources/cartridge/instances.yml @@ -2,38 +2,34 @@ testapp.router: workdir: ./tmp/db_dev/3301 advertise_uri: localhost:3301 http_port: 8081 + memtx_memory: 60000000 testapp.s1-master: workdir: ./tmp/db_dev/3302 advertise_uri: localhost:3302 http_port: 8082 + memtx_memory: 60000000 testapp.s1-replica: workdir: ./tmp/db_dev/3303 advertise_uri: localhost:3303 http_port: 8083 + memtx_memory: 60000000 testapp.s2-master: workdir: ./tmp/db_dev/3304 advertise_uri: localhost:3304 http_port: 8084 + memtx_memory: 60000000 testapp.s2-replica: workdir: ./tmp/db_dev/3305 advertise_uri: localhost:3305 http_port: 8085 + memtx_memory: 60000000 testapp-stateboard: workdir: ./tmp/db_dev/3310 listen: localhost:3310 password: passwd - -testapp.router-fixedport: - workdir: ./tmp/db_dev/13301 - advertise_uri: localhost:13301 - http_port: 18081 - -testapp.storage-fixedport: - workdir: ./tmp/db_dev/13302 - advertise_uri: localhost:13302 - http_port: 18082 + memtx_memory: 60000000 diff --git a/src/test/resources/cartridge/instances_fixedport.yml b/src/test/resources/cartridge/instances_fixedport.yml index b281999..608d097 100644 --- a/src/test/resources/cartridge/instances_fixedport.yml +++ b/src/test/resources/cartridge/instances_fixedport.yml @@ -2,8 +2,10 @@ testapp.router-fixedport: workdir: ./tmp/db_dev/13301 advertise_uri: localhost:13301 http_port: 18081 + memtx_memory: 60000000 testapp.storage-fixedport: workdir: ./tmp/db_dev/13302 advertise_uri: localhost:13302 http_port: 18082 + memtx_memory: 60000000