RedisBasedLock implements Lock { @Override public Future<Boolean> acquireLock(String lock, String token, long lockExpiryMs) { Future<Boolean> future = Future.future(); redisClient.setWithOptions(buildLockKey(lock), token, new SetOptions().setNX(true).setPX(lockExpiryMs), event -> { if(event.succeeded()){ future.complete("OK".equalsIgnoreCase(event.result())); } else { future.fail(event.cause().getMessage()); } }); return future; } RedisBasedLock(RedisClient redisClient); @Override Future<Boolean> acquireLock(String lock, String token, long lockExpiryMs); @Override Future<Boolean> releaseLock(String lock, String token); static final String STORAGE_PREFIX; }
@Test public void testAcquireLock(TestContext context){ Async async = context.async(); context.assertFalse(jedis.exists(lockKey(lock_1))); redisBasedLock.acquireLock(lock_1, token_1, 500).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertTrue(event.result()); context.assertTrue(jedis.exists(lockKey(lock_1))); context.assertEquals(token_1, jedis.get(lockKey(lock_1))); async.complete(); }); }
@Test public void testAcquireMultipleLocks(TestContext context){ Async async = context.async(); context.assertFalse(jedis.exists(lockKey(lock_1))); redisBasedLock.acquireLock(lock_1, token_1, 500).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertTrue(event.result()); context.assertTrue(jedis.exists(lockKey(lock_1))); context.assertEquals(token_1, jedis.get(lockKey(lock_1))); redisBasedLock.acquireLock("lock_2", "token_X", 300).setHandler(event1 -> { context.assertTrue(event1.succeeded()); context.assertTrue(event1.result()); context.assertTrue(jedis.exists(lockKey("lock_2"))); context.assertEquals("token_X", jedis.get(lockKey("lock_2"))); redisBasedLock.acquireLock("lock_3", "token_Z", 300).setHandler(event2 -> { context.assertTrue(event2.succeeded()); context.assertTrue(event2.result()); context.assertTrue(jedis.exists(lockKey("lock_3"))); context.assertEquals("token_Z", jedis.get(lockKey("lock_3"))); async.complete(); }); }); }); }
@Test public void testAcquireLockAgain(TestContext context){ Async async = context.async(); context.assertFalse(jedis.exists(lockKey(lock_1))); redisBasedLock.acquireLock(lock_1, token_1, 300).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertTrue(event.result()); context.assertTrue(jedis.exists(lockKey(lock_1))); context.assertEquals(token_1, jedis.get(lockKey(lock_1))); redisBasedLock.acquireLock(lock_1, "token_X", 350).setHandler(event1 -> { context.assertTrue(event1.succeeded()); context.assertFalse(event1.result()); context.assertTrue(jedis.exists(lockKey(lock_1))); context.assertEquals(token_1, jedis.get(lockKey(lock_1))); async.complete(); }); }); }
@Test public void testAcquireLockAfterExpired(TestContext context){ Async async = context.async(); context.assertFalse(jedis.exists(lockKey(lock_1))); redisBasedLock.acquireLock(lock_1, token_1, 300).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertTrue(event.result()); context.assertTrue(jedis.exists(lockKey(lock_1))); context.assertEquals(token_1, jedis.get(lockKey(lock_1))); waitMaxUntilExpired(lockKey(lock_1), 350); redisBasedLock.acquireLock(lock_1, "token_X", 500).setHandler(event1 -> { context.assertTrue(event1.succeeded()); context.assertTrue(event1.result()); context.assertTrue(jedis.exists(lockKey(lock_1))); context.assertEquals("token_X", jedis.get(lockKey(lock_1))); async.complete(); }); }); }