public void testJGroupsBackend() throws Exception {
//get slave session
Session slaveSession = getSlaveSession();
Transaction tx = slaveSession.beginTransaction();
TShirt ts = new TShirt();
ts.setLogo( "Boston" );
ts.setSize( "XXL" );
ts.setLength( 23.4d );
TShirt ts2 = new TShirt();
ts2.setLogo( "Mapple leaves" );
ts2.setSize( "L" );
ts2.setLength( 23.42d );
slaveSession.persist( ts );
slaveSession.persist( ts2 );
tx.commit();
QueryParser parser = new QueryParser(
TestConstants.getTargetLuceneVersion(),
"id",
TestConstants.stopAnalyzer
);
FullTextSession masterSession = Search.getFullTextSession( openSession() ); //this is the master Session
// since this is an async backend, we expect to see
// the values in the index *eventually*.
boolean failed = true;
for ( int i = 0; i < MAX_WAITS; i++ ) {
Thread.sleep( NETWORK_WAIT_MILLISECONDS );
masterSession.getTransaction().begin();
Query luceneQuery = parser.parse( "logo:Boston or logo:Mapple leaves" );
org.hibernate.Query query = masterSession.createFullTextQuery( luceneQuery );
List result = query.list();
masterSession.getTransaction().commit();
if ( result.size() == 2 ) { //the condition we're waiting for
failed = false;
break; //enough time wasted
}
}
if ( failed ) {
Assert.fail( "Lots of time waited and still the two documents are not indexed yet!" );
}
slaveSession = getSlaveSession();
tx = slaveSession.beginTransaction();
ts = (TShirt) slaveSession.get( TShirt.class, ts.getId() );
ts.setLogo( "Peter pan" );
tx.commit();
failed = true;
for ( int i = 0; i < MAX_WAITS; i++ ) {
//need to sleep for the message consumption
Thread.sleep( NETWORK_WAIT_MILLISECONDS );
Query luceneQuery = parser.parse( "logo:Peter pan" );
masterSession.getTransaction().begin();
org.hibernate.Query query = masterSession.createFullTextQuery( luceneQuery );
List result = query.list();
masterSession.getTransaction().commit();
if ( result.size() == 1 ) { //the condition we're waiting for
failed = false;
break; //enough time wasted
}
}
if ( failed ) {
Assert.fail( "Waited for long and still Peter Pan didn't fly in!" );
}
slaveSession = getSlaveSession();
tx = slaveSession.beginTransaction();
slaveSession.delete( slaveSession.get( TShirt.class, ts.getId() ) );
slaveSession.delete( slaveSession.get( TShirt.class, ts2.getId() ) );
tx.commit();
failed = true;
for ( int i = 0; i < MAX_WAITS; i++ ) {
//need to sleep for the message consumption