diff --git a/selector/roundrobin/roundrobin.go b/selector/roundrobin/roundrobin.go index bb83b642..19cf14ca 100644 --- a/selector/roundrobin/roundrobin.go +++ b/selector/roundrobin/roundrobin.go @@ -1,6 +1,8 @@ package roundrobin import ( + "math/rand" + "github.com/unistack-org/micro/v3/selector" ) @@ -17,7 +19,7 @@ func (r *roundrobin) Select(routes []string, opts ...selector.SelectOption) (sel return nil, selector.ErrNoneAvailable } - var i int + i := rand.Intn(len(routes)) return func() string { route := routes[i%len(routes)] diff --git a/selector/roundrobin/roundrobin_test.go b/selector/roundrobin/roundrobin_test.go index d416d239..0ceb082e 100644 --- a/selector/roundrobin/roundrobin_test.go +++ b/selector/roundrobin/roundrobin_test.go @@ -28,13 +28,22 @@ func TestRoundRobin(t *testing.T) { assert.Nil(t, err, "Error should be nil") assert.Equal(t, r2, r, "Expected route to be r2") - // Because r1 and r2 have been recently called, r3 should be chosen - - next, err = sel.Select([]string{r1, r2, r3}) - n1, n2, n3 := next(), next(), next() + routes := []string{r1, r2, r3} + next, err = sel.Select(routes) assert.Nil(t, err, "Error should be nil") - assert.Equal(t, r1, n1, "Expected route to be r3") - assert.Equal(t, r2, n2, "Expected route to be r3") - assert.Equal(t, r3, n3, "Expected route to be r3") + n1, n2, n3, n4 := next(), next(), next(), next() + // start element is random but then it should loop through in order + start := -1 + for i := 0; i < 3; i++ { + if n1 == routes[i] { + start = i + break + } + } + assert.NotEqual(t, start, -1) + assert.Equal(t, routes[start], n1, "Unexpected route") + assert.Equal(t, routes[(start+1)%3], n2, "Unexpected route") + assert.Equal(t, routes[(start+2)%3], n3, "Unexpected route") + assert.Equal(t, routes[(start+3)%3], n4, "Unexpected route") }