Fix TenantIdTest when VM has nanosecond precision

This commit is contained in:
Christian Beikov 2023-02-13 10:06:50 +01:00
parent b3b293578e
commit 02a4f91d46
2 changed files with 27 additions and 20 deletions

View File

@ -16,15 +16,16 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
*/ */
public class ClockHelper { public class ClockHelper {
private static final Duration TICK_8 = Duration.ofNanos( 10L ); private static final Clock TICK_9 = Clock.systemDefaultZone();
private static final Duration TICK_7 = Duration.ofNanos( 100L ); private static final Clock TICK_8 = Clock.tick( TICK_9, Duration.ofNanos( 10L ) );
private static final Duration TICK_6 = Duration.ofNanos( 1000L ); private static final Clock TICK_7 = Clock.tick( TICK_9, Duration.ofNanos( 100L ) );
private static final Duration TICK_5 = Duration.ofNanos( 10000L ); private static final Clock TICK_6 = Clock.tick( TICK_9, Duration.ofNanos( 1000L ) );
private static final Duration TICK_4 = Duration.ofNanos( 100000L ); private static final Clock TICK_5 = Clock.tick( TICK_9, Duration.ofNanos( 10000L ) );
private static final Duration TICK_3 = Duration.ofNanos( 1000000L ); private static final Clock TICK_4 = Clock.tick( TICK_9, Duration.ofNanos( 100000L ) );
private static final Duration TICK_2 = Duration.ofNanos( 10000000L ); private static final Clock TICK_3 = Clock.tick( TICK_9, Duration.ofNanos( 1000000L ) );
private static final Duration TICK_1 = Duration.ofNanos( 100000000L ); private static final Clock TICK_2 = Clock.tick( TICK_9, Duration.ofNanos( 10000000L ) );
private static final Duration TICK_0 = Duration.ofNanos( 1000000000L ); private static final Clock TICK_1 = Clock.tick( TICK_9, Duration.ofNanos( 100000000L ) );
private static final Clock TICK_0 = Clock.tick( TICK_9, Duration.ofNanos( 1000000000L ) );
public static Clock forPrecision(Integer precision, SharedSessionContractImplementor session) { public static Clock forPrecision(Integer precision, SharedSessionContractImplementor session) {
final int resolvedPrecision; final int resolvedPrecision;
@ -34,28 +35,27 @@ public class ClockHelper {
else { else {
resolvedPrecision = precision; resolvedPrecision = precision;
} }
final Clock clock = Clock.systemDefaultZone();
switch ( resolvedPrecision ) { switch ( resolvedPrecision ) {
case 0: case 0:
return Clock.tick( clock, TICK_0 ); return TICK_0;
case 1: case 1:
return Clock.tick( clock, TICK_1 ); return TICK_1;
case 2: case 2:
return Clock.tick( clock, TICK_2 ); return TICK_2;
case 3: case 3:
return Clock.tick( clock, TICK_3 ); return TICK_3;
case 4: case 4:
return Clock.tick( clock, TICK_4 ); return TICK_4;
case 5: case 5:
return Clock.tick( clock, TICK_5 ); return TICK_5;
case 6: case 6:
return Clock.tick( clock, TICK_6 ); return TICK_6;
case 7: case 7:
return Clock.tick( clock, TICK_7 ); return TICK_7;
case 8: case 8:
return Clock.tick( clock, TICK_8 ); return TICK_8;
case 9: case 9:
return clock; return TICK_9;
} }
throw new IllegalArgumentException( "Illegal precision: " + resolvedPrecision ); throw new IllegalArgumentException( "Illegal precision: " + resolvedPrecision );
} }

View File

@ -19,6 +19,8 @@ import org.hibernate.testing.orm.junit.SessionFactoryProducer;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting; import org.hibernate.testing.orm.junit.Setting;
import org.hibernate.binder.internal.TenantIdBinder; import org.hibernate.binder.internal.TenantIdBinder;
import org.hibernate.type.descriptor.DateTimeUtils;
import org.hibernate.testing.orm.junit.SkipForDialect; import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -136,6 +138,11 @@ public class TenantIdTest implements SessionFactoryProducer {
scope.inTransaction( s -> s.persist( record ) ); scope.inTransaction( s -> s.persist( record ) );
assertEquals( "mine", record.state.tenantId ); assertEquals( "mine", record.state.tenantId );
assertNotNull( record.state.updated ); assertNotNull( record.state.updated );
// Round the temporal to avoid issues when the VM produces nanosecond precision timestamps
record.state.updated = DateTimeUtils.roundToDefaultPrecision(
record.state.updated,
scope.getSessionFactory().getJdbcServices().getDialect()
);
scope.inTransaction( s -> { scope.inTransaction( s -> {
Record r = s.find( Record.class, record.id ); Record r = s.find( Record.class, record.id );
assertEquals( "mine", r.state.tenantId ); assertEquals( "mine", r.state.tenantId );