diff --git a/dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/latestDepTest/groovy/SpringSchedulingTest.groovy b/dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/latestDepTest/groovy/SpringSchedulingTest.groovy index 6e3b1760c11..f604744aa39 100644 --- a/dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/latestDepTest/groovy/SpringSchedulingTest.groovy +++ b/dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/latestDepTest/groovy/SpringSchedulingTest.groovy @@ -25,8 +25,14 @@ class SpringSchedulingTest extends InstrumentationSpecification { setup: def context = new AnnotationConfigApplicationContext(TriggerTaskConfig, SchedulingConfig) def task = context.getBean(TriggerTask) + def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint) task.blockUntilExecute() + // Capture cron tasks before closing the context (endpoint is unavailable after close). + def cronTasks = scheduledTaskEndpoint.scheduledTasks().getCron() + // Close the context immediately after the first execution to prevent a second cron + // firing before assertions complete, which would produce extra traces and cause flakiness. + context.close() expect: assert task != null @@ -54,13 +60,10 @@ class SpringSchedulingTest extends InstrumentationSpecification { } } and: - def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint) assert scheduledTaskEndpoint != null - scheduledTaskEndpoint.scheduledTasks().getCron().each { + cronTasks.each { it.getRunnable().getTarget() == TriggerTask.getName() } - cleanup: - context.close() } def "schedule interval test"() { diff --git a/dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy b/dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy index 6e3b1760c11..f604744aa39 100644 --- a/dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy +++ b/dd-java-agent/instrumentation/spring/spring-scheduling-3.1/src/test/groovy/SpringSchedulingTest.groovy @@ -25,8 +25,14 @@ class SpringSchedulingTest extends InstrumentationSpecification { setup: def context = new AnnotationConfigApplicationContext(TriggerTaskConfig, SchedulingConfig) def task = context.getBean(TriggerTask) + def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint) task.blockUntilExecute() + // Capture cron tasks before closing the context (endpoint is unavailable after close). + def cronTasks = scheduledTaskEndpoint.scheduledTasks().getCron() + // Close the context immediately after the first execution to prevent a second cron + // firing before assertions complete, which would produce extra traces and cause flakiness. + context.close() expect: assert task != null @@ -54,13 +60,10 @@ class SpringSchedulingTest extends InstrumentationSpecification { } } and: - def scheduledTaskEndpoint = context.getBean(ScheduledTasksEndpoint) assert scheduledTaskEndpoint != null - scheduledTaskEndpoint.scheduledTasks().getCron().each { + cronTasks.each { it.getRunnable().getTarget() == TriggerTask.getName() } - cleanup: - context.close() } def "schedule interval test"() {