-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathquery_cache_test.go
More file actions
260 lines (225 loc) · 6.78 KB
/
Copy pathquery_cache_test.go
File metadata and controls
260 lines (225 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package otelmetrics
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
)
// newTestClient creates an OtelMetricsClient pointing at a test HTTP server.
// Uses static credentials and a real signer; the test server ignores signatures.
func newTestClient(url string) *OtelMetricsClient {
return &OtelMetricsClient{
httpClient: &http.Client{},
signer: v4.NewSigner(),
creds: aws.CredentialsProviderFunc(func(ctx context.Context) (aws.Credentials, error) {
return aws.Credentials{AccessKeyID: "AKID", SecretAccessKey: "SECRET", SessionToken: "TOKEN"}, nil
}),
queryURL: url + "/api/v1/query",
region: "us-west-2",
signingService: "aps",
maxRetries: 1,
}
}
// promqlResponseJSON builds a minimal PromQL JSON response body.
func promqlResponseJSON(series []map[string]string) string {
type result struct {
Metric map[string]string `json:"metric"`
Value []json.RawMessage `json:"value"`
}
type data struct {
ResultType string `json:"resultType"`
Result []result `json:"result"`
}
type response struct {
Status string `json:"status"`
Data data `json:"data"`
}
results := make([]result, 0, len(series))
for _, labels := range series {
results = append(results, result{
Metric: labels,
Value: []json.RawMessage{[]byte(`1234567890`), []byte(`"1.0"`)},
})
}
resp := response{
Status: "success",
Data: data{ResultType: "vector", Result: results},
}
b, _ := json.Marshal(resp)
return string(b)
}
func TestQueryCache_EmptyResultNotStored(t *testing.T) {
// Server returns an empty result set (no series).
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, promqlResponseJSON(nil))
}))
defer srv.Close()
client := newTestClient(srv.URL)
qc := NewQueryCache(client, "test-cluster")
results, err := qc.Get(context.Background(), "empty_metric")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(results) != 0 {
t.Fatalf("expected empty results, got %d", len(results))
}
// Verify the entry was NOT stored in the cache.
qc.mu.RLock()
_, cached := qc.filtered["empty_metric"]
qc.mu.RUnlock()
if cached {
t.Fatal("empty result should not be cached")
}
}
func TestQueryCache_NonEmptyResultStored(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, promqlResponseJSON([]map[string]string{
{"__name__": "cpu_usage", "host": "node-1"},
}))
}))
defer srv.Close()
client := newTestClient(srv.URL)
qc := NewQueryCache(client, "test-cluster")
results, err := qc.Get(context.Background(), "cpu_usage")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
// Verify the entry WAS stored in the cache.
qc.mu.RLock()
_, cached := qc.filtered["cpu_usage"]
qc.mu.RUnlock()
if !cached {
t.Fatal("non-empty result should be cached")
}
}
func TestQueryCache_ErrorResultStored(t *testing.T) {
// Server returns HTTP 500 to trigger an error.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "internal error", http.StatusInternalServerError)
}))
defer srv.Close()
client := newTestClient(srv.URL)
qc := NewQueryCache(client, "test-cluster")
_, err := qc.Get(context.Background(), "error_metric")
if err == nil {
t.Fatal("expected error, got nil")
}
// Verify the error entry WAS stored in the cache.
qc.mu.RLock()
entry, cached := qc.filtered["error_metric"]
qc.mu.RUnlock()
if !cached {
t.Fatal("error result should be cached")
}
if entry.err == nil {
t.Fatal("cached entry should contain the error")
}
}
func TestQueryCache_WaiterGetsNilOnEmptyResult(t *testing.T) {
// Simulate the waiter path: register an inflight channel,
// close it without storing (empty result), verify waiter gets nil, nil.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, promqlResponseJSON(nil))
}))
defer srv.Close()
client := newTestClient(srv.URL)
qc := NewQueryCache(client, "test-cluster")
ch := make(chan struct{})
qc.mu.Lock()
qc.inflight["waiter_metric"] = ch
qc.mu.Unlock()
var waiterResults []MetricResult
var waiterErr error
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
<-ch
qc.mu.RLock()
entry, ok := qc.filtered["waiter_metric"]
qc.mu.RUnlock()
if !ok {
waiterResults = nil
waiterErr = nil
return
}
waiterResults = entry.results
waiterErr = entry.err
}()
// Simulate: fetcher got empty, did NOT store, cleans up inflight and closes ch.
qc.mu.Lock()
delete(qc.inflight, "waiter_metric")
qc.mu.Unlock()
close(ch)
wg.Wait()
if waiterResults != nil {
t.Fatalf("waiter expected nil results, got %v", waiterResults)
}
if waiterErr != nil {
t.Fatalf("waiter expected nil error, got %v", waiterErr)
}
}
func TestQueryCache_GetUnfiltered_EmptyNotStored(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, promqlResponseJSON(nil))
}))
defer srv.Close()
client := newTestClient(srv.URL)
qc := NewQueryCache(client, "test-cluster")
results, err := qc.GetUnfiltered(context.Background(), "empty_unfiltered")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(results) != 0 {
t.Fatalf("expected empty results, got %d", len(results))
}
qc.mu.RLock()
_, cached := qc.unfiltered["empty_unfiltered"]
qc.mu.RUnlock()
if cached {
t.Fatal("empty unfiltered result should not be cached")
}
}
func TestQueryCache_GetUnfiltered_ErrorStored(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "internal error", http.StatusInternalServerError)
}))
defer srv.Close()
client := newTestClient(srv.URL)
qc := NewQueryCache(client, "test-cluster")
_, err := qc.GetUnfiltered(context.Background(), "error_unfiltered")
if err == nil {
t.Fatal("expected error, got nil")
}
qc.mu.RLock()
entry, cached := qc.unfiltered["error_unfiltered"]
qc.mu.RUnlock()
if !cached {
t.Fatal("error unfiltered result should be cached")
}
if entry.err == nil {
t.Fatal("cached entry should contain the error")
}
}
func TestPromqlMetricSelector(t *testing.T) {
got := promqlMetricSelector("node.cpu.usage")
want := `{"__name__"="node.cpu.usage",`
if got != want {
t.Fatalf("promqlMetricSelector(dotted) = %q, want %q", got, want)
}
got = promqlMetricSelector("cpu_usage")
want = `cpu_usage{`
if got != want {
t.Fatalf("promqlMetricSelector(plain) = %q, want %q", got, want)
}
}