use of org.redisson.api.RLock 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.RLock 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.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testWriteLock.
@Test
public void testWriteLock() throws InterruptedException {
final RReadWriteLock lock = redisson.getReadWriteLock("lock");
final RLock writeLock = lock.writeLock();
writeLock.lock();
Assertions.assertTrue(lock.writeLock().tryLock());
Thread t = new Thread() {
public void run() {
Assertions.assertFalse(writeLock.isHeldByCurrentThread());
Assertions.assertTrue(writeLock.isLocked());
Assertions.assertFalse(lock.readLock().tryLock());
Assertions.assertFalse(redisson.getReadWriteLock("lock").readLock().tryLock());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Assertions.assertTrue(lock.readLock().tryLock());
Assertions.assertTrue(redisson.getReadWriteLock("lock").readLock().tryLock());
}
};
t.start();
t.join(50);
writeLock.unlock();
Assertions.assertTrue(lock.readLock().tryLock());
Assertions.assertTrue(writeLock.isHeldByCurrentThread());
writeLock.unlock();
Thread.sleep(1000);
Assertions.assertFalse(lock.writeLock().tryLock());
Assertions.assertFalse(lock.writeLock().isLocked());
Assertions.assertFalse(lock.writeLock().isHeldByCurrentThread());
lock.writeLock().forceUnlock();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonRedLockTest method testLockSuccess2.
@Test
public void testLockSuccess2() 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");
Thread t1 = new Thread() {
public void run() {
lock2.lock();
}
};
t1.start();
t1.join();
RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
assertThat(lock.tryLock(500, 5000, TimeUnit.MILLISECONDS)).isTrue();
Thread.sleep(3000);
lock.unlock();
client1.shutdown();
client2.shutdown();
assertThat(redis1.stop()).isEqualTo(0);
assertThat(redis2.stop()).isEqualTo(0);
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonRedLockTest method testConnectionFailed.
@Test
public void testConnectionFailed() 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");
assertThat(redis2.stop()).isEqualTo(0);
RLock lock3 = client2.getLock("lock3");
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();
client1.shutdown();
client2.shutdown();
assertThat(redis1.stop()).isEqualTo(0);
}
Aggregations