Pulse/internal/monitoring/metrics_history_concurrency_test.go
rcourtman 6ff345fb6b chore: fix staticcheck SA warnings
- Fix SA4006 unused value issues in ssh.go, validation.go, generator.go
- Replace deprecated ioutil with io/os in config.go
- Replace deprecated tar.TypeRegA with tar.TypeReg
- Remove deprecated rand.Seed calls (auto-seeded in Go 1.20+)
- Fix always-true nil check in main.go
- Fix impossible nil comparison in tempproxy/client.go
- Add nil check for config in monitor.New()
2025-11-27 09:16:53 +00:00

62 lines
1.5 KiB
Go

package monitoring
import (
"math/rand"
"sync"
"testing"
"time"
)
func TestMetricsHistoryConcurrentAccess(t *testing.T) {
mh := NewMetricsHistory(256, time.Minute)
const iterations = 1000
var wg sync.WaitGroup
wg.Add(4)
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
mh.AddGuestMetric("guest-1", "cpu", rand.Float64()*100, time.Now())
mh.AddGuestMetric("guest-1", "memory", rand.Float64()*100, time.Now())
mh.AddGuestMetric("guest-2", "disk", rand.Float64()*100, time.Now())
time.Sleep(time.Microsecond)
}
}()
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
mh.AddNodeMetric("node-1", "cpu", rand.Float64()*100, time.Now())
mh.AddNodeMetric("node-1", "memory", rand.Float64()*100, time.Now())
mh.AddStorageMetric("storage-1", "usage", rand.Float64()*100, time.Now())
time.Sleep(time.Microsecond)
}
}()
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
mh.GetGuestMetrics("guest-1", "cpu", time.Minute)
mh.GetGuestMetrics("guest-1", "memory", time.Minute)
mh.GetGuestMetrics("guest-2", "disk", time.Minute)
time.Sleep(time.Microsecond)
}
}()
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
mh.GetNodeMetrics("node-1", "cpu", time.Minute)
mh.GetNodeMetrics("node-1", "memory", time.Minute)
// Storage metrics are exposed through Monitor, but simulate reads via guest metrics map
mh.GetGuestMetrics("storage-1", "usage", time.Minute)
time.Sleep(time.Microsecond)
}
}()
wg.Wait()
}
// rand is automatically seeded in Go 1.20+