use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonRedLockTest method testLockLeasetime.
private void testLockLeasetime(final long leaseTime, final TimeUnit unit) 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(leaseTime, unit);
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.api.RLock in project redisson by redisson.
the class RedissonSpinLockTest method testTryLockAsyncSucceed.
@Test
public void testTryLockAsyncSucceed() throws InterruptedException, ExecutionException {
RLock lock = redisson.getSpinLock("lock");
Boolean result = lock.tryLockAsync().get();
assertThat(result).isTrue();
lock.unlock();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonSpinLockTest method testReentrancy.
@Test
public void testReentrancy() throws InterruptedException {
Lock lock = redisson.getSpinLock("lock1");
Assertions.assertTrue(lock.tryLock());
Assertions.assertTrue(lock.tryLock());
lock.unlock();
// next row for test renew expiration tisk.
// Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(RedissonLock.LOCK_EXPIRATION_INTERVAL_SECONDS*2));
Thread thread1 = new Thread() {
@Override
public void run() {
RLock lock1 = redisson.getSpinLock("lock1");
Assertions.assertFalse(lock1.tryLock());
}
};
thread1.start();
thread1.join();
lock.unlock();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonSpinLockTest method testForceUnlock.
@Test
public void testForceUnlock() {
RLock lock = redisson.getSpinLock("lock");
lock.lock();
lock.forceUnlock();
Assertions.assertFalse(lock.isLocked());
lock = redisson.getSpinLock("lock");
Assertions.assertFalse(lock.isLocked());
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonSpinLockTest method testGetHoldCount.
@Test
public void testGetHoldCount() {
RLock lock = redisson.getSpinLock("lock");
Assertions.assertEquals(0, lock.getHoldCount());
lock.lock();
Assertions.assertEquals(1, lock.getHoldCount());
lock.unlock();
Assertions.assertEquals(0, lock.getHoldCount());
lock.lock();
lock.lock();
Assertions.assertEquals(2, lock.getHoldCount());
lock.unlock();
Assertions.assertEquals(1, lock.getHoldCount());
lock.unlock();
Assertions.assertEquals(0, lock.getHoldCount());
}
Aggregations