|
| 1 | +# Flakiness Detection Workflow |
| 2 | +# Runs changed tests multiple times to detect flaky/unstable tests |
| 3 | +# Last Updated: 2025-11-05 |
| 4 | + |
| 5 | +name: Flakiness Detection |
| 6 | + |
| 7 | +on: |
| 8 | + pull_request: |
| 9 | + paths: |
| 10 | + - '**/*_test.go' # Run when test files change |
| 11 | + - 'pkg/**/*.go' # Run when production code changes (tests might become flaky) |
| 12 | + |
| 13 | +jobs: |
| 14 | + detect-flaky-tests: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + timeout-minutes: 30 |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout code |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 2 # Need previous commit for diff |
| 23 | + |
| 24 | + - name: Set up Go |
| 25 | + uses: actions/setup-go@v5 |
| 26 | + with: |
| 27 | + go-version: '1.24' |
| 28 | + cache: true |
| 29 | + |
| 30 | + - name: Get changed test files |
| 31 | + id: changed-tests |
| 32 | + run: | |
| 33 | + # Find all changed test files (both new and modified) |
| 34 | + git diff --name-only HEAD~1 HEAD | grep '_test.go$' > changed_tests.txt || true |
| 35 | +
|
| 36 | + if [ -s changed_tests.txt ]; then |
| 37 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 38 | + echo "::notice::Found $(wc -l < changed_tests.txt) changed test files" |
| 39 | + cat changed_tests.txt |
| 40 | + else |
| 41 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 42 | + echo "::notice::No test files changed" |
| 43 | + fi |
| 44 | +
|
| 45 | + - name: Run changed tests 10 times to detect flakiness |
| 46 | + if: steps.changed-tests.outputs.has_changes == 'true' |
| 47 | + id: flakiness-check |
| 48 | + continue-on-error: true |
| 49 | + run: | |
| 50 | + # Track failures |
| 51 | + FLAKY_TESTS="" |
| 52 | + EXIT_CODE=0 |
| 53 | +
|
| 54 | + while IFS= read -r test_file; do |
| 55 | + package_path=$(dirname "$test_file") |
| 56 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 57 | + echo "Testing $package_path for flakiness (10 runs with race detector)..." |
| 58 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 59 | +
|
| 60 | + # Run test 10 times with race detector |
| 61 | + if ! go test -count=10 -race -v "./$package_path"; then |
| 62 | + echo "::error file=$test_file::Flaky test detected - failed when run multiple times" |
| 63 | + FLAKY_TESTS="$FLAKY_TESTS\n- $test_file" |
| 64 | + EXIT_CODE=1 |
| 65 | + else |
| 66 | + echo "::notice file=$test_file::Test is stable (passed all 10 runs)" |
| 67 | + fi |
| 68 | +
|
| 69 | + echo "" |
| 70 | + done < changed_tests.txt |
| 71 | +
|
| 72 | + if [ $EXIT_CODE -ne 0 ]; then |
| 73 | + echo "flaky_tests<<EOF" >> $GITHUB_OUTPUT |
| 74 | + echo -e "$FLAKY_TESTS" >> $GITHUB_OUTPUT |
| 75 | + echo "EOF" >> $GITHUB_OUTPUT |
| 76 | + fi |
| 77 | +
|
| 78 | + exit $EXIT_CODE |
| 79 | +
|
| 80 | + - name: Comment on PR if flaky tests found |
| 81 | + if: failure() && steps.flakiness-check.outcome == 'failure' |
| 82 | + uses: actions/github-script@v7 |
| 83 | + with: |
| 84 | + script: | |
| 85 | + const flakyTests = process.env.FLAKY_TESTS || 'Unknown tests'; |
| 86 | +
|
| 87 | + const message = `## ⚠️ Flaky Test Detected! |
| 88 | +
|
| 89 | + One or more tests failed when run multiple times with the race detector. This indicates non-deterministic behavior that must be fixed before merging. |
| 90 | +
|
| 91 | + ### Flaky Tests |
| 92 | + ${flakyTests} |
| 93 | +
|
| 94 | + ### Common Causes |
| 95 | + - **Race conditions**: Use \`-race\` flag to detect data races |
| 96 | + - **Timing dependencies**: Replace \`time.Sleep()\` with polling + timeout |
| 97 | + - **Map iteration order**: Sort maps before comparing |
| 98 | + - **Shared global state**: Ensure proper test isolation |
| 99 | + - **Non-deterministic random values**: Use fixed seeds for testing |
| 100 | +
|
| 101 | + ### How to Fix |
| 102 | + 1. Run locally with \`go test -count=10 -race ./path/to/package\` |
| 103 | + 2. Review [Flakiness Prevention Guide](https://github.com/CodeMonkeyCybersecurity/eos/blob/main/INTEGRATION_TESTING.md#flakiness-prevention) |
| 104 | + 3. Consider quarantining with \`//go:build flaky\` tag if immediate fix isn't possible |
| 105 | +
|
| 106 | + ### Resources |
| 107 | + - [Go Testing Best Practices](https://go.dev/wiki/TestComments) |
| 108 | + - [Detecting Flakiness](https://circleci.com/blog/reducing-flaky-test-failures/) |
| 109 | + - [Eos Integration Testing Guide](/INTEGRATION_TESTING.md) |
| 110 | +
|
| 111 | + **This PR cannot be merged until flakiness is resolved.**`; |
| 112 | +
|
| 113 | + await github.rest.issues.createComment({ |
| 114 | + issue_number: context.issue.number, |
| 115 | + owner: context.repo.owner, |
| 116 | + repo: context.repo.repo, |
| 117 | + body: message |
| 118 | + }); |
| 119 | + env: |
| 120 | + FLAKY_TESTS: ${{ steps.flakiness-check.outputs.flaky_tests }} |
| 121 | + |
| 122 | + - name: Fail workflow if flaky tests detected |
| 123 | + if: failure() && steps.flakiness-check.outcome == 'failure' |
| 124 | + run: | |
| 125 | + echo "::error::Flaky tests detected. See PR comment for details." |
| 126 | + exit 1 |
0 commit comments