-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8324672: Update jdk/java/time/tck/java/time/TCKInstant.java now() to be more robust #21413
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
Conversation
|
👋 Welcome back rsunderbabu! A progress list of the required criteria for merging this PR into |
|
@rsunderbabu This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 101 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@RogerRiggs, @dfuch) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
|
@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
|
| 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. |
| beforeMillis = Instant.now(Clock.systemUTC()).toEpochMilli(); | ||
| instantMillis = Instant.now().toEpochMilli(); | ||
| afterMillis = Instant.now(Clock.systemUTC()).toEpochMilli(); | ||
| diff = instantMillis - beforeMillis; |
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.
Alternatively, you could set:
diff = afterMillis - beforeMillis;(range should be < 100ms, instant is anyhwere inside) or,diff = Math.abs(afterMillis / 2 - instantMillis + beforeMillis / 2);(distance from instant to midpoint should be < 100ms)
Not that it matters much I guess - but it would give more symmetry... Feel free to ignore :-)
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.
AFAIU, the original intention of the test is to check if Instant.now and Instant.now(Clock.systemUTC()) returns almost same time since the underlying clock is same.
Given that premise,
suggestion 1: it does the job but doesn't show the intention in a better way.
suggestion 2: it is not same as what the test wanted to check.
If you are fine, I would like to retain the logic I have in my latest commit.
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 fine with it.
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 @dfuch
RogerRiggs
left a comment
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.
LGTM
|
/integrate |
|
@rsunderbabu |
|
Roger and Daniel have reviewed this PR, I'll go ahead and sponsor this now. |
|
/sponsor |
|
Going to push as commit e94e3bb.
Your commit was automatically rebased without conflicts. |
|
@jaikiran @rsunderbabu Pushed as commit e94e3bb. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
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
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/21413/head:pull/21413$ git checkout pull/21413Update a local copy of the PR:
$ git checkout pull/21413$ git pull https://git.openjdk.org/jdk.git pull/21413/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 21413View PR using the GUI difftool:
$ git pr show -t 21413Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/21413.diff
Webrev
Link to Webrev Comment