1818import static com .rabbitmq .stream .impl .LoadBalancerClusterTest .LOAD_BALANCER_ADDRESS ;
1919import static com .rabbitmq .stream .impl .TestUtils .newLoggerLevel ;
2020import static com .rabbitmq .stream .impl .TestUtils .sync ;
21+ import static com .rabbitmq .stream .impl .TestUtils .waitAtMost ;
2122import static com .rabbitmq .stream .impl .ThreadUtils .threadFactory ;
2223import static com .rabbitmq .stream .impl .Tuples .pair ;
2324import static java .util .stream .Collectors .toList ;
2425import static java .util .stream .IntStream .range ;
2526import static org .assertj .core .api .Assertions .assertThat ;
27+ import static org .assertj .core .api .InstanceOfAssertFactories .stream ;
2628
2729import ch .qos .logback .classic .Level ;
2830import com .google .common .collect .Streams ;
4143import java .util .LinkedHashMap ;
4244import java .util .List ;
4345import java .util .Map ;
46+ import java .util .concurrent .Callable ;
4447import java .util .concurrent .ConcurrentHashMap ;
4548import java .util .concurrent .Executors ;
4649import java .util .concurrent .ScheduledExecutorService ;
@@ -289,7 +292,7 @@ void clusterRestart(boolean useLoadBalancer, boolean forceLeader) throws Interru
289292
290293 @ ParameterizedTest
291294 @ ValueSource (booleans = {true , false })
292- void sacWithClusterRestart (boolean superStream ) throws InterruptedException {
295+ void sacWithClusterRestart (boolean superStream ) throws Exception {
293296 environment =
294297 environmentBuilder
295298 .uris (URIS )
@@ -351,13 +354,31 @@ void sacWithClusterRestart(boolean superStream) throws InterruptedException {
351354 sync = consumers .get (0 ).waitForNewMessages (100 );
352355 assertThat (sync ).completes ();
353356
354- List <Cli .SubscriptionInfo > subscriptions =
355- Cli .listGroupConsumers (superStream ? s + "-0" : s , app );
356- assertThat (subscriptions ).hasSize (consumerCount );
357- assertThat (subscriptions .stream ().filter (sub -> sub .state ().startsWith ("active" )).count ())
358- .isEqualTo (1 );
359- assertThat (subscriptions .stream ().filter (sub -> sub .state ().startsWith ("waiting" )).count ())
360- .isEqualTo (2 );
357+ String streamArg = superStream ? s + "-0" : s ;
358+
359+ Callable <Void > checkConsumers =
360+ () -> {
361+ waitAtMost (
362+ () -> {
363+ List <Cli .SubscriptionInfo > subscriptions = Cli .listGroupConsumers (streamArg , app );
364+ LOGGER .info ("Group consumers: {}" , subscriptions );
365+ return subscriptions .size () == consumerCount
366+ && subscriptions .stream ()
367+ .filter (sub -> sub .state ().startsWith ("active" ))
368+ .count ()
369+ == 1
370+ && subscriptions .stream ()
371+ .filter (sub -> sub .state ().startsWith ("waiting" ))
372+ .count ()
373+ == 2 ;
374+ },
375+ () ->
376+ "Group consumers not in expected state: "
377+ + Cli .listGroupConsumers (streamArg , app ));
378+ return null ;
379+ };
380+
381+ checkConsumers .call ();
361382
362383 restartCluster ();
363384
@@ -375,12 +396,7 @@ void sacWithClusterRestart(boolean superStream) throws InterruptedException {
375396 sync = consumers .get (activeIndex ).waitForNewMessages (100 );
376397 assertThat (sync ).completes (ASSERTION_TIMEOUT );
377398
378- subscriptions = Cli .listGroupConsumers (superStream ? s + "-0" : s , app );
379- assertThat (subscriptions ).hasSize (consumerCount );
380- assertThat (subscriptions .stream ().filter (sub -> sub .state ().startsWith ("active" )).count ())
381- .isEqualTo (1 );
382- assertThat (subscriptions .stream ().filter (sub -> sub .state ().startsWith ("waiting" )).count ())
383- .isEqualTo (2 );
399+ checkConsumers .call ();
384400
385401 } finally {
386402 if (pState != null ) {
0 commit comments