Skip to content

Commit fbe118b

Browse files
authored
[Querier] Fix Flaky Response Compression Test (#6243)
* fix flaky response compression test Signed-off-by: Ahmed Hassan <[email protected]> * refactor test helper function Signed-off-by: Ahmed Hassan <[email protected]> --------- Signed-off-by: Ahmed Hassan <[email protected]>
1 parent 296fd47 commit fbe118b

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

pkg/querier/tripperware/instantquery/instant_query_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"net/http"
11+
"sort"
1112
"strconv"
1213
"testing"
1314
"time"
@@ -26,6 +27,12 @@ import (
2627

2728
const testHistogramResponse = `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"prometheus_http_request_duration_seconds","handler":"/metrics","instance":"localhost:9090","job":"prometheus"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"],[0,"0.0015060652591874421","0.001642375811042411","7"],[0,"0.001642375811042411","0.0017910235218841233","5"],[0,"0.0017910235218841233","0.001953125","13"],[0,"0.001953125","0.0021298979153618314","19"],[0,"0.0021298979153618314","0.0023226701464896895","13"],[0,"0.0023226701464896895","0.002532889755177753","13"],[0,"0.002532889755177753","0.002762135864009951","15"],[0,"0.002762135864009951","0.0030121305183748843","12"],[0,"0.0030121305183748843","0.003284751622084822","34"],[0,"0.003284751622084822","0.0035820470437682465","188"],[0,"0.0035820470437682465","0.00390625","372"],[0,"0.00390625","0.004259795830723663","400"],[0,"0.004259795830723663","0.004645340292979379","411"],[0,"0.004645340292979379","0.005065779510355506","425"],[0,"0.005065779510355506","0.005524271728019902","425"],[0,"0.005524271728019902","0.0060242610367497685","521"],[0,"0.0060242610367497685","0.006569503244169644","621"],[0,"0.006569503244169644","0.007164094087536493","593"],[0,"0.007164094087536493","0.0078125","506"],[0,"0.0078125","0.008519591661447326","458"],[0,"0.008519591661447326","0.009290680585958758","346"],[0,"0.009290680585958758","0.010131559020711013","285"],[0,"0.010131559020711013","0.011048543456039804","196"],[0,"0.011048543456039804","0.012048522073499537","129"],[0,"0.012048522073499537","0.013139006488339287","85"],[0,"0.013139006488339287","0.014328188175072986","65"],[0,"0.014328188175072986","0.015625","54"],[0,"0.015625","0.01703918332289465","53"],[0,"0.01703918332289465","0.018581361171917516","20"],[0,"0.018581361171917516","0.020263118041422026","21"],[0,"0.020263118041422026","0.022097086912079608","15"],[0,"0.022097086912079608","0.024097044146999074","11"],[0,"0.024097044146999074","0.026278012976678575","2"],[0,"0.026278012976678575","0.028656376350145972","3"],[0,"0.028656376350145972","0.03125","3"],[0,"0.04052623608284405","0.044194173824159216","2"]]}]}]}}`
2829

30+
func sortPrometheusResponseHeader(headers []*tripperware.PrometheusResponseHeader) {
31+
sort.Slice(headers, func(i, j int) bool {
32+
return headers[i].Name < headers[j].Name
33+
})
34+
}
35+
2936
func TestRequest(t *testing.T) {
3037
t.Parallel()
3138
codec := InstantQueryCodec
@@ -159,10 +166,11 @@ func TestCompressedResponse(t *testing.T) {
159166
h.Set("Content-Type", "application/json")
160167
}
161168

162-
h.Set("Content-Encoding", tc.compression)
163169
if tc.promBody != nil {
164-
tc.promBody.Headers = append(tc.promBody.Headers, &tripperware.PrometheusResponseHeader{Name: "Content-Encoding", Values: []string{"gzip"}})
170+
tc.promBody.Headers = append(tc.promBody.Headers, &tripperware.PrometheusResponseHeader{Name: "Content-Encoding", Values: []string{tc.compression}})
165171
}
172+
h.Set("Content-Encoding", tc.compression)
173+
166174
responseBody := &bytes.Buffer{}
167175
w := gzip.NewWriter(responseBody)
168176
_, err := w.Write(b)
@@ -179,6 +187,8 @@ func TestCompressedResponse(t *testing.T) {
179187

180188
if err == nil {
181189
require.NoError(t, err)
190+
sortPrometheusResponseHeader(tc.promBody.Headers)
191+
sortPrometheusResponseHeader(resp.(*tripperware.PrometheusResponse).Headers)
182192
require.Equal(t, tc.promBody, resp)
183193
}
184194
})
@@ -1738,7 +1748,6 @@ func Benchmark_Decode(b *testing.B) {
17381748
}
17391749
})
17401750
}
1741-
17421751
}
17431752

17441753
func Benchmark_Decode_Protobuf(b *testing.B) {
@@ -1802,5 +1811,4 @@ func Benchmark_Decode_Protobuf(b *testing.B) {
18021811
}
18031812
})
18041813
}
1805-
18061814
}

pkg/querier/tripperware/queryrange/query_range_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"io"
88
"net/http"
9+
"sort"
910
"strconv"
1011
"testing"
1112

@@ -23,6 +24,12 @@ import (
2324
"github.com/cortexproject/cortex/pkg/querier/tripperware"
2425
)
2526

27+
func sortPrometheusResponseHeader(headers []*tripperware.PrometheusResponseHeader) {
28+
sort.Slice(headers, func(i, j int) bool {
29+
return headers[i].Name < headers[j].Name
30+
})
31+
}
32+
2633
func TestRequest(t *testing.T) {
2734
t.Parallel()
2835
// Create a Copy parsedRequest to assign the expected headers to the request without affecting other tests using the global.
@@ -1263,10 +1270,11 @@ func TestCompressedResponse(t *testing.T) {
12631270
h.Set("Content-Type", tripperware.ApplicationJson)
12641271
}
12651272

1266-
h.Set("Content-Encoding", tc.compression)
12671273
if tc.promBody != nil {
1268-
tc.promBody.Headers = append(tc.promBody.Headers, &tripperware.PrometheusResponseHeader{Name: "Content-Encoding", Values: []string{"gzip"}})
1274+
tc.promBody.Headers = append(tc.promBody.Headers, &tripperware.PrometheusResponseHeader{Name: "Content-Encoding", Values: []string{tc.compression}})
12691275
}
1276+
h.Set("Content-Encoding", tc.compression)
1277+
12701278
responseBody := &bytes.Buffer{}
12711279
w := gzip.NewWriter(responseBody)
12721280
_, err := w.Write(b)
@@ -1283,7 +1291,9 @@ func TestCompressedResponse(t *testing.T) {
12831291

12841292
if err == nil {
12851293
require.NoError(t, err)
1286-
require.Equal(t, tc.promBody.Data, resp.(*tripperware.PrometheusResponse).Data)
1294+
sortPrometheusResponseHeader(tc.promBody.Headers)
1295+
sortPrometheusResponseHeader(resp.(*tripperware.PrometheusResponse).Headers)
1296+
require.Equal(t, tc.promBody, resp)
12871297
}
12881298
})
12891299
}

0 commit comments

Comments
 (0)