| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package metrix |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | ) |
| 10 | |
| 11 | func TestStoreReaderForEachMatch_VisibilityAndPredicate(t *testing.T) { |
| 12 | tests := map[string]struct { |
| 13 | raw bool |
| 14 | target string |
| 15 | wantValues []SampleValue |
| 16 | wantObserved int |
| 17 | }{ |
| 18 | "filtered read returns only latest successful series": { |
| 19 | raw: false, |
| 20 | target: "a", |
| 21 | wantValues: []SampleValue{30}, |
| 22 | wantObserved: 1, |
| 23 | }, |
| 24 | "raw read includes stale committed series": { |
| 25 | raw: true, |
| 26 | target: "b", |
| 27 | wantValues: []SampleValue{20}, |
| 28 | wantObserved: 2, |
| 29 | }, |
| 30 | } |
| 31 | |
| 32 | for name, tc := range tests { |
| 33 | t.Run(name, func(t *testing.T) { |
| 34 | s := NewCollectorStore() |
| 35 | cc := cycleController(t, s) |
| 36 | sm := s.Write().SnapshotMeter("svc") |
| 37 | g := sm.Gauge("load") |
| 38 | la := sm.LabelSet(Label{Key: "instance", Value: "a"}) |
| 39 | lb := sm.LabelSet(Label{Key: "instance", Value: "b"}) |
| 40 | |
| 41 | cc.BeginCycle() |
| 42 | g.Observe(10, la) |
| 43 | g.Observe(20, lb) |
| 44 | cc.CommitCycleSuccess() |
| 45 | |
| 46 | // Next successful cycle sees only instance=a. |
| 47 | cc.BeginCycle() |
| 48 | g.Observe(30, la) |
| 49 | cc.CommitCycleSuccess() |
| 50 | |
| 51 | reader := s.Read() |
| 52 | if tc.raw { |
| 53 | reader = s.Read(ReadRaw()) |
| 54 | } |
| 55 | |
| 56 | var observed int |
| 57 | var values []SampleValue |
| 58 | reader.ForEachMatch("svc.load", |
| 59 | func(labels LabelView) bool { |
| 60 | observed++ |
| 61 | instance, ok := labels.Get("instance") |
| 62 | return ok && instance == tc.target |
| 63 | }, |
| 64 | func(_ LabelView, v SampleValue) { |
| 65 | values = append(values, v) |
| 66 | }, |
| 67 | ) |
| 68 | |
| 69 | assert.Equal(t, tc.wantObserved, observed) |
| 70 | assert.Equal(t, tc.wantValues, values) |
| 71 | }) |
| 72 | } |
| 73 | } |