LockUtil { public static void releaseLock(Lock lockImpl, String lock, String token, Logger log){ if(lockImpl == null){ log.info("No lock implementation defined, going to pretend like we released the lock"); return; } log.debug("Trying to release lock '"+lock+"' with token '"+token+"'"); lockImpl.releaseLock(lock, token).setHandler(releaseEvent -> { if(releaseEvent.succeeded()){ if(releaseEvent.result()){ log.debug("Released lock '"+lock+"' with token '"+token+"'"); } } else { log.error("Could not release lock '"+lock+"'. Message: " + releaseEvent.cause().getMessage()); } }); } 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 testReleaseLockWithoutLockImplementationDefined(TestContext context) { LockUtil.releaseLock(null, "someLock", "someToken", log); Mockito.verify(log, Mockito.timeout(100).times(1)).info(Matchers.eq("No lock implementation defined, going to pretend like we released the lock")); }
@Test public void testReleaseLockSuccess(TestContext context) { Mockito.when(lock.releaseLock(anyString(), anyString())).thenReturn(Future.succeededFuture(Boolean.TRUE)); LockUtil.releaseLock(lock, "someLock", "someToken", log); Mockito.verify(log, Mockito.times(1)).debug(Matchers.eq("Trying to release lock 'someLock' with token 'someToken'")); Mockito.verify(log, Mockito.times(1)).debug(Matchers.eq("Released lock 'someLock' with token 'someToken'")); }
@Test public void testReleaseLockFail(TestContext context) { Mockito.when(lock.releaseLock(anyString(), anyString())).thenReturn(Future.succeededFuture(Boolean.FALSE)); LockUtil.releaseLock(lock, "someLock", "someToken", log); Mockito.verify(log, Mockito.times(1)).debug(Matchers.eq("Trying to release lock 'someLock' with token 'someToken'")); }
@Test public void testReleaseLockError(TestContext context) { Mockito.when(lock.releaseLock(anyString(), anyString())).thenReturn(Future.failedFuture("Booom")); LockUtil.releaseLock(lock, "someLock", "someToken", log); Mockito.verify(log, Mockito.times(1)).debug(Matchers.eq("Trying to release lock 'someLock' with token 'someToken'")); Mockito.verify(log, Mockito.times(1)).error(Matchers.eq("Could not release lock 'someLock'. Message: Booom")); }