diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/MonitorRunnerServiceIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/MonitorRunnerServiceIT.kt index 63b4d8286..b2b72f26d 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/MonitorRunnerServiceIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/MonitorRunnerServiceIT.kt @@ -47,6 +47,7 @@ import org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder import org.opensearch.search.aggregations.metrics.CardinalityAggregationBuilder import org.opensearch.search.aggregations.support.MultiTermsValuesSourceConfig import org.opensearch.search.builder.SearchSourceBuilder +import org.opensearch.test.OpenSearchTestCase import java.net.URLEncoder import java.time.Instant import java.time.ZonedDateTime @@ -138,7 +139,7 @@ class MonitorRunnerServiceIT : AlertingRestTestCase() { verifyAlert(firstRunAlert, monitor) // Runner uses ThreadPool.CachedTimeThread thread which only updates once every 200 ms. Wait a bit to // see lastNotificationTime change. - Thread.sleep(200) + OpenSearchTestCase.waitUntil(200) executeMonitor(monitor.id) val secondRunAlert = searchAlerts(monitor).single() verifyAlert(secondRunAlert, monitor) @@ -265,7 +266,7 @@ class MonitorRunnerServiceIT : AlertingRestTestCase() { // Runner uses ThreadPool.CachedTimeThread thread which only updates once every 200 ms. Wait a bit to // let lastNotificationTime change. W/o this sleep the test can result in a false negative. - Thread.sleep(200) + OpenSearchTestCase.waitUntil(200) val response = executeMonitor(monitor.id) val output = entityAsMap(response) @@ -765,7 +766,7 @@ class MonitorRunnerServiceIT : AlertingRestTestCase() { verifyAlert(activeAlert1.single(), monitor, ACTIVE) val actionResults1 = verifyActionExecutionResultInAlert(activeAlert1[0], mutableMapOf(Pair(actionThrottleEnabled.id, 0))) - Thread.sleep(200) + OpenSearchTestCase.waitUntil(200) updateMonitor(monitor.copy(triggers = listOf(trigger.copy(condition = NEVER_RUN)), id = monitor.id)) executeMonitor(monitor.id) val completedAlert = searchAlerts(monitor, AlertIndices.ALL_ALERT_INDEX_PATTERN).single() @@ -1398,7 +1399,7 @@ class MonitorRunnerServiceIT : AlertingRestTestCase() { // Runner uses ThreadPool.CachedTimeThread thread which only updates once every 200 ms. Wait a bit to // let lastNotificationTime change. W/o this sleep the test can result in a false negative. - Thread.sleep(200) + OpenSearchTestCase.waitUntil(200) executeMonitor(monitor.id) // Check that the lastNotification time of the acknowledged Alert wasn't updated and the active Alert's was @@ -1418,7 +1419,7 @@ class MonitorRunnerServiceIT : AlertingRestTestCase() { ) // Execute Monitor and check that both Alerts were updated - Thread.sleep(200) + OpenSearchTestCase.waitUntil(200) executeMonitor(monitor.id) currentAlerts = searchAlerts(monitor, AlertIndices.ALL_ALERT_INDEX_PATTERN) val completedAlerts = currentAlerts.filter { it.state == COMPLETED } @@ -1940,7 +1941,7 @@ class MonitorRunnerServiceIT : AlertingRestTestCase() { // Runner uses ThreadPool.CachedTimeThread thread which only updates once every 200 ms. Wait a bit to // let Action executionTime change. W/o this sleep the test can result in a false negative. - Thread.sleep(200) + OpenSearchTestCase.waitUntil(200) val monitorRunResultThrottled = entityAsMap(executeMonitor(monitor.id)) verifyActionThrottleResultsForBucketLevelMonitor( monitorRunResult = monitorRunResultThrottled, diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/alerts/AlertIndicesIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/alerts/AlertIndicesIT.kt index 948801296..b54cb86da 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/alerts/AlertIndicesIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/alerts/AlertIndicesIT.kt @@ -140,7 +140,7 @@ class AlertIndicesIT : AlertingRestTestCase() { executeMonitor(trueMonitor) // Allow for a rollover index. - Thread.sleep(2000) + OpenSearchTestCase.waitUntil(2000) assertTrue("Did not find 3 alert indices", getAlertIndices().size >= 3) } @@ -157,7 +157,7 @@ class AlertIndicesIT : AlertingRestTestCase() { executeMonitor(trueMonitor.id) // Allow for a rollover index. - Thread.sleep(2000) + OpenSearchTestCase.waitUntil(2000) assertTrue("Did not find 2 alert indices", getFindingIndices().size >= 2) } diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/bwc/AlertingBackwardsCompatibilityIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/bwc/AlertingBackwardsCompatibilityIT.kt index 68abd0548..69d496314 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/bwc/AlertingBackwardsCompatibilityIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/bwc/AlertingBackwardsCompatibilityIT.kt @@ -16,6 +16,7 @@ import org.opensearch.commons.alerting.model.Monitor import org.opensearch.core.rest.RestStatus import org.opensearch.index.query.QueryBuilders import org.opensearch.search.builder.SearchSourceBuilder +import org.opensearch.test.OpenSearchTestCase class AlertingBackwardsCompatibilityIT : AlertingRestTestCase() { @@ -69,7 +70,7 @@ class AlertingBackwardsCompatibilityIT : AlertingRestTestCase() { // the test execution by a lot (might have to wait for Job Scheduler plugin integration first) // Waiting a minute to ensure the Monitor ran again at least once before checking if the job is running // on time - Thread.sleep(60000) + OpenSearchTestCase.waitUntil(60000) verifyMonitorStats("/_plugins/_alerting") } } diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt index 000364bf0..c67e75abe 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt @@ -811,7 +811,7 @@ class MonitorRestApiIT : AlertingRestTestCase() { assertEquals("Delete request not successful", RestStatus.OK, deleteResponse.restStatus()) // Wait 5 seconds for event to be processed and alerts moved - Thread.sleep(5000) + OpenSearchTestCase.waitUntil(5000) val alerts = searchAlerts(monitor) assertEquals("Active alert was not deleted", 0, alerts.size) @@ -842,7 +842,7 @@ class MonitorRestApiIT : AlertingRestTestCase() { assertEquals("Update request not successful", RestStatus.OK, updateResponse.restStatus()) // Wait 5 seconds for event to be processed and alerts moved - Thread.sleep(5000) + OpenSearchTestCase.waitUntil(5000) val alerts = searchAlerts(monitor) assertEquals("Active alert was not deleted", 0, alerts.size) @@ -870,7 +870,7 @@ class MonitorRestApiIT : AlertingRestTestCase() { assertEquals("Update request not successful", RestStatus.OK, updateResponse.restStatus()) // Wait 5 seconds for event to be processed and alerts moved - Thread.sleep(5000) + OpenSearchTestCase.waitUntil(5000) val alerts = searchAlerts(monitor) assertEquals("Active alert was not deleted", 0, alerts.size) @@ -959,7 +959,7 @@ class MonitorRestApiIT : AlertingRestTestCase() { enableScheduledJob() val monitorId = createMonitor(randomQueryLevelMonitor(enabled = true), refresh = true).id - if (isMultiNode) Thread.sleep(2000) + if (isMultiNode) OpenSearchTestCase.waitUntil(2000) var alertingStats = getAlertingStats() assertAlertingStatsSweeperEnabled(alertingStats, true) assertEquals("Scheduled job index does not exist", true, alertingStats["scheduled_job_index_exists"]) @@ -992,7 +992,7 @@ class MonitorRestApiIT : AlertingRestTestCase() { enableScheduledJob() // Sleep briefly so sweep can reschedule the Monitor - Thread.sleep(2000) + OpenSearchTestCase.waitUntil(2000) alertingStats = getAlertingStats() assertAlertingStatsSweeperEnabled(alertingStats, true) @@ -1018,7 +1018,7 @@ class MonitorRestApiIT : AlertingRestTestCase() { enableScheduledJob() createRandomMonitor(refresh = true) - if (isMultiNode) Thread.sleep(2000) + if (isMultiNode) OpenSearchTestCase.waitUntil(2000) val responseMap = getAlertingStats() assertAlertingStatsSweeperEnabled(responseMap, true) assertEquals("Scheduled job index does not exist", true, responseMap["scheduled_job_index_exists"]) @@ -1051,7 +1051,7 @@ class MonitorRestApiIT : AlertingRestTestCase() { enableScheduledJob() createRandomMonitor(refresh = true) - if (isMultiNode) Thread.sleep(2000) + if (isMultiNode) OpenSearchTestCase.waitUntil(2000) val responseMap = getAlertingStats("/jobs_info") assertAlertingStatsSweeperEnabled(responseMap, true) assertEquals("Scheduled job index does not exist", true, responseMap["scheduled_job_index_exists"]) diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationMigrationUtilServiceIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationMigrationUtilServiceIT.kt index f9c40e465..7b76ba59a 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationMigrationUtilServiceIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationMigrationUtilServiceIT.kt @@ -18,6 +18,7 @@ import org.opensearch.alerting.util.DestinationType import org.opensearch.client.ResponseException import org.opensearch.commons.alerting.model.ScheduledJob.Companion.SCHEDULED_JOBS_INDEX import org.opensearch.core.rest.RestStatus +import org.opensearch.test.OpenSearchTestCase import java.time.Instant import java.util.UUID @@ -80,7 +81,7 @@ class DestinationMigrationUtilServiceIT : AlertingRestTestCase() { // Create cluster change event and wait for migration service to complete migrating data over client().updateSettings("indices.recovery.max_bytes_per_sec", "40mb") - Thread.sleep(120000) + OpenSearchTestCase.waitUntil(120000) for (id in ids) { val response = client().makeRequest(