apply micro commit 2eb19c2e97d9316438bc66cd2cda896e8c99d026

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-11-30 22:48:00 +03:00
parent 8d6eb34aee
commit 0ddc8de00b
2 changed files with 19 additions and 8 deletions

View File

@ -1,6 +1,8 @@
package roundrobin package roundrobin
import ( import (
"math/rand"
"github.com/unistack-org/micro/v3/selector" "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 return nil, selector.ErrNoneAvailable
} }
var i int i := rand.Intn(len(routes))
return func() string { return func() string {
route := routes[i%len(routes)] route := routes[i%len(routes)]

View File

@ -28,13 +28,22 @@ func TestRoundRobin(t *testing.T) {
assert.Nil(t, err, "Error should be nil") assert.Nil(t, err, "Error should be nil")
assert.Equal(t, r2, r, "Expected route to be r2") assert.Equal(t, r2, r, "Expected route to be r2")
// Because r1 and r2 have been recently called, r3 should be chosen routes := []string{r1, r2, r3}
next, err = sel.Select(routes)
next, err = sel.Select([]string{r1, r2, r3})
n1, n2, n3 := next(), next(), next()
assert.Nil(t, err, "Error should be nil") assert.Nil(t, err, "Error should be nil")
assert.Equal(t, r1, n1, "Expected route to be r3") n1, n2, n3, n4 := next(), next(), next(), next()
assert.Equal(t, r2, n2, "Expected route to be r3")
assert.Equal(t, r3, n3, "Expected route to be r3")
// 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")
} }