LockRefresher implements AutoCloseable { public void registerLocks(Collection<LockToken> tokens) { tokensToRefresh.addAll(tokens); } LockRefresher(
            ScheduledExecutorService executor,
            TimelockService timelockService,
            long refreshIntervalMillis); void registerLocks(Collection<LockToken> tokens); void unregisterLocks(Collection<LockToken> tokens); @Override void close();  }
@Test public void continuesRefreshingLocksThatAreReturned() { when(timelock.refreshLockLeases(TOKENS)).thenReturn(TOKENS); registerLocks(); tick(); tick(); verify(timelock, times(2)).refreshLockLeases(TOKENS); }
@Test public void stopsRefreshingLockIfItIsNotRefreshed() { ImmutableSet<LockToken> lockTokensToRefresh = ImmutableSet.of(TOKEN_1); when(timelock.refreshLockLeases(TOKENS)).thenReturn(lockTokensToRefresh); registerLocks(); tick(); verify(timelock).refreshLockLeases(TOKENS); tick(); verify(timelock).refreshLockLeases(lockTokensToRefresh); }
@Test public void doesNotFailIfDelegateThrows() { ImmutableSet<LockToken> lockTokensToRefresh = ImmutableSet.of(TOKEN_1); when(timelock.refreshLockLeases(any())) .thenThrow(new RuntimeException("test")) .thenReturn(lockTokensToRefresh); refresher.registerLocks(lockTokensToRefresh); tick(); tick(); verify(timelock, times(2)).refreshLockLeases(lockTokensToRefresh); }