Skip to content

Commit b8e5feb

Browse files
committed
Improve test assertion
1 parent b33fe34 commit b8e5feb

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/main/java/com/rabbitmq/stream/impl/StreamProducerBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
class StreamProducerBuilder implements ProducerBuilder {
3030

31-
static final boolean DEFAULT_DYNAMIC_BATCH = Boolean.parseBoolean(System.getProperty("rabbitmq.stream.producer.dynamic.batch",
32-
"true"));
31+
static final boolean DEFAULT_DYNAMIC_BATCH =
32+
Boolean.parseBoolean(System.getProperty("rabbitmq.stream.producer.dynamic.batch", "true"));
3333

3434
private final StreamEnvironment environment;
3535

src/test/java/com/rabbitmq/stream/impl/RecoveryClusterTest.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import static com.rabbitmq.stream.impl.LoadBalancerClusterTest.LOAD_BALANCER_ADDRESS;
1919
import static com.rabbitmq.stream.impl.TestUtils.newLoggerLevel;
2020
import static com.rabbitmq.stream.impl.TestUtils.sync;
21+
import static com.rabbitmq.stream.impl.TestUtils.waitAtMost;
2122
import static com.rabbitmq.stream.impl.ThreadUtils.threadFactory;
2223
import static com.rabbitmq.stream.impl.Tuples.pair;
2324
import static java.util.stream.Collectors.toList;
2425
import static java.util.stream.IntStream.range;
2526
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.InstanceOfAssertFactories.stream;
2628

2729
import ch.qos.logback.classic.Level;
2830
import com.google.common.collect.Streams;
@@ -41,6 +43,7 @@
4143
import java.util.LinkedHashMap;
4244
import java.util.List;
4345
import java.util.Map;
46+
import java.util.concurrent.Callable;
4447
import java.util.concurrent.ConcurrentHashMap;
4548
import java.util.concurrent.Executors;
4649
import 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

Comments
 (0)