testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

[Bug]: select now() return the same outdated result no matter when called inside the test

Open ogyct opened this issue 3 years ago • 4 comments

Module

PostgreSQL

Testcontainers version

1.17.6

Using the latest Testcontainers version?

Yes

Host OS

macos

Host Arch

arm

Docker version

20.10.21

What happened?

Running tests in micronaut and executing select now() query returns the same outdated result for each call. Example here: https://github.com/ogyct/testcontainers-bug

Query looks like this:

@Repository
public interface TestRepository extends CrudRepository<TestEntity, Long> {

    @Query(value = "select now()" ,nativeQuery = true)
    Instant getTime();
}

Test scenario which fails:

@MicronautTest
class TestcontainersBugTest {
    @Inject
    TestRepository testRepository;
    @Inject
    TestJDBCRepository testJDBCRepository;
    @Test
    void testTime() throws InterruptedException {
        var time = testRepository.getTime();
        Thread.sleep(100);
        var time2 = testRepository.getTime();
        Thread.sleep(100);
        var time3 = testJDBCRepository.getTime();
        assertAll(
                () -> Assertions.assertNotEquals(time, time2),
                () -> Assertions.assertNotEquals(time2, time3)
        );

    }
}

Tested on both jpa and jdbc. Springboot and micronaut. Apparently there is some issue with time inside container which was run by testcontainers for jupiter5. Unfortunately I lack knowledge to provide further info.

Relevant log output

No response

Additional Information

No response

ogyct avatar Mar 24 '23 19:03 ogyct

I just used plain JDBC (no frameworks) and I was not able to reproduce it. This test was modified with the content below for test purposes but no success.

@Test
public void testSimple() throws Exception {
    try (PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:12")) {
        postgres.start();

        ResultSet resultSet = performQuery(postgres, "SELECT now()");
        Timestamp resultSetInt = resultSet.getTimestamp(1);
        Thread.sleep(1000);
        ResultSet resultSet2 = performQuery(postgres, "SELECT now()");
        Timestamp resultSetInt2 = resultSet2.getTimestamp(1);

        assertThat(resultSetInt).as("A basic SELECT query succeeds").isNotEqualTo(resultSetInt2);
        assertHasCorrectExposedAndLivenessCheckPorts(postgres);
    }
}

Also, take into account that Testcontainers allows to start the container, nothing else.

eddumelendez avatar Mar 26 '23 21:03 eddumelendez

Thanks for the investigating. Do you have a clue, where can I address this issue to?

ogyct avatar Mar 27 '23 08:03 ogyct

Wow, I found an issue... It is postgres feature, inside transaction postgres 'freezes' the time. So My tests are wrapped inside transaction, that's why timestamp is always the same.

ogyct avatar Mar 28 '23 12:03 ogyct

Thanks for sharing! I was not aware of that

eddumelendez avatar Mar 28 '23 13:03 eddumelendez