RedisLock implements Lock { @Override public void acquireLock(String lockId) { RLock lock = redisson.getLock(parseLockId(lockId)); lock.lock(); } @Inject  RedisLock(Redisson redisson, RedisLockConfiguration configuration); @Override void acquireLock(String lockId); @Override boolean acquireLock(String lockId, long timeToTry, TimeUnit unit); @Override boolean acquireLock(String lockId, long timeToTry, long leaseTime, TimeUnit unit); @Override void releaseLock(String lockId); @Override void deleteLock(String lockId);  }
@Test public void testLocking() { redisson.getKeys().flushall(); String lockId = "abcd-1234"; assertTrue(redisLock.acquireLock(lockId, 1000, 1000, TimeUnit.MILLISECONDS)); }
@Test public void testLockExpiration() throws InterruptedException { redisson.getKeys().flushall(); String lockId = "abcd-1234"; boolean isLocked = redisLock.acquireLock(lockId, 1000, 1000, TimeUnit.MILLISECONDS); assertTrue(isLocked); Thread.sleep(2000); RLock lock = redisson.getLock(lockId); assertFalse(lock.isLocked()); }
@Test public void testLockReentry() throws InterruptedException { redisson.getKeys().flushall(); String lockId = "abcd-1234"; boolean isLocked = redisLock.acquireLock(lockId, 1000, 60000, TimeUnit.MILLISECONDS); assertTrue(isLocked); Thread.sleep(1000); isLocked = redisLock.acquireLock(lockId, 1000, 1000, TimeUnit.MILLISECONDS); assertTrue(isLocked); RLock lock = redisson.getLock(lockId); assertTrue(isLocked); }
@Test public void testDuplicateLockAcquireFailure() throws InterruptedException { redisson.getKeys().flushall(); String lockId = "abcd-1234"; Worker worker1 = new Worker(redisLock, lockId, 100L, 60000L); worker1.start(); worker1.join(); boolean isLocked = redisLock.acquireLock(lockId, 500L, 1000L, TimeUnit.MILLISECONDS); assertFalse(isLocked); assertTrue(worker1.isLocked); }
@Test public void testReacquireLostKey() throws InterruptedException { redisson.getKeys().flushall(); String lockId = "abcd-1234"; boolean isLocked = redisLock.acquireLock(lockId, 1000, 10000, TimeUnit.MILLISECONDS); assertTrue(isLocked); redisson.getKeys().flushall(); isLocked = redisLock.acquireLock(lockId, 100, 10000, TimeUnit.MILLISECONDS); assertTrue(isLocked); }