Search in sources :

Example 1 with RedisProcess

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

the class RedissonBlockingQueueTest method testTakeReattachCluster.

@Test
public void testTakeReattachCluster() throws IOException, InterruptedException {
    RedisRunner master1 = new RedisRunner().port(6890).randomDir().nosave();
    RedisRunner master2 = new RedisRunner().port(6891).randomDir().nosave();
    RedisRunner master3 = new RedisRunner().port(6892).randomDir().nosave();
    RedisRunner slave1 = new RedisRunner().port(6900).randomDir().nosave();
    RedisRunner slave2 = new RedisRunner().port(6901).randomDir().nosave();
    RedisRunner slave3 = new RedisRunner().port(6902).randomDir().nosave();
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
    ClusterProcesses process = clusterRunner.run();
    Thread.sleep(1000);
    Config config = new Config();
    config.useClusterServers().setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RedisProcess master = process.getNodes().stream().filter(x -> x.getRedisServerPort() == master1.getPort()).findFirst().get();
    List<RFuture<Integer>> futures = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        RBlockingQueue<Integer> queue = redisson.getBlockingQueue("queue" + i);
        RFuture<Integer> f = queue.takeAsync();
        try {
            f.toCompletableFuture().get(1, TimeUnit.SECONDS);
        } catch (ExecutionException | TimeoutException e) {
        // skip
        }
        futures.add(f);
    }
    master.stop();
    Thread.sleep(TimeUnit.SECONDS.toMillis(80));
    for (int i = 0; i < 10; i++) {
        RBlockingQueue<Integer> queue = redisson.getBlockingQueue("queue" + i);
        queue.put(i * 100);
    }
    for (int i = 0; i < 10; i++) {
        RFuture<Integer> f = futures.get(i);
        try {
            f.toCompletableFuture().get(20, TimeUnit.SECONDS);
        } catch (ExecutionException | TimeoutException e) {
        // skip
        }
        if (f.cause() != null) {
            f.cause().printStackTrace();
        }
        Integer result = f.toCompletableFuture().getNow(null);
        assertThat(result).isEqualTo(i * 100);
    }
    redisson.shutdown();
    process.shutdown();
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) ArrayList(java.util.ArrayList) RFuture(org.redisson.api.RFuture) RedissonClient(org.redisson.api.RedissonClient) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 2 with RedisProcess

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

the class RedissonBoundedBlockingQueueTest 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);
    redisson.getKeys().flushall();
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("testTakeReattach");
    assertThat(queue1.trySetCapacity(15)).isTrue();
    RFuture<Integer> f = queue1.takeAsync();
    try {
        f.toCompletableFuture().get(1, TimeUnit.SECONDS);
    } catch (ExecutionException | TimeoutException e) {
    // skip
    }
    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);
    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.jupiter.api.Test)

Example 3 with RedisProcess

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

the class RedissonBoundedBlockingQueueTest 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 RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue:pollTimeout");
    assertThat(queue1.trySetCapacity(5)).isTrue();
    RFuture<Integer> f = queue1.pollAsync(5, TimeUnit.SECONDS);
    try {
        f.toCompletableFuture().get(1, TimeUnit.SECONDS);
        Assertions.fail();
    } catch (TimeoutException e) {
    // skip
    }
    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.jupiter.api.Test)

Example 4 with RedisProcess

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

the class RedissonPriorityBlockingQueueTest method testTakeReattach.

@Test
public void testTakeReattach() throws Exception {
    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 = getQueue(redisson);
    RFuture<Integer> f = queue1.takeAsync();
    try {
        f.toCompletableFuture().get(1, TimeUnit.SECONDS);
    } catch (ExecutionException | TimeoutException e) {
    // skip
    }
    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 + 10000);
    }
    Integer result = f.get();
    assertThat(result).isEqualTo(123);
    assertThat(queue1.size()).isEqualTo(10);
    runner.stop();
    redisson.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 5 with RedisProcess

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

the class RedissonBucketTest method testSetListener.

@Test
public void testSetListener() throws FailedToStartRedisException, IOException, InterruptedException {
    RedisProcess instance = new RedisRunner().nosave().randomPort().randomDir().notifyKeyspaceEvents(KEYSPACE_EVENTS_OPTIONS.E, KEYSPACE_EVENTS_OPTIONS.$).run();
    Config config = new Config();
    config.useSingleServer().setAddress(instance.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RBucket<Integer> al = redisson.getBucket("test");
    CountDownLatch latch = new CountDownLatch(1);
    al.addListener(new SetObjectListener() {

        @Override
        public void onSet(String name) {
            latch.countDown();
        }
    });
    al.set(1);
    assertThat(latch.await(1, TimeUnit.SECONDS)).isTrue();
    redisson.shutdown();
    instance.stop();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) SetObjectListener(org.redisson.api.listener.SetObjectListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

RedisProcess (org.redisson.RedisRunner.RedisProcess)55 Test (org.junit.jupiter.api.Test)49 Config (org.redisson.config.Config)41 RedissonClient (org.redisson.api.RedissonClient)30 BaseTest (org.redisson.BaseTest)16 RedisRunner (org.redisson.RedisRunner)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 RLock (org.redisson.api.RLock)12 URL (java.net.URL)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 IOException (java.io.IOException)6 TimeoutException (java.util.concurrent.TimeoutException)6 MutableConfiguration (javax.cache.configuration.MutableConfiguration)6 URI (java.net.URI)5 ExecutionException (java.util.concurrent.ExecutionException)5 MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)5 HashMap (java.util.HashMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 ClusterProcesses (org.redisson.ClusterRunner.ClusterProcesses)4