Skip to content

Commit d2fff38

Browse files
committed
[hibernate#1724] Attempt to fix JDBCTimeZoneZonedTest sporadic failures probably due lack of database Timestamp precision
1 parent ce59900 commit d2fff38

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/timezones/JDBCTimeZoneZonedTest.java

+28-7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
import java.time.ZoneId;
1111
import java.time.ZoneOffset;
1212
import java.time.ZonedDateTime;
13+
import java.time.temporal.ChronoField;
14+
import java.time.temporal.ChronoUnit;
1315
import java.util.Collection;
1416
import java.util.List;
1517

1618
import org.hibernate.cfg.Configuration;
19+
import org.hibernate.dialect.Dialect;
20+
import org.hibernate.dialect.MySQLDialect;
21+
import org.hibernate.dialect.SybaseDialect;
1722
import org.hibernate.reactive.BaseReactiveTest;
1823
import org.hibernate.reactive.annotations.DisabledFor;
1924

@@ -31,7 +36,7 @@
3136
import static org.hibernate.cfg.AvailableSettings.TIMEZONE_DEFAULT_STORAGE;
3237
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
3338
import static org.hibernate.reactive.testing.ReactiveAssertions.assertWithTruncationThat;
34-
import static org.hibernate.type.descriptor.DateTimeUtils.roundToDefaultPrecision;
39+
import static org.hibernate.type.descriptor.DateTimeUtils.adjustToDefaultPrecision;
3540

3641
@Timeout(value = 10, timeUnit = MINUTES)
3742
@DisabledFor(value = DB2, reason = "Exception: IllegalStateException: Needed to have 6 in buffer but only had 0")
@@ -51,8 +56,24 @@ protected void setProperties(Configuration configuration) {
5156

5257
@Test
5358
public void test(VertxTestContext context) {
54-
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of( "CET" ) );
55-
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3 ) );
59+
final ZonedDateTime nowZoned;
60+
final OffsetDateTime nowOffset;
61+
final Dialect dialect = getDialect();
62+
if ( dialect instanceof SybaseDialect || dialect instanceof MySQLDialect ) {
63+
// Sybase has 1/300th sec precision
64+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") )
65+
.with( ChronoField.NANO_OF_SECOND, 0L );
66+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) )
67+
.with( ChronoField.NANO_OF_SECOND, 0L );
68+
}
69+
else if ( dialect.getDefaultTimestampPrecision() == 6 ) {
70+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
71+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
72+
}
73+
else {
74+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
75+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) );
76+
}
5677
test( context, getSessionFactory()
5778
.withTransaction( s -> {
5879
Zoned z = new Zoned();
@@ -63,11 +84,11 @@ public void test(VertxTestContext context) {
6384
.thenCompose( zid -> openSession()
6485
.thenCompose( s -> s.find( Zoned.class, zid )
6586
.thenAccept( z -> {
66-
assertWithTruncationThat( roundToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
67-
.isEqualTo( roundToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
87+
assertWithTruncationThat( adjustToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
88+
.isEqualTo( adjustToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
6889

69-
assertWithTruncationThat( roundToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
70-
.isEqualTo( roundToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
90+
assertWithTruncationThat( adjustToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
91+
.isEqualTo( adjustToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
7192

7293
ZoneId systemZone = ZoneId.systemDefault();
7394
ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() );

0 commit comments

Comments
 (0)