Skip to content

Commit 687313c

Browse files
authored
Fixes #31. Fix flacky test (#32)
1 parent 0bcbdeb commit 687313c

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

src/main/java/org/testcontainers/containers/TarantoolCartridgeContainer.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.testcontainers.containers;
22

33
import com.github.dockerjava.api.command.InspectContainerResponse;
4+
import io.tarantool.driver.exceptions.TarantoolConnectionException;
45
import org.testcontainers.images.builder.ImageFromDockerfile;
56

67
import java.net.URL;
@@ -431,29 +432,56 @@ protected void containerIsStarting(InspectContainerResponse containerInfo) {
431432
logger().info("Tarantool Cartridge cluster is starting");
432433
}
433434

434-
@Override
435-
protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) {
436-
super.containerIsStarted(containerInfo, reused);
437-
435+
private boolean setupTopology() {
438436
try {
439437
executeScript(topologyConfigurationFile).get();
440438
// The client connection will be closed after that command
441439
} catch (Exception e) {
442-
if (e instanceof ExecutionException && e.getCause() instanceof TimeoutException) {
443-
// Do nothing, the cluster is reloading
440+
if (e instanceof ExecutionException) {
441+
if (e.getCause() instanceof TimeoutException) {
442+
return true;
443+
// Do nothing, the cluster is reloading
444+
} else if (e.getCause() instanceof TarantoolConnectionException) {
445+
// Probably cluster is not ready
446+
logger().error("Failed to setup topology: {}", e.getMessage());
447+
return false;
448+
}
444449
} else {
445-
logger().error("Failed to change the app topology", e);
450+
throw new RuntimeException("Failed to change the app topology", e);
451+
}
452+
}
453+
return true;
454+
}
455+
456+
private void retryingSetupTopology() {
457+
if (!setupTopology()) {
458+
try {
459+
logger().info("Retrying setup topology in 10 seconds");
460+
Thread.sleep(10000);
461+
} catch (InterruptedException e) {
446462
throw new RuntimeException(e);
447463
}
464+
if (!setupTopology()) {
465+
throw new RuntimeException("Failed to change the app topology after retry");
466+
}
448467
}
468+
}
449469

450-
// The client must reconnect automatically
470+
private void bootstrapVshard() {
451471
try {
452472
executeCommand(VSHARD_BOOTSTRAP_COMMAND).get();
453473
} catch (Exception e) {
454474
logger().error("Failed to bootstrap vshard cluster", e);
455475
throw new RuntimeException(e);
456476
}
477+
}
478+
479+
@Override
480+
protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) {
481+
super.containerIsStarted(containerInfo, reused);
482+
483+
retryingSetupTopology();
484+
bootstrapVshard();
457485

458486
logger().info("Tarantool Cartridge cluster is started");
459487
logger().info("Tarantool Cartridge router is listening at {}:{}", getRouterHost(), getRouterPort());

src/test/java/org/testcontainers/containers/TarantoolCartridgeContainerTest.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,43 @@ public void test_ClusterContainer_StartsSuccessfully_ifFilesAreCopiedUnderRoot()
3939

4040
container.start();
4141
CartridgeContainerTestUtils.executeProfileReplaceSmokeTest(container);
42+
if(container.isRunning())
43+
container.stop();
4244
}
4345

4446
@Test
4547
public void test_ClusterContainer_StartsSuccessfully_ifFixedPortsAreConfigured() throws Exception {
48+
Map<String, String> buildArgs = new HashMap<String, String>() {
49+
{
50+
put("TARANTOOL_SERVER_USER", "root");
51+
put("TARANTOOL_SERVER_UID", "0");
52+
put("TARANTOOL_SERVER_GROUP", "root");
53+
put("TARANTOOL_SERVER_GID", "0");
54+
}
55+
};
56+
4657
TarantoolCartridgeContainer container =
4758
new TarantoolCartridgeContainer(
4859
"Dockerfile",
49-
"testcontainers-java-tarantool:test",
60+
"testcontainers-java-tarantool-fixport:test",
5061
"cartridge/instances_fixedport.yml",
51-
"cartridge/topology_fixedport.lua")
52-
.withDirectoryBinding("cartridge")
62+
"cartridge/topology_fixedport.lua",
63+
buildArgs)
64+
.withCopyFileToContainer(MountableFile.forClasspathResource("cartridge"), "/app")
65+
.withCopyFileToContainer(MountableFile.forClasspathResource("cartridge/instances_fixedport.yml"),"/app/instances.yml")
5366
.withStartupTimeout(Duration.ofSeconds(300))
5467
.withUseFixedPorts(true)
5568
.withAPIPort(18081)
5669
.withRouterPort(13301)
5770
.waitingFor(
58-
Wait.forLogMessage(".*Listening HTTP on.*", 1)
71+
Wait.forLogMessage(".*Listening HTTP on.*", 2)
5972
)
6073
.withLogConsumer(new Slf4jLogConsumer(
6174
LoggerFactory.getLogger(TarantoolCartridgeContainerTest.class)));
6275

6376
container.start();
6477
CartridgeContainerTestUtils.executeProfileReplaceSmokeTest(container);
78+
if(container.isRunning())
79+
container.stop();
6580
}
6681
}

