1818import static org .assertj .core .api .Assertions .assertThat ;
1919import static org .assertj .core .api .Assertions .catchThrowable ;
2020
21- public class ToxiproxyContainerTest {
21+ class ToxiproxyContainerTest {
2222
2323 private static final Duration JEDIS_TIMEOUT = Duration .ofSeconds (10 );
2424
@@ -43,22 +43,23 @@ public class ToxiproxyContainerTest {
4343 // spotless:on
4444
4545 @ BeforeEach
46- public void setUp () {
46+ void setUp () {
4747 redis .start ();
4848 toxiproxy .start ();
4949 }
5050
5151 @ Test
52- public void testDirect () {
53- final Jedis jedis = createJedis (redis .getHost (), redis .getFirstMappedPort ());
54- jedis .set ("somekey" , "somevalue" );
52+ void testDirect () {
53+ try ( Jedis jedis = createJedis (redis .getHost (), redis .getFirstMappedPort ())) {
54+ jedis .set ("somekey" , "somevalue" );
5555
56- final String s = jedis .get ("somekey" );
57- assertThat (s ).as ("direct access to the container works OK" ).isEqualTo ("somevalue" );
56+ final String s = jedis .get ("somekey" );
57+ assertThat (s ).as ("direct access to the container works OK" ).isEqualTo ("somevalue" );
58+ }
5859 }
5960
6061 @ Test
61- public void testLatencyViaProxy () throws IOException {
62+ void testLatencyViaProxy () throws IOException {
6263 // obtainProxyObject {
6364 final ToxiproxyClient toxiproxyClient = new ToxiproxyClient (toxiproxy .getHost (), toxiproxy .getControlPort ());
6465 final Proxy proxy = toxiproxyClient .createProxy ("redis" , "0.0.0.0:8666" , "redis:6379" );
@@ -69,60 +70,62 @@ public void testLatencyViaProxy() throws IOException {
6970 final int portViaToxiproxy = toxiproxy .getMappedPort (8666 );
7071 // }
7172
72- final Jedis jedis = createJedis (ipAddressViaToxiproxy , portViaToxiproxy );
73- jedis .set ("somekey" , "somevalue" );
73+ try ( Jedis jedis = createJedis (ipAddressViaToxiproxy , portViaToxiproxy )) {
74+ jedis .set ("somekey" , "somevalue" );
7475
75- checkCallWithLatency (jedis , "without interference" , 0 , 250 );
76+ checkCallWithLatency (jedis , "without interference" , 0 , 250 );
7677
77- // spotless:off
78- // addingLatency {
79- proxy .toxics ()
80- .latency ("latency" , ToxicDirection .DOWNSTREAM , 1_100 )
81- .setJitter (100 );
82- // from now on the connection latency should be from 1000-1200 ms.
83- // }
84- // spotless:on
78+ // spotless:off
79+ // addingLatency {
80+ proxy .toxics ()
81+ .latency ("latency" , ToxicDirection .DOWNSTREAM , 1_100 )
82+ .setJitter (100 );
83+ // from now on the connection latency should be from 1000-1200 ms.
84+ // }
85+ // spotless:on
8586
86- checkCallWithLatency (jedis , "with interference" , 1_000 , 1_500 );
87+ checkCallWithLatency (jedis , "with interference" , 1_000 , 1_500 );
88+ }
8789 }
8890
8991 @ Test
90- public void testConnectionCut () throws IOException {
92+ void testConnectionCut () throws IOException {
9193 final ToxiproxyClient toxiproxyClient = new ToxiproxyClient (toxiproxy .getHost (), toxiproxy .getControlPort ());
9294 final Proxy proxy = toxiproxyClient .createProxy ("redis" , "0.0.0.0:8666" , "redis:6379" );
93- final Jedis jedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8666 ));
94- jedis .set ("somekey" , "somevalue" );
95-
96- assertThat (jedis .get ("somekey" ))
97- .as ("access to the container works OK before cutting the connection" )
98- .isEqualTo ("somevalue" );
99-
100- // disableProxy {
101- proxy .toxics ().bandwidth ("CUT_CONNECTION_DOWNSTREAM" , ToxicDirection .DOWNSTREAM , 0 );
102- proxy .toxics ().bandwidth ("CUT_CONNECTION_UPSTREAM" , ToxicDirection .UPSTREAM , 0 );
103-
104- // for example, expect failure when the connection is cut
105- assertThat (
106- catchThrowable (() -> {
107- jedis .get ("somekey" );
108- })
109- )
110- .as ("calls fail when the connection is cut" )
111- .isInstanceOf (JedisConnectionException .class );
112-
113- proxy .toxics ().get ("CUT_CONNECTION_DOWNSTREAM" ).remove ();
114- proxy .toxics ().get ("CUT_CONNECTION_UPSTREAM" ).remove ();
115-
116- jedis .close ();
117- // and with the connection re-established, expect success
118- assertThat (jedis .get ("somekey" ))
119- .as ("access to the container works OK after re-establishing the connection" )
120- .isEqualTo ("somevalue" );
95+ try (Jedis jedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8666 ))) {
96+ jedis .set ("somekey" , "somevalue" );
97+
98+ assertThat (jedis .get ("somekey" ))
99+ .as ("access to the container works OK before cutting the connection" )
100+ .isEqualTo ("somevalue" );
101+
102+ // disableProxy {
103+ proxy .toxics ().bandwidth ("CUT_CONNECTION_DOWNSTREAM" , ToxicDirection .DOWNSTREAM , 0 );
104+ proxy .toxics ().bandwidth ("CUT_CONNECTION_UPSTREAM" , ToxicDirection .UPSTREAM , 0 );
105+
106+ // for example, expect failure when the connection is cut
107+ assertThat (
108+ catchThrowable (() -> {
109+ jedis .get ("somekey" );
110+ })
111+ )
112+ .as ("calls fail when the connection is cut" )
113+ .isInstanceOf (JedisConnectionException .class );
114+
115+ proxy .toxics ().get ("CUT_CONNECTION_DOWNSTREAM" ).remove ();
116+ proxy .toxics ().get ("CUT_CONNECTION_UPSTREAM" ).remove ();
117+
118+ jedis .close ();
119+ // and with the connection re-established, expect success
120+ assertThat (jedis .get ("somekey" ))
121+ .as ("access to the container works OK after re-establishing the connection" )
122+ .isEqualTo ("somevalue" );
123+ }
121124 // }
122125 }
123126
124127 @ Test
125- public void testMultipleProxiesCanBeCreated () throws IOException {
128+ void testMultipleProxiesCanBeCreated () throws IOException {
126129 try (
127130 GenericContainer <?> secondRedis = new GenericContainer <>("redis:6-alpine" )
128131 .withExposedPorts (6379 )
@@ -138,24 +141,26 @@ public void testMultipleProxiesCanBeCreated() throws IOException {
138141 final Proxy firstProxy = toxiproxyClient .createProxy ("redis1" , "0.0.0.0:8666" , "redis:6379" );
139142 toxiproxyClient .createProxy ("redis2" , "0.0.0.0:8667" , "redis2:6379" );
140143
141- final Jedis firstJedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8666 ));
142- final Jedis secondJedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8667 ));
143-
144- firstJedis .set ("somekey" , "somevalue" );
145- secondJedis .set ("somekey" , "somevalue" );
146-
147- firstProxy .toxics ().bandwidth ("CUT_CONNECTION_DOWNSTREAM" , ToxicDirection .DOWNSTREAM , 0 );
148- firstProxy .toxics ().bandwidth ("CUT_CONNECTION_UPSTREAM" , ToxicDirection .UPSTREAM , 0 );
149-
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" );
144+ try (
145+ Jedis firstJedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8666 ));
146+ Jedis secondJedis = createJedis (toxiproxy .getHost (), toxiproxy .getMappedPort (8667 ))
147+ ) {
148+ firstJedis .set ("somekey" , "somevalue" );
149+ secondJedis .set ("somekey" , "somevalue" );
150+
151+ firstProxy .toxics ().bandwidth ("CUT_CONNECTION_DOWNSTREAM" , ToxicDirection .DOWNSTREAM , 0 );
152+ firstProxy .toxics ().bandwidth ("CUT_CONNECTION_UPSTREAM" , ToxicDirection .UPSTREAM , 0 );
153+
154+ assertThat (
155+ catchThrowable (() -> {
156+ firstJedis .get ("somekey" );
157+ })
158+ )
159+ .as ("calls fail when the connection is cut, for only the relevant proxy" )
160+ .isInstanceOf (JedisConnectionException .class );
161+
162+ assertThat (secondJedis .get ("somekey" )).as ("access via a different proxy is OK" ).isEqualTo ("somevalue" );
163+ }
159164 }
160165 }
161166
0 commit comments