cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.83.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/promapi/failover_test.go

176lines · modecode

1package promapi
2
3import (
4 "testing"
5 "time"
6
7 "github.com/prometheus/client_golang/prometheus"
8 "github.com/stretchr/testify/require"
9)
10
11func TestCacheCleaner(t *testing.T) {
12 cache := newQueryCache(time.Minute, time.Now)
13 quit := make(chan bool)
14
15 // Add some entries to cache
16 cache.set(1, nil, 0)
17 cache.set(2, nil, 0)
18 require.Len(t, cache.entries, 2)
19
20 // Start cache cleaner with very short interval
21 go cacheCleaner(cache, time.Millisecond*10, quit)
22
23 // Wait for at least one gc cycle
24 time.Sleep(time.Millisecond * 50)
25
26 // Stop the cleaner
27 quit <- true
28}
29
30func TestFailoverGroupStartWorkers(t *testing.T) {
31 type testCaseT struct {
32 name string
33 callTwice bool
34 expectStarted bool
35 }
36
37 testCases := []testCaseT{
38 {
39 name: "starts workers on first call",
40 callTwice: false,
41 expectStarted: true,
42 },
43 {
44 name: "idempotent on second call",
45 callTwice: true,
46 expectStarted: true,
47 },
48 }
49
50 for _, tc := range testCases {
51 t.Run(tc.name, func(t *testing.T) {
52 fg := &FailoverGroup{
53 name: "test",
54 servers: []*Prometheus{},
55 }
56 reg := prometheus.NewRegistry()
57
58 fg.StartWorkers(reg)
59 require.Equal(t, tc.expectStarted, fg.started)
60
61 if tc.callTwice {
62 fg.StartWorkers(reg)
63 require.Equal(t, tc.expectStarted, fg.started)
64 }
65
66 fg.Close(reg)
67 })
68 }
69}
70
71func TestFailoverGroupClose(t *testing.T) {
72 type testCaseT struct {
73 name string
74 startFirst bool
75 }
76
77 testCases := []testCaseT{
78 {
79 name: "close on not started group is no-op",
80 startFirst: false,
81 },
82 {
83 name: "close on started group",
84 startFirst: true,
85 },
86 }
87
88 for _, tc := range testCases {
89 t.Run(tc.name, func(t *testing.T) {
90 fg := &FailoverGroup{
91 name: "test",
92 servers: []*Prometheus{},
93 }
94 reg := prometheus.NewRegistry()
95
96 if tc.startFirst {
97 fg.StartWorkers(reg)
98 require.True(t, fg.started)
99 }
100
101 // Close should not panic regardless of started state
102 require.NotPanics(t, func() {
103 fg.Close(reg)
104 })
105 })
106 }
107}
108
109func TestFailoverGroupCleanCache(t *testing.T) {
110 type testCaseT struct {
111 setup func() *FailoverGroup
112 name string
113 expectEmpty bool
114 expectPanics bool
115 }
116
117 testCases := []testCaseT{
118 {
119 name: "cleans stale cache entries",
120 setup: func() *FailoverGroup {
121 pastTime := time.Now().Add(-time.Hour)
122 cache := newQueryCache(time.Nanosecond, func() time.Time { return pastTime })
123 cache.set(1, nil, 0)
124 cache.now = time.Now
125 return &FailoverGroup{
126 name: "test",
127 servers: []*Prometheus{{cache: cache}},
128 }
129 },
130 expectEmpty: true,
131 expectPanics: false,
132 },
133 {
134 name: "handles nil cache without panic",
135 setup: func() *FailoverGroup {
136 return &FailoverGroup{
137 name: "test",
138 servers: []*Prometheus{{cache: nil}},
139 }
140 },
141 expectEmpty: false,
142 expectPanics: false,
143 },
144 {
145 name: "handles empty servers without panic",
146 setup: func() *FailoverGroup {
147 return &FailoverGroup{
148 name: "test",
149 servers: []*Prometheus{},
150 }
151 },
152 expectEmpty: false,
153 expectPanics: false,
154 },
155 }
156
157 for _, tc := range testCases {
158 t.Run(tc.name, func(t *testing.T) {
159 fg := tc.setup()
160
161 if tc.expectPanics {
162 require.Panics(t, func() {
163 fg.CleanCache()
164 })
165 } else {
166 require.NotPanics(t, func() {
167 fg.CleanCache()
168 })
169 }
170
171 if tc.expectEmpty && len(fg.servers) > 0 && fg.servers[0].cache != nil {
172 require.Empty(t, fg.servers[0].cache.entries)
173 }
174 })
175 }
176}
177