|
|
|
@ -13,12 +13,9 @@ import java.util.concurrent.Future;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.redisson.RedisRunner.RedisProcess;
|
|
|
|
|
import org.redisson.api.RPatternTopic;
|
|
|
|
@ -29,35 +26,7 @@ import org.redisson.api.listener.PatternMessageListener;
|
|
|
|
|
import org.redisson.api.listener.PatternStatusListener;
|
|
|
|
|
import org.redisson.config.Config;
|
|
|
|
|
|
|
|
|
|
public class RedissonTopicPatternTest {
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void beforeClass() throws IOException, InterruptedException {
|
|
|
|
|
if (!RedissonRuntimeEnvironment.isTravis) {
|
|
|
|
|
RedisRunner.startDefaultRedisServerInstance();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void afterClass() throws IOException, InterruptedException {
|
|
|
|
|
if (!RedissonRuntimeEnvironment.isTravis) {
|
|
|
|
|
RedisRunner.shutDownDefaultRedisServerInstance();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void before() throws IOException, InterruptedException {
|
|
|
|
|
if (RedissonRuntimeEnvironment.isTravis) {
|
|
|
|
|
RedisRunner.startDefaultRedisServerInstance();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void after() throws InterruptedException {
|
|
|
|
|
if (RedissonRuntimeEnvironment.isTravis) {
|
|
|
|
|
RedisRunner.shutDownDefaultRedisServerInstance();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public class RedissonTopicPatternTest extends BaseTest {
|
|
|
|
|
|
|
|
|
|
public static class Message {
|
|
|
|
|
|
|
|
|
@ -96,17 +65,37 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
return "Message{" + "name='" + name + '\'' + '}';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testMultiType() throws InterruptedException {
|
|
|
|
|
RPatternTopic topic1 = redisson.getPatternTopic("topic1.*");
|
|
|
|
|
AtomicInteger str = new AtomicInteger();
|
|
|
|
|
topic1.addListener(String.class, (pattern, channel, msg) -> {
|
|
|
|
|
str.incrementAndGet();
|
|
|
|
|
});
|
|
|
|
|
AtomicInteger i = new AtomicInteger();
|
|
|
|
|
topic1.addListener(Integer.class, (pattern, channel, msg) -> {
|
|
|
|
|
i.incrementAndGet();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
redisson.getTopic("topic1.str").publish("123");
|
|
|
|
|
redisson.getTopic("topic1.int").publish(123);
|
|
|
|
|
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals(i.get(), 1);
|
|
|
|
|
Assert.assertEquals(str.get(), 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testUnsubscribe() throws InterruptedException {
|
|
|
|
|
final CountDownLatch messageRecieved = new CountDownLatch(1);
|
|
|
|
|
|
|
|
|
|
RedissonClient redisson = BaseTest.createInstance();
|
|
|
|
|
RPatternTopic<Message> topic1 = redisson.getPatternTopic("topic1.*");
|
|
|
|
|
int listenerId = topic1.addListener((pattern, channel, msg) -> {
|
|
|
|
|
RPatternTopic topic1 = redisson.getPatternTopic("topic1.*");
|
|
|
|
|
int listenerId = topic1.addListener(Message.class, (pattern, channel, msg) -> {
|
|
|
|
|
Assert.fail();
|
|
|
|
|
});
|
|
|
|
|
topic1.addListener((pattern, channel, msg) -> {
|
|
|
|
|
topic1.addListener(Message.class, (pattern, channel, msg) -> {
|
|
|
|
|
Assert.assertTrue(pattern.equals("topic1.*"));
|
|
|
|
|
Assert.assertTrue(channel.equals("topic1.t3"));
|
|
|
|
|
Assert.assertEquals(new Message("123"), msg);
|
|
|
|
@ -117,8 +106,6 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
redisson.getTopic("topic1.t3").publish(new Message("123"));
|
|
|
|
|
|
|
|
|
|
Assert.assertTrue(messageRecieved.await(5, TimeUnit.SECONDS));
|
|
|
|
|
|
|
|
|
|
redisson.shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -126,8 +113,8 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
final CountDownLatch messageRecieved = new CountDownLatch(1);
|
|
|
|
|
|
|
|
|
|
RedissonClient redisson1 = BaseTest.createInstance();
|
|
|
|
|
RPatternTopic<Message> topic1 = redisson1.getPatternTopic("topic.*");
|
|
|
|
|
int listenerId = topic1.addListener((pattern, channel, msg) -> {
|
|
|
|
|
RPatternTopic topic1 = redisson1.getPatternTopic("topic.*");
|
|
|
|
|
int listenerId = topic1.addListener(Message.class, (pattern, channel, msg) -> {
|
|
|
|
|
Assert.fail();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -136,8 +123,8 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
|
|
|
|
|
RedissonClient redisson2 = BaseTest.createInstance();
|
|
|
|
|
RPatternTopic<Message> topic2 = redisson2.getPatternTopic("topic.*");
|
|
|
|
|
topic2.addListener((pattern, channel, msg) -> {
|
|
|
|
|
RPatternTopic topic2 = redisson2.getPatternTopic("topic.*");
|
|
|
|
|
topic2.addListener(Message.class, (pattern, channel, msg) -> {
|
|
|
|
|
Assert.assertTrue(pattern.equals("topic.*"));
|
|
|
|
|
Assert.assertTrue(channel.equals("topic.t1"));
|
|
|
|
|
Assert.assertEquals(new Message("123"), msg);
|
|
|
|
@ -159,7 +146,7 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
|
|
|
|
|
final CountDownLatch statusRecieved = new CountDownLatch(1);
|
|
|
|
|
RedissonClient redisson1 = BaseTest.createInstance();
|
|
|
|
|
RPatternTopic<Message> topic1 = redisson1.getPatternTopic("topic.*");
|
|
|
|
|
RPatternTopic topic1 = redisson1.getPatternTopic("topic.*");
|
|
|
|
|
topic1.addListener(new BasePatternStatusListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onPSubscribe(String pattern) {
|
|
|
|
@ -167,7 +154,7 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
statusRecieved.countDown();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
topic1.addListener((pattern, channel, msg) -> {
|
|
|
|
|
topic1.addListener(Message.class, (pattern, channel, msg) -> {
|
|
|
|
|
Assert.assertEquals(new Message("123"), msg);
|
|
|
|
|
messageRecieved.countDown();
|
|
|
|
|
});
|
|
|
|
@ -199,7 +186,7 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
@Test
|
|
|
|
|
public void testListenerRemove() throws InterruptedException {
|
|
|
|
|
RedissonClient redisson1 = BaseTest.createInstance();
|
|
|
|
|
RPatternTopic<Message> topic1 = redisson1.getPatternTopic("topic.*");
|
|
|
|
|
RPatternTopic topic1 = redisson1.getPatternTopic("topic.*");
|
|
|
|
|
final CountDownLatch l = new CountDownLatch(1);
|
|
|
|
|
topic1.addListener(new BasePatternStatusListener() {
|
|
|
|
|
@Override
|
|
|
|
@ -208,7 +195,7 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
l.countDown();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
int id = topic1.addListener((pattern, channel, msg) -> {
|
|
|
|
|
int id = topic1.addListener(Message.class, (pattern, channel, msg) -> {
|
|
|
|
|
Assert.fail();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -223,9 +210,6 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testConcurrentTopic() throws Exception {
|
|
|
|
|
Config config = BaseTest.createConfig();
|
|
|
|
|
RedissonClient redisson = Redisson.create(config);
|
|
|
|
|
|
|
|
|
|
int threads = 30;
|
|
|
|
|
int loops = 50000;
|
|
|
|
|
|
|
|
|
@ -238,7 +222,7 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
for (int j = 0; j < loops; j++) {
|
|
|
|
|
RPatternTopic<String> t = redisson.getPatternTopic("PUBSUB*");
|
|
|
|
|
RPatternTopic t = redisson.getPatternTopic("PUBSUB*");
|
|
|
|
|
int listenerId = t.addListener(new PatternStatusListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onPUnsubscribe(String channel) {
|
|
|
|
@ -262,8 +246,6 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
for (Future<?> future : futures) {
|
|
|
|
|
future.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
redisson.shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -280,8 +262,8 @@ public class RedissonTopicPatternTest {
|
|
|
|
|
|
|
|
|
|
final AtomicBoolean executed = new AtomicBoolean();
|
|
|
|
|
|
|
|
|
|
RPatternTopic<Integer> topic = redisson.getPatternTopic("topic*");
|
|
|
|
|
topic.addListener(new PatternMessageListener<Integer>() {
|
|
|
|
|
RPatternTopic topic = redisson.getPatternTopic("topic*");
|
|
|
|
|
topic.addListener(Integer.class, new PatternMessageListener<Integer>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onMessage(CharSequence pattern, CharSequence channel, Integer msg) {
|
|
|
|
|
if (msg == 1) {
|
|
|
|
|