Search in sources :

Example 1 with BackendMock

use of org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock in project hibernate-search by hibernate.

the class JtaAndSpringOutboxApplicationConfiguration method backendMock.

@Override
public BackendMock backendMock() {
    BackendMock backendMock = super.backendMock();
    backendMock.indexingWorkExpectations(CoordinationStrategyExpectations.outboxPolling().indexingWorkExpectations);
    return backendMock;
}
Also used : BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock)

Example 2 with BackendMock

use of org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock in project hibernate-search by hibernate.

the class SessionProxyIT method useSingleSearchSessionFromMultipleThreads.

@Test
public void useSingleSearchSessionFromMultipleThreads() {
    TransactionTemplate template = new TransactionTemplate(transactionManager);
    template.execute(status -> {
        IndexedEntity entityFromThread1_1 = helperService.simulateSearch(backendMock);
        assertThat(entityFromThread1_1).returns(true, helperService.entityManager::contains);
        // Same call from the same thread and transaction:
        // the same underlying session is used, so we should get the same entity instance
        IndexedEntity entityFromThread1_2 = helperService.simulateSearch(backendMock);
        assertThat(entityFromThread1_2).returns(entityFromThread1_1.getId(), IndexedEntity::getId);
        assertThat(entityFromThread1_2).isSameAs(entityFromThread1_1);
        // Same call from another thread: another underlying session is used, so we should a different entity instance
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(10));
        CompletableFuture<IndexedEntity> future = CompletableFuture.supplyAsync(() -> template.execute(status2 -> {
            IndexedEntity entityFromThread2 = helperService.simulateSearch(backendMock);
            assertThat(entityFromThread2).returns(true, helperService.entityManager::contains);
            assertThat(entityFromThread2).returns(entityFromThread1_1.getId(), IndexedEntity::getId);
            assertThat(entityFromThread2).isNotSameAs(entityFromThread1_1);
            return entityFromThread2;
        }), executorService);
        IndexedEntity entityFromThread2 = Futures.unwrappedExceptionJoin(future);
        assertThat(entityFromThread2).returns(false, helperService.entityManager::contains);
        return null;
    });
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Search(org.hibernate.search.mapper.orm.Search) BeforeClass(org.junit.BeforeClass) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Futures(org.hibernate.search.util.common.impl.Futures) EntityScan(org.springframework.boot.autoconfigure.domain.EntityScan) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StubBackendUtils.reference(org.hibernate.search.util.impl.integrationtest.common.stub.backend.StubBackendUtils.reference) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) AbstractSpringITConfig(org.hibernate.search.integrationtest.spring.testsupport.AbstractSpringITConfig) Service(org.springframework.stereotype.Service) SpringRunner(org.springframework.test.context.junit4.SpringRunner) StubSearchWorkBehavior(org.hibernate.search.util.impl.integrationtest.common.rule.StubSearchWorkBehavior) Id(javax.persistence.Id) Before(org.junit.Before) Entity(javax.persistence.Entity) Test(org.junit.Test) EntityManager(javax.persistence.EntityManager) Indexed(org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ComponentScan(org.springframework.context.annotation.ComponentScan) TimeUnit(java.util.concurrent.TimeUnit) Configuration(org.springframework.context.annotation.Configuration) Rule(org.junit.Rule) SearchSession(org.hibernate.search.mapper.orm.session.SearchSession) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with BackendMock

use of org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock in project hibernate-search by hibernate.

the class ReusableOrmSetupHolder method methodStatement.

private Statement methodStatement(Statement base, Object testInstance) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            try {
                setupSessionFactory(testInstance);
                inMethodStatement = true;
                base.evaluate();
                // we must explicitly force the verify/reset after each test method.
                for (BackendMock backendMock : allBackendMocks) {
                    backendMock.verifyExpectationsMet();
                }
            } finally {
                inMethodStatement = false;
                for (BackendMock backendMock : allBackendMocks) {
                    backendMock.resetExpectations();
                }
            }
        }
    };
}
Also used : BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) Statement(org.junit.runners.model.Statement)

Example 4 with BackendMock

use of org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock in project hibernate-search by hibernate.

the class ReusableOrmSetupHolder method setupSessionFactory.

private void setupSessionFactory(Object testInstance) {
    if (!inClassStatement) {
        throw new Error("This usage of " + getClass().getSimpleName() + " is invalid and may result" + " in the session factory not being closed." + " Did you use the rule as explained in the javadoc, with both a @ClassRule and a @Rule," + " on two separate fields?");
    }
    Collection<?> testParams = testParams(testInstance);
    if (sessionFactory != null) {
        if (testParams.equals(testParamsForSessionFactory)) {
            log.infof("Test parameters did not change (%s vs %s). Clearing data and reusing the same session factory.", testParamsForSessionFactory, testParams);
            try {
                clearAllData(sessionFactory);
            } catch (RuntimeException e) {
                throw new Error("Failed to clear data before test execution: " + e.getMessage(), e);
            }
            return;
        } else {
            log.infof("Test parameters changed (%s vs %s). Closing the current session factory and creating another one.", testParamsForSessionFactory, testParams);
            tearDownSessionFactory();
        }
    }
    OrmSetupHelper.SetupContext setupContext = setupHelper.start();
    config = new DataClearConfigImpl();
    TestCustomSetup customSetup = new TestCustomSetup(testInstance);
    customSetup.callSetupMethods(setupContext, config);
    sessionFactory = setupContext.setup().unwrap(SessionFactoryImplementor.class);
    testParamsForSessionFactory = testParams;
    // If any backend expectations where set during setup, verify them immediately.
    for (BackendMock backendMock : allBackendMocks) {
        backendMock.verifyExpectationsMet();
    }
}
Also used : BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor)

Aggregations

BackendMock (org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock)4 CompletableFuture (java.util.concurrent.CompletableFuture)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TimeUnit (java.util.concurrent.TimeUnit)1 Entity (javax.persistence.Entity)1 EntityManager (javax.persistence.EntityManager)1 Id (javax.persistence.Id)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 AbstractSpringITConfig (org.hibernate.search.integrationtest.spring.testsupport.AbstractSpringITConfig)1 Search (org.hibernate.search.mapper.orm.Search)1 SearchSession (org.hibernate.search.mapper.orm.session.SearchSession)1 Indexed (org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed)1 Futures (org.hibernate.search.util.common.impl.Futures)1 StubSearchWorkBehavior (org.hibernate.search.util.impl.integrationtest.common.rule.StubSearchWorkBehavior)1 StubBackendUtils.reference (org.hibernate.search.util.impl.integrationtest.common.stub.backend.StubBackendUtils.reference)1 Before (org.junit.Before)1 BeforeClass (org.junit.BeforeClass)1 Rule (org.junit.Rule)1