MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestLabelSetTracker

Function TestLabelSetTracker

pkg/util/labelset/tracker_test.go:12–71  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestLabelSetTracker(t *testing.T) {
13 tracker := NewLabelSetTracker()
14 userID := "1"
15 userID2 := "2"
16 userID3 := "3"
17
18 tracker.Track(userID, 0, labels.FromStrings("foo", "bar"))
19 require.True(t, tracker.labelSetExists(userID, 0, labels.FromStrings("foo", "bar")))
20 tracker.Track(userID, 1, labels.FromStrings("foo", "baz"))
21 require.True(t, tracker.labelSetExists(userID, 1, labels.FromStrings("foo", "baz")))
22 tracker.Track(userID, 3, labels.EmptyLabels())
23 require.True(t, tracker.labelSetExists(userID, 3, labels.EmptyLabels()))
24 tracker.Track(userID2, 0, labels.FromStrings("foo", "bar"))
25 require.True(t, tracker.labelSetExists(userID2, 0, labels.FromStrings("foo", "bar")))
26 tracker.Track(userID2, 2, labels.FromStrings("cluster", "us-west-2"))
27 require.True(t, tracker.labelSetExists(userID2, 2, labels.FromStrings("cluster", "us-west-2")))
28
29 // Increment metrics and add a new user.
30 tracker.Track(userID, 3, labels.EmptyLabels())
31 require.True(t, tracker.labelSetExists(userID, 3, labels.EmptyLabels()))
32 tracker.Track(userID2, 2, labels.FromStrings("cluster", "us-west-2"))
33 require.True(t, tracker.labelSetExists(userID2, 2, labels.FromStrings("cluster", "us-west-2")))
34 tracker.Track(userID2, 4, labels.FromStrings("cluster", "us-west-2"))
35 require.True(t, tracker.labelSetExists(userID2, 4, labels.FromStrings("cluster", "us-west-2")))
36 tracker.Track(userID3, 4, labels.FromStrings("cluster", "us-east-1"))
37 require.True(t, tracker.labelSetExists(userID3, 4, labels.FromStrings("cluster", "us-east-1")))
38
39 // Remove user 2.
40 userSet := map[string]map[uint64]struct {
41 }{
42 userID: {0: struct{}{}, 1: struct{}{}, 2: struct{}{}, 3: struct{}{}},
43 userID3: {0: struct{}{}, 1: struct{}{}, 2: struct{}{}, 3: struct{}{}, 4: struct{}{}},
44 }
45 tracker.UpdateMetrics(userSet, noopDeleteMetrics)
46 // user 2 removed.
47 require.False(t, tracker.userExists(userID2))
48 // user 1 and 3 remain unchanged.
49 require.True(t, tracker.labelSetExists(userID, 0, labels.FromStrings("foo", "bar")))
50 require.True(t, tracker.labelSetExists(userID, 1, labels.FromStrings("foo", "baz")))
51 require.True(t, tracker.labelSetExists(userID, 3, labels.EmptyLabels()))
52 require.True(t, tracker.labelSetExists(userID3, 4, labels.FromStrings("cluster", "us-east-1")))
53
54 // Simulate existing limits removed for each user.
55 userSet = map[string]map[uint64]struct {
56 }{
57 userID: {0: struct{}{}},
58 userID2: {},
59 userID3: {},
60 }
61 tracker.UpdateMetrics(userSet, noopDeleteMetrics)
62 // User 2 and 3 removed. User 1 exists.
63 require.True(t, tracker.userExists(userID))
64 require.False(t, tracker.userExists(userID2))
65 require.False(t, tracker.userExists(userID3))
66
67 require.True(t, tracker.labelSetExists(userID, 0, labels.FromStrings("foo", "bar")))
68 require.False(t, tracker.labelSetExists(userID, 1, labels.FromStrings("foo", "baz")))
69 require.False(t, tracker.labelSetExists(userID, 3, labels.EmptyLabels()))

Callers

nothing calls this directly

Calls 5

TrackMethod · 0.95
labelSetExistsMethod · 0.95
UpdateMetricsMethod · 0.95
userExistsMethod · 0.95
NewLabelSetTrackerFunction · 0.85

Tested by

no test coverage detected