Reliably removing indices in embedded elasticsearch

I am experiencing some frustration when using the embedded Elasticsearch in integration tests. In the "before" of the test suite I run a createIndices and in the "after" I delete them; that randomly results in an IndexAlreadyExistsException.

I tried calling refresh and flush, I tried clearing the caches in Elasticsearch, I tried retrying many times with delete and create before giving up but the exception stays and there seems to be no way around it.

To make things even more puzzling I tried getting the indices from Elasticsearch right before creating the index and as a result I always get an empty list, even when then the create request fails. If I do the same after the create index succeedes there are the expected indices in the response.

I am running on ES 2.3.1 but I also tried using the client for 2.3.1 with the server 2.4.0; that resulted in exactly the same behaviour.

Any idea on what else could I try? Thanks!

For what it is worth embedding Elasticsearch really isn't supported. Ultimately you really should spin Elasticsearch up externally and test against it.

On the other hand I don't see what is causing the trouble here. I'd expect async things somewhere are part of it. It is super important that one test doesn't leak some asynchronous thing into the next test. And creating and deleting indexes is asynchronous. It'll be synch-when-possible in 5.0 but isn't in 2.x.

After a bit more experiments with this issue I found that increasing the limit for number of open file descriptors in the system solved the problem. Thanks anyway for the advice.

Weird! We're there any logs from elasticsearch or lucene about running out
of fds?