|
1 | 1 | /* |
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
@@ -187,10 +187,21 @@ public void constant_MAX() { |
187 | 187 | //----------------------------------------------------------------------- |
188 | 188 | @Test |
189 | 189 | 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); |
194 | 205 | } |
195 | 206 |
|
196 | 207 | //----------------------------------------------------------------------- |
|
0 commit comments