Skip to content

test(kates): the tests that stub a mock a scheduled job also calls run with the scheduler off, so the job cannot take the stub - #385

Merged
bmscomp merged 1 commit into
mainfrom
fix/mock-repository-scheduler-race
Oct 17, 2026
Merged

bmscomp merged 1 commit into
mainfrom
fix/mock-repository-scheduler-race

Conversation

@bmscomp

@bmscomp bmscomp commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Why

AdvisorResourceTest.analyzeReturns404ForMissingRun failed on PR #376 (run 39062142823, attempt 1) with WrongTypeOfReturnValue: Optional cannot be returned by findByStatus(). When you write when(mock.call()).thenReturn(...), Mockito attaches the stub to the most recent call on that mock, whichever thread made it. Every 60 s the timeout reaper calls findByStatus on the mocked TestRunRepository. If that call happens between the test's findById and its thenReturn, the stub lands on findByStatus. doReturn().when() has the same gap.

In each of the five classes, the test stubs a mock that a scheduled job also calls:

Class Mock Job
AdvisorResourceTest, ReportResourceTest TestRunRepository TestTimeoutReaper.reapStuckTests
ScheduleResourceTest ScheduledTestRunRepository, TrogdorClient TestScheduler.evaluateSchedules, run reconciler
TestResourceTest TrogdorClient TestOrchestrator.reconcileActiveRuns
TrendServiceTest ClusterHealthService KafkaReachabilityCache.refresh

What

  • New SchedulerOffTestProfile, which sets quarkus.scheduler.enabled=false.
  • The five classes run under it. None of them needs a job to fire: they read runs over GET, and GET refreshes the run itself.
  • Every other test keeps the scheduler as before.

Tests

  • A throwaway test (not committed) reproduced the exact CI error in under 3 s: one thread called findByStatus while the main thread stubbed findById.
  • The five classes ran 10 times in a row: 103 tests each time, all passed. The log shows "Simple scheduler is disabled by config property".
  • Full ./mvnw -o test: 1466 tests, 0 failures.
  • spotless:check passes.
  • go test ./cmd -run TestMCPCaveat passes.

…n with the scheduler off, so the job cannot take the stub

AdvisorResourceTest.analyzeReturns404ForMissingRun failed on CI (PR #376)
with "WrongTypeOfReturnValue: Optional cannot be returned by
findByStatus()". Mockito ties when(mock.call()).thenReturn(...) to the
last call made on that mock, from any thread. The timeout reaper calls
findByStatus on the mocked TestRunRepository every 60 s; when that call
lands between the test's findById and its thenReturn, the stub goes to
findByStatus. doReturn().when() has the same window.

Each of the five classes stubs a mock that a job calls:
- AdvisorResourceTest, ReportResourceTest: TestRunRepository, called by
  TestTimeoutReaper.reapStuckTests (findByStatus).
- ScheduleResourceTest: ScheduledTestRunRepository, called by
  TestScheduler.evaluateSchedules (findAllEnabled); TrogdorClient, called
  by the run reconciler for the runs the test starts.
- TestResourceTest: TrogdorClient, called by
  TestOrchestrator.reconcileActiveRuns for the runs the test starts.
- TrendServiceTest: ClusterHealthService, called by
  KafkaReachabilityCache.refresh (isReachable).

None of them needs a job to fire: they read runs over GET, which
refreshes the run itself. SchedulerOffTestProfile sets
quarkus.scheduler.enabled=false and the five classes use it. Every other
test keeps the scheduler as before.
@bmscomp
bmscomp merged commit feb9acb into main Oct 17, 2026
56 checks passed
@bmscomp
bmscomp deleted the fix/mock-repository-scheduler-race branch October 17, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant