Search in sources :

Example 81 with Config

use of org.redisson.config.Config in project redisson by redisson.

the class JCacheTest method testClear.

@Test
public void testClear() 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.clear();
    assertThat(cache.get("1")).isNull();
    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.jupiter.api.Test)

Example 82 with Config

use of org.redisson.config.Config in project redisson by redisson.

the class JCacheTest method testAsync.

@Test
public void testAsync() 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);
    CacheAsync<String, String> async = cache.unwrap(CacheAsync.class);
    async.putAsync("1", "2").get();
    assertThat(async.getAsync("1").get()).isEqualTo("2");
    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.jupiter.api.Test)

Example 83 with Config

use of org.redisson.config.Config 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 84 with Config

use of org.redisson.config.Config 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 85 with Config

use of org.redisson.config.Config in project redisson by redisson.

the class RedissonScheduledExecutorServiceTest method testTaskFailover.

@Test
public void testTaskFailover() throws Exception {
    AtomicInteger counter = new AtomicInteger();
    new MockUp<TasksRunnerService>() {

        @Mock
        void finish(Invocation invocation, String requestId, boolean removeTask) {
            if (counter.incrementAndGet() > 1) {
                invocation.proceed();
            }
        }
    };
    Config config = createConfig();
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("test2", 1));
    node.shutdown();
    node = RedissonNode.create(nodeConfig);
    node.start();
    RScheduledExecutorService executor = redisson.getExecutorService("test2", ExecutorOptions.defaults().taskRetryInterval(10, TimeUnit.SECONDS));
    long start = System.currentTimeMillis();
    RExecutorFuture<?> f = executor.schedule(new IncrementRunnableTask("counter"), 1, TimeUnit.SECONDS);
    f.toCompletableFuture().join();
    assertThat(System.currentTimeMillis() - start).isBetween(900L, 1300L);
    assertThat(redisson.getAtomicLong("counter").get()).isEqualTo(1);
    Thread.sleep(2000);
    node.shutdown();
    node = RedissonNode.create(nodeConfig);
    node.start();
    Thread.sleep(8500);
    assertThat(redisson.getAtomicLong("counter").get()).isEqualTo(2);
    Thread.sleep(16000);
    assertThat(redisson.getAtomicLong("counter").get()).isEqualTo(2);
    redisson.getKeys().delete("counter");
    assertThat(redisson.getKeys().count()).isEqualTo(1);
}
Also used : Invocation(mockit.Invocation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(org.redisson.config.Config) RedissonNodeConfig(org.redisson.config.RedissonNodeConfig) RedissonNodeConfig(org.redisson.config.RedissonNodeConfig) MockUp(mockit.MockUp) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

Config (org.redisson.config.Config)182 Test (org.junit.jupiter.api.Test)109 RedissonClient (org.redisson.api.RedissonClient)69 RedisProcess (org.redisson.RedisRunner.RedisProcess)52 RandomLoadBalancer (org.redisson.connection.balancer.RandomLoadBalancer)33 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)30 ClusterProcesses (org.redisson.ClusterRunner.ClusterProcesses)23 IOException (java.io.IOException)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 RedisRunner (org.redisson.RedisRunner)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)16 MethodSource (org.junit.jupiter.params.provider.MethodSource)15 BaseTest (org.redisson.BaseTest)15 RandomString (net.bytebuddy.utility.RandomString)14 URL (java.net.URL)12 RedisClientConfig (org.redisson.client.RedisClientConfig)12 Test (org.junit.Test)11 RLock (org.redisson.api.RLock)10 RedissonNodeConfig (org.redisson.config.RedissonNodeConfig)10 CountDownLatch (java.util.concurrent.CountDownLatch)9