Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8262437

Instant.now() returns same instant after first call

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      ADDITIONAL SYSTEM INFORMATION :
      I am using Intellij IDEA 2020.1 and installed openjdk-15 via the Project Structure dialogue. The same error happens with a manually installed version (11.05) as well.

      A DESCRIPTION OF THE PROBLEM :
      If I use Instant.now() repeatedly, later calls return the same value even if time should have passed already.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Initialize two variables with Instant.now() and print one after each other.

      Instant now1 = Instant.now();
      System.out.println(now1);
      Instant now2 = Instant.now();
      System.out.println(now2);


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The variables should be different.
      ACTUAL -
      The variables are the same:
      2021-02-25T15:49:57.512378100Z
      2021-02-25T15:49:57.512378100Z

      ---------- BEGIN SOURCE ----------
      import org.junit.jupiter.api.RepeatedTest;
      import org.junit.jupiter.api.Test;

      import java.time.Instant;

      import static org.assertj.core.api.Assertions.assertThat;

      class InstantTest {

          @Test
          public void succeedsIfRunInIsolation() {
              assertThat(Instant.now()).isNotEqualTo(Instant.now());
          }

          @RepeatedTest(10)
          public void allButFirstFailIfRunInIsolation() {
              assertThat(Instant.now()).isNotEqualTo(Instant.now());
          }

          @Test
          void failsAlways() {
              Instant now1 = Instant.now();
              System.out.println(now1);
              Instant now2 = Instant.now();
              System.out.println(now2);

              assertThat(now1).isNotEqualTo(now2);
          }

      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              naoto Naoto Sato
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: