|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.action; |
| 21 | + |
| 22 | +import org.elasticsearch.test.ESTestCase; |
| 23 | +import org.elasticsearch.threadpool.TestThreadPool; |
| 24 | +import org.elasticsearch.threadpool.ThreadPool; |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Before; |
| 27 | + |
| 28 | +import java.util.concurrent.CountDownLatch; |
| 29 | +import java.util.concurrent.atomic.AtomicInteger; |
| 30 | +import java.util.function.Consumer; |
| 31 | + |
| 32 | +import static org.hamcrest.Matchers.equalTo; |
| 33 | + |
| 34 | +public class StepListenerTests extends ESTestCase { |
| 35 | + private ThreadPool threadPool; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setUpThreadPool() { |
| 39 | + threadPool = new TestThreadPool(getTestName()); |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + public void tearDownThreadPool() { |
| 44 | + terminate(threadPool); |
| 45 | + } |
| 46 | + |
| 47 | + public void testSimpleSteps() throws Exception { |
| 48 | + CountDownLatch latch = new CountDownLatch(1); |
| 49 | + Consumer<Exception> onFailure = e -> { |
| 50 | + latch.countDown(); |
| 51 | + fail("test a happy path"); |
| 52 | + }; |
| 53 | + |
| 54 | + StepListener<String> step1 = new StepListener<>(); //[a]sync provide a string |
| 55 | + executeAction(() -> step1.onResponse("hello")); |
| 56 | + StepListener<Integer> step2 = new StepListener<>(); //[a]sync calculate the length of the string |
| 57 | + step1.whenComplete(str -> executeAction(() -> step2.onResponse(str.length())), onFailure); |
| 58 | + step2.whenComplete(length -> executeAction(latch::countDown), onFailure); |
| 59 | + latch.await(); |
| 60 | + assertThat(step1.result(), equalTo("hello")); |
| 61 | + assertThat(step2.result(), equalTo(5)); |
| 62 | + } |
| 63 | + |
| 64 | + public void testAbortOnFailure() throws Exception { |
| 65 | + CountDownLatch latch = new CountDownLatch(1); |
| 66 | + int failedStep = randomBoolean() ? 1 : 2; |
| 67 | + AtomicInteger failureNotified = new AtomicInteger(); |
| 68 | + Consumer<Exception> onFailure = e -> { |
| 69 | + failureNotified.getAndIncrement(); |
| 70 | + latch.countDown(); |
| 71 | + assertThat(e.getMessage(), equalTo("failed at step " + failedStep)); |
| 72 | + }; |
| 73 | + |
| 74 | + StepListener<String> step1 = new StepListener<>(); //[a]sync provide a string |
| 75 | + if (failedStep == 1) { |
| 76 | + executeAction(() -> step1.onFailure(new RuntimeException("failed at step 1"))); |
| 77 | + } else { |
| 78 | + executeAction(() -> step1.onResponse("hello")); |
| 79 | + } |
| 80 | + |
| 81 | + StepListener<Integer> step2 = new StepListener<>(); //[a]sync calculate the length of the string |
| 82 | + step1.whenComplete(str -> { |
| 83 | + if (failedStep == 2) { |
| 84 | + executeAction(() -> step2.onFailure(new RuntimeException("failed at step 2"))); |
| 85 | + } else { |
| 86 | + executeAction(() -> step2.onResponse(str.length())); |
| 87 | + } |
| 88 | + }, onFailure); |
| 89 | + |
| 90 | + step2.whenComplete(length -> latch.countDown(), onFailure); |
| 91 | + latch.await(); |
| 92 | + assertThat(failureNotified.get(), equalTo(1)); |
| 93 | + |
| 94 | + if (failedStep == 1) { |
| 95 | + assertThat(expectThrows(RuntimeException.class, step1::result).getMessage(), |
| 96 | + equalTo("failed at step 1")); |
| 97 | + assertThat(expectThrows(RuntimeException.class, step2::result).getMessage(), |
| 98 | + equalTo("step is not completed yet")); |
| 99 | + } else { |
| 100 | + assertThat(step1.result(), equalTo("hello")); |
| 101 | + assertThat(expectThrows(RuntimeException.class, step2::result).getMessage(), |
| 102 | + equalTo("failed at step 2")); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void executeAction(Runnable runnable) { |
| 107 | + if (randomBoolean()) { |
| 108 | + threadPool.generic().execute(runnable); |
| 109 | + } else { |
| 110 | + runnable.run(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments