Skip to content

Commit f2fc02c

Browse files
worldtikibsideup
authored andcommitted
Toxiproxy: Fix multi-proxy use case (remove static "name" argument) (#1335)
* Use hostname instead of hardcoded string to allow for multiple proxies to be created * Update modules/toxiproxy/src/main/java/org/testcontainers/containers/ToxiproxyContainer.java Co-Authored-By: worldtiki <[email protected]> * Add a test covering creation of multiple proxies
1 parent bb5c6fc commit f2fc02c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

modules/toxiproxy/src/main/java/org/testcontainers/containers/ToxiproxyContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public ContainerProxy getProxy(String hostname, int port) {
8686
throw new IllegalStateException("Maximum number of proxies exceeded");
8787
}
8888

89-
final Proxy proxy = client.createProxy("name", "0.0.0.0:" + toxiPort, upstream);
89+
final Proxy proxy = client.createProxy(upstream, "0.0.0.0:" + toxiPort, upstream);
9090
return new ContainerProxy(proxy, getContainerIpAddress(), getMappedPort(toxiPort));
9191
} catch (IOException e) {
9292
throw new RuntimeException("Proxy could not be created", e);

modules/toxiproxy/src/test/java/org/testcontainers/containers/ToxiproxyTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,34 @@ public void testConnectionCut() {
8989
// }
9090
}
9191

92+
@Test
93+
public void testMultipleProxiesCanBeCreated() {
94+
try (GenericContainer secondRedis = new GenericContainer("redis:5.0.4")
95+
.withExposedPorts(6379)
96+
.withNetwork(network)) {
97+
98+
secondRedis.start();
99+
100+
final ToxiproxyContainer.ContainerProxy firstProxy = toxiproxy.getProxy(redis, 6379);
101+
final ToxiproxyContainer.ContainerProxy secondProxy = toxiproxy.getProxy(secondRedis, 6379);
102+
103+
final Jedis firstJedis = new Jedis(firstProxy.getContainerIpAddress(), firstProxy.getProxyPort());
104+
final Jedis secondJedis = new Jedis(secondProxy.getContainerIpAddress(), secondProxy.getProxyPort());
105+
106+
firstJedis.set("somekey", "somevalue");
107+
secondJedis.set("somekey", "somevalue");
108+
109+
firstProxy.setConnectionCut(true);
110+
111+
assertThrows("calls fail when the connection is cut, for only the relevant proxy",
112+
JedisConnectionException.class, () -> {
113+
firstJedis.get("somekey");
114+
});
115+
116+
assertEquals("access via a different proxy is OK", "somevalue", secondJedis.get("somekey"));
117+
}
118+
}
119+
92120
private void checkCallWithLatency(Jedis jedis, final String description, int expectedMinLatency, long expectedMaxLatency) {
93121
final long start = System.currentTimeMillis();
94122
String s = jedis.get("somekey");

0 commit comments

Comments
 (0)