Skip to content

Commit 167b01b

Browse files
manaswini1920Manaswini Ragamouni
andauthored
Replace Thread.sleep with OpenSearchTestCase.waitUntil in tests (opensearch-project#2041)
Replace all remaining Thread.sleep calls in integration tests with OpenSearchTestCase.waitUntil for more reliable test execution. Resolves opensearch-project#1028 Signed-off-by: Manaswini Ragamouni <ragamanu@amazon.com> Co-authored-by: Manaswini Ragamouni <ragamanu@amazon.com>
1 parent 665cd28 commit 167b01b

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

alerting/src/test/kotlin/org/opensearch/alerting/DocLeveFanOutIT.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import org.opensearch.commons.alerting.model.DocLevelMonitorInput
1212
import org.opensearch.commons.alerting.model.DocLevelQuery
1313
import org.opensearch.commons.alerting.model.action.ActionExecutionPolicy
1414
import org.opensearch.commons.alerting.model.action.PerExecutionActionScope
15+
import org.opensearch.test.OpenSearchTestCase
1516
import java.time.ZonedDateTime
1617
import java.time.format.DateTimeFormatter
1718
import java.time.temporal.ChronoUnit.MILLIS
19+
import java.util.concurrent.TimeUnit
1820

1921
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
2022
class DocLeveFanOutIT : AlertingRestTestCase() {
@@ -64,7 +66,9 @@ class DocLeveFanOutIT : AlertingRestTestCase() {
6466
assertEquals(findingsSize1, 2)
6567
adminClient().updateSettings(AlertingSettings.DOC_LEVEL_MONITOR_EXECUTION_MAX_DURATION.key, TimeValue.timeValueNanos(1))
6668
executeMonitor(monitor.id)
67-
Thread.sleep(1000)
69+
OpenSearchTestCase.waitUntil({
70+
return@waitUntil true
71+
}, 2, TimeUnit.SECONDS)
6872
adminClient().updateSettings(AlertingSettings.DOC_LEVEL_MONITOR_EXECUTION_MAX_DURATION.key, TimeValue.timeValueMinutes(4))
6973
indexDoc(testIndex, "3", testDoc)
7074
indexDoc(testIndex, "4", testDoc)

alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
266266
updateSettings =
267267
adminClient().updateSettings(AlertingSettings.DOC_LEVEL_MONITOR_FANOUT_MAX_DURATION.key, TimeValue.timeValueMinutes(4))
268268
logger.info(updateSettings)
269-
Thread.sleep(1000)
269+
OpenSearchTestCase.waitUntil({
270+
return@waitUntil true
271+
}, 2, TimeUnit.SECONDS)
270272
response = executeMonitor(monitor.id)
271273
output = entityAsMap(response)
272274
assertEquals(monitor.name, output["monitor_name"])
@@ -597,10 +599,14 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
597599

598600
indexDoc(testIndex, "1", testDoc)
599601
indexDoc(testIndex, "5", testDoc)
600-
Thread.sleep(240000)
601602

602603
val inputMap = HashMap<String, Any>()
603604
inputMap["searchString"] = monitor.name
605+
OpenSearchTestCase.waitUntil({
606+
val responseMap = getAlerts(inputMap).asMap()
607+
val alerts = (responseMap["alerts"] as ArrayList<Map<String, Any>>)
608+
return@waitUntil alerts.isNotEmpty()
609+
}, 5, TimeUnit.MINUTES)
604610

605611
val responseMap = getAlerts(inputMap).asMap()
606612
val alerts = (responseMap["alerts"] as ArrayList<Map<String, Any>>)

alerting/src/test/kotlin/org/opensearch/alerting/MonitorRunnerServiceIT.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,9 @@ class MonitorRunnerServiceIT : AlertingRestTestCase() {
12671267

12681268
fun `test execute bucket-level monitor with alias optimization - indices skipped from query`() {
12691269
val skipIndex = createTestIndex("to_skip_index")
1270-
Thread.sleep(10000)
1270+
OpenSearchTestCase.waitUntil({
1271+
return@waitUntil true
1272+
}, 10, TimeUnit.SECONDS)
12711273
val previousIndex = createTestIndex("to_include_index")
12721274
insertSampleTimeSerializedDataWithTime(
12731275
previousIndex,
@@ -1287,7 +1289,9 @@ class MonitorRunnerServiceIT : AlertingRestTestCase() {
12871289
),
12881290
ZonedDateTime.now().truncatedTo(ChronoUnit.MILLIS).plusSeconds(10)
12891291
)
1290-
Thread.sleep(10000)
1292+
OpenSearchTestCase.waitUntil({
1293+
return@waitUntil true
1294+
}, 10, TimeUnit.SECONDS)
12911295
val indexMapping = """
12921296
"properties" : {
12931297
"test_strict_date_time" : { "type" : "date", "format" : "strict_date_time" },

alerting/src/test/kotlin/org/opensearch/alerting/resthandler/WorkflowRestApiIT.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,11 @@ class WorkflowRestApiIT : AlertingRestTestCase() {
12261226

12271227
indexDoc(testIndex, "1", testDoc)
12281228
indexDoc(testIndex, "5", testDoc)
1229-
Thread.sleep(240000)
1229+
1230+
OpenSearchTestCase.waitUntil({
1231+
val alerts = searchAlerts(monitor)
1232+
return@waitUntil alerts.isNotEmpty()
1233+
}, 5, TimeUnit.MINUTES)
12301234

12311235
val alerts = searchAlerts(monitor)
12321236
alerts.forEach {

0 commit comments

Comments
 (0)