LockLeaseService { LockResponse lock(LockRequest request) { return lockService.lock(request); } @VisibleForTesting  LockLeaseService(
            NamespacedConjureTimelockService delegate,
            UUID clientId);   }
@Test public void lockResponseHasCorrectLeasedLock() { Lease lease = getLease(); when(timelock.lock(any())) .thenReturn(ConjureLockResponse.successful(SuccessfulLockResponse.of(LOCK_TOKEN, lease))); LockResponse clientResponse = lockLeaseService.lock(lockRequest); verify(timelock).lock(any()); LeasedLockToken leasedLockToken = (LeasedLockToken) clientResponse.getToken(); assertThat(leasedLockToken.serverToken()).isEqualTo(LOCK_TOKEN); assertThat(leasedLockToken.getLease()).isEqualTo(lease); }
@Test public void shouldHandleUnsuccessfulLockResponses() { when(timelock.lock(any())).thenReturn(ConjureLockResponse.unsuccessful(UnsuccessfulLockResponse.of())); LockResponse clientResponse = lockLeaseService.lock(lockRequest); assertThat(clientResponse.wasSuccessful()).isFalse(); }
@Test public void returnedTokenShouldHaveCorrectServerToken() { when(timelock.lock(any())).thenReturn( ConjureLockResponse.successful(SuccessfulLockResponse.of(LOCK_TOKEN, getLease()))); LockResponse lockResponse = lockLeaseService.lock(lockRequest); LeasedLockToken leasedToken = (LeasedLockToken) lockResponse.getToken(); assertThat(leasedToken.serverToken()).isEqualTo(LOCK_TOKEN); }
@Test public void leasedTokenShouldHaveValidLeaseForTheLeasePeriod() { when(timelock.lock(any())).thenReturn( ConjureLockResponse.successful(SuccessfulLockResponse.of(LOCK_TOKEN, getLease()))); LockResponse lockResponse = lockLeaseService.lock(lockRequest); assertValid(lockResponse.getToken()); }
@Test public void lockAcquireTimeoutIsBounded() { when(lockRequest.getAcquireTimeoutMs()).thenReturn(TIMEOUT_GREATER_THAN_MAX_PERMISSIBLE_TIMEOUT.toMillis()); when(timelock.lock(any())) .thenReturn(ConjureLockResponse.successful(SuccessfulLockResponse.of(LOCK_TOKEN, getLease()))); LockResponse lockResponse = lockLeaseService.lock(lockRequest); assertValid(lockResponse.getToken()); verify(timelock) .lock(argThat(req -> req.getAcquireTimeoutMs() == BlockEnforcingLockService.MAX_PERMISSIBLE_LOCK_ACQUIRE_TIMEOUT.toMillis())); }
@Test public void lockAcquireTimeoutIsBoundedAndRequestRetried() { when(lockRequest.getAcquireTimeoutMs()).thenReturn(TIMEOUT_GREATER_THAN_MAX_PERMISSIBLE_TIMEOUT.toMillis()); when(timelock.lock(any())) .thenThrow(TIMEOUT_EXCEPTION) .thenReturn(ConjureLockResponse.successful(SuccessfulLockResponse.of(LOCK_TOKEN, getLease()))); LockResponse lockResponse = lockLeaseService.lock(lockRequest); assertValid(lockResponse.getToken()); verify(timelock, times(2)).lock(any()); }