From 8ac8585e44a6ef2217314db758767616b7ed6546 Mon Sep 17 00:00:00 2001 From: Devin Trejo Date: Fri, 26 Aug 2022 16:18:44 -0400 Subject: [PATCH] Test for empty set of provided uris --- .../httpclient/internal/rr_selector.go | 5 +++ .../httpclient/internal/rr_selector_test.go | 44 +++++++++++-------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/conjure-go-client/httpclient/internal/rr_selector.go b/conjure-go-client/httpclient/internal/rr_selector.go index 5ff95f72..67d97134 100644 --- a/conjure-go-client/httpclient/internal/rr_selector.go +++ b/conjure-go-client/httpclient/internal/rr_selector.go @@ -18,6 +18,8 @@ import ( "math/rand" "net/http" "sync" + + werror "github.com/palantir/witchcraft-go-error" ) type roundRobinSelector struct { @@ -42,6 +44,9 @@ func NewRoundRobinURISelector(nanoClock func() int64) URISelector { func (s *roundRobinSelector) Select(uris []string, _ http.Header) ([]string, error) { s.Lock() defer s.Unlock() + if len(uris) == 0 { + return nil, werror.Error("no valid uris provided to round robin uri-selector") + } s.updateURIs(uris) s.offset = (s.offset + 1) % len(uris) diff --git a/conjure-go-client/httpclient/internal/rr_selector_test.go b/conjure-go-client/httpclient/internal/rr_selector_test.go index cb0606ba..38637999 100644 --- a/conjure-go-client/httpclient/internal/rr_selector_test.go +++ b/conjure-go-client/httpclient/internal/rr_selector_test.go @@ -19,27 +19,35 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestRoundRobinSelector_Select(t *testing.T) { - uris := []string{"uri1", "uri2", "uri3", "uri4", "uri5"} scorer := NewRoundRobinURISelector(func() int64 { return time.Now().UnixNano() }) - const iterations = 100 - observed := make(map[string]int, iterations) - for i := 0; i < iterations; i++ { - uri, err := scorer.Select(uris, nil) - assert.NoError(t, err) - assert.Len(t, uri, 1) - observed[uri[0]] = observed[uri[0]] + 1 - } - - occurences := make([]int, 0, len(observed)) - for _, count := range observed { - occurences = append(occurences, count) - } - - for _, v := range occurences { - assert.Equal(t, occurences[0], v) - } + t.Run("round robins across valid connections", func(t *testing.T) { + uris := []string{"uri1", "uri2", "uri3", "uri4", "uri5"} + const iterations = 100 + observed := make(map[string]int, iterations) + for i := 0; i < iterations; i++ { + uri, err := scorer.Select(uris, nil) + assert.NoError(t, err) + assert.Len(t, uri, 1) + observed[uri[0]] = observed[uri[0]] + 1 + } + + occurences := make([]int, 0, len(observed)) + for _, count := range observed { + occurences = append(occurences, count) + } + + for _, v := range occurences { + assert.Equal(t, occurences[0], v) + } + }) + + t.Run("erorrs with empty set of provided uris", func(t *testing.T) { + _, err := scorer.Select([]string{}, nil) + require.Error(t, err) + }) }