2020-06-30 16:54:38 +03:00
|
|
|
package roundrobin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-08-20 15:23:41 +03:00
|
|
|
"github.com/unistack-org/micro/v3/selector"
|
2020-06-30 16:54:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRoundRobin(t *testing.T) {
|
|
|
|
selector.Tests(t, NewSelector())
|
|
|
|
|
2020-08-18 00:44:45 +03:00
|
|
|
r1 := "127.0.0.1:8000"
|
|
|
|
r2 := "127.0.0.1:8001"
|
|
|
|
r3 := "127.0.0.1:8002"
|
2020-06-30 16:54:38 +03:00
|
|
|
|
|
|
|
sel := NewSelector()
|
|
|
|
|
|
|
|
// By passing r1 and r2 first, it forces a set sequence of (r1 => r2 => r3 => r1)
|
|
|
|
|
2020-08-18 00:44:45 +03:00
|
|
|
next, err := sel.Select([]string{r1})
|
|
|
|
r := next()
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
2020-08-18 00:44:45 +03:00
|
|
|
assert.Equal(t, r1, r, "Expected route to be r1")
|
2020-06-30 16:54:38 +03:00
|
|
|
|
2020-08-18 00:44:45 +03:00
|
|
|
next, err = sel.Select([]string{r2})
|
|
|
|
r = next()
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
2020-08-18 00:44:45 +03:00
|
|
|
assert.Equal(t, r2, r, "Expected route to be r2")
|
2020-06-30 16:54:38 +03:00
|
|
|
|
2020-11-30 22:48:00 +03:00
|
|
|
routes := []string{r1, r2, r3}
|
|
|
|
next, err = sel.Select(routes)
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
2020-11-30 22:48:00 +03:00
|
|
|
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")
|
2020-06-30 16:54:38 +03:00
|
|
|
}
|