Skip to content

Commit

Permalink
add rateDeviation
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie17Li committed Mar 27, 2023
1 parent 6985c2b commit 4a87fe1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
16 changes: 6 additions & 10 deletions test/ingress/conformance/tests/httproute-canary-weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ var HTTPRouteCanaryWeight = suite.ConformanceTest{
Manifests: []string{"tests/httproute-canary-weight.yaml"},
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
tt := []struct {
assertion http.Assertion
minSuccRate float32
maxSuccRate float32
assertion http.Assertion
succRate float32
}{
{
minSuccRate: 0.9,
maxSuccRate: 1.0,
succRate: 1.0,
assertion: http.Assertion{
// test if the weight is 0
Meta: http.AssertionMeta{
Expand All @@ -57,8 +55,7 @@ var HTTPRouteCanaryWeight = suite.ConformanceTest{
},
},
}, { // test if the weight is 100
minSuccRate: 0.9,
maxSuccRate: 1.0,
succRate: 1.0,
assertion: http.Assertion{
Meta: http.AssertionMeta{
TargetBackend: "infra-backend-v2",
Expand All @@ -77,8 +74,7 @@ var HTTPRouteCanaryWeight = suite.ConformanceTest{
},
},
}, {
minSuccRate: 0.4,
maxSuccRate: 0.6,
succRate: 0.5,
assertion: http.Assertion{
Meta: http.AssertionMeta{
TargetBackend: "infra-backend-v2",
Expand All @@ -101,7 +97,7 @@ var HTTPRouteCanaryWeight = suite.ConformanceTest{

t.Run("Canary HTTPRoute Traffic Split", func(t *testing.T) {
for _, testcase := range tt {
http.MakeRequestAndCountExpectedResponse(t, suite.RoundTripper, suite.GatewayAddress, testcase.assertion, testcase.minSuccRate, testcase.maxSuccRate)
http.MakeRequestAndCountExpectedResponse(t, suite.RoundTripper, suite.GatewayAddress, testcase.assertion, testcase.succRate)
}
})
},
Expand Down
10 changes: 7 additions & 3 deletions test/ingress/conformance/utils/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package http

import (
"fmt"
"math"
"net/url"
"strings"
"testing"
Expand Down Expand Up @@ -96,6 +97,8 @@ const requiredConsecutiveSuccesses = 3

const totalRequest = 100

const rateDeviation = 0.1

// MakeRequestAndExpectEventuallyConsistentResponse makes a request with the given parameters,
// understanding that the request may fail for some amount of time.
//
Expand Down Expand Up @@ -369,8 +372,8 @@ func getRequest(gwAddr string, expected Assertion) roundtripper.Request {
return req
}

// MakeRequestAndCountExpectedResponse make 'totReq' requests and determine whether to test results according to 'fn' callback function
func MakeRequestAndCountExpectedResponse(t *testing.T, r roundtripper.RoundTripper, gwAddr string, expected Assertion, minSuccRate, maxSuccRate float32) {
// MakeRequestAndCountExpectedResponse make 'totReq' requests and determine whether to test results according to the succRate
func MakeRequestAndCountExpectedResponse(t *testing.T, r roundtripper.RoundTripper, gwAddr string, expected Assertion, succRate float64) {
t.Helper()

if expected.Request.ActualRequest.Method == "" {
Expand Down Expand Up @@ -403,7 +406,8 @@ func MakeRequestAndCountExpectedResponse(t *testing.T, r roundtripper.RoundTripp
succ += 1
}

rate := float32(succ) / totalRequest
rate := float64(succ) / totalRequest
minSuccRate, maxSuccRate := math.Min(succRate-rateDeviation, 0), math.Max(succRate+rateDeviation, 1.0)
if 0 < minSuccRate && rate < minSuccRate {
t.Errorf("Test failed, expect the minSuccRate is %v, got %v", minSuccRate, rate)
return
Expand Down

0 comments on commit 4a87fe1

Please sign in to comment.