src/test/java/org/testcontainers/containers/TarantoolCartridgeStaticContainerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class TarantoolCartridgeStaticContainerTest {
1818
private static final TarantoolCartridgeContainer container =
1919
new TarantoolCartridgeContainer(
2020
"Dockerfile",
21-
"testcontainers-java-tarantool:test",
2221
"cartridge/instances.yml",
2322
"cartridge/topology.lua")
2423
.withDirectoryBinding("cartridge")
@@ -27,7 +26,7 @@ public class TarantoolCartridgeStaticContainerTest {
2726
LoggerFactory.getLogger(TarantoolCartridgeStaticContainerTest.class)));
2827

2928
@Test
30-
public void testContainerWithParameters() throws Exception {
29+
public void test_StaticClusterContainer_StartsSuccessfully_ifDirectoryBinndingIsUsed() throws Exception {
3130
CartridgeContainerTestUtils.executeProfileReplaceSmokeTest(container);
3231
}
3332
}

src/test/resources/cartridge/instances.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,34 @@ testapp.router:
22
workdir: ./tmp/db_dev/3301
33
advertise_uri: localhost:3301
44
http_port: 8081
5+
memtx_memory: 60000000
56

67
testapp.s1-master:
78
workdir: ./tmp/db_dev/3302
89
advertise_uri: localhost:3302
910
http_port: 8082
11+
memtx_memory: 60000000
1012

1113
testapp.s1-replica:
1214
workdir: ./tmp/db_dev/3303
1315
advertise_uri: localhost:3303
1416
http_port: 8083
17+
memtx_memory: 60000000
1518

1619
testapp.s2-master:
1720
workdir: ./tmp/db_dev/3304
1821
advertise_uri: localhost:3304
1922
http_port: 8084
23+
memtx_memory: 60000000
2024

2125
testapp.s2-replica:
2226
workdir: ./tmp/db_dev/3305
2327
advertise_uri: localhost:3305
2428
http_port: 8085
29+
memtx_memory: 60000000
2530

2631
testapp-stateboard:
2732
workdir: ./tmp/db_dev/3310
2833
listen: localhost:3310
2934
password: passwd
30-
31-
testapp.router-fixedport:
32-
workdir: ./tmp/db_dev/13301
33-
advertise_uri: localhost:13301
34-
http_port: 18081
35-
36-
testapp.storage-fixedport:
37-
workdir: ./tmp/db_dev/13302
38-
advertise_uri: localhost:13302
39-
http_port: 18082
35+
memtx_memory: 60000000

src/test/resources/cartridge/instances_fixedport.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ testapp.router-fixedport:
22
workdir: ./tmp/db_dev/13301
33
advertise_uri: localhost:13301
44
http_port: 18081
5+
memtx_memory: 60000000
56

67
testapp.storage-fixedport:
78
workdir: ./tmp/db_dev/13302
89
advertise_uri: localhost:13302
910
http_port: 18082
11+
memtx_memory: 60000000

0 commit comments

Comments
 (0)