Exemplo n.º 1
0
  //    @Test
  public void testMultiThreads() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance(6320);

    Config config1 = new Config();
    config1.useSingleServer().setAddress("127.0.0.1:6320");
    RedissonClient client = Redisson.create(config1);

    RLock lock1 = client.getLock("lock1");
    RLock lock2 = client.getLock("lock2");
    RLock lock3 = client.getLock("lock3");

    Thread t =
        new Thread() {
          public void run() {
            RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
            lock.lock();

            try {
              Thread.sleep(3000);
            } catch (InterruptedException e) {
            }

            lock.unlock();
          };
        };
    t.start();
    t.join(1000);

    RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
    lock.lock();
    lock.unlock();

    assertThat(redis1.stop()).isEqualTo(0);
  }
Exemplo n.º 2
0
 @Test(expected = WriteRedisConnectionException.class)
 public void testSer() {
   Config config = new Config();
   config.useSingleServer().setAddress("127.0.0.1:6379");
   config.setCodec(new SerializationCodec());
   RedissonClient r = Redisson.create(config);
   r.getMap("test").put("1", new Dummy());
 }
Exemplo n.º 3
0
 private RedissonClient createClient(NioEventLoopGroup group, String host) {
   Config config1 = new Config();
   config1.useSingleServer().setAddress(host);
   config1.setEventLoopGroup(group);
   RedissonClient client1 = Redisson.create(config1);
   client1.getKeys().flushdb();
   return client1;
 }
Exemplo n.º 4
0
  @Test
  public void testConnectionListener() throws IOException, InterruptedException, TimeoutException {

    Process p = runRedis();

    final Waiter onConnectWaiter = new Waiter();
    final Waiter onDisconnectWaiter = new Waiter();

    Config config = new Config();
    config.useSingleServer().setAddress("127.0.0.1:6319").setFailedAttempts(1).setRetryAttempts(1);
    config.setConnectionListener(
        new ConnectionListener() {

          @Override
          public void onDisconnect(InetSocketAddress addr) {
            onDisconnectWaiter.assertEquals(new InetSocketAddress("127.0.0.1", 6319), addr);
            onDisconnectWaiter.resume();
          }

          @Override
          public void onConnect(InetSocketAddress addr) {
            onConnectWaiter.assertEquals(new InetSocketAddress("127.0.0.1", 6319), addr);
            onConnectWaiter.resume();
          }
        });

    RedissonClient r = Redisson.create(config);
    r.getBucket("1").get();

    p.destroy();
    Assert.assertEquals(1, p.waitFor());

    try {
      r.getBucket("1").get();
    } catch (Exception e) {
    }

    p = runRedis();

    r.getBucket("1").get();

    r.shutdown();

    p.destroy();
    Assert.assertEquals(1, p.waitFor());

    onConnectWaiter.await(1, TimeUnit.SECONDS, 2);
    onDisconnectWaiter.await();
  }
Exemplo n.º 5
0
  @Test
  public void testLockSuccess() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance(6320);
    RedisProcess redis2 = redisTestMultilockInstance(6321);

    RedissonClient client1 = createClient("127.0.0.1:6320");
    RedissonClient client2 = createClient("127.0.0.1:6321");

    RLock lock1 = client1.getLock("lock1");
    RLock lock2 = client1.getLock("lock2");
    RLock lock3 = client2.getLock("lock3");

    Thread t1 =
        new Thread() {
          public void run() {
            lock3.lock();
          };
        };
    t1.start();
    t1.join();

    Thread t =
        new Thread() {
          public void run() {
            RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
            lock.lock();

            try {
              Thread.sleep(3000);
            } catch (InterruptedException e) {
            }

            lock.unlock();
          };
        };
    t.start();
    t.join(1000);

    RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
    lock.lock();
    lock.unlock();

    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
  }
Exemplo n.º 6
0
  //    @Test
  public void testSentinel() {
    NodesGroup<Node> nodes = redisson.getNodesGroup();
    Assert.assertEquals(5, nodes.getNodes().size());

    for (Node node : nodes.getNodes()) {
      Assert.assertTrue(node.ping());
    }

    Assert.assertTrue(nodes.pingAll());
  }
Exemplo n.º 7
0
  //    @Test
  public void test() {
    NodesGroup<Node> nodes = redisson.getNodesGroup();
    Assert.assertEquals(1, nodes.getNodes().size());
    Iterator<Node> iter = nodes.getNodes().iterator();

    Node node1 = iter.next();
    Assert.assertTrue(node1.ping());

    Assert.assertTrue(nodes.pingAll());
  }
Exemplo n.º 8
0
  @Test
  public void testCluster() {
    NodesGroup<ClusterNode> nodes = redisson.getClusterNodesGroup();
    Assert.assertEquals(2, nodes.getNodes().size());

    for (ClusterNode node : nodes.getNodes()) {
      Map<String, String> params = node.info();
      Assert.assertNotNull(params);
      Assert.assertTrue(node.ping());
    }

    Assert.assertTrue(nodes.pingAll());
  }
  @Test
  public void testSingleCountDownAwait_SingleInstance() throws InterruptedException {
    final int iterations = Runtime.getRuntime().availableProcessors() * 3;

    RedissonClient redisson = BaseTest.createInstance();
    final RCountDownLatch latch = redisson.getCountDownLatch("latch");
    latch.trySetCount(iterations);

    final AtomicInteger counter = new AtomicInteger();
    ExecutorService executor = Executors.newScheduledThreadPool(iterations);
    for (int i = 0; i < iterations; i++) {
      executor.execute(
          () -> {
            try {
              latch.await();
              Assert.assertEquals(0, latch.getCount());
              Assert.assertEquals(iterations, counter.get());
            } catch (InterruptedException e) {
              Assert.fail();
            }
          });
    }

    ExecutorService countDownExecutor = Executors.newFixedThreadPool(iterations);
    for (int i = 0; i < iterations; i++) {
      countDownExecutor.execute(
          () -> {
            latch.countDown();
            counter.incrementAndGet();
          });
    }

    executor.shutdown();
    Assert.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));

    redisson.shutdown();
  }
Exemplo n.º 10
0
  @Test
  public void testShutdown() {
    Config config = new Config();
    config.useSingleServer().setAddress("127.0.0.1:6379");

    RedissonClient r = Redisson.create(config);
    Assert.assertFalse(r.isShuttingDown());
    Assert.assertFalse(r.isShutdown());
    r.shutdown();
    Assert.assertTrue(r.isShuttingDown());
    Assert.assertTrue(r.isShutdown());
  }
Exemplo n.º 11
0
  //    @Test
  public void test() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance(6320);
    RedisProcess redis2 = redisTestMultilockInstance(6321);
    RedisProcess redis3 = redisTestMultilockInstance(6322);

    NioEventLoopGroup group = new NioEventLoopGroup();

    RedissonClient client1 = createClient(group, "127.0.0.1:6320");
    RedissonClient client2 = createClient(group, "127.0.0.1:6321");
    RedissonClient client3 = createClient(group, "127.0.0.1:6322");

    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();

    assertThat(redis1.stop()).isEqualTo(0);

    assertThat(redis2.stop()).isEqualTo(0);

    assertThat(redis3.stop()).isEqualTo(0);
  }