Search in sources :

Example 11 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class RedissonTopicTest method testReattach.

@Test
public void testReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().randomPort().run();
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    final AtomicBoolean executed = new AtomicBoolean();
    RTopic<Integer> topic = redisson.getTopic("topic");
    topic.addListener(new MessageListener<Integer>() {

        @Override
        public void onMessage(String channel, Integer msg) {
            if (msg == 1) {
                executed.set(true);
            }
        }
    });
    runner.stop();
    runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
    Thread.sleep(1000);
    redisson.getTopic("topic").publish(1);
    await().atMost(5, TimeUnit.SECONDS).untilTrue(executed);
    redisson.shutdown();
    runner.stop();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Test(org.junit.Test)

Example 12 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class RedissonMultiLockTest method test.

@Test
public void test() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    RedisProcess redis3 = redisTestMultilockInstance();
    NioEventLoopGroup group = new NioEventLoopGroup();
    RedissonClient client1 = createClient(group, redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(group, redis2.getRedisServerAddressAndPort());
    RedissonClient client3 = createClient(group, redis3.getRedisServerAddressAndPort());
    final RLock lock1 = client1.getLock("lock1");
    final RLock lock2 = client2.getLock("lock2");
    final RLock lock3 = client3.getLock("lock3");
    RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
    lock.lock();
    final AtomicBoolean executed = new AtomicBoolean();
    Thread t = new Thread() {

        @Override
        public void run() {
            RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
            assertThat(lock.tryLock()).isFalse();
            assertThat(lock.tryLock()).isFalse();
            executed.set(true);
        }
    };
    t.start();
    t.join();
    await().atMost(5, TimeUnit.SECONDS).until(() -> assertThat(executed.get()).isTrue());
    lock.unlock();
    client1.shutdown();
    client2.shutdown();
    client3.shutdown();
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
    assertThat(redis3.stop()).isEqualTo(0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) RLock(org.redisson.api.RLock) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 13 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class RedissonTest method testMemoryScript.

@Test(expected = RedisOutOfMemoryException.class)
public void testMemoryScript() throws IOException, InterruptedException {
    RedisProcess p = redisTestSmallMemory();
    Config config = new Config();
    config.useSingleServer().setAddress(p.getRedisServerAddressAndPort()).setTimeout(100000);
    RedissonClient r = null;
    try {
        r = Redisson.create(config);
        r.getKeys().flushall();
        for (int i = 0; i < 10000; i++) {
            r.getMap("test").put("" + i, "" + i);
        }
    } finally {
        r.shutdown();
        p.stop();
    }
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Test(org.junit.Test)

Example 14 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class RedissonTopicPatternTest method testReattach.

@Test
public void testReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().randomPort().run();
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    final AtomicBoolean executed = new AtomicBoolean();
    RPatternTopic<Integer> topic = redisson.getPatternTopic("topic*");
    topic.addListener(new PatternMessageListener<Integer>() {

        @Override
        public void onMessage(String pattern, String channel, Integer msg) {
            if (msg == 1) {
                executed.set(true);
            }
        }
    });
    runner.stop();
    runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
    Thread.sleep(1000);
    redisson.getTopic("topic1").publish(1);
    await().atMost(5, TimeUnit.SECONDS).untilTrue(executed);
    redisson.shutdown();
    runner.stop();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Test(org.junit.Test)

Example 15 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class JCacheTest method testRedissonConfig.

@Test
public void testRedissonConfig() throws InterruptedException, IllegalArgumentException, URISyntaxException, IOException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().port(6311).run();
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager().createCache("test", config);
    cache.put("1", "2");
    Assert.assertEquals("2", cache.get("1"));
    cache.close();
    runner.stop();
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) RedisRunner(org.redisson.RedisRunner) URL(java.net.URL) BaseTest(org.redisson.BaseTest) Test(org.junit.Test)

Aggregations

RedisProcess (org.redisson.RedisRunner.RedisProcess)25 Test (org.junit.Test)23 RedissonClient (org.redisson.api.RedissonClient)23 Config (org.redisson.config.Config)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 RLock (org.redisson.api.RLock)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 ExecutorService (java.util.concurrent.ExecutorService)2 BaseTest (org.redisson.BaseTest)2 RedisRunner (org.redisson.RedisRunner)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeoutException (java.util.concurrent.TimeoutException)1 MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)1 MutableConfiguration (javax.cache.configuration.MutableConfiguration)1