...
1 package sinks
2
3 import (
4 "sync"
5 "testing"
6 "time"
7
8 "github.com/cybertec-postgresql/pgwatch/v5/internal/metrics"
9 "github.com/cybertec-postgresql/pgwatch/v5/internal/testutil"
10 "github.com/prometheus/client_golang/prometheus"
11 )
12
13 func TestCollect_RaceCondition_Real(_ *testing.T) {
14
15 promw, _ := NewPrometheusWriter(testutil.TestContext, "127.0.0.1:0/pgwatch")
16
17
18 _ = promw.SyncMetric("race_db", "test_metric", AddOp)
19
20 var wg sync.WaitGroup
21 done := make(chan struct{})
22
23
24 wg.Go(func() {
25 for {
26 select {
27 case <-done:
28 return
29 default:
30
31 _ = promw.Write(metrics.MeasurementEnvelope{
32 DBName: "race_db",
33 MetricName: "test_metric",
34 Data: metrics.Measurements{
35 {
36 metrics.EpochColumnName: time.Now().UnixNano(),
37 "value": int64(100),
38 },
39 },
40 })
41
42 }
43 }
44 })
45
46
47 wg.Go(func() {
48
49 ch := make(chan prometheus.Metric, 10000)
50
51
52 for range 50 {
53
54 promw.Collect(ch)
55
56
57 drainLoop:
58 for {
59 select {
60 case <-ch:
61 default:
62 break drainLoop
63 }
64 }
65 }
66 close(done)
67 })
68
69 wg.Wait()
70 }
71