|
| 1 | +package sinks |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/cybertec-postgresql/pgwatch/v5/internal/log" |
| 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 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func newTestPrometheusWriter(namespace string) *PrometheusWriter { |
| 16 | + return &PrometheusWriter{ |
| 17 | + ctx: testutil.TestContext, |
| 18 | + logger: log.GetLogger(testutil.TestContext), |
| 19 | + Namespace: namespace, |
| 20 | + Cache: make(PromMetricCache), |
| 21 | + lastScrapeErrors: prometheus.NewGauge(prometheus.GaugeOpts{ |
| 22 | + Namespace: namespace, |
| 23 | + Name: "test_last_scrape_errors", |
| 24 | + }), |
| 25 | + totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{ |
| 26 | + Namespace: namespace, |
| 27 | + Name: "test_total_scrapes", |
| 28 | + }), |
| 29 | + totalScrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{ |
| 30 | + Namespace: namespace, |
| 31 | + Name: "test_total_scrape_failures", |
| 32 | + }), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// TestLazyInitialization_WriteAfterCollect verifies that Write() works after |
| 37 | +// Collect() clears the cache. Collect() no longer pre-creates maps, so Write() |
| 38 | +// must create them lazily. |
| 39 | +func TestLazyInitialization_WriteAfterCollect(t *testing.T) { |
| 40 | + promw := newTestPrometheusWriter("test") |
| 41 | + |
| 42 | + // Write initial data |
| 43 | + msg := metrics.MeasurementEnvelope{ |
| 44 | + DBName: "db1", |
| 45 | + MetricName: "metric1", |
| 46 | + Data: metrics.Measurements{ |
| 47 | + {metrics.EpochColumnName: time.Now().UnixNano(), "value": int64(100)}, |
| 48 | + }, |
| 49 | + } |
| 50 | + require.NoError(t, promw.Write(msg)) |
| 51 | + |
| 52 | + // Collect clears the cache |
| 53 | + ch := make(chan prometheus.Metric, 100) |
| 54 | + promw.Collect(ch) |
| 55 | + assert.Empty(t, promw.Cache, "cache should be empty after Collect") |
| 56 | + |
| 57 | + // Write after Collect - must work via lazy initialization |
| 58 | + msg.Data[0]["value"] = int64(200) |
| 59 | + require.NoError(t, promw.Write(msg)) |
| 60 | + |
| 61 | + assert.Contains(t, promw.Cache, "db1") |
| 62 | + assert.Equal(t, int64(200), promw.Cache["db1"]["metric1"].Data[0]["value"]) |
| 63 | +} |
| 64 | + |
| 65 | +// TestCollect_NoPreallocation verifies Collect() creates an empty cache |
| 66 | +// without pre-allocating maps for each database (O(1) instead of O(N)). |
| 67 | +func TestCollect_NoPreallocation(t *testing.T) { |
| 68 | + promw := newTestPrometheusWriter("test") |
| 69 | + |
| 70 | + // Populate cache with multiple databases |
| 71 | + for _, db := range []string{"db1", "db2", "db3", "db4", "db5"} { |
| 72 | + promw.Cache[db] = map[string]metrics.MeasurementEnvelope{ |
| 73 | + "metric": { |
| 74 | + DBName: db, |
| 75 | + MetricName: "metric", |
| 76 | + Data: metrics.Measurements{ |
| 77 | + {metrics.EpochColumnName: time.Now().UnixNano(), "value": int64(1)}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + } |
| 82 | + assert.Len(t, promw.Cache, 5) |
| 83 | + |
| 84 | + // Collect |
| 85 | + ch := make(chan prometheus.Metric, 100) |
| 86 | + promw.Collect(ch) |
| 87 | + |
| 88 | + // New cache should be empty - no pre-allocated maps |
| 89 | + assert.Empty(t, promw.Cache) |
| 90 | +} |
0 commit comments