From c40f9290a9594c83996bf0611075ada3671989fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Fri, 29 Sep 2023 10:30:01 +0200 Subject: [PATCH] HSEARCH-4487 Simplify Jakarta Batch test utils --- .../EntityManagerFactoryRetrievalIT.java | 20 +--- .../batch/massindexing/MassIndexingJobIT.java | 104 +++++------------- .../MassIndexingJobWithCompositeIdIT.java | 59 +++++----- .../MassIndexingJobWithMultiTenancyIT.java | 16 +-- .../batch/massindexing/RestartChunkIT.java | 7 +- .../jakarta/batch/util/JobTestUtil.java | 43 ++++++-- 6 files changed, 98 insertions(+), 151 deletions(-) diff --git a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/EntityManagerFactoryRetrievalIT.java b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/EntityManagerFactoryRetrievalIT.java index 7afa6dc10aa..0061cf1ed5a 100644 --- a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/EntityManagerFactoryRetrievalIT.java +++ b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/EntityManagerFactoryRetrievalIT.java @@ -6,15 +6,12 @@ */ package org.hibernate.search.integrationtest.jakarta.batch.massindexing; -import static org.hibernate.search.integrationtest.jakarta.batch.util.JobTestUtil.JOB_TIMEOUT_MS; import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with; import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.List; -import jakarta.batch.operations.JobOperator; -import jakarta.batch.runtime.JobExecution; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; @@ -49,12 +46,10 @@ public class EntityManagerFactoryRetrievalIT { private static final String SESSION_FACTORY_NAME = "primary_session_factory"; - protected JobOperator jobOperator; protected EntityManagerFactory emf; @Before public void setup() { - jobOperator = JobTestUtil.getAndCheckRuntime(); List companies = new ArrayList<>(); List people = new ArrayList<>(); List whos = new ArrayList<>(); @@ -97,16 +92,13 @@ public void defaultNamespace() throws Exception { List companies = JobTestUtil.findIndexedResults( emf, Company.class, "name", "Google" ); assertEquals( 0, companies.size() ); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntity( Company.class ) .checkpointInterval( CHECKPOINT_INTERVAL ) .entityManagerFactoryReference( getPersistenceUnitName() ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); companies = JobTestUtil.findIndexedResults( emf, Company.class, "name", "Google" ); assertEquals( INSTANCES_PER_DATA_TEMPLATE, companies.size() ); @@ -117,8 +109,7 @@ public void persistenceUnitNamespace() throws Exception { List companies = JobTestUtil.findIndexedResults( emf, Company.class, "name", "Google" ); assertEquals( 0, companies.size() ); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntity( Company.class ) .checkpointInterval( CHECKPOINT_INTERVAL ) @@ -126,8 +117,6 @@ public void persistenceUnitNamespace() throws Exception { .entityManagerFactoryReference( getPersistenceUnitName() ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); companies = JobTestUtil.findIndexedResults( emf, Company.class, "name", "Google" ); assertEquals( INSTANCES_PER_DATA_TEMPLATE, companies.size() ); @@ -138,8 +127,7 @@ public void sessionFactoryNamespace() throws Exception { List companies = JobTestUtil.findIndexedResults( emf, Company.class, "name", "Google" ); assertEquals( 0, companies.size() ); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntity( Company.class ) .checkpointInterval( CHECKPOINT_INTERVAL ) @@ -147,8 +135,6 @@ public void sessionFactoryNamespace() throws Exception { .entityManagerFactoryReference( SESSION_FACTORY_NAME ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); companies = JobTestUtil.findIndexedResults( emf, Company.class, "name", "Google" ); assertEquals( INSTANCES_PER_DATA_TEMPLATE, companies.size() ); diff --git a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobIT.java b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobIT.java index 2adc1511223..9d626c1d769 100644 --- a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobIT.java +++ b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobIT.java @@ -8,18 +8,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; -import static org.hibernate.search.integrationtest.jakarta.batch.util.JobTestUtil.JOB_TIMEOUT_MS; import static org.junit.Assert.assertEquals; import static org.junit.Assume.assumeTrue; import java.io.IOException; -import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.List; import java.util.Map; -import jakarta.batch.operations.JobOperator; -import jakarta.batch.runtime.BatchStatus; import jakarta.batch.runtime.JobExecution; import jakarta.batch.runtime.StepExecution; import jakarta.persistence.EntityManagerFactory; @@ -36,7 +32,6 @@ import org.hibernate.search.integrationtest.jakarta.batch.massindexing.entity.WhoAmI; import org.hibernate.search.integrationtest.jakarta.batch.util.BackendConfigurations; import org.hibernate.search.integrationtest.jakarta.batch.util.JobTestUtil; -import org.hibernate.search.jakarta.batch.core.logging.impl.Log; import org.hibernate.search.jakarta.batch.core.massindexing.MassIndexingJob; import org.hibernate.search.jakarta.batch.core.massindexing.step.impl.StepProgress; import org.hibernate.search.mapper.orm.Search; @@ -44,7 +39,6 @@ import org.hibernate.search.mapper.orm.session.SearchSession; import org.hibernate.search.mapper.orm.work.SearchIndexingPlan; import org.hibernate.search.util.common.AssertionFailure; -import org.hibernate.search.util.common.logging.impl.LoggerFactory; import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper; import org.hibernate.search.util.impl.integrationtest.mapper.orm.ReusableOrmSetupHolder; import org.hibernate.search.util.impl.test.annotation.TestForIssue; @@ -60,8 +54,6 @@ */ public class MassIndexingJobIT { - private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() ); - protected static final int INSTANCES_PER_DATA_TEMPLATE = 100; // We have three data templates per entity type (see setup) @@ -82,7 +74,6 @@ public class MassIndexingJobIT { public MethodRule setupHolderMethodRule = setupHolder.methodRule(); private EntityManagerFactory emf; - private JobOperator jobOperator; @ReusableOrmSetupHolder.Setup public void setup(OrmSetupHelper.SetupContext setupContext, ReusableOrmSetupHolder.DataClearConfig dataClearConfig) { @@ -95,7 +86,6 @@ public void setup(OrmSetupHelper.SetupContext setupContext, ReusableOrmSetupHold @Before public void initData() { emf = setupHolder.entityManagerFactory(); - jobOperator = JobTestUtil.getAndCheckRuntime(); List companies = new ArrayList<>(); List people = new ArrayList<>(); List whos = new ArrayList<>(); @@ -145,19 +135,15 @@ public void simple() assertEquals( 0, people.size() ); assertEquals( 0, whos.size() ); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobExecution execution = JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntities( Company.class, Person.class, WhoAmI.class ) .checkpointInterval( CHECKPOINT_INTERVAL ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); - assertCompletion( executionId ); - assertProgress( executionId, Person.class, INSTANCE_PER_ENTITY_TYPE ); - assertProgress( executionId, Company.class, INSTANCE_PER_ENTITY_TYPE ); - assertProgress( executionId, WhoAmI.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, Person.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, Company.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, WhoAmI.class, INSTANCE_PER_ENTITY_TYPE ); companies = JobTestUtil.findIndexedResults( emf, Company.class, "name", "Google" ); people = JobTestUtil.findIndexedResults( emf, Person.class, "firstName", "Linus" ); @@ -178,18 +164,14 @@ public void simple_defaultCheckpointInterval() assertEquals( 0, people.size() ); assertEquals( 0, whos.size() ); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobExecution execution = JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntities( Company.class, Person.class, WhoAmI.class ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); - assertCompletion( executionId ); - assertProgress( executionId, Person.class, INSTANCE_PER_ENTITY_TYPE ); - assertProgress( executionId, Company.class, INSTANCE_PER_ENTITY_TYPE ); - assertProgress( executionId, WhoAmI.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, Person.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, Company.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, WhoAmI.class, INSTANCE_PER_ENTITY_TYPE ); companies = JobTestUtil.findIndexedResults( emf, Company.class, "name", "Google" ); people = JobTestUtil.findIndexedResults( emf, Person.class, "firstName", "Linus" ); @@ -212,17 +194,13 @@ public void indexedEmbeddedCollection() throws InterruptedException { assertEquals( 0, groupsContainingRedHat.size() ); assertEquals( 0, groupsContainingMicrosoft.size() ); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobExecution execution = JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntities( CompanyGroup.class ) .checkpointInterval( CHECKPOINT_INTERVAL ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); - assertCompletion( executionId ); - assertProgress( executionId, CompanyGroup.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, CompanyGroup.class, INSTANCE_PER_ENTITY_TYPE ); groupsContainingGoogle = JobTestUtil.findIndexedResults( emf, CompanyGroup.class, "companies.name", "Google" ); groupsContainingRedHat = JobTestUtil.findIndexedResults( emf, CompanyGroup.class, "companies.name", "Red Hat" ); @@ -251,8 +229,7 @@ public void indexedEmbeddedCollection_idFetchSize_entityFetchSize_mysql() throws assertEquals( 0, groupsContainingRedHat.size() ); assertEquals( 0, groupsContainingMicrosoft.size() ); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobExecution execution = JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntities( CompanyGroup.class ) .checkpointInterval( CHECKPOINT_INTERVAL ) @@ -260,10 +237,7 @@ public void indexedEmbeddedCollection_idFetchSize_entityFetchSize_mysql() throws .idFetchSize( Integer.MIN_VALUE ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); - assertCompletion( executionId ); - assertProgress( executionId, CompanyGroup.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, CompanyGroup.class, INSTANCE_PER_ENTITY_TYPE ); groupsContainingGoogle = JobTestUtil.findIndexedResults( emf, CompanyGroup.class, "companies.name", "Google" ); groupsContainingRedHat = JobTestUtil.findIndexedResults( emf, CompanyGroup.class, "companies.name", "Red Hat" ); @@ -285,16 +259,13 @@ public void purge() throws InterruptedException, IOException { * Request a mass indexing with a filter matching nothing, * which should effectively amount to a simple purge. */ - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntity( Company.class ) .purgeAllOnStart( true ) .reindexOnly( "name like :name", Map.of( "name", "NEVER_MATCH" ) ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); assertEquals( 0, JobTestUtil.nbDocumentsInIndex( emf, Company.class ) ); } @@ -311,16 +282,13 @@ public void noPurge() throws InterruptedException, IOException { * Request a mass indexing with a filter matching nothing, and requesting no purge at all, * which should effectively amount to a no-op. */ - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntity( Company.class ) .purgeAllOnStart( false ) .reindexOnly( "name like :name", Map.of( "name", "NEVER_MATCH" ) ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); assertEquals( expectedCount, JobTestUtil.nbDocumentsInIndex( emf, Company.class ) ); } @@ -335,16 +303,13 @@ public void reindexOnly() assertEquals( 0, JobTestUtil.findIndexedResults( emf, Company.class, "name", "Red Hat" ).size() ); assertEquals( 0, JobTestUtil.findIndexedResults( emf, Company.class, "name", "Microsoft" ).size() ); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntity( Company.class ) .checkpointInterval( CHECKPOINT_INTERVAL ) .reindexOnly( "name like 'Google%' or name like 'Red Hat%'", Map.of() ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); assertEquals( INSTANCES_PER_DATA_TEMPLATE, JobTestUtil.findIndexedResults( emf, Company.class, "name", "Google" ).size() ); @@ -363,8 +328,7 @@ public void reindexOnly_maxResults() int maxResults = CHECKPOINT_INTERVAL + 1; - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntity( Company.class ) .checkpointInterval( CHECKPOINT_INTERVAL ) @@ -372,8 +336,6 @@ public void reindexOnly_maxResults() .maxResultsPerEntity( maxResults ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); assertEquals( maxResults, JobTestUtil.nbDocumentsInIndex( emf, Company.class ) ); } @@ -389,22 +351,18 @@ public void partitioned() assertEquals( 0, people.size() ); assertEquals( 0, whos.size() ); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobExecution execution = JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntities( Company.class, Person.class, WhoAmI.class ) .checkpointInterval( CHECKPOINT_INTERVAL ) .rowsPerPartition( INSTANCE_PER_ENTITY_TYPE - 1 ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); - assertCompletion( executionId ); - assertProgress( executionId, Person.class, INSTANCE_PER_ENTITY_TYPE ); - assertProgress( executionId, Company.class, INSTANCE_PER_ENTITY_TYPE ); - assertProgress( executionId, WhoAmI.class, INSTANCE_PER_ENTITY_TYPE ); - - StepProgress progress = getMainStepProgress( executionId ); + assertProgress( execution, Person.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, Company.class, INSTANCE_PER_ENTITY_TYPE ); + assertProgress( execution, WhoAmI.class, INSTANCE_PER_ENTITY_TYPE ); + + StepProgress progress = getMainStepProgress( execution ); Map partitionProgress = progress.getPartitionProgress(); assertThat( partitionProgress ) .as( "Entities processed per partition" ) @@ -429,29 +387,21 @@ public void partitioned() assertEquals( INSTANCES_PER_DATA_TEMPLATE, whos.size() ); } - private void assertCompletion(long executionId) { - List stepExecutions = jobOperator.getStepExecutions( executionId ); - for ( StepExecution stepExecution : stepExecutions ) { - BatchStatus batchStatus = stepExecution.getBatchStatus(); - log.infof( "step %s executed.", stepExecution.getStepName() ); - assertEquals( BatchStatus.COMPLETED, batchStatus ); - } - } - - private void assertProgress(long executionId, Class entityType, int progressValue) { + private void assertProgress(JobExecution execution, Class entityType, int progressValue) { /* * We cannot check the metrics, which in JBatch are set to 0 * for partitioned steps (the metrics are handled separately for * each partition). * Thus we check our own object. */ - StepProgress progress = getMainStepProgress( executionId ); + StepProgress progress = getMainStepProgress( execution ); assertEquals( Long.valueOf( progressValue ), progress.getEntityProgress().get( emf.getMetamodel().entity( entityType ).getName() ) ); } - private StepProgress getMainStepProgress(long executionId) { - List stepExecutions = jobOperator.getStepExecutions( executionId ); + private StepProgress getMainStepProgress(JobExecution execution) { + List stepExecutions = JobTestUtil.getOperator() + .getStepExecutions( execution.getExecutionId() ); for ( StepExecution stepExecution : stepExecutions ) { switch ( stepExecution.getStepName() ) { case MAIN_STEP_NAME: diff --git a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobWithCompositeIdIT.java b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobWithCompositeIdIT.java index 717d70a34ee..fdb316817c3 100644 --- a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobWithCompositeIdIT.java +++ b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobWithCompositeIdIT.java @@ -7,7 +7,6 @@ package org.hibernate.search.integrationtest.jakarta.batch.massindexing; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.search.integrationtest.jakarta.batch.util.JobTestUtil.JOB_TIMEOUT_MS; import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with; import java.io.Serializable; @@ -16,7 +15,6 @@ import java.time.temporal.ChronoUnit; import java.util.Locale; import java.util.Map; -import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -95,12 +93,13 @@ public void initData() throws Exception { @Test @Ignore("HSEARCH-4033") // TODO HSEARCH-4033 Support mass-indexing of composite id entities public void canHandleIdClass() throws Exception { - Properties props = MassIndexingJob.parameters() - .forEntities( EntityWithIdClass.class ) - .rowsPerPartition( 13 ) // Ensure there're more than 1 partition, so that a WHERE clause is applied. - .checkpointInterval( 4 ) - .build(); - JobTestUtil.startJobAndWait( MassIndexingJob.NAME, props, JOB_TIMEOUT_MS ); + JobTestUtil.startJobAndWaitForSuccessNoRetry( + MassIndexingJob.parameters() + .forEntities( EntityWithIdClass.class ) + .rowsPerPartition( 13 ) // Ensure there're more than 1 partition, so that a WHERE clause is applied. + .checkpointInterval( 4 ) + .build() + ); int expectedDays = (int) ChronoUnit.DAYS.between( START, END ); assertThat( JobTestUtil.nbDocumentsInIndex( emf, EntityWithIdClass.class ) ).isEqualTo( expectedDays ); @@ -109,13 +108,14 @@ public void canHandleIdClass() throws Exception { @Test @Ignore("HSEARCH-4033") // TODO HSEARCH-4033 Support mass-indexing of composite id entities public void canHandleIdClass_reindexOnly() throws Exception { - Properties props = MassIndexingJob.parameters() - .forEntities( EntityWithIdClass.class ) - .reindexOnly( "month >= :month", Map.of( "month", 7 ) ) - .rowsPerPartition( 13 ) // Ensure there're more than 1 partition, so that a WHERE clause is applied. - .checkpointInterval( 4 ) - .build(); - JobTestUtil.startJobAndWait( MassIndexingJob.NAME, props, JOB_TIMEOUT_MS ); + JobTestUtil.startJobAndWaitForSuccessNoRetry( + MassIndexingJob.parameters() + .forEntities( EntityWithIdClass.class ) + .reindexOnly( "month >= :month", Map.of( "month", 7 ) ) + .rowsPerPartition( 13 ) // Ensure there're more than 1 partition, so that a WHERE clause is applied. + .checkpointInterval( 4 ) + .build() + ); int expectedDays = (int) ChronoUnit.DAYS.between( LocalDate.of( 2017, 7, 1 ), END ); int actualDays = JobTestUtil.nbDocumentsInIndex( emf, EntityWithIdClass.class ); @@ -124,13 +124,13 @@ public void canHandleIdClass_reindexOnly() throws Exception { @Test public void canHandleEmbeddedId() throws Exception { - Properties props = MassIndexingJob.parameters() - .forEntities( EntityWithEmbeddedId.class ) - .rowsPerPartition( 13 ) // Ensure there're more than 1 partition, so that a WHERE clause is applied. - .checkpointInterval( 4 ) - .build(); - - JobTestUtil.startJobAndWait( MassIndexingJob.NAME, props, JOB_TIMEOUT_MS ); + JobTestUtil.startJobAndWaitForSuccessNoRetry( + MassIndexingJob.parameters() + .forEntities( EntityWithEmbeddedId.class ) + .rowsPerPartition( 13 ) // Ensure there're more than 1 partition, so that a WHERE clause is applied. + .checkpointInterval( 4 ) + .build() + ); int expectedDays = (int) ChronoUnit.DAYS.between( START, END ); int actualDays = JobTestUtil.nbDocumentsInIndex( emf, EntityWithEmbeddedId.class ); @@ -139,13 +139,14 @@ public void canHandleEmbeddedId() throws Exception { @Test public void canHandleEmbeddedId_reindexOnly() throws Exception { - Properties props = MassIndexingJob.parameters() - .forEntities( EntityWithEmbeddedId.class ) - .reindexOnly( "embeddableDateId.month >= :month", Map.of( "month", 7 ) ) - .rowsPerPartition( 13 ) // Ensure there're more than 1 partition, so that a WHERE clause is applied. - .checkpointInterval( 4 ) - .build(); - JobTestUtil.startJobAndWait( MassIndexingJob.NAME, props, JOB_TIMEOUT_MS ); + JobTestUtil.startJobAndWaitForSuccessNoRetry( + MassIndexingJob.parameters() + .forEntities( EntityWithEmbeddedId.class ) + .reindexOnly( "embeddableDateId.month >= :month", Map.of( "month", 7 ) ) + .rowsPerPartition( 13 ) // Ensure there're more than 1 partition, so that a WHERE clause is applied. + .checkpointInterval( 4 ) + .build() + ); int expectedDays = (int) ChronoUnit.DAYS.between( LocalDate.of( 2017, 7, 1 ), END ); int actualDays = JobTestUtil.nbDocumentsInIndex( emf, EntityWithEmbeddedId.class ); diff --git a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobWithMultiTenancyIT.java b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobWithMultiTenancyIT.java index 865846fd67a..426a1388952 100644 --- a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobWithMultiTenancyIT.java +++ b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/MassIndexingJobWithMultiTenancyIT.java @@ -7,16 +7,11 @@ package org.hibernate.search.integrationtest.jakarta.batch.massindexing; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.search.integrationtest.jakarta.batch.util.JobTestUtil.JOB_TIMEOUT_MS; import static org.hibernate.search.integrationtest.jakarta.batch.util.JobTestUtil.findIndexedResultsInTenant; import java.util.Arrays; import java.util.List; -import jakarta.batch.operations.JobOperator; -import jakarta.batch.runtime.BatchStatus; -import jakarta.batch.runtime.JobExecution; - import org.hibernate.SessionFactory; import org.hibernate.search.integrationtest.jakarta.batch.massindexing.entity.Company; import org.hibernate.search.integrationtest.jakarta.batch.util.BackendConfigurations; @@ -48,8 +43,6 @@ public class MassIndexingJobWithMultiTenancyIT { @Rule public MethodRule setupHolderMethodRule = setupHolder.methodRule(); - private JobOperator jobOperator; - private final List companies = Arrays.asList( new Company( "Google" ), new Company( "Red Hat" ), @@ -66,8 +59,6 @@ public void setup(OrmSetupHelper.SetupContext setupContext) { @Before public void initData() { - jobOperator = JobTestUtil.getAndCheckRuntime(); - setupHolder.with( TARGET_TENANT_ID ) .runInTransaction( session -> companies.forEach( session::persist ) ); setupHolder.with( TARGET_TENANT_ID ) @@ -78,18 +69,13 @@ public void initData() { public void shouldHandleTenantIds() throws Exception { SessionFactory sessionFactory = setupHolder.sessionFactory(); - long executionId = jobOperator.start( - MassIndexingJob.NAME, + JobTestUtil.startJobAndWaitForSuccessNoRetry( MassIndexingJob.parameters() .forEntity( Company.class ) .tenantId( TARGET_TENANT_ID ) .build() ); - JobExecution jobExecution = jobOperator.getJobExecution( executionId ); - JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS ); - assertThat( jobExecution.getBatchStatus() ).isEqualTo( BatchStatus.COMPLETED ); - assertThat( findIndexedResultsInTenant( sessionFactory, Company.class, "name", "Google", TARGET_TENANT_ID ) ) .hasSize( 1 ); assertThat( findIndexedResultsInTenant( sessionFactory, Company.class, "name", "Red Hat", TARGET_TENANT_ID ) ) diff --git a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/RestartChunkIT.java b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/RestartChunkIT.java index fb4d84025cc..d8a6194325f 100644 --- a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/RestartChunkIT.java +++ b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/massindexing/RestartChunkIT.java @@ -6,7 +6,6 @@ */ package org.hibernate.search.integrationtest.jakarta.batch.massindexing; -import static org.hibernate.search.integrationtest.jakarta.batch.util.JobTestUtil.JOB_TIMEOUT_MS; import static org.junit.Assert.assertEquals; import java.io.IOException; @@ -70,7 +69,7 @@ public void setup(OrmSetupHelper.SetupContext setupContext) { public void initData() { SimulatedFailure.reset(); emf = setupHolder.entityManagerFactory(); - jobOperator = JobTestUtil.getAndCheckRuntime(); + jobOperator = JobTestUtil.getOperator(); String[] str = new String[] { "Google", @@ -148,7 +147,7 @@ private void doTest(String reindexOnly, long expectedTotal, long expectedGoogle) parameters ); JobExecution jobExec1 = jobOperator.getJobExecution( execId1 ); - JobTestUtil.waitForTermination( jobOperator, jobExec1, JOB_TIMEOUT_MS ); + JobTestUtil.waitForTermination( jobExec1 ); // job will be stopped by the SimulatedFailure assertEquals( BatchStatus.FAILED, getMainStepStatus( execId1 ) ); @@ -159,7 +158,7 @@ private void doTest(String reindexOnly, long expectedTotal, long expectedGoogle) */ long execId2 = jobOperator.restart( execId1, parameters ); JobExecution jobExec2 = jobOperator.getJobExecution( execId2 ); - JobTestUtil.waitForTermination( jobOperator, jobExec2, JOB_TIMEOUT_MS ); + JobTestUtil.waitForTermination( jobExec2 ); assertEquals( BatchStatus.COMPLETED, getMainStepStatus( execId2 ) ); // search again diff --git a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/util/JobTestUtil.java b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/util/JobTestUtil.java index 273e7cdf5bd..ebc740e8339 100644 --- a/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/util/JobTestUtil.java +++ b/integrationtest/mapper/orm-jakarta-batch/src/test/java/org/hibernate/search/integrationtest/jakarta/batch/util/JobTestUtil.java @@ -16,11 +16,13 @@ import jakarta.batch.runtime.BatchRuntime; import jakarta.batch.runtime.BatchStatus; import jakarta.batch.runtime.JobExecution; +import jakarta.batch.runtime.StepExecution; import jakarta.persistence.EntityManagerFactory; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.search.jakarta.batch.core.logging.impl.Log; +import org.hibernate.search.jakarta.batch.core.massindexing.MassIndexingJob; import org.hibernate.search.jakarta.batch.core.massindexing.util.impl.EntityTypeDescriptor; import org.hibernate.search.mapper.orm.Search; import org.hibernate.search.mapper.orm.loading.spi.LoadingTypeContext; @@ -41,10 +43,24 @@ public final class JobTestUtil { private static final int THREAD_SLEEP_MS = 100; private static final String JAKARTA_BATCH_TYPE_FOR_IDE_TESTS = "jbatch"; + private static volatile JobOperator operator; + private JobTestUtil() { } - public static JobOperator getAndCheckRuntime() { + public static JobOperator getOperator() { + if ( operator == null ) { + synchronized (JobTestUtil.class) { + if ( operator == null ) { + operator = createAndCheckOperator(); + } + } + } + + return operator; + } + + private static JobOperator createAndCheckOperator() { JobOperator operator = BatchRuntime.getJobOperator(); String expectedType = System.getProperty( "org.hibernate.search.integrationtest.jakarta.batch.type" ); @@ -59,17 +75,26 @@ public static JobOperator getAndCheckRuntime() { return operator; } - public static void startJobAndWait(String jobName, Properties jobParams, int timeoutInMs) throws InterruptedException { - JobOperator jobOperator = getAndCheckRuntime(); - long execId = jobOperator.start( jobName, jobParams ); + public static JobExecution startJobAndWaitForSuccessNoRetry(Properties jobParams) throws InterruptedException { + JobOperator jobOperator = getOperator(); + long execId = jobOperator.start( MassIndexingJob.NAME, jobParams ); JobExecution jobExec = jobOperator.getJobExecution( execId ); - jobExec = JobTestUtil.waitForTermination( jobOperator, jobExec, timeoutInMs ); - assertThat( jobExec.getBatchStatus() ).isEqualTo( BatchStatus.COMPLETED ); + jobExec = JobTestUtil.waitForTermination( jobExec ); + assertThat( jobExec.getBatchStatus() ) + .as( "Status of job " + jobExec.getJobName() ) + .isEqualTo( BatchStatus.COMPLETED ); + List stepExecutions = jobOperator.getStepExecutions( jobExec.getExecutionId() ); + for ( StepExecution stepExecution : stepExecutions ) { + assertThat( stepExecution.getBatchStatus() ) + .as( "Status of step " + stepExecution.getStepName() ) + .isEqualTo( BatchStatus.COMPLETED ); + } + return jobExec; } - public static JobExecution waitForTermination(JobOperator jobOperator, JobExecution jobExecution, int timeoutInMs) + public static JobExecution waitForTermination(JobExecution jobExecution) throws InterruptedException { - long endTime = System.nanoTime() + timeoutInMs * 1_000_000L; + long endTime = System.nanoTime() + JOB_TIMEOUT_MS * 1_000_000L; while ( !jobExecution.getBatchStatus().equals( BatchStatus.COMPLETED ) && !jobExecution.getBatchStatus().equals( BatchStatus.STOPPED ) @@ -84,7 +109,7 @@ public static JobExecution waitForTermination(JobOperator jobOperator, JobExecut THREAD_SLEEP_MS ); Thread.sleep( THREAD_SLEEP_MS ); - jobExecution = jobOperator.getJobExecution( executionId ); + jobExecution = getOperator().getJobExecution( executionId ); } return jobExecution;