Search in sources :

Example 26 with RedisProcess

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

the class JCacheTest method testUpdate.

@Test
public void testUpdate() throws IOException, InterruptedException, URISyntaxException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().port(6311).run();
    MutableConfiguration<String, String> config = new MutableConfiguration<>();
    config.setStoreByValue(true);
    URI configUri = getClass().getResource("redisson-jcache.yaml").toURI();
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager(configUri, null).createCache("test", config);
    CountDownLatch latch = new CountDownLatch(1);
    String key = "123";
    UpdatedListener clientListener = new UpdatedListener(latch, key, "80", "90");
    MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(clientListener), null, true, true);
    cache.registerCacheEntryListener(listenerConfiguration);
    cache.put(key, "80");
    assertThat(cache.get(key)).isNotNull();
    cache.put(key, "90");
    latch.await();
    assertThat(cache.get(key)).isNotNull();
    cache.close();
    runner.stop();
}
Also used : CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) RedisProcess(org.redisson.RedisRunner.RedisProcess) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) RedisRunner(org.redisson.RedisRunner) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) MutableConfiguration(javax.cache.configuration.MutableConfiguration) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 27 with RedisProcess

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

the class JCacheTest method testRemoveAll.

@Test
public void testRemoveAll() throws Exception {
    RedisProcess runner = new RedisRunner().nosave().randomDir().port(6311).run();
    URL configUrl = getClass().getResource("redisson-jcache.yaml");
    Config cfg = Config.fromYAML(configUrl);
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager().createCache("test", config);
    cache.put("1", "2");
    cache.put("3", "4");
    cache.put("4", "4");
    cache.put("5", "5");
    Set<? extends String> keys = new HashSet<String>(Arrays.asList("1", "3", "4", "5"));
    cache.removeAll(keys);
    assertThat(cache.containsKey("1")).isFalse();
    assertThat(cache.containsKey("3")).isFalse();
    assertThat(cache.containsKey("4")).isFalse();
    assertThat(cache.containsKey("5")).isFalse();
    cache.close();
    runner.stop();
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) RedisRunner(org.redisson.RedisRunner) URL(java.net.URL) HashSet(java.util.HashSet) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 28 with RedisProcess

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

the class WeightedRoundRobinBalancerTest method testUseMasterForReadsIfNoConnectionsToSlaves.

@Test
public void testUseMasterForReadsIfNoConnectionsToSlaves() {
    Assertions.assertThrows(WriteRedisConnectionException.class, () -> {
        RedisProcess master = null;
        RedisProcess slave = null;
        RedissonClient client = null;
        try {
            master = redisTestInstance();
            slave = redisTestInstance();
            Map<String, Integer> weights = new HashMap<>();
            weights.put(master.getRedisServerAddressAndPort(), 1);
            weights.put(slave.getRedisServerAddressAndPort(), 2);
            Config config = new Config();
            config.useMasterSlaveServers().setReadMode(ReadMode.SLAVE).setMasterAddress(master.getRedisServerAddressAndPort()).addSlaveAddress(slave.getRedisServerAddressAndPort()).setLoadBalancer(new WeightedRoundRobinBalancer(weights, 1));
            client = Redisson.create(config);
            // To simulate network connection issues to slave, stop the slave
            // after creating the client. Cannot create the client without the
            // slave running. See https://github.com/mrniko/redisson/issues/580
            slave.stop();
            RedissonClient clientCopy = client;
            assertThat(clientCopy.getBucket("key").get()).isNull();
        } finally {
            if (master != null) {
                master.stop();
            }
            if (slave != null) {
                slave.stop();
            }
            if (client != null) {
                client.shutdown();
            }
        }
    });
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) HashMap(java.util.HashMap) Config(org.redisson.config.Config) Test(org.junit.jupiter.api.Test)

Example 29 with RedisProcess

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

the class RedissonRedLockTest method testLockLeasetime.

@Test
public void testLockLeasetime() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    RedissonClient client1 = createClient(redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(redis2.getRedisServerAddressAndPort());
    RLock lock1 = client1.getLock("lock1");
    RLock lock2 = client1.getLock("lock2");
    RLock lock3 = client2.getLock("lock3");
    RLock lock4 = client2.getLock("lock4");
    RLock lock5 = client2.getLock("lock5");
    RLock lock6 = client2.getLock("lock6");
    RLock lock7 = client2.getLock("lock7");
    RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3, lock4, lock5, lock6, lock7);
    ExecutorService executor = Executors.newFixedThreadPool(10);
    AtomicInteger counter = new AtomicInteger();
    for (int i = 0; i < 10; i++) {
        executor.submit(() -> {
            for (int j = 0; j < 5; j++) {
                try {
                    lock.lock(2, TimeUnit.SECONDS);
                    int nextValue = counter.get() + 1;
                    Thread.sleep(1000);
                    counter.set(nextValue);
                    lock.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    executor.shutdown();
    assertThat(executor.awaitTermination(2, TimeUnit.MINUTES)).isTrue();
    assertThat(counter.get()).isEqualTo(50);
    client1.shutdown();
    client2.shutdown();
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 30 with RedisProcess

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

the class RedissonTest method testConnectionListener.

@Test
public void testConnectionListener() throws IOException, InterruptedException, TimeoutException {
    final RedisProcess p = redisTestConnection();
    final AtomicInteger connectCounter = new AtomicInteger();
    final AtomicInteger disconnectCounter = new AtomicInteger();
    Config config = new Config();
    config.useSingleServer().setAddress(p.getRedisServerAddressAndPort());
    config.setConnectionListener(new ConnectionListener() {

        @Override
        public void onDisconnect(InetSocketAddress addr) {
            assertThat(addr).isEqualTo(new InetSocketAddress(p.getRedisServerBindAddress(), p.getRedisServerPort()));
            disconnectCounter.incrementAndGet();
        }

        @Override
        public void onConnect(InetSocketAddress addr) {
            assertThat(addr).isEqualTo(new InetSocketAddress(p.getRedisServerBindAddress(), p.getRedisServerPort()));
            connectCounter.incrementAndGet();
        }
    });
    RedissonClient r = Redisson.create(config);
    r.getBucket("1").get();
    Assertions.assertEquals(0, p.stop());
    await().atMost(2, TimeUnit.SECONDS).until(() -> disconnectCounter.get() == 1);
    try {
        r.getBucket("1").get();
    } catch (Exception e) {
    }
    assertThat(connectCounter.get()).isEqualTo(1);
    assertThat(disconnectCounter.get()).isEqualTo(1);
    RedisProcess pp = new RedisRunner().nosave().port(p.getRedisServerPort()).randomDir().run();
    r.getBucket("1").get();
    assertThat(connectCounter.get()).isEqualTo(2);
    assertThat(disconnectCounter.get()).isEqualTo(1);
    r.shutdown();
    Assertions.assertEquals(0, pp.stop());
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(org.redisson.config.Config) InetSocketAddress(java.net.InetSocketAddress) ConnectionListener(org.redisson.connection.ConnectionListener) IOException(java.io.IOException) 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