make the mock sliggghtly more useful
This commit is contained in:
parent
93b923261c
commit
ad0744a95f
@ -4,10 +4,13 @@ import (
|
|||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockRegistry struct{}
|
type MockRegistry struct {
|
||||||
|
Services map[string][]*registry.Service
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MockRegistry) GetService(service string) ([]*registry.Service, error) {
|
func (m *MockRegistry) init() {
|
||||||
return []*registry.Service{
|
// add some mock data
|
||||||
|
m.Services["foo"] = []*registry.Service{
|
||||||
{
|
{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Version: "1.0.0",
|
Version: "1.0.0",
|
||||||
@ -46,11 +49,24 @@ func (m *MockRegistry) GetService(service string) ([]*registry.Service, error) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, nil
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockRegistry) GetService(service string) ([]*registry.Service, error) {
|
||||||
|
s, ok := m.Services[service]
|
||||||
|
if !ok {
|
||||||
|
return nil, registry.ErrNotFound
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockRegistry) ListServices() ([]*registry.Service, error) {
|
func (m *MockRegistry) ListServices() ([]*registry.Service, error) {
|
||||||
return []*registry.Service{}, nil
|
var services []*registry.Service
|
||||||
|
for _, service := range m.Services {
|
||||||
|
services = append(services, service...)
|
||||||
|
}
|
||||||
|
return services, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockRegistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
func (m *MockRegistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
||||||
@ -70,5 +86,7 @@ func (m *MockRegistry) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewRegistry() *MockRegistry {
|
func NewRegistry() *MockRegistry {
|
||||||
return &MockRegistry{}
|
m := &MockRegistry{Services: make(map[string][]*registry.Service)}
|
||||||
|
m.init()
|
||||||
|
return m
|
||||||
}
|
}
|
||||||
|
@ -9,13 +9,13 @@ import (
|
|||||||
func TestRandomSelector(t *testing.T) {
|
func TestRandomSelector(t *testing.T) {
|
||||||
counts := map[string]int{}
|
counts := map[string]int{}
|
||||||
|
|
||||||
bl := &randomSelector{
|
rs := &randomSelector{
|
||||||
so: Options{
|
so: Options{
|
||||||
Registry: mock.NewRegistry(),
|
Registry: mock.NewRegistry(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
next, err := bl.Select("foo")
|
next, err := rs.Select("foo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error calling random select: %v", err)
|
t.Errorf("Unexpected error calling random select: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user