|
19 | 19 |
|
20 | 20 | package org.elasticsearch.action.ingest;
|
21 | 21 |
|
| 22 | +import org.elasticsearch.action.ActionListener; |
| 23 | +import org.elasticsearch.index.VersionType; |
| 24 | +import org.elasticsearch.ingest.AbstractProcessor; |
22 | 25 | import org.elasticsearch.ingest.CompoundProcessor;
|
23 | 26 | import org.elasticsearch.ingest.DropProcessor;
|
24 | 27 | import org.elasticsearch.ingest.IngestDocument;
|
|
29 | 32 | import org.elasticsearch.ingest.TestProcessor;
|
30 | 33 | import org.elasticsearch.test.ESTestCase;
|
31 | 34 | import org.elasticsearch.threadpool.TestThreadPool;
|
32 |
| -import org.elasticsearch.threadpool.TestThreadPool; |
| 35 | +import org.elasticsearch.threadpool.ThreadPool; |
33 | 36 | import org.junit.After;
|
34 | 37 | import org.junit.Before;
|
35 | 38 |
|
| 39 | +import java.util.ArrayList; |
36 | 40 | import java.util.Collections;
|
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.List; |
37 | 43 | import java.util.Map;
|
38 | 44 | import java.util.concurrent.CountDownLatch;
|
| 45 | +import java.util.concurrent.TimeUnit; |
39 | 46 | import java.util.concurrent.atomic.AtomicReference;
|
| 47 | +import java.util.function.BiConsumer; |
40 | 48 |
|
41 | 49 | import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
|
42 | 50 | import static org.hamcrest.Matchers.equalTo;
|
43 | 51 | import static org.hamcrest.Matchers.instanceOf;
|
| 52 | +import static org.hamcrest.Matchers.is; |
44 | 53 | import static org.hamcrest.Matchers.not;
|
45 | 54 | import static org.hamcrest.Matchers.notNullValue;
|
46 | 55 | import static org.hamcrest.Matchers.nullValue;
|
@@ -331,4 +340,56 @@ public void testDropDocumentVerboseExtraProcessor() throws Exception {
|
331 | 340 | assertThat(verboseResult.getProcessorResults().get(1).getFailure(), nullValue());
|
332 | 341 | }
|
333 | 342 |
|
| 343 | + public void testAsyncSimulation() throws Exception { |
| 344 | + int numDocs = randomIntBetween(1, 64); |
| 345 | + List<IngestDocument> documents = new ArrayList<>(numDocs); |
| 346 | + for (int id = 0; id < numDocs; id++) { |
| 347 | + documents.add(new IngestDocument("_index", "_type", Integer.toString(id), null, 0L, VersionType.INTERNAL, new HashMap<>())); |
| 348 | + } |
| 349 | + Processor processor1 = new AbstractProcessor(null) { |
| 350 | + |
| 351 | + @Override |
| 352 | + public void execute(IngestDocument ingestDocument, BiConsumer<IngestDocument, Exception> handler) { |
| 353 | + threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { |
| 354 | + ingestDocument.setFieldValue("processed", true); |
| 355 | + handler.accept(ingestDocument, null); |
| 356 | + }); |
| 357 | + } |
| 358 | + |
| 359 | + @Override |
| 360 | + public IngestDocument execute(IngestDocument ingestDocument) throws Exception { |
| 361 | + throw new UnsupportedOperationException(); |
| 362 | + } |
| 363 | + |
| 364 | + @Override |
| 365 | + public String getType() { |
| 366 | + return "none-of-your-business"; |
| 367 | + } |
| 368 | + }; |
| 369 | + Pipeline pipeline = new Pipeline("_id", "_description", version, new CompoundProcessor(processor1)); |
| 370 | + SimulatePipelineRequest.Parsed request = new SimulatePipelineRequest.Parsed(pipeline, documents, false); |
| 371 | + |
| 372 | + AtomicReference<SimulatePipelineResponse> responseHolder = new AtomicReference<>(); |
| 373 | + AtomicReference<Exception> errorHolder = new AtomicReference<>(); |
| 374 | + CountDownLatch latch = new CountDownLatch(1); |
| 375 | + executionService.execute(request, ActionListener.wrap(response -> { |
| 376 | + responseHolder.set(response); |
| 377 | + latch.countDown(); |
| 378 | + }, e -> { |
| 379 | + errorHolder.set(e); |
| 380 | + latch.countDown(); |
| 381 | + })); |
| 382 | + latch.await(1, TimeUnit.MINUTES); |
| 383 | + assertThat(errorHolder.get(), nullValue()); |
| 384 | + SimulatePipelineResponse response = responseHolder.get(); |
| 385 | + assertThat(response, notNullValue()); |
| 386 | + assertThat(response.getResults().size(), equalTo(numDocs)); |
| 387 | + |
| 388 | + for (int id = 0; id < numDocs; id++) { |
| 389 | + SimulateDocumentBaseResult result = (SimulateDocumentBaseResult) response.getResults().get(id); |
| 390 | + assertThat(result.getIngestDocument().getMetadata().get(IngestDocument.MetaData.ID), equalTo(Integer.toString(id))); |
| 391 | + assertThat(result.getIngestDocument().getSourceAndMetadata().get("processed"), is(true)); |
| 392 | + } |
| 393 | + } |
| 394 | + |
334 | 395 | }
|
0 commit comments