Skip to content

Commit bbca5fd

Browse files
authored
test: use atomics in TestSystemMetricsReport to avoid race condition (#233)
## What does this PR do? replace bool with atomic.Bool to avoid race condition with multiple goroutines accessing the variables ## Why is it important? tests with race detector will fail ## Checklist <!-- Mandatory Add a checklist of things that are required to be reviewed in order to have the PR approved List here all the items you have verified BEFORE sending this PR. Please DO NOT remove any item, striking through those that do not apply. (Just in case, strikethrough uses two tildes. ~~Scratch this.~~) --> - [ ] My code follows the style guidelines of this project - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have added an entry in `CHANGELOG.md` ## Author's Checklist <!-- Recommended Add a checklist of things that are required to be reviewed in order to have the PR approved --> - [ ] ## Related issues Discovered in #232
1 parent c6c7b05 commit bbca5fd

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

report/metrics_report_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package report
1919

2020
import (
2121
"sync"
22+
"sync/atomic"
2223
"testing"
2324

2425
"github.com/stretchr/testify/assert"
@@ -34,16 +35,16 @@ func TestSystemMetricsReport(t *testing.T) {
3435
err := SetupMetrics(logger, "TestSys", "test")
3536
require.NoError(t, err)
3637

37-
var gotCPU, gotMem, gotInfo bool
38+
var gotCPU, gotMem, gotInfo atomic.Bool
3839
testFunc := func(key string, val interface{}) {
3940
if key == "info.uptime.ms" {
40-
gotInfo = true
41+
gotInfo.Store(true)
4142
}
4243
if key == "cpu.total.ticks" {
43-
gotCPU = true
44+
gotCPU.Store(true)
4445
}
4546
if key == "memstats.rss" {
46-
gotMem = true
47+
gotMem.Store(true)
4748
}
4849
}
4950

@@ -64,7 +65,7 @@ func TestSystemMetricsReport(t *testing.T) {
6465
close(ch)
6566

6667
wait.Wait()
67-
assert.True(t, gotCPU, "Didn't find cpu.total.ticks")
68-
assert.True(t, gotMem, "Didn't find memstats.rss")
69-
assert.True(t, gotInfo, "Didn't find info.uptime.ms")
68+
assert.True(t, gotCPU.Load(), "Didn't find cpu.total.ticks")
69+
assert.True(t, gotMem.Load(), "Didn't find memstats.rss")
70+
assert.True(t, gotInfo.Load(), "Didn't find info.uptime.ms")
7071
}

0 commit comments

Comments
 (0)