Skip to content

test(network): ack NATS messages before signalling test completion - #4488

Merged
stevenvegt merged 1 commit into
masterfrom
fix/nats-ack-before-done
Sep 9, 2026
Merged

test(network): ack NATS messages before signalling test completion#4488
stevenvegt merged 1 commit into
masterfrom
fix/nats-ack-before-done

Conversation

@stevenvegt

Copy link
Copy Markdown
Member

Fixes the flaky TestNetwork_Reprocess (seen on #4487 and earlier on an unrelated feature branch in July).

The message handler in the test called wg.Done() before msg.Ack(). Done releases the test's wg.Wait(), the subtest returns, its cleanup shuts down the embedded NATS server, and only then does the callback goroutine send the ack over a closed connection: nats: connection closed. On a fast machine the ack usually wins that race, on a loaded CI runner it sometimes loses. The fix defers wg.Done() so the ack always precedes it.

The other three NATS handlers in tests were checked for the same pattern. Two in network_integration_test.go are safe because they ack while holding the mutex the test polls on. Their require.NoError calls are changed to assert.NoError, since require calls FailNow, which must run on the test goroutine. The one in events/integration_test.go wrote the ack error into a variable shared with the test goroutine; it now uses a dedicated variable read under the mutex.

Verified with go test -race -count=5 on the affected packages.

Backport to V6.2: follows as a separate PR.

TestNetwork_Reprocess called wg.Done() before msg.Ack(). Once Done
released the test, its cleanup shut down the embedded NATS server, and
the ack on the callback goroutine failed with "nats: connection closed"
on slow CI runners.

Also stop calling require inside NATS callback goroutines (FailNow must
run on the test goroutine) and read the ack error in the events
integration test under the mutex instead of through a shared variable.

Assisted-by: AI
@qltysh

qltysh Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Qlty


Coverage Impact

This PR will not change total coverage.

🚦 See full report on Qlty Cloud »

🛟 Help
  • Diff Coverage: Coverage for added or modified lines of code (excludes deleted files). Learn more.

  • Total Coverage: Coverage for the whole repository, calculated as the sum of all File Coverage. Learn more.

  • File Coverage: Covered Lines divided by Covered Lines plus Missed Lines. (Excludes non-executable lines including blank lines and comments.)

    • Indirect Changes: Changes to File Coverage for files that were not modified in this PR. Learn more.

@stevenvegt
stevenvegt merged commit 4cfcbfd into master Sep 9, 2026
14 checks passed
@stevenvegt
stevenvegt deleted the fix/nats-ack-before-done branch September 9, 2026 08:59
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.

2 participants