Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor healthy_endpoints_test.go #2986

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 30 additions & 52 deletions proxy/healthy_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zalando/skipper/routing"
)

Expand All @@ -26,6 +27,31 @@ func defaultEndpointRegistry() *routing.EndpointRegistry {
})
}

func sendGetRequest(t *testing.T, ps *httptest.Server, consistentHashKey int) *http.Response {
req, err := http.NewRequest("GET", ps.URL, nil)
if err != nil {
require.NoError(t, err)
}
req.Header.Add("ConsistentHashKey", fmt.Sprintf("%d", consistentHashKey))

rsp, err := ps.Client().Do(req)
if err != nil {
require.NoError(t, err)
}
return rsp
}

func sendGetRequests(t *testing.T, ps *httptest.Server) (failed int) {
for i := 0; i < nRequests; i++ {
rsp := sendGetRequest(t, ps, i)
if rsp.StatusCode != http.StatusOK {
failed++
}
rsp.Body.Close()
}
return
}

func TestPHCWithoutRequests(t *testing.T) {
services := []*httptest.Server{}
for i := 0; i < 3; i++ {
Expand Down Expand Up @@ -54,10 +80,7 @@ func TestPHCWithoutRequests(t *testing.T) {
ps := httptest.NewServer(tp.proxy)
defer ps.Close()

rsp, err := ps.Client().Get(ps.URL)
if err != nil {
t.Fatal(err)
}
rsp := sendGetRequest(t, ps, 0)
assert.Equal(t, http.StatusOK, rsp.StatusCode)
rsp.Body.Close()

Expand Down Expand Up @@ -87,18 +110,7 @@ func TestPHCForSingleHealthyEndpoint(t *testing.T) {
ps := httptest.NewServer(tp.proxy)
defer ps.Close()

failedReqs := 0
for i := 0; i < nRequests; i++ {
rsp, err := ps.Client().Get(ps.URL)
if err != nil {
t.Fatal(err)
}

if rsp.StatusCode != http.StatusOK {
failedReqs++
}
rsp.Body.Close()
}
failedReqs := sendGetRequests(t, ps)
assert.Equal(t, 0, failedReqs)
}

Expand Down Expand Up @@ -130,24 +142,7 @@ func TestPHCForMultipleHealthyEndpoints(t *testing.T) {
ps := httptest.NewServer(tp.proxy)
defer ps.Close()

failedReqs := 0
for i := 0; i < nRequests; i++ {
req, err := http.NewRequest("GET", ps.URL, nil)
if err != nil {
t.Fatal(err)
}
req.Header.Add("ConsistentHashKey", fmt.Sprintf("%d", i))

rsp, err := ps.Client().Do(req)
if err != nil {
t.Fatal(err)
}

if rsp.StatusCode != http.StatusOK {
failedReqs++
}
rsp.Body.Close()
}
failedReqs := sendGetRequests(t, ps)
assert.Equal(t, 0, failedReqs)
})
}
Expand Down Expand Up @@ -186,24 +181,7 @@ func TestPHCForMultipleHealthyAndOneUnhealthyEndpoints(t *testing.T) {
ps := httptest.NewServer(tp.proxy)
defer ps.Close()

failedReqs := 0
for i := 0; i < nRequests; i++ {
req, err := http.NewRequest("GET", ps.URL, nil)
if err != nil {
t.Fatal(err)
}
req.Header.Add("ConsistentHashKey", fmt.Sprintf("%d", i))

rsp, err := ps.Client().Do(req)
if err != nil {
t.Fatal(err)
}

if rsp.StatusCode != http.StatusOK {
failedReqs++
}
rsp.Body.Close()
}
failedReqs := sendGetRequests(t, ps)
assert.InDelta(t, 0.33*rtFailureProbability*(1.0-rtFailureProbability)*float64(nRequests), failedReqs, 0.1*float64(nRequests))
})
}
Expand Down
Loading