-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TEST] Make sure we restart the global cluster after each test failure
CurrentTestFailedMarker is a RunListener that gets notified whenever a test fails, and we were using it to be able to restart the global cluster after each failure. We were checking whether a test had failed in the @after method though, which runs before the listener gets notified, so the failed flag would always be false. This commit makes sure that the global cluster gets restarted not only when there are problems in the afterInternal method, but also after each test failure. In order to achieve this, we need to reset the cluster afterwards, when we get to know about both of the events (problem in afterInternal and test failure), and before resetting the currentCluster. Introduced a TestRule that keeps track of test failures and allows to execute arbitrary tasks when a test fails and when a test is completed (regardless of its result). Allows also to force the execution of the failure task (used in case of afterInternal issues rather than actual test failure). Also updated ElasticsearchRestTests to make sure that the RestClient gets re-initialized in case we restart the global cluster, otherwise all the subsequent tests fail. Improved this mechanism also to relate it directly to the restart of the cluster instead of checking whether the addresses have changed, which doesn't work anyway as the new cluster will use the same addresses but the client needs to be recreated anyway. Closes #9015
- Loading branch information
Showing
7 changed files
with
155 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.test; | ||
|
||
import org.junit.rules.TestWatcher; | ||
import org.junit.runner.Description; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* A {@link org.junit.rules.TestRule} that detects test failures and allows to run an arbitrary task after a test failed. | ||
* Allows also to run an arbitrary task in any case, regardless of the test result. | ||
* It is possible to force running the first arbitrary task from the outside, as if the test was failed, when e.g. it needs | ||
* to be performed based on external events. | ||
* | ||
* We need it to be able to reset the suite level cluster after each failure, or if there is a problem | ||
* during the after test operations. | ||
*/ | ||
public class AfterTestRule extends TestWatcher { | ||
|
||
private final AtomicBoolean failed = new AtomicBoolean(false); | ||
|
||
private final Task task; | ||
|
||
public AfterTestRule(Task task) { | ||
this.task = task; | ||
} | ||
|
||
void forceFailure() { | ||
failed.set(true); | ||
} | ||
|
||
@Override | ||
protected void failed(Throwable e, Description description) { | ||
failed.set(true); | ||
} | ||
|
||
@Override | ||
protected void finished(Description description) { | ||
if (failed.compareAndSet(true, false)) { | ||
task.onTestFailed(); | ||
} | ||
task.onTestFinished(); | ||
} | ||
|
||
/** | ||
* Task to be executed after each test if required, no-op by default | ||
*/ | ||
public static class Task { | ||
/** | ||
* The task that needs to be executed after a test fails | ||
*/ | ||
void onTestFailed() { | ||
|
||
} | ||
|
||
/** | ||
* The task that needs to be executed when a test is completed, regardless of its result | ||
*/ | ||
void onTestFinished() { | ||
|
||
} | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
src/test/java/org/elasticsearch/test/CurrentTestFailedMarker.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters