@Test
public void testPut_WithActivityPreventingExpiry() throws Exception {
int duration = 200;
UserDto expectedClientUser = createClientUser();
InMemorySessionTokenCache
.INSTANCE
.reset(duration, TimeUnit.MILLISECONDS)
.put(
expectedClientUser.getSessionToken(),
expectedClientUser);
// Wait for a duration within the expiry time
Thread.sleep(duration / 2);
// This should reset the cache expiry giving the entry longer life
Optional<UserDto> clientUserOptional = InMemorySessionTokenCache
.INSTANCE
.getBySessionToken(Optional.of(expectedClientUser.getSessionToken()));
assertTrue("Unexpected expiry of client user (early expiration)", clientUserOptional.isPresent());
// Wait for a duration that now extends beyond the original expiry time
Thread.sleep(duration * 3 / 4);
// This should again reset the cache expiry giving the entry longer life
clientUserOptional = InMemorySessionTokenCache
.INSTANCE
.getBySessionToken(Optional.of(expectedClientUser.getSessionToken()));
assertTrue("Unexpected expiry of client user (refresh failed)", clientUserOptional.isPresent());
// Wait for a duration that now extends beyond the refreshed expiry time
Thread.sleep(duration * 2);
// This should again reset the cache expiry giving the entry longer life
clientUserOptional = InMemorySessionTokenCache
.INSTANCE
.getBySessionToken(Optional.of(expectedClientUser.getSessionToken()));
assertFalse("Unexpected presence of client user (late expiration)",clientUserOptional.isPresent());
}