My unit/integration tests includes tests for search functionality.
My idea is to have empty search index before each test. So, I’m trying to remove all elements in index on setup method (it’s Groovy code):
Client client = searchConnection.client
SearchResponse response = client.prepareSearch("item")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(termQuery('name', 'test')) //tried also matchAllQuery()
.setFrom(0).setSize(100).setExplain(false).execute().actionGet()
List<String> ids = response.hits.hits.collect {
return it.id
}
client.close()
client = searchConnection.client
ids.each {
DeleteResponse delete = client.prepareDelete("item", "item", it)
.setOperationThreaded(false)
.execute().actionGet()
}
client.close()
Seems that it’s processing all deletions asynchronously, so I’ve added Thread.sleep(5000) after it. As you see i’m trying to open/close connection few times – it doesn’t help there.
The problem that sometimes it requires more time, sometimes it needs more that 5 seconds to delete, sometimes it can’t find just added data (from previous test), etc, etc. And most annoying that integration tests becomes unstable. Putting Thread.sleep() everywhere where it’s possible looks as not so good solution.
It there any way to commit last changes, or make an lock until all data will be written?
Found solution:
and
after putting new data into index.