2020-06-30 16:54:38 +03:00
|
|
|
package selector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/v2/router"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Tests runs all the tests against a selector to ensure the implementations are consistent
|
|
|
|
func Tests(t *testing.T, s Selector) {
|
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"}
|
2020-06-30 16:54:38 +03:00
|
|
|
|
|
|
|
t.Run("Select", func(t *testing.T) {
|
|
|
|
t.Run("NoRoutes", func(t *testing.T) {
|
2020-06-30 17:51:26 +03:00
|
|
|
srv, err := s.Select([]router.Route{})
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, srv, "Route should be nil")
|
|
|
|
assert.Equal(t, ErrNoneAvailable, err, "Expected error to be none available")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("OneRoute", func(t *testing.T) {
|
2020-06-30 17:51:26 +03:00
|
|
|
srv, err := s.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, *srv, "Expected the route to be returned")
|
2020-06-30 16:54:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MultipleRoutes", func(t *testing.T) {
|
2020-06-30 17:51:26 +03:00
|
|
|
srv, err := s.Select([]router.Route{r1, r2})
|
2020-06-30 16:54:38 +03:00
|
|
|
assert.Nil(t, err, "Error should be nil")
|
|
|
|
if srv.Address != r1.Address && srv.Address != r2.Address {
|
|
|
|
t.Errorf("Expected the route to be one of the inputs")
|
|
|
|
}
|
|
|
|
})
|
2020-07-02 18:09:48 +03:00
|
|
|
|
|
|
|
t.Run("Filters", func(t *testing.T) {
|
|
|
|
var filterApplied bool
|
|
|
|
filter := func(rts []router.Route) []router.Route {
|
|
|
|
filterApplied = true
|
|
|
|
return rts
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := s.Select([]router.Route{r1, r2}, WithFilter(filter))
|
|
|
|
assert.Nil(t, err, "Error should be nil")
|
|
|
|
assert.True(t, filterApplied, "Filters should be applied")
|
|
|
|
})
|
2020-06-30 16:54:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Record", func(t *testing.T) {
|
|
|
|
err := s.Record(r1, nil)
|
|
|
|
assert.Nil(t, err, "Expected the error to be nil")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("String", func(t *testing.T) {
|
|
|
|
assert.NotEmpty(t, s.String(), "String returned a blank string")
|
|
|
|
})
|
|
|
|
}
|