Tests: @Async probably does not run synchronously under the test profile #212

Open
opened 2026-12-01 22:34:01 +01:00 by midas · 0 comments
Owner

AGENTS.md and several tests assume @Async runs synchronously under the test profile because TestAsyncConfig provides a SyncTaskExecutor.

While fixing #207 this turned out to be doubtful (reasoned from the code, not proven by a run): Spring resolves the default async executor with getBean(TaskExecutor.class), and TestAsyncConfig also declares a @Primary taskScheduler (a single-thread ThreadPoolTaskScheduler, which is a TaskExecutor too). The primary bean wins, so @Async listeners would run on the test-scheduling- thread, in parallel with the test's next request. Comments in Phase3IntegrationTest ("@Async listener may also create a duplicate", "may not fire in time") point the same way, and it explains how #207 could race in CI.

To do

  • Confirm which executor @Async uses in tests (log the thread name in a listener, or assert on it).
  • If confirmed: make the sync executor the one @Async picks (e.g. an AsyncConfigurer, or name the bean taskExecutor and drop @Primary from the scheduler), then remove workarounds in tests that wait for or tolerate async listeners.
  • Correct the statement in AGENTS.md either way.
`AGENTS.md` and several tests assume `@Async` runs synchronously under the `test` profile because `TestAsyncConfig` provides a `SyncTaskExecutor`. While fixing #207 this turned out to be doubtful (reasoned from the code, not proven by a run): Spring resolves the default async executor with `getBean(TaskExecutor.class)`, and `TestAsyncConfig` also declares a `@Primary taskScheduler` (a single-thread `ThreadPoolTaskScheduler`, which is a `TaskExecutor` too). The primary bean wins, so `@Async` listeners would run on the `test-scheduling-` thread, in parallel with the test's next request. Comments in `Phase3IntegrationTest` ("@Async listener may also create a duplicate", "may not fire in time") point the same way, and it explains how #207 could race in CI. ## To do - Confirm which executor `@Async` uses in tests (log the thread name in a listener, or assert on it). - If confirmed: make the sync executor the one `@Async` picks (e.g. an `AsyncConfigurer`, or name the bean `taskExecutor` and drop `@Primary` from the scheduler), then remove workarounds in tests that wait for or tolerate async listeners. - Correct the statement in `AGENTS.md` either way.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
midas/podradar#212
No description provided.