Skip to content

Commit 1cd70f2

Browse files
tsegismontvietj
authored andcommitted
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 <[email protected]>
1 parent 7c28f4b commit 1cd70f2

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

vertx-mssql-client/src/test/java/io/vertx/mssqlclient/data/MSSQLPreparedQueryNotNullableDataTypeTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.math.BigDecimal;
2727
import java.time.*;
28+
import java.util.concurrent.ThreadLocalRandom;
2829
import java.util.function.Consumer;
2930

3031
@RunWith(VertxUnitRunner.class)
@@ -148,7 +149,17 @@ public void testEncodeDateTime(TestContext ctx) {
148149
@Test
149150
@Repeat(100)
150151
public void testEncodeOffsetDateTime(TestContext ctx) {
151-
OffsetDateTime now = LocalDateTime.now().atOffset(ZoneOffset.ofHoursMinutes(-3, -15));
152+
/*
153+
Starting from Java 11, LocalDateTime.now() can have microseconds precision.
154+
But the test database defines the test_datetimeoffset column with scale of 5.
155+
156+
Therefore, in order to get consistent behavior between Java 8 and Java 11,
157+
we erase the nanoOfSecond value obtained from the clock with a random number
158+
which has at most tens of microseconds precision.
159+
*/
160+
int nanoOfSecond = ThreadLocalRandom.current().nextInt(100_000) * 10_000;
161+
OffsetDateTime now = LocalDateTime.now().withNano(nanoOfSecond)
162+
.atOffset(ZoneOffset.ofHoursMinutes(-3, -15));
152163
testPreparedQueryEncodeGeneric(ctx, "not_nullable_datatype", "test_datetimeoffset", now, row -> {
153164
ColumnChecker.checkColumn(0, "test_datetimeoffset")
154165
.returns(Tuple::getValue, Row::getValue, now)

0 commit comments

Comments
 (0)