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 1f5c5c1
Showing 1 changed file with 30 additions and 52 deletions.
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

0 comments on commit 1f5c5c1

Please sign in to comment.