Skip to content

Commit 75b3433

Browse files
committed
Fix testTokenExpiry flaky test (#42585)
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.
1 parent 7f86048 commit 75b3433

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,8 @@ public void testTokenExpiry() throws Exception {
531531
}
532532

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

0 commit comments

Comments
 (0)