2020-06-30 14:54:38 +01:00
|
|
|
package roundrobin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/selector"
|
2020-06-30 14:54:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRoundRobin(t *testing.T) {
|
|
|
|
selector.Tests(t, NewSelector())
|
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
r1 := "127.0.0.1:8000"
|
|
|
|
r2 := "127.0.0.1:8001"
|
|
|
|
r3 := "127.0.0.1:8002"
|
2020-06-30 14:54:38 +01:00
|
|
|
|
|
|
|
sel := NewSelector()
|
|
|
|
|
|
|
|
// By passing r1 and r2 first, it forces a set sequence of (r1 => r2 => r3 => r1)
|
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
next, err := sel.Select([]string{r1})
|
2021-02-13 15:35:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-17 22:44:45 +01:00
|
|
|
r := next()
|
2021-02-13 15:35:56 +03:00
|
|
|
|
|
|
|
if r1 != r {
|
|
|
|
t.Fatal("Expected route to be r == r1")
|
|
|
|
}
|
2020-06-30 14:54:38 +01:00
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
next, err = sel.Select([]string{r2})
|
2021-02-13 15:35:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-17 22:44:45 +01:00
|
|
|
r = next()
|
2021-02-13 15:35:56 +03:00
|
|
|
if r2 != r {
|
|
|
|
t.Fatal("Expected route to be r2")
|
|
|
|
}
|
2020-06-30 14:54:38 +01:00
|
|
|
|
2020-11-30 22:48:00 +03:00
|
|
|
routes := []string{r1, r2, r3}
|
|
|
|
next, err = sel.Select(routes)
|
2021-02-13 15:35:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-02-13 15:35:56 +03:00
|
|
|
if start == -1 {
|
|
|
|
t.Fatalf("start == -1 %v %v", start, -1)
|
|
|
|
}
|
|
|
|
if routes[start] != n1 {
|
|
|
|
t.Fatal("Unexpected route")
|
|
|
|
}
|
|
|
|
if routes[(start+1)%3] != n2 {
|
|
|
|
t.Fatal("Unexpected route")
|
|
|
|
}
|
|
|
|
if routes[(start+2)%3] != n3 {
|
|
|
|
t.Fatal("Unexpected route")
|
|
|
|
}
|
|
|
|
if routes[(start+3)%3] != n4 {
|
|
|
|
t.Fatal("Unexpected route")
|
|
|
|
}
|
2020-06-30 14:54:38 +01:00
|
|
|
}
|