Skip to content

Commit

Permalink
Fix Race in AsyncTwoPhaseIndexerTests.testStateMachine (elastic#40947)
Browse files Browse the repository at this point in the history
* 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 elastic#40946
  • Loading branch information
original-brownbear authored and Gurkan Kaymak committed May 27, 2019
1 parent 4f6b4bb commit 5187f87
Showing 1 changed file with 3 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private class MockIndexer extends AsyncTwoPhaseIndexer<Integer, MockJobStats> {

private final CountDownLatch latch;
// test the execution order
private int step;
private volatile int step;

protected MockIndexer(Executor executor, AtomicReference<IndexerState> initialState, Integer initialPosition,
CountDownLatch latch) {
Expand Down Expand Up @@ -113,8 +113,8 @@ protected void onFailure(Exception exc) {
protected void onFinish(ActionListener<Void> listener) {
assertThat(step, equalTo(4));
++step;
isFinished.set(true);
listener.onResponse(null);
isFinished.set(true);
}

@Override
Expand Down Expand Up @@ -206,8 +206,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
}

@AwaitsFix( bugUrl = "https://github.com/elastic/elasticsearch/issues/40946")
public void testStateMachine() throws InterruptedException {
public void testStateMachine() throws Exception {
AtomicReference<IndexerState> state = new AtomicReference<>(IndexerState.STOPPED);
final ExecutorService executor = Executors.newFixedThreadPool(1);
isFinished.set(false);
Expand Down

0 comments on commit 5187f87

Please sign in to comment.