-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix flaky tests for issue 19422 to remove Thread.sleep #20262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…it until the exported max value becomes stable using assertBusy. Signed-off-by: Joe Liu <[email protected]>
WalkthroughRefactored Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (1)
plugins/telemetry-otel/src/internalClusterTest/java/org/opensearch/telemetry/metrics/TelemetryMetricsEnabledSanityIT.java (1)
174-187: Stability check correctly applied, with same style note as testGauge.This stability check mirrors the implementation in
testGauge(lines 143-154) and correctly addresses the flaky test root cause. The same import inconsistency applies here—prefer importingAtomicReferencerather than using the fully-qualified name. See the earlier comment on lines 143-154 for the suggested refactor.
🧹 Nitpick comments (1)
plugins/telemetry-otel/src/internalClusterTest/java/org/opensearch/telemetry/metrics/TelemetryMetricsEnabledSanityIT.java (1)
143-154: Stability check logic is correct, but address import inconsistency.The stability detection approach correctly handles the race condition described in the PR: allowing in-flight gauge collections to complete after
close(), then verifying the value eventually stops changing. The use ofDouble.comparefor floating-point comparison is appropriate.However,
AtomicReferenceis used with a fully-qualified name whileAtomicIntegeris imported (line 27). For consistency, add an import forAtomicReference.Apply this diff to add the import at the top of the file:
import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier;Then simplify the declaration:
- final java.util.concurrent.atomic.AtomicReference<Double> lastSeen = new java.util.concurrent.atomic.AtomicReference<>( + final AtomicReference<Double> lastSeen = new AtomicReference<>( getMaxObservableGaugeValue(exporter, metricName) );Optional: Extract duplicated stability check logic.
The stability verification logic is duplicated in both
testGaugeandtestGaugeWithValueAndTagSupplier. Consider extracting it to a helper method like:private void assertGaugeStabilityAfterClose(InMemorySingletonMetricsExporter exporter, String metricName) { final AtomicReference<Double> lastSeen = new AtomicReference<>( getMaxObservableGaugeValue(exporter, metricName) ); assertBusy(() -> { double now = getMaxObservableGaugeValue(exporter, metricName); double prev = lastSeen.get(); if (Double.compare(prev, now) != 0) { lastSeen.set(now); fail("Gauge value changed after close (in-flight publish still running): prev=" + prev + ", now=" + now); } }); }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
plugins/telemetry-otel/src/internalClusterTest/java/org/opensearch/telemetry/metrics/TelemetryMetricsEnabledSanityIT.java(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
- GitHub Check: gradle-check
- GitHub Check: Analyze (java)
- GitHub Check: detect-breaking-change
- GitHub Check: assemble (25, ubuntu-24.04-arm)
- GitHub Check: assemble (25, windows-latest)
- GitHub Check: assemble (21, ubuntu-24.04-arm)
- GitHub Check: assemble (25, ubuntu-latest)
- GitHub Check: assemble (21, macos-15)
- GitHub Check: assemble (21, windows-latest)
- GitHub Check: assemble (21, ubuntu-latest)
🔇 Additional comments (4)
plugins/telemetry-otel/src/internalClusterTest/java/org/opensearch/telemetry/metrics/TelemetryMetricsEnabledSanityIT.java (4)
136-136: LGTM: Lambda simplification improves readability.The simplified lambda expression is more idiomatic and maintains identical behavior.
140-141: Good replacement of Thread.sleep with assertBusy.Using
assertBusyto wait for observable gauge values eliminates the timing-based flakiness of fixed sleeps. This approach correctly synchronizes on actual published data.
163-163: LGTM: Lambda simplification improves readability.Consistent with the simplification on line 136, this change improves readability while preserving behavior.
167-172: Excellent use of assertBusy for both value and attribute verification.The pre-close assertions now properly wait for observable conditions: both the gauge value reaching the threshold and the metric attributes containing the expected tag. This eliminates race conditions that could occur with fixed sleeps.
|
❌ Gradle check result for e77e425: FAILURE Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
| Thread.sleep(1200); | ||
| assertEquals(observableGaugeValueAfterStop, getMaxObservableGaugeValue(exporter, metricName), 0.0); | ||
|
|
||
| // After close, allow any in-flight collection to finish, but ensure the value eventually becomes stable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this going to wait for in-flight collections to finish? The code is going to invoke getMaxObservableGaugeValue(exporter, metricName) twice in very quick succession, and assuming it returns the same value both times it will then pass.
Description
Fix 2 flaky tests
org.opensearch.telemetry.metrics.TelemetryMetricsEnabledSanityIT.testGaugeandorg.opensearch.telemetry.metrics.TelemetryMetricsEnabledSanityIT.testGaugeWithValueAndTagSupplierwhere after gaugeCloseable.close(), there can still be an in-flight / already-scheduled collection that calls your valueProvider one more time, so the max jumps from 2.0 to 3.0 and the “no change” assertEquals() becomes flaky.Use assertBusy instead of Thread.sleep(2200)
Related Issues
Resolves #19422
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.