Skip to content

Commit

Permalink
HSEARCH-4134 Test concurrent embedded update
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever committed Mar 31, 2021
1 parent ccb0fcb commit 9eb7e01
Showing 1 changed file with 40 additions and 13 deletions.
Expand Up @@ -27,17 +27,19 @@
import org.hibernate.search.integrationtest.mapper.orm.realbackend.testsupport.BackendConfigurations;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings;
import org.hibernate.search.mapper.orm.outbox.impl.OutboxEvent;
import org.hibernate.search.mapper.orm.session.SearchSession;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IndexedEmbedded;
import org.hibernate.search.util.common.impl.SuppressingCloser;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import org.awaitility.Awaitility;

/**
* See "limitations-parallel-embedded-update" in the documentation.
*/
Expand All @@ -47,17 +49,43 @@ public class ConcurrentEmbeddedUpdateLimitationIT {
public OrmSetupHelper setupHelper = OrmSetupHelper.withSingleBackend( BackendConfigurations.simple() );

private SessionFactory sessionFactory;
private boolean synchronizationAsync;

@Before
public void setup() {
@Test
public void indexingStrategySession() {
synchronizationAsync = false;
sessionFactory = setupHelper.start()
// This is absolutely necessary to avoid false positives in this test
.withProperty( HibernateOrmMapperSettings.AUTOMATIC_INDEXING_SYNCHRONIZATION_STRATEGY, "sync" )
.setup( Book.class, Author.class, BookEdition.class );

reproducer();

// The previous data isn't there: good.
assertThat( countByEditionAndAuthor( "12th", "asimov" ) ).isEqualTo( 0L );

// The new data isn't there either: bad!
assertThat( countByEditionAndAuthor( "13th", "vonnegut" ) ).isEqualTo( 0L );

// Turns out only half of the update came through...
assertThat( countByEditionAndAuthor( "13th", "asimov" ) ).isEqualTo( 1L );
}

@Test
public void reproducer() {
public void indexingStrategyOutbox() {
synchronizationAsync = true;
sessionFactory = setupHelper.start()
.withProperty( "hibernate.search.automatic_indexing.strategy", "outbox-polling" )
.setup( Book.class, Author.class, BookEdition.class );

reproducer();

assertThat( countByEditionAndAuthor( "12th", "asimov" ) ).isEqualTo( 0L );
assertThat( countByEditionAndAuthor( "13th", "vonnegut" ) ).isEqualTo( 1L );
assertThat( countByEditionAndAuthor( "13th", "asimov" ) ).isEqualTo( 0L );
}

private void reproducer() {
with( sessionFactory ).run( session -> {
Book book = new Book();
book.setTitle( "The Caves Of Steel" );
Expand Down Expand Up @@ -121,19 +149,14 @@ public void reproducer() {
throw e;
}
}

// The previous data isn't there: good.
assertThat( countByEditionAndAuthor( "12th", "asimov" ) ).isEqualTo( 0L );

// The new data isn't there either: bad!
assertThat( countByEditionAndAuthor( "13th", "vonnegut" ) ).isEqualTo( 0L );

// Turns out only half of the update came through...
assertThat( countByEditionAndAuthor( "13th", "asimov" ) ).isEqualTo( 1L );
}

long countByEditionAndAuthor(String editionLabel, String authorName) {
return with( sessionFactory ).apply( session -> {
if ( synchronizationAsync ) {
Awaitility.await().until( () -> noMoreOutboxEvents( session ) );
}

SearchSession searchSession = Search.session( session );

return searchSession.search( Book.class )
Expand All @@ -144,6 +167,10 @@ long countByEditionAndAuthor(String editionLabel, String authorName) {
} );
}

private static boolean noMoreOutboxEvents(Session session) {
return session.createQuery( "select e from OutboxEvent e order by id", OutboxEvent.class ).list().isEmpty();
}

@Entity(name = Author.NAME)
public static class Author {
public static final String NAME = "Author";
Expand Down

0 comments on commit 9eb7e01

Please sign in to comment.