Skip to content

Commit

Permalink
Refactor healthy_endpoints_test.go
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Zavodskikh <[email protected]>
  • Loading branch information
Roman Zavodskikh committed Mar 15, 2024
1 parent 1077e46 commit e81b56f
Showing 1 changed file with 29 additions and 52 deletions.
81 changes: 29 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,30 @@ 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()
}
}

Check failure on line 52 in proxy/healthy_endpoints_test.go

View workflow job for this annotation

GitHub Actions / check-race

missing return

Check failure on line 52 in proxy/healthy_endpoints_test.go

View workflow job for this annotation

GitHub Actions / tests

missing return

func TestPHCWithoutRequests(t *testing.T) {
services := []*httptest.Server{}
for i := 0; i < 3; i++ {
Expand Down Expand Up @@ -54,10 +79,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 +109,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 +141,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 +180,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

0 comments on commit e81b56f

Please sign in to comment.