Commit 1c510bf
authored
test: deflake TestBaseMetricObject with testing/synctest (#2686)
# Description
`TestBaseMetricObject` races against real timers and fails
intermittently in the `Test Retina Image` workflow. It failed two runs
in the last week: [Aug 23, merge
queue](https://github.com/microsoft/retina/actions/runs/32652429087),
[Aug 24, PR
run](https://github.com/microsoft/retina/actions/runs/32736068992/attempts/2).
The second failure sits in an earlier attempt of a re-run, so the run
list hides it.
```
basemetricsobject_test.go:100: expected number of goroutines to be the same as before, expected 4, got 3
```
The test has three timing defects:
- It compares two process-wide `runtime.NumGoroutine()` snapshots.
Unrelated goroutine churn breaks the comparison. This caused both CI
failures.
- It sleeps `101ms`, then reads `expireCalled`. The expiration goroutine
writes that variable. The race detector reports the pair.
- The expiration ticker starts before `b.updated`. A late first
assertion can observe the label already expired.
This PR moves the test body into a `testing/synctest` bubble (standard
library since Go 1.25 — `go.mod` requires `go 1.26.0`):
- The fake clock makes the ticker fire deterministically and removes all
real sleeps. This closes the early-expiration gap.
- `synctest.Wait()` orders the callback write before the test reads it.
This removes the data race.
- The bubble fails the test if the expiration goroutine leaks. This
replaces the process-wide count. `t.Cleanup(b.clean)` stops the
goroutine on every exit path.
## Checklist
- [x] I have read the [contributing
documentation](https://retina.sh/docs/Contributing/overview).
- [x] I signed and signed-off the commits (`git commit -S -s ...`). See
[this
documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification)
on signing commits.
- [x] I have correctly attributed the author(s) of the code.
- [x] I have tested the changes locally.
- [x] I have followed the project's style guidelines.
- [ ] I have updated the documentation, if necessary.
- [ ] I have added tests, if applicable.
## Screenshots (if applicable) or Testing Completed
```bash
go test -tags=unit,dashboard -run 'TestBaseMetricObject' -count=100 ./pkg/module/metrics/
go test -race -tags=unit,dashboard -run 'TestBaseMetricObject' -count=100 ./pkg/module/metrics/
```
Both pass. The 100 plain iterations complete in `0.04s` because the
bubble removes the real sleeps. Before this change, the race detector
failed the test on the first run. `go vet` is clean on the package.
Signed-off-by: Quang Nguyen <28567936+nddq@users.noreply.github.com>1 parent 366a5e0 commit 1c510bf
1 file changed
Lines changed: 46 additions & 53 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
59 | 63 | | |
60 | | - | |
61 | | - | |
| 64 | + | |
| 65 | + | |
62 | 66 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
67 | 76 | | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | 77 | | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
83 | 93 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
| 94 | + | |
102 | 95 | | |
103 | 96 | | |
104 | 97 | | |
0 commit comments