HHH-13986 SequenceHiLoGeneratorNoIncrementTest is not testing the right Optimiser

This commit is contained in:
Andrea Boriero 2020-04-28 15:16:09 +01:00 committed by Sanne Grinovero
parent ae4e6ef729
commit 859ffb608a
2 changed files with 138 additions and 24 deletions

View File

@ -8,7 +8,6 @@ package org.hibernate.id;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
@ -47,7 +46,7 @@ public class SequenceHiLoGeneratorNoIncrementTest extends BaseUnitTestCase {
private StandardServiceRegistry serviceRegistry;
private SessionFactoryImplementor sessionFactory;
private SequenceStyleGenerator generator;
private SequenceGenerator generator;
private SessionImplementor sessionImpl;
private SequenceValueExtractor sequenceValueExtractor;
@ -58,22 +57,19 @@ public class SequenceHiLoGeneratorNoIncrementTest extends BaseUnitTestCase {
.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
.build();
generator = new SequenceStyleGenerator();
MetadataBuildingContext buildingContext = new MetadataBuildingContextTestingImpl( serviceRegistry );
// Build the properties used to configure the id generator
Properties properties = new Properties();
properties.setProperty( SequenceStyleGenerator.SEQUENCE_PARAM, TEST_SEQUENCE );
properties.setProperty( SequenceStyleGenerator.OPT_PARAM, "legacy-hilo" );
properties.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "0" ); // JPA allocationSize of 1
properties.setProperty( SequenceGenerator.SEQUENCE, TEST_SEQUENCE );
properties.setProperty( SequenceHiLoGenerator.MAX_LO, "0" ); // JPA allocationSize of 1
properties.put(
PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
new ObjectNameNormalizer() {
@Override
protected MetadataBuildingContext getBuildingContext() {
return new MetadataBuildingContextTestingImpl( serviceRegistry );
}
}
buildingContext.getObjectNameNormalizer()
);
generator = new SequenceHiLoGenerator();
generator.configure( StandardBasicTypes.LONG, properties, serviceRegistry );
final Metadata metadata = new MetadataSources( serviceRegistry ).buildMetadata();
@ -84,9 +80,9 @@ public class SequenceHiLoGeneratorNoIncrementTest extends BaseUnitTestCase {
}
@After
public void tearDown() throws Exception {
public void tearDown() {
if ( sessionImpl != null && !sessionImpl.isClosed() ) {
((Session) sessionImpl).close();
sessionImpl.close();
}
if ( sessionFactory != null ) {
sessionFactory.close();
@ -100,31 +96,23 @@ public class SequenceHiLoGeneratorNoIncrementTest extends BaseUnitTestCase {
public void testHiLoAlgorithm() {
sessionImpl = (SessionImpl) sessionFactory.openSession();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// historically the hilo generators skipped the initial block of values;
// so the first generated id value is maxlo + 1, here be 4
assertEquals( 1L, generateValue() );
// which should also perform the first read on the sequence which should set it to its "start with" value (1)
assertEquals( 1L, extractSequenceValue() );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
assertEquals( 2L, generateValue() );
assertEquals( 2L, extractSequenceValue() );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
assertEquals( 3L, generateValue() );
assertEquals( 3L, extractSequenceValue() );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
assertEquals( 4L, generateValue() );
assertEquals( 4L, extractSequenceValue() );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
assertEquals( 5L, generateValue() );
assertEquals( 5L, extractSequenceValue() );
((Session) sessionImpl).close();
sessionImpl.close();
}
private long extractSequenceValue() {
@ -133,7 +121,7 @@ public class SequenceHiLoGeneratorNoIncrementTest extends BaseUnitTestCase {
private long generateValue() {
Long generatedValue;
Transaction transaction = ((Session) sessionImpl).beginTransaction();
Transaction transaction = sessionImpl.beginTransaction();
generatedValue = (Long) generator.generate( sessionImpl, null );
transaction.commit();
return generatedValue.longValue();

View File

@ -0,0 +1,126 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.id;
import java.util.Properties;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ObjectNameNormalizer;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.internal.SessionImpl;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.boot.MetadataBuildingContextTestingImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author Andrea Boriero
*/
@RequiresDialectFeature(DialectChecks.SupportsSequences.class)
public class SequenceStyleGeneratorBehavesLikeSequeceHiloGeneratorWitZeroIncrementSizeTest {
private static final String TEST_SEQUENCE = "test_sequence";
private StandardServiceRegistry serviceRegistry;
private SessionFactoryImplementor sessionFactory;
private SequenceStyleGenerator generator;
private SessionImplementor sessionImpl;
private SequenceValueExtractor sequenceValueExtractor;
@Before
public void setUp() throws Exception {
serviceRegistry = new StandardServiceRegistryBuilder()
.enableAutoClose()
.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
.build();
generator = new SequenceStyleGenerator();
// Build the properties used to configure the id generator
Properties properties = new Properties();
properties.setProperty( SequenceStyleGenerator.SEQUENCE_PARAM, TEST_SEQUENCE );
properties.setProperty( SequenceStyleGenerator.OPT_PARAM, "legacy-hilo" );
properties.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "0" ); // JPA allocationSize of 1
properties.put(
PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
new ObjectNameNormalizer() {
@Override
protected MetadataBuildingContext getBuildingContext() {
return new MetadataBuildingContextTestingImpl( serviceRegistry );
}
}
);
generator.configure( StandardBasicTypes.LONG, properties, serviceRegistry );
final Metadata metadata = new MetadataSources( serviceRegistry ).buildMetadata();
generator.registerExportables( metadata.getDatabase() );
sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
sequenceValueExtractor = new SequenceValueExtractor( sessionFactory.getDialect(), TEST_SEQUENCE );
}
@After
public void tearDown() {
if ( sessionImpl != null && !sessionImpl.isClosed() ) {
sessionImpl.close();
}
if ( sessionFactory != null ) {
sessionFactory.close();
}
if ( serviceRegistry != null ) {
StandardServiceRegistryBuilder.destroy( serviceRegistry );
}
}
@Test
public void testHiLoAlgorithm() {
sessionImpl = (SessionImpl) sessionFactory.openSession();
assertEquals( 1L, generateValue() );
assertEquals( 1L, extractSequenceValue() );
assertEquals( 2L, generateValue() );
assertEquals( 2L, extractSequenceValue() );
assertEquals( 3L, generateValue() );
assertEquals( 3L, extractSequenceValue() );
assertEquals( 4L, generateValue() );
assertEquals( 4L, extractSequenceValue() );
assertEquals( 5L, generateValue() );
assertEquals( 5L, extractSequenceValue() );
sessionImpl.close();
}
private long extractSequenceValue() {
return sequenceValueExtractor.extractSequenceValue( sessionImpl );
}
private long generateValue() {
Long generatedValue;
Transaction transaction = sessionImpl.beginTransaction();
generatedValue = (Long) generator.generate( sessionImpl, null );
transaction.commit();
return generatedValue.longValue();
}
}