From fdbd031deaf4e3923c6a75b9935ee4fa090b3415 Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Wed, 28 Jul 2021 14:27:24 +0200 Subject: [PATCH] Fix testEncodeOffsetDateTime on JDK11 Fixes #1014 Starting from Java 11, LocalDateTime.now() can have microseconds precision. But the test database defines the test_datetimeoffset column with scale of 5. Therefore, in order to get consistent behavior between Java 8 and Java 11, we erase the nanoOfSecond value obtained from the clock with a random number which has at most tens of microseconds precision. Signed-off-by: Thomas Segismont --- .../MSSQLPreparedQueryNotNullableDataTypeTest.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/data/MSSQLPreparedQueryNotNullableDataTypeTest.java b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/data/MSSQLPreparedQueryNotNullableDataTypeTest.java index d053ddc29..1049bd365 100644 --- a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/data/MSSQLPreparedQueryNotNullableDataTypeTest.java +++ b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/data/MSSQLPreparedQueryNotNullableDataTypeTest.java @@ -25,6 +25,7 @@ import java.math.BigDecimal; import java.time.*; +import java.util.concurrent.ThreadLocalRandom; import java.util.function.Consumer; @RunWith(VertxUnitRunner.class) @@ -148,7 +149,17 @@ public void testEncodeDateTime(TestContext ctx) { @Test @Repeat(100) public void testEncodeOffsetDateTime(TestContext ctx) { - OffsetDateTime now = LocalDateTime.now().atOffset(ZoneOffset.ofHoursMinutes(-3, -15)); + /* + Starting from Java 11, LocalDateTime.now() can have microseconds precision. + But the test database defines the test_datetimeoffset column with scale of 5. + + Therefore, in order to get consistent behavior between Java 8 and Java 11, + we erase the nanoOfSecond value obtained from the clock with a random number + which has at most tens of microseconds precision. + */ + int nanoOfSecond = ThreadLocalRandom.current().nextInt(100_000) * 10_000; + OffsetDateTime now = LocalDateTime.now().withNano(nanoOfSecond) + .atOffset(ZoneOffset.ofHoursMinutes(-3, -15)); testPreparedQueryEncodeGeneric(ctx, "not_nullable_datatype", "test_datetimeoffset", now, row -> { ColumnChecker.checkColumn(0, "test_datetimeoffset") .returns(Tuple::getValue, Row::getValue, now)