[Bug]: select now() return the same outdated result no matter when called inside the test
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
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.
Thanks for the investigating. Do you have a clue, where can I address this issue to?
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.
Thanks for sharing! I was not aware of that