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 c74816f
Showing 1 changed file with 43 additions and 46 deletions.
89 changes: 43 additions & 46 deletions proxy/healthy_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@ func defaultEndpointRegistry() *routing.EndpointRegistry {
})
}

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

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

return rsp, nil
}

func sendGetRequests(t *testing.T, ps *httptest.Server) (int, error) {
failedReqs := 0

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

if rsp.StatusCode != http.StatusOK {
failedReqs++
}
rsp.Body.Close()
}

return failedReqs, nil
}

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

rsp, err := ps.Client().Get(ps.URL)
rsp, err := sendGetRequest(t, ps, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -87,17 +120,9 @@ 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, err := sendGetRequests(t, ps)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 0, failedReqs)
}
Expand Down Expand Up @@ -130,23 +155,9 @@ 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, err := sendGetRequests(t, ps)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 0, failedReqs)
})
Expand Down Expand Up @@ -186,23 +197,9 @@ 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, err := sendGetRequests(t, ps)
if err != nil {
t.Fatal(err)
}
assert.InDelta(t, 0.33*rtFailureProbability*(1.0-rtFailureProbability)*float64(nRequests), failedReqs, 0.1*float64(nRequests))
})
Expand Down

0 comments on commit c74816f

Please sign in to comment.