[PR #2848] [MERGED] Fix flaky TestShuffleDERPMapDeterministic by ensuring deterministic map iteration #2891

Closed
opened 2025-12-29 04:19:32 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/juanfont/headscale/pull/2848
Author: @Copilot
Created: 11/1/2025
Status: Merged
Merged: 11/2/2025
Merged by: @kradalby

Base: mainHead: copilot/fix-issue-2847


📝 Commits (3)

  • 6bb7f43 Initial plan
  • 1980ee6 Fix TestShuffleDERPMapDeterministic flakiness by ensuring deterministic region iteration
  • 18fb3f0 Add additional test cases to verify different base domains produce different shuffle orders

📊 Changes

2 files changed (+84 additions, -5 deletions)

View changed files

📝 hscontrol/derp/derp.go (+12 -1)
📝 hscontrol/derp/derp_test.go (+72 -4)

📄 Description

Fix flaky TestShuffleDERPMapDeterministic by ensuring deterministic map iteration

Completed:

  • Understand the root cause of test flakiness
  • Reproduce the issue locally
  • Implement fix to ensure deterministic behavior
  • Verify fix with repeated test runs
  • Run full test suite for derp package
  • Add additional test cases to verify different base domains produce different shuffle orders

Root Cause:
The test was flaky because Go's map iteration order is non-deterministic. In shuffleDERPMap(), the code iterates over dm.Regions (a map), and the iteration order varies between test runs. Since each region shuffle advances the random number generator's state, different iteration orders produce different shuffle results, even with the same seed.

Solution:
Modified shuffleDERPMap() in hscontrol/derp/derp.go to:

  1. Collect all region IDs into a slice
  2. Sort the region IDs numerically to ensure deterministic iteration order
  3. Iterate over regions in sorted order

Updated test expectations in hscontrol/derp/derp_test.go to match the new deterministic shuffle results (regions are now shuffled in ascending ID order).

Added additional test cases to verify that:

  • Different base domains with the same dataset produce different shuffle orders
  • The shuffle is truly deterministic for each base domain

Testing:

  • ✓ Ran test 50 times without race detector: all passed
  • ✓ Ran test 50 times with race detector: all passed
  • ✓ All derp package tests pass
  • ✓ Code compiles successfully
  • ✓ Code is properly formatted (gofmt)
  • ✓ Added 2 additional test cases with different base domains

Changes:

  • hscontrol/derp/derp.go: Added deterministic region iteration with sorted IDs
  • hscontrol/derp/derp_test.go: Updated expected shuffle results and added test cases for different base domains
Original prompt

Can you try to fix https://github.com/juanfont/headscale/issues/2847


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/juanfont/headscale/pull/2848 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 11/1/2025 **Status:** ✅ Merged **Merged:** 11/2/2025 **Merged by:** [@kradalby](https://github.com/kradalby) **Base:** `main` ← **Head:** `copilot/fix-issue-2847` --- ### 📝 Commits (3) - [`6bb7f43`](https://github.com/juanfont/headscale/commit/6bb7f4383e224f9995bfb9221c8f8528645e674d) Initial plan - [`1980ee6`](https://github.com/juanfont/headscale/commit/1980ee6ca490e590348f91132999d46f8f923ab8) Fix TestShuffleDERPMapDeterministic flakiness by ensuring deterministic region iteration - [`18fb3f0`](https://github.com/juanfont/headscale/commit/18fb3f00a861c8a9fd3f2ddb10764c64c0f5dc27) Add additional test cases to verify different base domains produce different shuffle orders ### 📊 Changes **2 files changed** (+84 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `hscontrol/derp/derp.go` (+12 -1) 📝 `hscontrol/derp/derp_test.go` (+72 -4) </details> ### 📄 Description Fix flaky TestShuffleDERPMapDeterministic by ensuring deterministic map iteration **Completed:** - [x] Understand the root cause of test flakiness - [x] Reproduce the issue locally - [x] Implement fix to ensure deterministic behavior - [x] Verify fix with repeated test runs - [x] Run full test suite for derp package - [x] Add additional test cases to verify different base domains produce different shuffle orders **Root Cause:** The test was flaky because Go's map iteration order is non-deterministic. In `shuffleDERPMap()`, the code iterates over `dm.Regions` (a map), and the iteration order varies between test runs. Since each region shuffle advances the random number generator's state, different iteration orders produce different shuffle results, even with the same seed. **Solution:** Modified `shuffleDERPMap()` in `hscontrol/derp/derp.go` to: 1. Collect all region IDs into a slice 2. Sort the region IDs numerically to ensure deterministic iteration order 3. Iterate over regions in sorted order Updated test expectations in `hscontrol/derp/derp_test.go` to match the new deterministic shuffle results (regions are now shuffled in ascending ID order). Added additional test cases to verify that: - Different base domains with the same dataset produce different shuffle orders - The shuffle is truly deterministic for each base domain **Testing:** - ✓ Ran test 50 times without race detector: all passed - ✓ Ran test 50 times with race detector: all passed - ✓ All derp package tests pass - ✓ Code compiles successfully - ✓ Code is properly formatted (gofmt) - ✓ Added 2 additional test cases with different base domains **Changes:** - `hscontrol/derp/derp.go`: Added deterministic region iteration with sorted IDs - `hscontrol/derp/derp_test.go`: Updated expected shuffle results and added test cases for different base domains <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > Can you try to fix https://github.com/juanfont/headscale/issues/2847 </details> <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 04:19:32 +01:00
adam closed this issue 2025-12-29 04:19:32 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/headscale#2891