Last active
August 31, 2016 02:38
-
-
Save tusharm/05ac8dd454f09bd0ec89ef1eeb74b1b5 to your computer and use it in GitHub Desktop.
Testcase for Redis client pipelining
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.lambdaworks.redis.RedisClient; | |
import com.lambdaworks.redis.RedisConnectionPool; | |
import com.lambdaworks.redis.api.async.RedisAsyncCommands; | |
import org.junit.After; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.util.concurrent.CompletableFuture; | |
import static java.lang.Double.valueOf; | |
import static java.time.Instant.now; | |
import static java.util.concurrent.CompletableFuture.completedFuture; | |
import static org.junit.Assert.assertEquals; | |
public class RedisClientTest { | |
private RedisClient redisClient; | |
@Before | |
public void beforeEach() { | |
redisClient = RedisClient.create("redis://localhost:6379"); | |
} | |
@Test | |
public void testCommandPipelining() throws Exception { | |
try (RedisAsyncCommands<String, String> commands = redisClient.connect().async()) { | |
commands.setAutoFlushCommands(false); | |
Double now = valueOf(now().toEpochMilli()); | |
String testMessage = "{\"message\":\"all good\"}"; | |
String channelName = "my-test-channel"; | |
// add events to a sorted set | |
CompletableFuture<Long> zadd = commands.zadd(channelName, now, testMessage).toCompletableFuture(); | |
// publish | |
CompletableFuture<Long> publish = commands.pubsubChannels() | |
.thenCompose(list -> { | |
// if the channel is not active, return a completed future | |
if (!list.contains(channelName)) { | |
return completedFuture(null); | |
} | |
return commands.publish(channelName, testMessage).toCompletableFuture(); | |
}).toCompletableFuture(); | |
commands.flushCommands(); | |
// wait for futures to complete | |
CompletableFuture.allOf(zadd, publish).get(); | |
commands.setAutoFlushCommands(true); | |
assertEquals(commands.zcard(channelName).get().longValue(), 1L); | |
} | |
} | |
@After | |
public void afterEach() { | |
redisClient.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If no clients are subscribed to the channel "my-test-channel", the test passes. However, if there is an active subscription, the test just keeps running forever and never returns. I suspect the Netty event loop gets blocked somehow..not sure why.