Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rsunderbabu
Copy link
Contributor

@rsunderbabu rsunderbabu commented Oct 8, 2024

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

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8324672: Update jdk/java/time/tck/java/time/TCKInstant.java now() to be more robust (Bug - P4)

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

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 8, 2024

👋 Welcome back rsunderbabu! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Oct 8, 2024

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 8, 2024
@openjdk
Copy link

openjdk bot commented Oct 8, 2024

@rsunderbabu The following label will be automatically applied to this pull request:

  • core-libs

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.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Oct 8, 2024
@mlbridge
Copy link

mlbridge bot commented Oct 8, 2024

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
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

@rsunderbabu rsunderbabu Oct 9, 2024

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

Copy link
Contributor

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.

Copy link
Contributor Author

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
    }

Copy link
Contributor

@RogerRiggs RogerRiggs Oct 10, 2024

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.

Copy link
Member

@dfuch dfuch Oct 11, 2024

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.

@rsunderbabu
Copy link
Contributor Author

Background of the issue:

        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

In normal cases, the difference between the test and expected stay within the threshold of 100ms.
Issue happens when the code is run with compilation options such as -Xcomp, -Xbatch etc. The aggressive JIT optimizations and other stop-the-world pauses introduce delay in execution and the difference ends up more than 100ms.
The difference is increased to 10s to account for such delays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants