Skip to content

Commit aa57bcf

Browse files
bm1549claude
andcommitted
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>
1 parent cc12228 commit aa57bcf

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ 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+
// Close the context immediately after the first execution to prevent a second cron
32+
// firing before assertions complete, which would produce extra traces and cause flakiness.
33+
context.close()
3034

3135
expect:
3236
assert task != null
@@ -54,13 +58,10 @@ class SpringSchedulingTest extends InstrumentationSpecification {
5458
}
5559
}
5660
and:
57-
def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint)
5861
assert scheduledTaskEndpoint != null
5962
scheduledTaskEndpoint.scheduledTasks().getCron().each {
6063
it.getRunnable().getTarget() == TriggerTask.getName()
6164
}
62-
cleanup:
63-
context.close()
6465
}
6566

6667
def "schedule interval test"() {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ 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+
// Close the context immediately after the first execution to prevent a second cron
32+
// firing before assertions complete, which would produce extra traces and cause flakiness.
33+
context.close()
3034

3135
expect:
3236
assert task != null
@@ -54,13 +58,10 @@ class SpringSchedulingTest extends InstrumentationSpecification {
5458
}
5559
}
5660
and:
57-
def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint)
5861
assert scheduledTaskEndpoint != null
5962
scheduledTaskEndpoint.scheduledTasks().getCron().each {
6063
it.getRunnable().getTarget() == TriggerTask.getName()
6164
}
62-
cleanup:
63-
context.close()
6465
}
6566

6667
def "schedule interval test"() {

0 commit comments

Comments
 (0)