Skip to content

Commit

Permalink
Refactored code inside healthy_endpoints_test.go to reduce repetitions (
Browse files Browse the repository at this point in the history
#2999)

Signed-off-by: Roman Zavodskikh <[email protected]>
Co-authored-by: Roman Zavodskikh <[email protected]>
  • Loading branch information
RomanZavodskikh and Roman Zavodskikh authored Apr 4, 2024
1 parent cbf7d04 commit 1eeaab1
Showing 1 changed file with 75 additions and 106 deletions.
181 changes: 75 additions & 106 deletions proxy/healthy_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@ 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)
}
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)
}
require.NoError(t, err)
return rsp
}

Expand All @@ -52,6 +48,20 @@ func sendGetRequests(t *testing.T, ps *httptest.Server) (failed int) {
return
}

func setupProxy(t *testing.T, doc string) (*testProxy, *httptest.Server) {
endpointRegistry := defaultEndpointRegistry()

tp, err := newTestProxyWithParams(doc, Params{
EnablePassiveHealthCheck: true,
EndpointRegistry: endpointRegistry,
})
require.NoError(t, err)

ps := httptest.NewServer(tp.proxy)

return tp, ps
}

func TestPHCWithoutRequests(t *testing.T) {
services := []*httptest.Server{}
for i := 0; i < 3; i++ {
Expand All @@ -63,43 +73,32 @@ func TestPHCWithoutRequests(t *testing.T) {
}

for _, algorithm := range []string{"random", "consistentHash", "roundRobin", "powerOfRandomNChoices"} {
balanceFactors := []bool{false}
if algorithm == "consistentHash" {
balanceFactors = []bool{false, true}
}

for _, balanceFactor := range balanceFactors {
t.Run(fmt.Sprintf("%s_%t", algorithm, balanceFactor), func(t *testing.T) {
endpointRegistry := defaultEndpointRegistry()

balanceFactorStr := ""
if balanceFactor {
balanceFactorStr = ` -> consistentHashBalanceFactor(1.25)`
}
doc := fmt.Sprintf(`* %s -> <%s, "%s", "%s", "%s">`,
balanceFactorStr, algorithm, services[0].URL, services[1].URL, services[2].URL)

tp, err := newTestProxyWithParams(doc, Params{
EnablePassiveHealthCheck: true,
EndpointRegistry: endpointRegistry,
})
if err != nil {
t.Fatal(err)
}
defer tp.close()

ps := httptest.NewServer(tp.proxy)
defer ps.Close()

rsp := sendGetRequest(t, ps, 0)
assert.Equal(t, http.StatusOK, rsp.StatusCode)
rsp.Body.Close()

time.Sleep(10 * period)
/* this test is needed to check PHC will not crash without requests sent during period at all */
})
}
t.Run(algorithm, func(t *testing.T) {
tp, ps := setupProxy(t, fmt.Sprintf(`* -> <%s, "%s", "%s", "%s">`,
algorithm, services[0].URL, services[1].URL, services[2].URL))
defer tp.close()
defer ps.Close()
rsp := sendGetRequest(t, ps, 0)
assert.Equal(t, http.StatusOK, rsp.StatusCode)
rsp.Body.Close()

time.Sleep(10 * period)
/* this test is needed to check PHC will not crash without requests sent during period at all */
})
}

t.Run("consistent hash with balance factor", func(t *testing.T) {
tp, ps := setupProxy(t, fmt.Sprintf(`* -> consistentHashBalanceFactor(1.25) -> <consistentHash, "%s", "%s", "%s">`,
services[0].URL, services[1].URL, services[2].URL))
defer tp.close()
defer ps.Close()
rsp := sendGetRequest(t, ps, 0)
assert.Equal(t, http.StatusOK, rsp.StatusCode)
rsp.Body.Close()

time.Sleep(10 * period)
/* this test is needed to check PHC will not crash without requests sent during period at all */
})
}

func TestPHCForSingleHealthyEndpoint(t *testing.T) {
Expand Down Expand Up @@ -137,39 +136,24 @@ func TestPHCForMultipleHealthyEndpoints(t *testing.T) {
}

for _, algorithm := range []string{"random", "consistentHash", "roundRobin", "powerOfRandomNChoices"} {
balanceFactors := []bool{false}
if algorithm == "consistentHash" {
balanceFactors = []bool{false, true}
}

for _, balanceFactor := range balanceFactors {
t.Run(fmt.Sprintf("%s_%t", algorithm, balanceFactor), func(t *testing.T) {
endpointRegistry := defaultEndpointRegistry()

balanceFactorStr := ""
if balanceFactor {
balanceFactorStr = ` -> consistentHashBalanceFactor(1.25)`
}
doc := fmt.Sprintf(`* -> consistentHashKey("${request.header.ConsistentHashKey}") %s -> <%s, "%s", "%s", "%s">`,
balanceFactorStr, algorithm, services[0].URL, services[1].URL, services[2].URL)

tp, err := newTestProxyWithParams(doc, Params{
EnablePassiveHealthCheck: true,
EndpointRegistry: endpointRegistry,
})
if err != nil {
t.Fatal(err)
}
defer tp.close()

ps := httptest.NewServer(tp.proxy)
defer ps.Close()

failedReqs := sendGetRequests(t, ps)
assert.Equal(t, 0, failedReqs)
})
}
t.Run(algorithm, func(t *testing.T) {
tp, ps := setupProxy(t, fmt.Sprintf(`* -> consistentHashKey("${request.header.ConsistentHashKey}") -> <%s, "%s", "%s", "%s">`,
algorithm, services[0].URL, services[1].URL, services[2].URL))
defer tp.close()
defer ps.Close()
failedReqs := sendGetRequests(t, ps)
assert.Equal(t, 0, failedReqs)
})
}

t.Run("consistent hash with balance factor", func(t *testing.T) {
tp, ps := setupProxy(t, fmt.Sprintf(`* -> consistentHashKey("${request.header.ConsistentHashKey}") -> consistentHashBalanceFactor(1.25) -> <consistentHash, "%s", "%s", "%s">`,
services[0].URL, services[1].URL, services[2].URL))
defer tp.close()
defer ps.Close()
failedReqs := sendGetRequests(t, ps)
assert.Equal(t, 0, failedReqs)
})
}

func TestPHCForMultipleHealthyAndOneUnhealthyEndpoints(t *testing.T) {
Expand All @@ -188,37 +172,22 @@ func TestPHCForMultipleHealthyAndOneUnhealthyEndpoints(t *testing.T) {
}

for _, algorithm := range []string{"random", "consistentHash", "roundRobin", "powerOfRandomNChoices"} {
balanceFactors := []bool{false}
if algorithm == "consistentHash" {
balanceFactors = []bool{false, true}
}

for _, balanceFactor := range balanceFactors {
t.Run(fmt.Sprintf("%s_%t", algorithm, balanceFactor), func(t *testing.T) {
endpointRegistry := defaultEndpointRegistry()

balanceFactorStr := ""
if balanceFactor {
balanceFactorStr = ` -> consistentHashBalanceFactor(1.25)`
}
doc := fmt.Sprintf(`* -> backendTimeout("5ms") -> consistentHashKey("${request.header.ConsistentHashKey}") %s -> <%s, "%s", "%s", "%s">`,
balanceFactorStr, algorithm, services[0].URL, services[1].URL, services[2].URL)

tp, err := newTestProxyWithParams(doc, Params{
EnablePassiveHealthCheck: true,
EndpointRegistry: endpointRegistry,
})
if err != nil {
t.Fatal(err)
}
defer tp.close()

ps := httptest.NewServer(tp.proxy)
defer ps.Close()

failedReqs := sendGetRequests(t, ps)
assert.InDelta(t, 0.33*rtFailureProbability*(1.0-rtFailureProbability)*float64(nRequests), failedReqs, 0.1*float64(nRequests))
})
}
t.Run(algorithm, func(t *testing.T) {
tp, ps := setupProxy(t, fmt.Sprintf(`* -> backendTimeout("5ms") -> consistentHashKey("${request.header.ConsistentHashKey}") -> <%s, "%s", "%s", "%s">`,
algorithm, services[0].URL, services[1].URL, services[2].URL))
defer tp.close()
defer ps.Close()
failedReqs := sendGetRequests(t, ps)
assert.InDelta(t, 0.33*rtFailureProbability*(1.0-rtFailureProbability)*float64(nRequests), failedReqs, 0.1*float64(nRequests))
})
}

t.Run("consistent hash with balance factor", func(t *testing.T) {
tp, ps := setupProxy(t, fmt.Sprintf(`* -> backendTimeout("5ms") -> consistentHashKey("${request.header.ConsistentHashKey}") -> consistentHashBalanceFactor(1.25) -> <consistentHash, "%s", "%s", "%s">`,
services[0].URL, services[1].URL, services[2].URL))
defer tp.close()
defer ps.Close()
failedReqs := sendGetRequests(t, ps)
assert.InDelta(t, 0.33*rtFailureProbability*(1.0-rtFailureProbability)*float64(nRequests), failedReqs, 0.1*float64(nRequests))
})
}

0 comments on commit 1eeaab1

Please sign in to comment.