[client] Make conntrack cap and UDP cleanup tests deterministic

Merged
ML
2d ago
+64-35

Describe your changes

Two conntrack tests flake in CI on PRs that don't touch the client (seen on #8619 and #8620).

TestUDPCapEvicts (Windows): eviction takes the entry with the smallest lastSeen and breaks ties by map iteration order. On Windows' coarse clock, flows admitted back to back can share a timestamp, so the flow the test calls "oldest" can survive. I reproduced this on Linux by giving every flow the same lastSeen, and the same flow (SrcPort 0x7530) survived. The cap tests now set each flow's lastSeen to a distinct, increasing value after it's admitted, so "oldest" is always well defined. The TCP and ICMP cap tests had the same weakness and get the same fix. I left production eviction alone: tied entries are equally old as far as the tracker can tell, and the scan is already a sampled approximation of LRU.

TestUDPTracker_Cleanup (macOS): it slept 100ms and hoped the 25ms ticker had fired. Under CPU load it failed 1 run in 200 locally. It now sends the cleanup routine a tick over a channel the test owns, expires flows by setting lastSeen directly, and waits for the routine to exit. It also adds a fresh flow that has to survive cleanup.

Verified with go test -race -count=200 (and -count=1000), with GOMAXPROCS=1 and with 16 busy loops on 4 cores: no failures. Both tests still fail when eviction is changed to pick the newest flow, or cleanup is changed to remove everything.

Issue ticket number and link

No issue: this changes tests only.

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • I ran and tested this change locally — I did not rely on CI to find out whether it works
  • This PR has a single purpose (not a fix + refactor + feature in one)
  • This change is a trivial fix, OR it links an issue the NetBird team agreed on beforehand. Changes to the public API, gRPC protocols, functionality behavior, CLI / service flags, or new features always need that agreement first. See CONTRIBUTING.md.

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Only test code changes; nothing user-facing.

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__


Generated by Claude Code

Summary by CodeRabbit

  • Tests
    • Improved coverage for connection limits across TCP, UDP, and ICMP flows.
    • Made UDP cleanup tests more predictable by checking that expired connections are removed while fresh connections remain.