Fix race condition in NATS transport tests #705
Merged
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.
The tests for PR #680 have a small race that leads to sporadic test failures (https://travis-ci.org/go-kit/kit/jobs/372140473#L1732).
Closing subscriptions and connections is asynchronous, so when the next test runs and tries to receive messages there may still be an old connection subscribed to the same topic used by the next test.
Since the tests are using queue subscriptions only one client will ever get a message, meaning that a message may never arrive on the newer connection.
This PR refactors the logic for opening and closing the connections to check for open subscriptions before opening new connections and, if there are any, waiting a bit before failing.
Other options are to use different topic or queue names or even not using queue subscriptions, but checking the server seemed safer to me and requires less changes to the tests.