-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8324672: Update jdk/java/time/tck/java/time/TCKInstant.java now() to be more robust #21413
base: master
Are you sure you want to change the base?
Conversation
👋 Welcome back rsunderbabu! A progress list of the required criteria for merging this PR into |
❗ This change is not yet ready to be integrated. |
@rsunderbabu The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
@@ -190,7 +190,7 @@ public void now() { | |||
Instant expected = Instant.now(Clock.systemUTC()); | |||
Instant test = Instant.now(); | |||
long diff = Math.abs(test.toEpochMilli() - expected.toEpochMilli()); | |||
assertTrue(diff < 100); // less than 0.1 secs | |||
assertTrue(diff < 10_000); // less than 10 secs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given arbitrary delays between the two executions; the premise of the test itself is suspect; especially if the allowed time is increased. I think the test is supposed to be testing that the default clock for Instant.now() is the SystemUTC clock.
I'd expect expected to be less than or equal to test.
The use of math.abs allows the clock to go backwards; that might happen if the time was re-set manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @RogerRiggs for the comments. I increased the diff allowance to absorb any compilation related delays. What would you propose here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If your objection is primarily on Math.abs, is this ok?
long diff = test.toEpochMilli() - expected.toEpochMilli(); assertTrue(diff >= 0 && diff < 10_000); // less than 10 secs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there is any way to meaningfully and reliably test the assertion that Instant.now()
is the using the same clock as Instant.now(Clock.systemUTC())
given the potential delays in execution of the two statements.
It might be possible to ignore well known delays due to gc or compilation by making sure the code is warmed up by repeating the test until the delta meets the .1 sec limit. If it was really a bug, the test would timeout after a couple of minutes. Putting a while loop around the body of the test would cover that.
I'd leave the code using abs
alone to avoid exposing some other unanticipated change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am keeping the timeout as 60 seconds. is this ok?
@Test(timeOut=60000)
public void now() {
Instant expected, test;
long diff;
do {
expected = Instant.now(Clock.systemUTC());
test = Instant.now();
diff = Math.abs(test.toEpochMilli() - expected.toEpochMilli());
} while( diff > 100 ); // retry if more than 0.1 sec
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, looks fine; The normal JTREG timeout is 2 minutes. 60 seconds is fine for the testng timeout.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW - when I updated the System UTC clock to get sub-milliseconds resolution from the OS I added this test:
https://github.com/openjdk/jdk/blob/master/test/jdk/java/time/test/java/time/TestClock_System.java
Maybe some similar technique could be used here. That is - take System.currentTimeMillis(), Take Instant.now(), take System.currentTimeMillis() again, and then verify that the instant lies between the two snapshots: greater or equal to the first, less or equal to the second. That should always be true (unless the UTC clock is adjusted by NTP). But you could hopefully detect that and retry if you observe that the second call to System.currentTimeMillis() has returned a value which is before the first call.
If the difference between the two System::curentTimeMillis calls is too big, then if you wish you might want to try again too.
I believe this would provide a more robust test strategy.
Background of the issue:
In normal cases, the difference between the test and expected stay within the threshold of 100ms. |
The time difference check might fail for scenarios such as batch compilation. It is safer to give a bigger allowance of 10 seconds instead of 0.1 sec.
Testing: The test was run for 100 times with -Xcomp option.
Progress
Issue
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/21413/head:pull/21413
$ git checkout pull/21413
Update a local copy of the PR:
$ git checkout pull/21413
$ git pull https://git.openjdk.org/jdk.git pull/21413/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 21413
View PR using the GUI difftool:
$ git pr show -t 21413
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/21413.diff
Webrev
Link to Webrev Comment