Skip to content

Commit

Permalink
Toxiproxy: Fix multi-proxy use case (remove static "name" argument) (t…
Browse files Browse the repository at this point in the history
…estcontainers#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
  • Loading branch information
worldtiki authored and bsideup committed Mar 24, 2019
1 parent bb5c6fc commit f2fc02c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ContainerProxy getProxy(String hostname, int port) {
throw new IllegalStateException("Maximum number of proxies exceeded");
}

final Proxy proxy = client.createProxy("name", "0.0.0.0:" + toxiPort, upstream);
final Proxy proxy = client.createProxy(upstream, "0.0.0.0:" + toxiPort, upstream);
return new ContainerProxy(proxy, getContainerIpAddress(), getMappedPort(toxiPort));
} catch (IOException e) {
throw new RuntimeException("Proxy could not be created", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ public void testConnectionCut() {
// }
}

@Test
public void testMultipleProxiesCanBeCreated() {
try (GenericContainer secondRedis = new GenericContainer("redis:5.0.4")
.withExposedPorts(6379)
.withNetwork(network)) {

secondRedis.start();

final ToxiproxyContainer.ContainerProxy firstProxy = toxiproxy.getProxy(redis, 6379);
final ToxiproxyContainer.ContainerProxy secondProxy = toxiproxy.getProxy(secondRedis, 6379);

final Jedis firstJedis = new Jedis(firstProxy.getContainerIpAddress(), firstProxy.getProxyPort());
final Jedis secondJedis = new Jedis(secondProxy.getContainerIpAddress(), secondProxy.getProxyPort());

firstJedis.set("somekey", "somevalue");
secondJedis.set("somekey", "somevalue");

firstProxy.setConnectionCut(true);

assertThrows("calls fail when the connection is cut, for only the relevant proxy",
JedisConnectionException.class, () -> {
firstJedis.get("somekey");
});

assertEquals("access via a different proxy is OK", "somevalue", secondJedis.get("somekey"));
}
}

private void checkCallWithLatency(Jedis jedis, final String description, int expectedMinLatency, long expectedMaxLatency) {
final long start = System.currentTimeMillis();
String s = jedis.get("somekey");
Expand Down

0 comments on commit f2fc02c

Please sign in to comment.