Skip to content

Commit dbb0729

Browse files
bm1549devflow.devflow-routing-intake
andauthored
Fix flaky cron trigger test in SpringSchedulingTest (#10792)
Fix flaky cron trigger test by closing context before assertions The "schedule trigger test according to cron expression" test was flaky because the cron expression (every 5 seconds) could fire a second time between blockUntilExecute() returning and the assertTraces() call completing, producing 3 or 4 traces instead of the expected 2. Fix by closing the Spring application context immediately after the first execution completes, before entering the expect block. The ScheduledTasksEndpoint bean reference is captured before closing so the endpoint assertion in the and: block still works. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Fix ConditionNotSatisfiedError by capturing cron tasks before context close The scheduledTaskEndpoint.scheduledTasks() call fails after context.close() because the endpoint is no longer available. Capture the cron tasks list in setup: before closing the context so the and: assertion block uses the pre-captured value. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Add deterministic reproduction delay to Spring scheduling cron test After closing the Spring context (which stops the scheduler), add a sleep longer than the cron interval to prove that no extra traces appear even under worst-case timing. This makes the previously-flaky race condition fully deterministic: without context.close(), the sleep guarantees extra cron executions; with it, the scheduler is stopped and assertions pass. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Update dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/latestDepTest/groovy/SpringSchedulingTest.groovy Update dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy Merge branch 'master' into brian.marks/fix-spring-scheduling-flaky-test Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent 2604968 commit dbb0729

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/latestDepTest/groovy/SpringSchedulingTest.groovy

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ class SpringSchedulingTest extends InstrumentationSpecification {
2525
setup:
2626
def context = new AnnotationConfigApplicationContext(TriggerTaskConfig, SchedulingConfig)
2727
def task = context.getBean(TriggerTask)
28+
def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint)
2829

2930
task.blockUntilExecute()
31+
// Capture cron tasks before closing the context (endpoint is unavailable after close).
32+
def cronTasks = scheduledTaskEndpoint.scheduledTasks().getCron()
33+
// Close the context immediately after the first execution to prevent a second cron
34+
// firing before assertions complete, which would produce extra traces and cause flakiness.
35+
context.close()
3036

3137
expect:
3238
assert task != null
@@ -54,13 +60,10 @@ class SpringSchedulingTest extends InstrumentationSpecification {
5460
}
5561
}
5662
and:
57-
def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint)
5863
assert scheduledTaskEndpoint != null
59-
scheduledTaskEndpoint.scheduledTasks().getCron().each {
64+
cronTasks.each {
6065
it.getRunnable().getTarget() == TriggerTask.getName()
6166
}
62-
cleanup:
63-
context.close()
6467
}
6568

6669
def "schedule interval test"() {

dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ class SpringSchedulingTest extends InstrumentationSpecification {
2525
setup:
2626
def context = new AnnotationConfigApplicationContext(TriggerTaskConfig, SchedulingConfig)
2727
def task = context.getBean(TriggerTask)
28+
def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint)
2829

2930
task.blockUntilExecute()
31+
// Capture cron tasks before closing the context (endpoint is unavailable after close).
32+
def cronTasks = scheduledTaskEndpoint.scheduledTasks().getCron()
33+
// Close the context immediately after the first execution to prevent a second cron
34+
// firing before assertions complete, which would produce extra traces and cause flakiness.
35+
context.close()
3036

3137
expect:
3238
assert task != null
@@ -54,13 +60,10 @@ class SpringSchedulingTest extends InstrumentationSpecification {
5460
}
5561
}
5662
and:
57-
def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint)
5863
assert scheduledTaskEndpoint != null
59-
scheduledTaskEndpoint.scheduledTasks().getCron().each {
64+
cronTasks.each {
6065
it.getRunnable().getTarget() == TriggerTask.getName()
6166
}
62-
cleanup:
63-
context.close()
6467
}
6568

6669
def "schedule interval test"() {

0 commit comments

Comments
 (0)