Skip to content

Commit dcb0b4a

Browse files
committed
use testutil.TestContext
1 parent 923bfd0 commit dcb0b4a

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

internal/sinks/prometheus_race_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package sinks
22

33
import (
4-
"context"
54
"sync"
65
"testing"
76
"time"
87

98
"github.com/cybertec-postgresql/pgwatch/v5/internal/metrics"
9+
"github.com/cybertec-postgresql/pgwatch/v5/internal/testutil"
1010
"github.com/prometheus/client_golang/prometheus"
1111
)
1212

1313
func TestCollect_RaceCondition_Real(_ *testing.T) {
1414
// 1. Initialize the real PrometheusWriter
1515
// Note: In the current buggy code, this shares the global 'promAsyncMetricCache'
16-
promw, _ := NewPrometheusWriter(context.Background(), "127.0.0.1:0/pgwatch")
16+
promw, _ := NewPrometheusWriter(testutil.TestContext, "127.0.0.1:0/pgwatch")
1717

1818
// 2. Register a metric so Write() actually puts data into the map
1919
_ = promw.SyncMetric("race_db", "test_metric", AddOp)
@@ -22,9 +22,7 @@ func TestCollect_RaceCondition_Real(_ *testing.T) {
2222
done := make(chan struct{})
2323

2424
// --- The Writer (Simulating Database Updates) ---
25-
wg.Add(1)
26-
go func() {
27-
defer wg.Done()
25+
wg.Go(func() {
2826
for {
2927
select {
3028
case <-done:
@@ -44,17 +42,15 @@ func TestCollect_RaceCondition_Real(_ *testing.T) {
4442
// No sleep here -> hammer the map as fast as possible
4543
}
4644
}
47-
}()
45+
})
4846

4947
// --- The Collector (Simulating Prometheus Scrapes) ---
50-
wg.Add(1)
51-
go func() {
52-
defer wg.Done()
48+
wg.Go(func() {
5349
// Prometheus provides a channel to receive metrics
5450
ch := make(chan prometheus.Metric, 10000)
5551

5652
// Scrape 50 times (more than enough to trigger a race in a tight loop)
57-
for i := 0; i < 50; i++ {
53+
for range 50 {
5854
// Call the REAL Collect method
5955
promw.Collect(ch)
6056

@@ -69,7 +65,7 @@ func TestCollect_RaceCondition_Real(_ *testing.T) {
6965
}
7066
}
7167
close(done) // Tell the writer to stop
72-
}()
68+
})
7369

7470
wg.Wait()
7571
}

0 commit comments

Comments
 (0)