Test: use refresh and assertBusy
Original commit: elastic/x-pack-elasticsearch@44e0dab7ef
This commit is contained in:
parent
e0eecc5550
commit
619afe51af
|
@ -34,6 +34,7 @@ import java.util.concurrent.TimeUnit;
|
|||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
|
||||
/**
|
||||
|
@ -66,48 +67,37 @@ public class AlertThrottleTests extends AbstractAlertingTests {
|
|||
PutAlertResponse putAlertResponse = alertsClient.preparePutAlert().setAlertName("throttled-alert").setAlertSource(jsonBuilder.bytes()).get();
|
||||
assertTrue(putAlertResponse.indexResponse().isCreated());
|
||||
|
||||
|
||||
Thread.sleep(20000);
|
||||
|
||||
|
||||
AckAlertResponse ackResponse = alertsClient.prepareAckAlert("throttled-alert").get();
|
||||
assertEquals(AlertAckState.ACKED, ackResponse.getAlertAckState());
|
||||
|
||||
SearchResponse countResponse = client()
|
||||
refresh();
|
||||
SearchResponse searchResponse = client()
|
||||
.prepareSearch("action-index")
|
||||
.setTypes("action-type")
|
||||
.setSearchType(SearchType.COUNT)
|
||||
.setSource(searchSource().query(matchAllQuery()).buildAsBytes())
|
||||
.get();
|
||||
|
||||
Thread.sleep(10000); //Let any currently running stop
|
||||
|
||||
long countAfterAck = countResponse.getHits().getTotalHits();
|
||||
long countAfterAck = searchResponse.getHits().getTotalHits();
|
||||
assertThat(countAfterAck, greaterThanOrEqualTo((long) 1));
|
||||
|
||||
Thread.sleep(20000);
|
||||
|
||||
countResponse = client()
|
||||
refresh();
|
||||
searchResponse = client()
|
||||
.prepareSearch("action-index")
|
||||
.setTypes("action-type")
|
||||
.setSearchType(SearchType.COUNT)
|
||||
.setSource(searchSource().query(matchAllQuery()).buildAsBytes())
|
||||
.get();
|
||||
long countAfterSleep = countResponse.getHits().getTotalHits();
|
||||
|
||||
assertThat("There shouldn't be more entries in the index after we ack the alert",
|
||||
countAfterAck,
|
||||
equalTo(countAfterSleep));
|
||||
|
||||
long countAfterSleep = searchResponse.getHits().getTotalHits();
|
||||
assertThat("There shouldn't be more entries in the index after we ack the alert", countAfterAck, equalTo(countAfterSleep));
|
||||
|
||||
//Now delete the event and the ack state should change to NOT_TRIGGERED
|
||||
|
||||
DeleteResponse response = client().prepareDelete("test-index", "test-type", dummyEventIndexResponse.getId()).get();
|
||||
assertTrue(response.isFound());
|
||||
|
||||
Thread.sleep(20000);
|
||||
|
||||
GetAlertResponse getAlertResponse = alertsClient.prepareGetAlert("throttled-alert").get();
|
||||
|
||||
assertTrue(getAlertResponse.getResponse().isExists());
|
||||
|
||||
final AlertsStore alertsStore =
|
||||
|
@ -115,14 +105,12 @@ public class AlertThrottleTests extends AbstractAlertingTests {
|
|||
|
||||
Alert parsedAlert = alertsStore.parseAlert(getAlertResponse.getResponse().getId(),
|
||||
getAlertResponse.getResponse().getSourceAsBytesRef());
|
||||
|
||||
assertThat(parsedAlert.getAckState(), equalTo(AlertAckState.NOT_TRIGGERED));
|
||||
|
||||
CountResponse countOfThrottledActions = client()
|
||||
.prepareCount(AlertActionManager.ALERT_HISTORY_INDEX)
|
||||
.setQuery(QueryBuilders.matchQuery(AlertActionManager.STATE, AlertActionState.THROTTLED.toString()))
|
||||
.get();
|
||||
|
||||
assertThat(countOfThrottledActions.getCount(), greaterThan(0L));
|
||||
}
|
||||
|
||||
|
@ -154,42 +142,47 @@ public class AlertThrottleTests extends AbstractAlertingTests {
|
|||
assertTrue(putAlertResponse.indexResponse().isCreated());
|
||||
|
||||
Thread.sleep(5*1000);
|
||||
assertBusy(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
refresh();
|
||||
CountResponse countResponse = client()
|
||||
.prepareCount("action-index")
|
||||
.setTypes("action-type")
|
||||
.setSource(searchSource().query(matchAllQuery()).buildAsBytes())
|
||||
.get();
|
||||
|
||||
refresh();
|
||||
if (countResponse.getCount() != 1){
|
||||
SearchResponse actionResponse = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
|
||||
.setQuery(matchAllQuery())
|
||||
.get();
|
||||
for (SearchHit hit : actionResponse.getHits()) {
|
||||
logger.info("Got action hit [{}]", hit.getSourceRef().toUtf8());
|
||||
}
|
||||
}
|
||||
|
||||
CountResponse countResponse = client()
|
||||
.prepareCount("action-index")
|
||||
.setTypes("action-type")
|
||||
.setSource(searchSource().query(matchAllQuery()).buildAsBytes())
|
||||
.get();
|
||||
|
||||
if (countResponse.getCount() != 1){
|
||||
SearchResponse actionResponse = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
|
||||
.setQuery(matchAllQuery())
|
||||
.get();
|
||||
for (SearchHit hit : actionResponse.getHits()) {
|
||||
logger.info("Got action hit [{}]", hit.getSourceRef().toUtf8());
|
||||
assertThat(countResponse.getCount(), equalTo(1L));
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(countResponse.getCount(), equalTo(1L));
|
||||
});
|
||||
|
||||
Thread.sleep(15*1000);
|
||||
|
||||
countResponse = client()
|
||||
.prepareCount("action-index")
|
||||
.setTypes("action-type")
|
||||
.setSource(searchSource().query(matchAllQuery()).buildAsBytes())
|
||||
.get();
|
||||
refresh();
|
||||
|
||||
assertThat(countResponse.getCount(), equalTo(2L));
|
||||
assertBusy(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
refresh();
|
||||
CountResponse countResponse = client()
|
||||
.prepareCount("action-index")
|
||||
.setTypes("action-type")
|
||||
.setSource(searchSource().query(matchAllQuery()).buildAsBytes())
|
||||
.get();
|
||||
assertThat(countResponse.getCount(), equalTo(2L));
|
||||
}
|
||||
});
|
||||
|
||||
CountResponse countOfThrottledActions = client()
|
||||
.prepareCount(AlertActionManager.ALERT_HISTORY_INDEX)
|
||||
.setQuery(QueryBuilders.matchQuery(AlertActionManager.STATE, AlertActionState.THROTTLED.toString()))
|
||||
.get();
|
||||
|
||||
assertThat(countOfThrottledActions.getCount(), greaterThan(0L));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue