Skip to content

Commit

Permalink
Fix testTokenExpiry flaky test (elastic#42585)
Browse files Browse the repository at this point in the history
Test was using ClockMock#rewind passing the amount of nanoseconds
in order to "strip" nanos from the time value. This was intentional
as the expiration time of the UserToken doesn't have nanosecond
precision.
However, ClockMock#rewind doesn't support nanos either, so when it's
called with a TimeValue, it rewinds the clock by the TimeValue's
millis instead. This was causing the clock to go enough millis
before token expiration time and the test was passing. Once every
few hundred times though, the TimeValue by which we attempted to
rewind the clock only had nanos and no millis, so rewind moved the
clock back just a few millis, but still after expiration time.

This change moves the clock explicitly to the same instant as expiration,
using clock.setTime and disregarding nanos.
  • Loading branch information
jkakavas authored May 30, 2019
1 parent f32f7d8 commit f70cd1c
Showing 1 changed file with 2 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,8 @@ public void testTokenExpiry() throws Exception {
}

try (ThreadContext.StoredContext ignore = requestContext.newStoredContext(true)) {
// move to expiry
clock.fastForwardSeconds(Math.toIntExact(defaultExpiration.getSeconds()) - fastForwardAmount);
clock.rewind(TimeValue.timeValueNanos(clock.instant().getNano())); // trim off nanoseconds since don't store them in the index
// move to expiry, stripping nanoseconds, as we don't store them in the security-tokens index
clock.setTime(userToken.getExpirationTime().truncatedTo(ChronoUnit.MILLIS).atZone(clock.getZone()));
PlainActionFuture<UserToken> future = new PlainActionFuture<>();
tokenService.getAndValidateToken(requestContext, future);
assertAuthentication(authentication, future.get().getAuthentication());
Expand Down

0 comments on commit f70cd1c

Please sign in to comment.