1515import static org .junit .jupiter .api .Assertions .assertEquals ;
1616import static org .junit .jupiter .api .Assertions .assertThrows ;
1717import static org .junit .jupiter .api .Assertions .assertTrue ;
18+ import static org .testcontainers .containers .utils .TarantoolContainerClientHelper .createTarantoolContainer ;
19+ import static org .testcontainers .containers .utils .TarantoolContainerClientHelper .execInitScript ;
20+ import static org .testcontainers .containers .utils .TarantoolContainerClientHelper .executeCommandDecoded ;
1821import io .micrometer .core .instrument .MeterRegistry ;
1922import io .netty .util .HashedWheelTimer ;
2023import io .netty .util .Timer ;
24+ import org .junit .jupiter .api .AfterEach ;
2125import org .junit .jupiter .api .BeforeEach ;
2226import org .junit .jupiter .api .Test ;
2327import org .junit .jupiter .api .Timeout ;
2428import org .slf4j .Logger ;
2529import org .slf4j .LoggerFactory ;
26- import org .testcontainers .containers .TarantoolContainer ;
27- import org .testcontainers .containers .output .Slf4jLogConsumer ;
28- import org .testcontainers .junit .jupiter .Container ;
29- import org .testcontainers .junit .jupiter .Testcontainers ;
30+ import org .testcontainers .containers .tarantool .TarantoolContainer ;
3031
3132import static io .tarantool .core .protocol .requests .IProtoConstant .IPROTO_DATA ;
3233import io .tarantool .balancer .TarantoolBalancer ;
4041import io .tarantool .pool .IProtoClientPoolImpl ;
4142import io .tarantool .pool .InstanceConnectionGroup ;
4243
43- @ Timeout (value = 15 )
44- @ Testcontainers
44+ @ Timeout (value = 25 )
4545public class DistributingRoundRobinBalancerTest extends BaseTest {
4646
4747 private static final Logger log =
4848 LoggerFactory .getLogger (DistributingRoundRobinBalancerTest .class );
4949
50- @ Container
51- private final TarantoolContainer tt1 =
52- new TarantoolContainer ()
53- .withEnv (ENV_MAP )
54- .withExposedPort (3305 )
55- .withLogConsumer (new Slf4jLogConsumer (log ));
56-
57- @ Container
58- private final TarantoolContainer tt2 =
59- new TarantoolContainer ()
60- .withEnv (ENV_MAP )
61- .withExposedPort (3305 )
62- .withLogConsumer (new Slf4jLogConsumer (log ));
50+ private static TarantoolContainer <?> tt1 ;
51+ private static TarantoolContainer <?> tt2 ;
6352
6453 @ BeforeEach
6554 public void setUp () {
55+ tt1 = createTarantoolContainer ().withEnv (ENV_MAP ).withExposedPorts (3305 );
56+ tt2 = createTarantoolContainer ().withEnv (ENV_MAP ).withExposedPorts (3305 );
57+
58+ tt1 .start ();
59+ tt2 .start ();
60+
61+ execInitScript (tt1 );
62+ execInitScript (tt2 );
6663 do {
6764 count1 = ThreadLocalRandom .current ().nextInt (MIN_CONNECTION_COUNT , MAX_CONNECTION_COUNT + 1 );
6865 count2 = ThreadLocalRandom .current ().nextInt (MIN_CONNECTION_COUNT , MAX_CONNECTION_COUNT + 1 );
6966 } while (count1 == count2 );
7067 }
7168
69+ @ AfterEach
70+ void tearDown () {
71+ tt1 .stop ();
72+ tt2 .stop ();
73+ }
74+
7275 private static IProtoClientPool createClientPool (
7376 boolean gracefulShutdown , HeartbeatOpts heartbeatOpts , MeterRegistry metricsRegistry ) {
7477 ManagedResource <Timer > timerResource =
@@ -78,25 +81,28 @@ private static IProtoClientPool createClientPool(
7881 factory , timerResource , gracefulShutdown , heartbeatOpts , null , metricsRegistry );
7982 }
8083
81- private int getSessionCounter (TarantoolContainer tt ) throws Exception {
82- List <?> result = tt . executeCommandDecoded ("return get_session_counter()" );
84+ private int getSessionCounter (TarantoolContainer <?> tt ) throws Exception {
85+ List <?> result = executeCommandDecoded (tt , "return get_session_counter()" );
8386 return (Integer ) result .get (0 );
8487 }
8588
86- private int getCallCounter (TarantoolContainer tt ) throws Exception {
87- List <?> result = tt . executeCommandDecoded ("return get_call_counter()" );
89+ private int getCallCounter (TarantoolContainer <?> tt ) throws Exception {
90+ List <?> result = executeCommandDecoded (tt , "return get_call_counter()" );
8891 return (Integer ) result .get (0 );
8992 }
9093
91- private void execLua (TarantoolContainer container , String command ) {
94+ private void execLua (TarantoolContainer <?> container , String command ) {
9295 try {
93- container . executeCommandDecoded (command );
96+ executeCommandDecoded (container , command );
9497 } catch (Exception e ) {
9598 }
9699 }
97100
98101 private void wakeUpAllConnects (
99- TarantoolBalancer rrBalancer , int nodeVisits , TarantoolContainer tt1 , TarantoolContainer tt2 )
102+ TarantoolBalancer rrBalancer ,
103+ int nodeVisits ,
104+ TarantoolContainer <?> tt1 ,
105+ TarantoolContainer <?> tt2 )
100106 throws Exception {
101107 walkAndJoin (rrBalancer , nodeVisits * 2 );
102108 assertEquals (count1 , getSessionCounter (tt1 ));
@@ -130,13 +136,13 @@ public void testDistributingRoundRobin() throws Exception {
130136 Arrays .asList (
131137 InstanceConnectionGroup .builder ()
132138 .withHost (tt1 .getHost ())
133- .withPort (tt1 .getPort ())
139+ .withPort (tt1 .getFirstMappedPort ())
134140 .withSize (count1 )
135141 .withTag ("node-a-00" )
136142 .build (),
137143 InstanceConnectionGroup .builder ()
138144 .withHost (tt2 .getHost ())
139- .withPort (tt2 .getPort ())
145+ .withPort (tt2 .getFirstMappedPort ())
140146 .withSize (count2 )
141147 .withTag ("node-b-00" )
142148 .build ()));
@@ -160,7 +166,7 @@ public void testDistributingRoundRobinWithUnavailableNodeA() throws Exception {
160166 .build (),
161167 InstanceConnectionGroup .builder ()
162168 .withHost (tt2 .getHost ())
163- .withPort (tt2 .getPort ())
169+ .withPort (tt2 .getFirstMappedPort ())
164170 .withSize (count2 )
165171 .withTag ("node-b-01" )
166172 .build ()));
@@ -176,8 +182,10 @@ public void testDistributingRoundRobinWithUnavailableNodeA() throws Exception {
176182 execLua (tt2 , "reset_call_counter()" );
177183
178184 log .info ("waiting for invalidation" );
179- Thread .sleep (INVALIDATION_TIMEOUT );
185+ Thread .sleep (EXTENDED_INVALIDATION_TIMEOUT );
180186
187+ execLua (tt1 , "reset_call_counter()" );
188+ execLua (tt2 , "reset_call_counter()" );
181189 log .info ("walk on node B only" );
182190 walkAndJoin (rrBalancer , nodeVisits * 2 );
183191 assertEquals (0 , getCallCounter (tt1 ));
@@ -217,7 +225,7 @@ public void testDistributingRoundRobinWithUnavailableNodeANoUnlock() throws Exce
217225 .build (),
218226 InstanceConnectionGroup .builder ()
219227 .withHost (tt2 .getHost ())
220- .withPort (tt2 .getPort ())
228+ .withPort (tt2 .getFirstMappedPort ())
221229 .withSize (count2 )
222230 .withTag ("node-b-02" )
223231 .build ()));
@@ -233,8 +241,10 @@ public void testDistributingRoundRobinWithUnavailableNodeANoUnlock() throws Exce
233241 execLua (tt2 , "reset_call_counter()" );
234242
235243 log .info ("waiting for invalidation" );
236- Thread .sleep (INVALIDATION_TIMEOUT );
244+ Thread .sleep (EXTENDED_INVALIDATION_TIMEOUT );
237245
246+ execLua (tt1 , "reset_call_counter()" );
247+ execLua (tt2 , "reset_call_counter()" );
238248 log .info ("walk on node B only" );
239249 walkAndJoin (rrBalancer , nodeVisits * 2 );
240250 assertEquals (0 , getCallCounter (tt1 ));
@@ -288,7 +298,7 @@ public void testDistributingRoundRobinNoAvailableClients() throws Exception {
288298 execLua (tt2 , "lock_pipe(true); reset_call_counter()" );
289299
290300 log .info ("waiting for invalidation" );
291- Thread .sleep (INVALIDATION_TIMEOUT );
301+ Thread .sleep (EXTENDED_INVALIDATION_TIMEOUT );
292302
293303 Throwable exc = assertThrows (ExecutionException .class , () -> rrBalancer .getNext ().get ());
294304 assertEquals (NoAvailableClientsException .class , exc .getCause ().getClass ());
@@ -323,12 +333,12 @@ public void testDistributingRoundRobinStartWithStuckNodeA() throws Exception {
323333 Arrays .asList (
324334 InstanceConnectionGroup .builder ()
325335 .withHost (tt1 .getHost ())
326- .withPort (tt1 .getPort ())
336+ .withPort (tt1 .getFirstMappedPort ())
327337 .withTag ("node-a-01" )
328338 .build (),
329339 InstanceConnectionGroup .builder ()
330340 .withHost (tt2 .getHost ())
331- .withPort (tt2 .getPort ())
341+ .withPort (tt2 .getFirstMappedPort ())
332342 .withTag ("node-b-01" )
333343 .build ()));
334344 pool .setConnectTimeout (3_000 );
0 commit comments