# BulkIngester and Integration Testing

**URL:** https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [August 24, 2023, 5:14pm UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595 "2023-08-24T17:14:07Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)
#### Post date: [August 24, 2023, 5:14pm UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595/1 "2023-08-24T17:14:07Z")

</div>

Can someone point me to some examples of integration testing while using the BulkIngester? Specifically how to test a class method that uses the BulkIngester. I'm having timing issues with calling flush on the BulkIngester so that the test fails randomly. Thanks.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [August 24, 2023, 6:28pm UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595/2 "2023-08-24T18:28:08Z")

</div>

This might help:

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/be89f7cc7530cebfaeadc7c5e9bf109c71ee4caa/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java#L338>

---

<div class="post-metadata">

### Author: ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)
#### Post date: [August 24, 2023, 6:44pm UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595/3 "2023-08-24T18:44:21Z")

</div>

Thanks. I looked at that already. I can't call .close() on the BulkIngester or it causes other tests to fail. I have many classes that encapsulate the BulkIngester and add operations to it. I'm trying to test those classes. For example, trying to test method1 below is problematic. Any recommendations?

```auto
public void method1() {
    BulkIngester.instance().add( // operation);
}

```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [August 24, 2023, 7:57pm UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595/4 "2023-08-24T19:57:22Z")

</div>

What I do in that case is to wait up for 30 seconds to have the expected number of documents.

See

> <https://github.com/dadoonet/fscrawler/blob/76991b5a8791708f8075d1f4009877dab8abf55b/integration-tests/src/test/java/fr/pilato/elasticsearch/crawler/fs/test/integration/AbstractITCase.java#L366>

---

<div class="post-metadata">

### Author: ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)
#### Post date: [August 25, 2023, 1:04pm UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595/5 "2023-08-25T13:04:42Z")

</div>

Thanks.

---

<div class="post-metadata">

### Author: ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)
#### Post date: [September 12, 2023, 6:41pm UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595/6 "2023-09-12T18:41:04Z")

</div>

When I create an index in a integration test and then try to use it I get random failures. I tried something like this and it seemed to work at first but then starting failing.

```auto
    protected boolean waitForIndexToBeAvailable(String index) throws IOException {
        HealthResponse healthResponse = elasticsearchClient.cluster().health(hr -> hr
            .index(index)
            .waitForStatus(HealthStatus.Yellow)
            .waitForActiveShards(w -> w
                .count(1))
            .timeout(t -> t
                .time("10s")));

        return !healthResponse.timedOut();
    }

```

This is really frustrating. Any advice?

Thanks.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [September 15, 2023, 4:49am UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595/7 "2023-09-15T04:49:27Z")

</div>

What are the failures?

---

<div class="post-metadata">

### Author: ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)
#### Post date: [September 15, 2023, 12:29pm UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595/8 "2023-09-15T12:29:29Z")

</div>

Thanks. The test passes sometimes and fails other times. Usually the failure is on the first assertion (assertEquals). It's random. Seems to be a timing issue. The SUT creates two indices so in my test I have assertions that both indices exist. Then in the test I run two searches to verify the count of docs in each of the two indices. I'm using the ElasticsearchClient (not async client) and the test and SUT is single threaded. Here's the test:

```auto
	@Test
	public void testRunRetention() throws IOException {
		HistoryRetentionTask task = new HistoryRetentionTask(systemIds);
		task.runRetention();

        // Need to flush the bulk ingester now since it was used in runRetention
        bulkIngester.flush();

        if (!waitForIndexToBeAvailable("test-" + dateSuffix)) {
            fail("Index not available");
        }

        if (!waitForIndexToBeAvailable("test1-" + dateSuffix)) {
            fail("Index not available");
        }

        // Refresh so we can search for docs
        refresh("test-" + dateSuffix);
        refresh("test1-" + dateSuffix);

		// Check correct number of results in each system id index
        assertEquals(3, getHitsTotal("test-" + dateSuffix));
        assertEquals(1, getHitsTotal("test1-" + dateSuffix));
	}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 13, 2023, 12:30pm UTC](https://discuss.elastic.co/t/bulkingester-and-integration-testing/341595/9 "2023-10-13T12:30:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
