1- package org .testcontainers .containers ;
1+ package org .testcontainers .toxiproxy ;
22
33import eu .rekawek .toxiproxy .Proxy ;
44import eu .rekawek .toxiproxy .ToxiproxyClient ;
55import eu .rekawek .toxiproxy .model .ToxicDirection ;
66import org .junit .jupiter .api .AutoClose ;
77import org .junit .jupiter .api .BeforeEach ;
88import org .junit .jupiter .api .Test ;
9+ import org .testcontainers .containers .GenericContainer ;
10+ import org .testcontainers .containers .Network ;
911import redis .clients .jedis .Jedis ;
1012import redis .clients .jedis .exceptions .JedisConnectionException ;
1113
1618import static org .assertj .core .api .Assertions .assertThat ;
1719import static org .assertj .core .api .Assertions .catchThrowable ;
1820
19- class ToxiproxyTest {
21+ public class ToxiproxyContainerTest {
2022
2123 private static final Duration JEDIS_TIMEOUT = Duration .ofSeconds (10 );
2224
@@ -41,23 +43,22 @@ class ToxiproxyTest {
4143 // spotless:on
4244
4345 @ BeforeEach
44- void setUp () {
46+ public void setUp () {
4547 redis .start ();
4648 toxiproxy .start ();
4749 }
4850
4951 @ Test
50- void testDirect () {
51- try ( Jedis jedis = createJedis (redis .getHost (), redis .getFirstMappedPort ())) {
52- jedis .set ("somekey" , "somevalue" );
52+ public void testDirect () {
53+ final Jedis jedis = createJedis (redis .getHost (), redis .getFirstMappedPort ());
54+ jedis .set ("somekey" , "somevalue" );
5355
54- final String s = jedis .get ("somekey" );
55- assertThat (s ).as ("direct access to the container works OK" ).isEqualTo ("somevalue" );
56- }
56+ final String s = jedis .get ("somekey" );
57+ assertThat (s ).as ("direct access to the container works OK" ).isEqualTo ("somevalue" );
5758 }
5859
5960 @ Test
60- void testLatencyViaProxy () throws IOException {
61+ public void testLatencyViaProxy () throws IOException {
6162 // obtainProxyObject {
6263 final ToxiproxyClient toxiproxyClient = new ToxiproxyClient (toxiproxy .getHost (), toxiproxy .getControlPort ());
6364 final Proxy proxy = toxiproxyClient .createProxy ("redis" , "0.0.0.0:8666" , "redis:6379" );
@@ -86,7 +87,7 @@ void testLatencyViaProxy() throws IOException {
8687 }
8788
8889 @ Test
89- void testConnectionCut () throws IOException {
90+ public void testConnectionCut () throws IOException {
9091 final ToxiproxyClient toxiproxyClient = new ToxiproxyClient (toxiproxy .getHost (), toxiproxy .getControlPort ());
9192 final Proxy proxy = toxiproxyClient .createProxy ("redis" , "0.0.0.0:8666" , "redis:6379" );
9293 final Jedis jedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8666 ));
@@ -121,9 +122,13 @@ void testConnectionCut() throws IOException {
121122 }
122123
123124 @ Test
124- void testMultipleProxiesCanBeCreated () throws IOException {
125- try (GenericContainer <?> secondRedis = new GenericContainer <>("redis:6-alpine" )) {
126- secondRedis .withExposedPorts (6379 ).withNetwork (network ).withNetworkAliases ("redis2" );
125+ public void testMultipleProxiesCanBeCreated () throws IOException {
126+ try (
127+ GenericContainer <?> secondRedis = new GenericContainer <>("redis:6-alpine" )
128+ .withExposedPorts (6379 )
129+ .withNetwork (network )
130+ .withNetworkAliases ("redis2" )
131+ ) {
127132 secondRedis .start ();
128133
129134 final ToxiproxyClient toxiproxyClient = new ToxiproxyClient (
@@ -133,54 +138,25 @@ void testMultipleProxiesCanBeCreated() throws IOException {
133138 final Proxy firstProxy = toxiproxyClient .createProxy ("redis1" , "0.0.0.0:8666" , "redis:6379" );
134139 toxiproxyClient .createProxy ("redis2" , "0.0.0.0:8667" , "redis2:6379" );
135140
136- try (
137- Jedis firstJedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8666 ));
138- Jedis secondJedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8667 ))
139- ) {
140- firstJedis .set ("somekey" , "somevalue" );
141- secondJedis .set ("somekey" , "somevalue" );
142-
143- firstProxy .toxics ().bandwidth ("CUT_CONNECTION_DOWNSTREAM" , ToxicDirection .DOWNSTREAM , 0 );
144- firstProxy .toxics ().bandwidth ("CUT_CONNECTION_UPSTREAM" , ToxicDirection .UPSTREAM , 0 );
145-
146- assertThat (
147- catchThrowable (() -> {
148- firstJedis .get ("somekey" );
149- })
150- )
151- .as ("calls fail when the connection is cut, for only the relevant proxy" )
152- .isInstanceOf (JedisConnectionException .class );
153-
154- assertThat (secondJedis .get ("somekey" )).as ("access via a different proxy is OK" ).isEqualTo ("somevalue" );
155- }
156- }
157- }
141+ final Jedis firstJedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8666 ));
142+ final Jedis secondJedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8667 ));
158143
159- @ Test
160- void testOriginalAndMappedPorts () {
161- final ToxiproxyContainer .ContainerProxy proxy = toxiproxy .getProxy ("hostname" , 7070 );
162-
163- final int portViaToxiproxy = proxy .getOriginalProxyPort ();
164- assertThat (portViaToxiproxy ).as ("original port is correct" ).isEqualTo (8666 );
165-
166- final ToxiproxyContainer .ContainerProxy proxy1 = toxiproxy .getProxy ("hostname1" , 8080 );
167- assertThat (proxy1 .getOriginalProxyPort ()).as ("original port is correct" ).isEqualTo (8667 );
168- assertThat (proxy1 .getProxyPort ())
169- .as ("mapped port is correct" )
170- .isEqualTo (toxiproxy .getMappedPort (proxy1 .getOriginalProxyPort ()));
171-
172- final ToxiproxyContainer .ContainerProxy proxy2 = toxiproxy .getProxy ("hostname2" , 9090 );
173- assertThat (proxy2 .getOriginalProxyPort ()).as ("original port is correct" ).isEqualTo (8668 );
174- assertThat (proxy2 .getProxyPort ())
175- .as ("mapped port is correct" )
176- .isEqualTo (toxiproxy .getMappedPort (proxy2 .getOriginalProxyPort ()));
177- }
144+ firstJedis .set ("somekey" , "somevalue" );
145+ secondJedis .set ("somekey" , "somevalue" );
178146
179- @ Test
180- void testProxyName () {
181- final ToxiproxyContainer .ContainerProxy proxy = toxiproxy .getProxy ("hostname" , 7070 );
147+ firstProxy .toxics ().bandwidth ("CUT_CONNECTION_DOWNSTREAM" , ToxicDirection .DOWNSTREAM , 0 );
148+ firstProxy .toxics ().bandwidth ("CUT_CONNECTION_UPSTREAM" , ToxicDirection .UPSTREAM , 0 );
182149
183- assertThat (proxy .getName ()).as ("proxy name is hostname and port" ).isEqualTo ("hostname:7070" );
150+ assertThat (
151+ catchThrowable (() -> {
152+ firstJedis .get ("somekey" );
153+ })
154+ )
155+ .as ("calls fail when the connection is cut, for only the relevant proxy" )
156+ .isInstanceOf (JedisConnectionException .class );
157+
158+ assertThat (secondJedis .get ("somekey" )).as ("access via a different proxy is OK" ).isEqualTo ("somevalue" );
159+ }
184160 }
185161
186162 private void checkCallWithLatency (
0 commit comments