mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
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"
|
||||
)
|
||||
|
||||
type MockRegistry struct{}
|
||||
type MockRegistry struct {
|
||||
Services map[string][]*registry.Service
|
||||
}
|
||||
|
||||
func (m *MockRegistry) GetService(service string) ([]*registry.Service, error) {
|
||||
return []*registry.Service{
|
||||
func (m *MockRegistry) init() {
|
||||
// add some mock data
|
||||
m.Services["foo"] = []*registry.Service{
|
||||
{
|
||||
Name: "foo",
|
||||
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) {
|
||||
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 {
|
||||
@ -70,5 +86,7 @@ func (m *MockRegistry) String() string {
|
||||
}
|
||||
|
||||
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) {
|
||||
counts := map[string]int{}
|
||||
|
||||
bl := &randomSelector{
|
||||
rs := &randomSelector{
|
||||
so: Options{
|
||||
Registry: mock.NewRegistry(),
|
||||
},
|
||||
}
|
||||
|
||||
next, err := bl.Select("foo")
|
||||
next, err := rs.Select("foo")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error calling random select: %v", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user