From e71db0531eb763ce91a82e8c7f1820719cc7d86c Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Tue, 9 Apr 2019 19:40:01 +0200 Subject: [PATCH] Fix Race in AsyncTwoPhaseIndexerTests.testStateMachine (#40947) (#41013) * The step is incremented by the listner in `org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexerTests.MockIndexer#onFinish` after isFinished is set to true, but the test only waited for `isFinished`, fixed by calling `isFinished` last * Also made `step` volatile since we are reading it from different thread from the one incrementing it * Closes #40946 --- .../xpack/core/indexing/AsyncTwoPhaseIndexerTests.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java index 39a8807c2d5..f7f97288b23 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java @@ -39,7 +39,7 @@ public class AsyncTwoPhaseIndexerTests extends ESTestCase { private final CountDownLatch latch; // test the execution order - private int step; + private volatile int step; protected MockIndexer(Executor executor, AtomicReference initialState, Integer initialPosition, CountDownLatch latch) { @@ -113,8 +113,8 @@ public class AsyncTwoPhaseIndexerTests extends ESTestCase { protected void onFinish(ActionListener listener) { assertThat(step, equalTo(4)); ++step; - isFinished.set(true); listener.onResponse(null); + isFinished.set(true); } @Override @@ -206,8 +206,7 @@ public class AsyncTwoPhaseIndexerTests extends ESTestCase { } } - @AwaitsFix( bugUrl = "https://github.com/elastic/elasticsearch/issues/40946") - public void testStateMachine() throws InterruptedException { + public void testStateMachine() throws Exception { AtomicReference state = new AtomicReference<>(IndexerState.STOPPED); final ExecutorService executor = Executors.newFixedThreadPool(1); isFinished.set(false);