Skip to content

test(zebra): isolate TaskFailFast tests by organization - #1361

Draft
adbatista wants to merge 2 commits into
mainfrom
adb/zebra-fail-fast-test-isolation
Draft

adbatista wants to merge 2 commits into
mainfrom
adb/zebra-fail-fast-test-isolation

Conversation

@adbatista

Copy link
Copy Markdown
Contributor

📝 Description

Fixes a flaky test: Zebra.Workers.TaskFailFastTest ".query when the strategy is cancel => return all non-started jobs" sometimes fails with assert length(tuples) == 3 (left: 2).

Cause. Every job created by Support.Factories.Job uses the same compile-time organization id. Job.enqueue, finish, stop and force_finish call Scheduler.lock_and_process_async/1, which spawns an unlinked process. About 100ms later that process locks every enqueued job of the organization FOR UPDATE and holds the lock while it loads the org. When it outlives the test that spawned it, it locks the next test's rows. TaskFailFast.query/1 uses FOR UPDATE ... SKIP LOCKED, so it leaves the locked job out. In the failing CI log, the scheduler's selection query runs between the test's inserts and its query.

Production behaves correctly here: SKIP LOCKED is intended, and the worker picks up skipped jobs on its next tick, about a second later. Only the tests share state.

Change. Test-only. Each test gets its own organization id, and its jobs are created through a small create_job/3 helper that sets it. Leftover scheduler processes can no longer lock this test's jobs. Production code is unchanged.

For reviewers. The root cause is in the test harness: processes spawned with spawn/1 in lock_and_process_async/1 can outlive their test, and the DataCase truncates don't stop them. Fixing that globally would mean running those tasks under a Task.Supervisor and stopping its children in DataCase setup. That is left out of this PR on purpose.

✅ Checklist

  • I have tested this change: ran the file in Docker 3 times (10 tests, 0 failures each); make format.ex and make lint.ex are clean
  • This change requires documentation update

Factory jobs share one compile-time organization id. Job.enqueue/finish/
stop spawn an unlinked scheduler process that, ~100ms later, locks every
enqueued job of that organization FOR UPDATE. When it outlives the test
that spawned it, it locks the next test's rows, and TaskFailFast.query
(FOR UPDATE SKIP LOCKED) skips them, so ".query when the strategy is
cancel" sometimes returns 2 jobs instead of 3.

Give each test its own organization id so leftover scheduler processes
cannot lock its jobs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant