Search in sources :

Example 21 with RedisProcess

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

the class RedissonBlockingQueueTest method testPollReattach.

@Test
public void testPollReattach() 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();
    Thread t = new Thread() {

        public void run() {
            try {
                RBlockingQueue<Integer> queue1 = redisson.getBlockingQueue("queue:pollany");
                long start = System.currentTimeMillis();
                Integer res = queue1.poll(10, TimeUnit.SECONDS);
                assertThat(System.currentTimeMillis() - start).isGreaterThan(2000);
                assertThat(res).isEqualTo(123);
                executed.set(true);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        ;
    };
    t.start();
    t.join(1000);
    runner.stop();
    runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
    Thread.sleep(1000);
    RBlockingQueue<Integer> queue1 = redisson.getBlockingQueue("queue:pollany");
    queue1.put(123);
    t.join();
    await().atMost(5, TimeUnit.SECONDS).until(() -> assertThat(executed.get()).isTrue());
    redisson.shutdown();
    runner.stop();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 22 with RedisProcess

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

the class RedissonBlockingQueueTest method testPollWithBrokenConnection.

@Test
public void testPollWithBrokenConnection() throws IOException, InterruptedException, ExecutionException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().randomPort().run();
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    final RBlockingQueue<Integer> queue1 = redisson.getBlockingQueue("queue:pollTimeout");
    RFuture<Integer> f = queue1.pollAsync(5, TimeUnit.SECONDS);
    Assert.assertFalse(f.await(1, TimeUnit.SECONDS));
    runner.stop();
    long start = System.currentTimeMillis();
    assertThat(f.get()).isNull();
    assertThat(System.currentTimeMillis() - start).isGreaterThan(3800);
    redisson.shutdown();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Test(org.junit.Test)

Example 23 with RedisProcess

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

the class RedissonBlockingQueueTest method testTakeReattach.

@Test
public void testTakeReattach() 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);
    RBlockingQueue<Integer> queue1 = redisson.getBlockingQueue("testTakeReattach");
    RFuture<Integer> f = queue1.takeAsync();
    f.await(1, TimeUnit.SECONDS);
    runner.stop();
    runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
    queue1.put(123);
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i);
    }
    assertThat(queue1.size()).isEqualTo(10);
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    runner.stop();
    redisson.shutdown();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Test(org.junit.Test)

Example 24 with RedisProcess

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

the class RedissonBoundedBlockingQueueTest method testPollReattach.

@Test
public void testPollReattach() 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);
    redisson.getKeys().flushall();
    final AtomicBoolean executed = new AtomicBoolean();
    Thread t = new Thread() {

        public void run() {
            try {
                RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("queue:pollany");
                assertThat(queue1.trySetCapacity(10)).isTrue();
                long start = System.currentTimeMillis();
                Integer res = queue1.poll(10, TimeUnit.SECONDS);
                assertThat(System.currentTimeMillis() - start).isGreaterThan(2000);
                assertThat(res).isEqualTo(123);
                executed.set(true);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        ;
    };
    t.start();
    t.join(1000);
    runner.stop();
    runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
    Thread.sleep(1000);
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("queue:pollany");
    assertThat(queue1.trySetCapacity(10)).isTrue();
    queue1.put(123);
    t.join();
    await().atMost(5, TimeUnit.SECONDS).until(() -> assertThat(executed.get()).isTrue());
    redisson.shutdown();
    runner.stop();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 25 with RedisProcess

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

the class RedissonBoundedBlockingQueueTest method testPollAsyncReattach.

@Test
public void testPollAsyncReattach() 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);
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("queue:pollany");
    RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
    f.await(1, TimeUnit.SECONDS);
    runner.stop();
    runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
    assertThat(queue1.trySetCapacity(15)).isTrue();
    queue1.put(123);
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i);
    }
    assertThat(queue1.size()).isEqualTo(10);
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    redisson.shutdown();
    runner.stop();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) 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