Skip to content

Commit 7c9a5a7

Browse files
committed
rewrite RedisPubSubClusterTest to use executeWhenConditionSatisfied
This test used to simply wait 1 second between subscription and publication. This commit rewrites it to use `executeWhenConditionSatisfied()`, which shows the intent much better.
1 parent 479fd42 commit 7c9a5a7

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/test/java/io/vertx/tests/redis/client/RedisPubSubClusterTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.vertx.tests.redis.client;
22

3+
import io.vertx.core.json.JsonObject;
34
import io.vertx.ext.unit.Async;
45
import io.vertx.ext.unit.TestContext;
56
import io.vertx.ext.unit.junit.RunTestOnContext;
@@ -18,6 +19,10 @@
1819
import org.junit.Test;
1920
import org.junit.runner.RunWith;
2021

22+
import java.util.concurrent.atomic.AtomicInteger;
23+
24+
import static io.vertx.tests.redis.client.TestUtils.executeWhenConditionSatisfied;
25+
2126
@RunWith(VertxUnitRunner.class)
2227
public class RedisPubSubClusterTest {
2328

@@ -69,22 +74,29 @@ public void testSubscribeMultipleTimes(TestContext should) {
6974
});
7075
}
7176

72-
rule.vertx().eventBus().consumer("io.vertx.redis." + channel, msg -> {
73-
rule.vertx().eventBus().publish(channel, msg.body());
77+
AtomicInteger subs = new AtomicInteger();
78+
rule.vertx().eventBus().<JsonObject>consumer("io.vertx.redis." + channel, msg -> {
79+
if ("subscribe".equals(msg.body().getString("type"))) {
80+
subs.incrementAndGet();
81+
}
82+
if ("message".equals(msg.body().getString("type"))) {
83+
rule.vertx().eventBus().publish(channel, msg.body());
84+
}
7485
});
7586

7687
subConn.handler(EventBusHandler.create(rule.vertx()));
7788
subUnsub(channel, N, should.async(N));
7889

79-
rule.vertx().setTimer(1000, id -> {
80-
pubConn.send(Request.cmd(Command.PUBLISH).arg(channel).arg("hello"))
81-
.onComplete(should.asyncAssertSuccess());
90+
executeWhenConditionSatisfied(rule.vertx(), () -> subs.get() == 10, () -> {
91+
pubConn.send(Request.cmd(Command.PUBLISH).arg(channel).arg("hello")).onComplete(should.asyncAssertSuccess(publish -> {
92+
should.assertEquals(1, publish.toInteger());
93+
}));
8294
});
8395
}
8496

8597
private void subUnsub(String channel, int attempts, Async testSub) {
86-
subConn.send(Request.cmd(Command.UNSUBSCRIBE).arg(channel)).onComplete(unreply -> {
87-
subConn.send(Request.cmd(Command.SUBSCRIBE).arg(channel)).onComplete(reply -> {
98+
subConn.send(Request.cmd(Command.UNSUBSCRIBE).arg(channel)).onComplete(unsub -> {
99+
subConn.send(Request.cmd(Command.SUBSCRIBE).arg(channel)).onComplete(sub -> {
88100
testSub.countDown();
89101
if (attempts > 1) {
90102
subUnsub(channel, attempts - 1, testSub);

0 commit comments

Comments
 (0)