Skip to content

Commit e94e3bb

Browse files
Ramkumar Sunderbabujaikiran
Ramkumar Sunderbabu
authored andcommitted
8324672: Update jdk/java/time/tck/java/time/TCKInstant.java now() to be more robust
Reviewed-by: rriggs, dfuchs
1 parent 6d7e679 commit e94e3bb

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

test/jdk/java/time/tck/java/time/TCKInstant.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -187,10 +187,21 @@ public void constant_MAX() {
187187
//-----------------------------------------------------------------------
188188
@Test
189189
public void now() {
190-
Instant expected = Instant.now(Clock.systemUTC());
191-
Instant test = Instant.now();
192-
long diff = Math.abs(test.toEpochMilli() - expected.toEpochMilli());
193-
assertTrue(diff < 100); // less than 0.1 secs
190+
long beforeMillis, instantMillis, afterMillis, diff;
191+
int retryRemaining = 5; // MAX_RETRY_COUNT
192+
do {
193+
beforeMillis = Instant.now(Clock.systemUTC()).toEpochMilli();
194+
instantMillis = Instant.now().toEpochMilli();
195+
afterMillis = Instant.now(Clock.systemUTC()).toEpochMilli();
196+
diff = instantMillis - beforeMillis;
197+
if (instantMillis < beforeMillis || instantMillis > afterMillis) {
198+
throw new RuntimeException(": Invalid instant: (~" + instantMillis + "ms)"
199+
+ " when systemUTC in millis is in ["
200+
+ beforeMillis + ", "
201+
+ afterMillis + "]");
202+
}
203+
} while (diff > 100 && --retryRemaining > 0); // retry if diff more than 0.1 sec
204+
assertTrue(retryRemaining > 0);
194205
}
195206

196207
//-----------------------------------------------------------------------

0 commit comments

Comments
 (0)