Fix the flaky worker integration tests - #1502
Merged
Merged
Conversation
The tests picked a port at random from a thousand wide range and started a server on it, without checking the number was free. There are around a dozen of those picks in a run, so two of them landing on the same number happened often enough to fail the job every few runs: the second server couldn't bind, its setup hook never finished, and the whole file sat pending until the two minute timeout. The files run one at a time and each is its own process, so handing out ports from a counter is enough. This also removes the three files that all hardcoded 4321, which was harmless while the files run serially but would bite anyone who turned on concurrency later. Closes #1501
Two more causes of the intermittent failures, both in the tests. The worker server is handed back before it has connected to Lightning - it fetches the collections version over http first and only then opens the socket. Several tests swap the worker out and queue a run straight afterwards, so the run could end up sitting in the queue with nothing listening, and the file would hang until the timeout. That's the four pending tests we kept seeing in the integration file. Waiting for the socket to come up removes the race for every test at once. The sigterm test failed whenever a claim already in flight arrived after we sent the signal, which the worker can't do anything about. It now keys off the worker telling us it received the signal instead.
The helper every test in that file goes through listened with a plain once and then checked the run id inside the callback. Any completion for another run - a late one from the previous test, or a duplicate - used up the listener without resolving, so the promise hung for good and the rest of the file never ran. The mock already has onSocketEvent for exactly this: it only releases the listener once the id matches.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes the worker integration tests pass reliably. Closes #1501.
That job was failing at random - a different Node version each run, and the same tree giving different results across reruns. It turned out to be four separate bugs, all in the tests rather than the worker:
Ports were guessed. Servers were started on a random number between 2000 and 3000 with no check that it was free. A run makes around a dozen of those picks, so collisions happened every few runs. That was the
EADDRINUSE :::2492we kept seeing. Ports now come from a counter.The worker wasn't connected yet.
initWorkerhanded the server back before it had connected to Lightning - it fetches the collections version over http first, then opens the socket. Seven tests swap the worker out and queue a run straight afterwards, so the run could sit in the queue with nothing listening, hanging the file until ava's timeout. It now waits for the socket.The sigterm test raced its own signal. It failed if a claim already in flight arrived after we sent the signal, which the worker can't help. It now keys off the worker reporting that it received it.
A lost listener in the exit reason tests. The helper every test there goes through listened with a plain
onceand checked the run id inside the callback, so a completion for any other run consumed the listener without resolving - hanging that promise for good. It now uses the mock'sonSocketEvent, which only releases on an id match.The four fixes are separate commits, worth reading individually.
Validation steps
cd integration-tests/worker && pnpm testlocally.AI Usage
Please disclose whether you've used AI anywhere in this PR (it's cool, we just want to know!):
You can read more details in our Responsible AI Policy