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