micro/client/node_selector_test.go

71 lines
1.2 KiB
Go
Raw Normal View History

2015-11-08 04:48:48 +03:00
package client
import (
"testing"
2015-11-20 19:17:33 +03:00
"github.com/micro/go-micro/registry"
2015-12-08 03:08:23 +03:00
"golang.org/x/net/context"
2015-11-08 04:48:48 +03:00
)
2015-12-08 03:08:23 +03:00
type mockRegistry struct{}
func (m *mockRegistry) GetService(service string) ([]*registry.Service, error) {
return []*registry.Service{
2015-11-08 04:48:48 +03:00
{
Name: "foo",
Version: "1.0.0",
Nodes: []*registry.Node{
{
Id: "foo-123",
Address: "localhost",
Port: 9999,
},
},
},
{
Name: "foo",
Version: "1.0.1",
Nodes: []*registry.Node{
{
Id: "foo-321",
Address: "localhost",
Port: 6666,
},
},
},
2015-12-08 03:08:23 +03:00
}, nil
}
func (m *mockRegistry) ListServices() ([]*registry.Service, error) {
return []*registry.Service{}, nil
}
func (m *mockRegistry) Register(s *registry.Service) error {
return nil
}
func (m *mockRegistry) Deregister(s *registry.Service) error {
return nil
}
2015-11-08 04:48:48 +03:00
2015-12-08 03:08:23 +03:00
func (m *mockRegistry) Watch() (registry.Watcher, error) {
return nil, nil
}
func TestNodeSelector(t *testing.T) {
2015-11-08 04:48:48 +03:00
counts := map[string]int{}
2015-12-08 03:08:23 +03:00
n := &nodeSelector{
&mockRegistry{},
}
2015-11-08 04:48:48 +03:00
for i := 0; i < 100; i++ {
2015-12-08 03:11:51 +03:00
n, err := n.Select(context.Background(), newRpcRequest("foo", "Foo.Bar", nil, ""))
2015-11-08 04:48:48 +03:00
if err != nil {
t.Errorf("Expected node, got err: %v", err)
}
counts[n.Id]++
}
t.Logf("Counts %v", counts)
}