2020-06-30 16:54:38 +03:00
|
|
|
package selector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Tests runs all the tests against a selector to ensure the implementations are consistent
|
|
|
|
func Tests(t *testing.T, s Selector) {
|
2020-08-18 00:44:45 +03:00
|
|
|
r1 := "127.0.0.1:8000"
|
|
|
|
r2 := "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-08-18 00:44:45 +03:00
|
|
|
_, err := s.Select([]string{})
|
2021-02-13 15:35:56 +03:00
|
|
|
if err != ErrNoneAvailable {
|
|
|
|
t.Fatal("Expected error to be none available")
|
|
|
|
}
|
2020-06-30 16:54:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("OneRoute", func(t *testing.T) {
|
2020-08-18 00:44:45 +03:00
|
|
|
next, err := s.Select([]string{r1})
|
2021-02-13 15:35:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error should be nil")
|
|
|
|
}
|
2020-08-18 00:44:45 +03:00
|
|
|
srv := next()
|
2021-02-13 15:35:56 +03:00
|
|
|
if r1 != srv {
|
|
|
|
t.Fatal("Expected the route to be returned")
|
|
|
|
}
|
2020-06-30 16:54:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MultipleRoutes", func(t *testing.T) {
|
2020-08-18 00:44:45 +03:00
|
|
|
next, err := s.Select([]string{r1, r2})
|
2021-02-13 15:35:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error should be nil")
|
|
|
|
}
|
2020-08-18 00:44:45 +03:00
|
|
|
srv := next()
|
|
|
|
if srv != r1 && srv != r2 {
|
2020-06-30 16:54:38 +03:00
|
|
|
t.Errorf("Expected the route to be one of the inputs")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Record", func(t *testing.T) {
|
2021-02-13 15:35:56 +03:00
|
|
|
if err := s.Record(r1, nil); err != nil {
|
|
|
|
t.Fatal("Expected the error to be nil")
|
|
|
|
}
|
2020-06-30 16:54:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("String", func(t *testing.T) {
|
2021-02-13 15:35:56 +03:00
|
|
|
if s.String() == "" {
|
|
|
|
t.Fatal("String returned a blank string")
|
|
|
|
}
|
2020-06-30 16:54:38 +03:00
|
|
|
})
|
|
|
|
}
|