-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: [Hibernate Search] Clean Lucene Indexes After Test Method
PostPosted: Mon Sep 05, 2011 4:37 pm 
Newbie

Joined: Mon Sep 05, 2011 4:04 pm
Posts: 3
Hi,

I have a test class using testng and extends SeamTest, with two test methods. Both tests are calling a method to setup data for the test in the doBefore method.

I am testing repository methods that doing search using lucene as follow:
Code:
public int countResults(...){
       FullTextQuery query = buildSearchQuery(...);
       return query.getResultSize();
}

public List<T> search(...){
       FullTextQuery query = buildSearchQuery(...);
       return query.getResultList();
}


The first test method pass, but the second test fail when it asserts count equals some value (say n), b/c the returned count is 2n, the wired is the search method returning results that of size n. And if I changed the count method to return query.getResultList().size(), my test pass.

So it is a problem w/ lucene indexes seems it is not cleaned after the first test method, and since getResultSize() not accessing db to fetch data, I got count = 2n.

I am using HSQL db for unit tests, and it is configured to create-drop data, and I am using RAMDirectory to have lucene indexes in-memory.

The last thing, I made a workaround to make my tests pass. In the doBefore I am reindexing my entity before calling setupData because if I called it after setup data, I also got wrong counts but now at the first test too.

Here's my doBefore method:

Code:
   
       @BeforeMethod
   public void begin() {
      super.begin();
      componentTest = new AbstractTransactionEnabledComponentTest() {
         @Override
         public void doBefore() throws Exception {
            super.doBefore();
                                . . .
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
            try {
               fullTextEntityManager.createIndexer(MyEntity.class).startAndWait();
            } catch (InterruptedException e) {
               log.warn("Interrupted while indexing entire search index: {}", e.toString());
            }

            setUpData();
         }
      };
   }


What I think is when reindexing a class and it has zero entities in the db, the index is cleaned, but if it has entities, index is updated for the new entities.

So what should be the correct fix to this problem? how to clean lucene index after each test method, as hibernate does for the database?


Top
 Profile  
 
 Post subject: Re: [Hibernate Search] Clean Lucene Indexes After Test Method
PostPosted: Tue Sep 06, 2011 1:37 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
so I'm understanding correctly that each of your tests is closing the SessionFactory, and so running the drop script on the database (or deleting the in-memory database) ?

In this case the RAMDirectory should be perfect, you mentioned you're using it. Could you double-check that you're not creating indexes on the file system?

which version of Hibernate Search?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: [Hibernate Search] Clean Lucene Indexes After Test Method
PostPosted: Wed Sep 07, 2011 9:12 am 
Newbie

Joined: Mon Sep 05, 2011 4:04 pm
Posts: 3
Hibernate Search version is 3.4.0-Final
Hibernate version 3.5.3-Final

And I am not creating indexes manually, hibernate creates it automatically.

Here's the configuration in persistence.xml

Code:
         
                        <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
         <property name="hibernate.max_fetch_depth" value="3" />
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
         <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:testdb" />
         <property name="hibernate.connection.username" value="sa" />
         <property name="dialect" value="org.hibernate.dialect.HSQLDialect" />
         <property name="hibernate.search.default.directory_provider"
            value="org.hibernate.search.store.RAMDirectoryProvider" />
         <property name="hibernate.cache.use_second_level_cache" value="true" />
         <property name="hibernate.transaction.flush_before_completion" value="true" />
         <property name="hibernate.cache.provider_class"
            value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
         <property name="hibernate.generate_statistics" value="true" />


Top
 Profile  
 
 Post subject: Re: [Hibernate Search] Clean Lucene Indexes After Test Method
PostPosted: Wed Sep 07, 2011 9:35 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
configuration looks like correct.

Quote:
And I am not creating indexes manually, hibernate creates it automatically.

yes sorry, what I meant to ask is if you could double-check that Hibernate Search isn't creating the indexes on disk anyway.

Could you check also if the EntityManagerFactory is correctly closed at the end of each test? If it leaks out it's possible that subsequent test runs are able to find the existing RAMDirectory.
For both cases I think adding some detail to the logs should help.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: [Hibernate Search] Clean Lucene Indexes After Test Method
PostPosted: Mon Sep 19, 2011 5:34 pm 
Newbie

Joined: Mon Sep 05, 2011 4:04 pm
Posts: 3
You are right, it is using same EntityManagerFactory for all methods in the test class. This is the problem.

Thanks for your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.