use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testDelete.
@Test
public void testDelete() {
RReadWriteLock lock = redisson.getReadWriteLock("lock");
Assert.assertFalse(lock.delete());
lock.readLock().lock();
Assert.assertTrue(lock.delete());
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testIsLocked.
@Test
public void testIsLocked() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
Assertions.assertFalse(lock.isLocked());
lock.lock();
Assertions.assertTrue(lock.isLocked());
lock.unlock();
Assertions.assertFalse(lock.isLocked());
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testExpireRead.
@Test
public void testExpireRead() throws InterruptedException {
RReadWriteLock lock = redisson.getReadWriteLock("lock");
lock.readLock().lock(2, TimeUnit.SECONDS);
final long startTime = System.currentTimeMillis();
Thread t = new Thread() {
public void run() {
RReadWriteLock lock1 = redisson.getReadWriteLock("lock");
lock1.readLock().lock();
long spendTime = System.currentTimeMillis() - startTime;
Assertions.assertTrue(spendTime < 2050);
lock1.readLock().unlock();
}
};
t.start();
t.join();
lock.readLock().unlock();
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testWriteLockExpiration.
@Test
public void testWriteLockExpiration() throws InterruptedException {
RReadWriteLock rw1 = redisson.getReadWriteLock("test2s3");
RLock l1 = rw1.writeLock();
assertThat(l1.tryLock(10000, 10000, TimeUnit.MILLISECONDS)).isTrue();
RLock l2 = rw1.writeLock();
assertThat(l2.tryLock(1000, 1000, TimeUnit.MILLISECONDS)).isTrue();
await().atMost(Duration.ofSeconds(10)).until(() -> {
RReadWriteLock rw2 = redisson.getReadWriteLock("test2s3");
try {
return !rw2.writeLock().tryLock(3000, 1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
});
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testInCluster.
@Test
public void testInCluster() throws Exception {
RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave();
ClusterRunner clusterRunner = new ClusterRunner().addNode(master1).addNode(master2).addNode(master3);
ClusterProcesses process = clusterRunner.run();
Config config = new Config();
config.useClusterServers().addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
RedissonClient redisson = Redisson.create(config);
RReadWriteLock s = redisson.getReadWriteLock("1234");
s.writeLock().lock();
s.readLock().lock();
s.readLock().unlock();
s.writeLock().unlock();
redisson.shutdown();
process.shutdown();
}
Aggregations