2020-06-30 16:54:38 +03:00
|
|
|
package roundrobin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/v2/router"
|
|
|
|
"github.com/micro/go-micro/v2/selector"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRoundRobin(t *testing.T) {
|
|
|
|
selector.Tests(t, NewSelector())
|
|
|
|
|
2020-06-30 17:51:26 +03:00
|
|
|
r1 := router.Route{Service: "go.micro.service.foo", Address: "127.0.0.1:8000"}
|
|
|
|
r2 := router.Route{Service: "go.micro.service.foo", Address: "127.0.0.1:8001"}
|
|
|
|
r3 := router.Route{Service: "go.micro.service.foo", Address: "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-06-30 17:51:26 +03:00
|
|
|
r, err := sel.Select([]router.Route{r1})
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
2020-06-30 17:51:26 +03:00
|
|
|
assert.Equal(t, r1, *r, "Expected route to be r1")
|
2020-06-30 16:54:38 +03:00
|
|
|
|
2020-06-30 17:51:26 +03:00
|
|
|
r, err = sel.Select([]router.Route{r2})
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
2020-06-30 17:51:26 +03:00
|
|
|
assert.Equal(t, r2, *r, "Expected route to be r2")
|
2020-06-30 16:54:38 +03:00
|
|
|
|
|
|
|
// Because r1 and r2 have been recently called, r3 should be chosen
|
|
|
|
|
2020-06-30 17:51:26 +03:00
|
|
|
r, err = sel.Select([]router.Route{r1, r2, r3})
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
2020-06-30 17:51:26 +03:00
|
|
|
assert.Equal(t, r3, *r, "Expected route to be r3")
|
2020-06-30 16:54:38 +03:00
|
|
|
|
|
|
|
// r1 was called longest ago, so it should be prioritised
|
|
|
|
|
2020-06-30 17:51:26 +03:00
|
|
|
r, err = sel.Select([]router.Route{r1, r2, r3})
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
2020-06-30 17:51:26 +03:00
|
|
|
assert.Equal(t, r1, *r, "Expected route to be r1")
|
2020-06-30 16:54:38 +03:00
|
|
|
|
2020-06-30 17:51:26 +03:00
|
|
|
r, err = sel.Select([]router.Route{r1, r2, r3})
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
2020-06-30 17:51:26 +03:00
|
|
|
assert.Equal(t, r2, *r, "Expected route to be r2")
|
2020-06-30 16:54:38 +03:00
|
|
|
|
2020-06-30 17:51:26 +03:00
|
|
|
r, err = sel.Select([]router.Route{r1, r2, r3})
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
2020-06-30 17:51:26 +03:00
|
|
|
assert.Equal(t, r3, *r, "Expected route to be r3")
|
2020-06-30 16:54:38 +03:00
|
|
|
}
|