2015-12-09 22:23:16 +03:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
2016-02-26 03:09:06 +03:00
|
|
|
type MockRegistry struct {
|
|
|
|
Services map[string][]*registry.Service
|
|
|
|
}
|
2015-12-09 22:23:16 +03:00
|
|
|
|
2016-03-14 22:15:42 +03:00
|
|
|
var (
|
|
|
|
mockData = map[string][]*registry.Service{
|
|
|
|
"foo": []*registry.Service{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Version: "1.0.0",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
{
|
|
|
|
Id: "foo-1.0.0-123",
|
|
|
|
Address: "localhost",
|
|
|
|
Port: 9999,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "foo-1.0.0-321",
|
|
|
|
Address: "localhost",
|
|
|
|
Port: 9999,
|
|
|
|
},
|
2015-12-09 22:23:16 +03:00
|
|
|
},
|
|
|
|
},
|
2016-03-14 22:15:42 +03:00
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Version: "1.0.1",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
{
|
|
|
|
Id: "foo-1.0.1-321",
|
|
|
|
Address: "localhost",
|
|
|
|
Port: 6666,
|
|
|
|
},
|
2015-12-09 22:23:16 +03:00
|
|
|
},
|
|
|
|
},
|
2016-03-14 22:15:42 +03:00
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Version: "1.0.3",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
{
|
|
|
|
Id: "foo-1.0.3-345",
|
|
|
|
Address: "localhost",
|
|
|
|
Port: 8888,
|
|
|
|
},
|
2015-12-09 22:23:16 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-02-26 03:09:06 +03:00
|
|
|
}
|
2016-03-14 22:15:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func (m *MockRegistry) init() {
|
|
|
|
// add some mock data
|
|
|
|
m.Services = mockData
|
2016-02-26 03:09:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockRegistry) GetService(service string) ([]*registry.Service, error) {
|
|
|
|
s, ok := m.Services[service]
|
|
|
|
if !ok {
|
|
|
|
return nil, registry.ErrNotFound
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
|
2015-12-09 22:23:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockRegistry) ListServices() ([]*registry.Service, error) {
|
2016-02-26 03:09:06 +03:00
|
|
|
var services []*registry.Service
|
|
|
|
for _, service := range m.Services {
|
|
|
|
services = append(services, service...)
|
|
|
|
}
|
|
|
|
return services, nil
|
2015-12-09 22:23:16 +03:00
|
|
|
}
|
|
|
|
|
2016-01-27 03:18:44 +03:00
|
|
|
func (m *MockRegistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
2016-03-14 22:15:42 +03:00
|
|
|
services := addServices(m.Services[s.Name], []*registry.Service{s})
|
|
|
|
m.Services[s.Name] = services
|
2015-12-09 22:23:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockRegistry) Deregister(s *registry.Service) error {
|
2016-03-14 22:15:42 +03:00
|
|
|
services := delServices(m.Services[s.Name], []*registry.Service{s})
|
|
|
|
m.Services[s.Name] = services
|
2015-12-09 22:23:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockRegistry) Watch() (registry.Watcher, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-12-20 00:56:14 +03:00
|
|
|
func (m *MockRegistry) String() string {
|
|
|
|
return "mock"
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:23:16 +03:00
|
|
|
func NewRegistry() *MockRegistry {
|
2016-02-26 03:09:06 +03:00
|
|
|
m := &MockRegistry{Services: make(map[string][]*registry.Service)}
|
|
|
|
m.init()
|
|
|
|
return m
|
2015-12-09 22:23:16 +03:00
|
|
|
}
|