LockUtil { public static Future<Boolean> acquireLock(Lock lockImpl, String lock, String token, long lockExpiryMs, Logger log){ Future<Boolean> future = Future.future(); if(lockImpl == null){ log.info("No lock implementation defined, going to pretend like we got the lock"); future.complete(Boolean.TRUE); return future; } log.debug("Trying to acquire lock '"+lock+"' with token '"+token+"' and expiry " + lockExpiryMs + "ms"); lockImpl.acquireLock(lock, token, lockExpiryMs).setHandler(lockEvent -> { if(lockEvent.succeeded()){ if(lockEvent.result()){ log.debug("Acquired lock '"+lock+"' with token '"+token+"'"); future.complete(Boolean.TRUE); } else { future.complete(Boolean.FALSE); } } else { future.fail(lockEvent.cause()); } }); return future; } private  LockUtil(); static Future<Boolean> acquireLock(Lock lockImpl, String lock, String token, long lockExpiryMs, Logger log); static void releaseLock(Lock lockImpl, String lock, String token, Logger log);  }
@Test public void testAcquireLockWithoutLockImplementationDefined(TestContext context) { Async async = context.async(); LockUtil.acquireLock(null, "someLock", "someToken", 100, log).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertTrue(event.result()); Mockito.verify(log, Mockito.times(1)).info(Matchers.eq("No lock implementation defined, going to pretend like we got the lock")); async.complete(); }); }
@Test public void testAcquireLockSuccess(TestContext context) { Mockito.when(lock.acquireLock(anyString(), anyString(), anyLong())).thenReturn(Future.succeededFuture(Boolean.TRUE)); Async async = context.async(); LockUtil.acquireLock(lock, "someLock", "someToken", 100, log).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertTrue(event.result()); Mockito.verify(log, Mockito.times(1)).debug(Matchers.eq("Trying to acquire lock 'someLock' with token 'someToken' and expiry 100ms")); Mockito.verify(log, Mockito.times(1)).debug(Matchers.eq("Acquired lock 'someLock' with token 'someToken'")); async.complete(); }); }
@Test public void testAcquireLockFail(TestContext context) { Mockito.when(lock.acquireLock(anyString(), anyString(), anyLong())).thenReturn(Future.succeededFuture(Boolean.FALSE)); Async async = context.async(); LockUtil.acquireLock(lock, "someLock", "someToken", 100, log).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertFalse(event.result()); Mockito.verify(log, Mockito.times(1)).debug(Matchers.eq("Trying to acquire lock 'someLock' with token 'someToken' and expiry 100ms")); async.complete(); }); }
@Test public void testAcquireLockError(TestContext context) { Mockito.when(lock.acquireLock(anyString(), anyString(), anyLong())).thenReturn(Future.failedFuture("Booom")); Async async = context.async(); LockUtil.acquireLock(lock, "someLock", "someToken", 100, log).setHandler(event -> { context.assertFalse(event.succeeded()); context.assertEquals("Booom", event.cause().getMessage()); async.complete(); }); }