Skip to content

test: use atomics in TestSystemMetricsReport to avoid race condition #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions report/metrics_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package report

import (
"sync"
"sync/atomic"
"testing"

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

var gotCPU, gotMem, gotInfo bool
var gotCPU, gotMem, gotInfo atomic.Bool
testFunc := func(key string, val interface{}) {
if key == "info.uptime.ms" {
gotInfo = true
gotInfo.Store(true)
}
if key == "cpu.total.ticks" {
gotCPU = true
gotCPU.Store(true)
}
if key == "memstats.rss" {
gotMem = true
gotMem.Store(true)
}
}

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

wait.Wait()
assert.True(t, gotCPU, "Didn't find cpu.total.ticks")
assert.True(t, gotMem, "Didn't find memstats.rss")
assert.True(t, gotInfo, "Didn't find info.uptime.ms")
assert.True(t, gotCPU.Load(), "Didn't find cpu.total.ticks")
assert.True(t, gotMem.Load(), "Didn't find memstats.rss")
assert.True(t, gotInfo.Load(), "Didn't find info.uptime.ms")
}
Loading