1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/registry/mock/mock.go

103 lines
2.0 KiB
Go
Raw Normal View History

2015-12-09 21:23:16 +02:00
package mock
import (
"github.com/micro/go-micro/registry"
)
2016-03-18 00:54:18 +02:00
type mockRegistry struct {
2016-02-26 02:09:06 +02:00
Services map[string][]*registry.Service
}
2015-12-09 21:23:16 +02:00
2016-03-14 21:15:42 +02: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 21:23:16 +02:00
},
},
2016-03-14 21:15:42 +02:00
{
Name: "foo",
Version: "1.0.1",
Nodes: []*registry.Node{
{
Id: "foo-1.0.1-321",
Address: "localhost",
Port: 6666,
},
2015-12-09 21:23:16 +02:00
},
},
2016-03-14 21:15:42 +02:00
{
Name: "foo",
Version: "1.0.3",
Nodes: []*registry.Node{
{
Id: "foo-1.0.3-345",
Address: "localhost",
Port: 8888,
},
2015-12-09 21:23:16 +02:00
},
},
},
2016-02-26 02:09:06 +02:00
}
2016-03-14 21:15:42 +02:00
)
2016-03-18 00:54:18 +02:00
func (m *mockRegistry) init() {
2016-03-14 21:15:42 +02:00
// add some mock data
m.Services = mockData
2016-02-26 02:09:06 +02:00
}
2016-03-18 00:54:18 +02:00
func (m *mockRegistry) GetService(service string) ([]*registry.Service, error) {
2016-02-26 02:09:06 +02:00
s, ok := m.Services[service]
if !ok || len(s) == 0 {
2016-02-26 02:09:06 +02:00
return nil, registry.ErrNotFound
}
return s, nil
2015-12-09 21:23:16 +02:00
}
2016-03-18 00:54:18 +02:00
func (m *mockRegistry) ListServices() ([]*registry.Service, error) {
2016-02-26 02:09:06 +02:00
var services []*registry.Service
for _, service := range m.Services {
services = append(services, service...)
}
return services, nil
2015-12-09 21:23:16 +02:00
}
2016-03-18 00:54:18 +02:00
func (m *mockRegistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
2016-03-14 21:15:42 +02:00
services := addServices(m.Services[s.Name], []*registry.Service{s})
m.Services[s.Name] = services
2015-12-09 21:23:16 +02:00
return nil
}
2016-03-18 00:54:18 +02:00
func (m *mockRegistry) Deregister(s *registry.Service) error {
2016-03-14 21:15:42 +02:00
services := delServices(m.Services[s.Name], []*registry.Service{s})
m.Services[s.Name] = services
2015-12-09 21:23:16 +02:00
return nil
}
2016-03-18 00:54:18 +02:00
func (m *mockRegistry) Watch() (registry.Watcher, error) {
2016-04-26 19:32:43 +02:00
return &mockWatcher{exit: make(chan bool)}, nil
2015-12-09 21:23:16 +02:00
}
2016-03-18 00:54:18 +02:00
func (m *mockRegistry) String() string {
2015-12-19 23:56:14 +02:00
return "mock"
}
2016-03-18 00:54:18 +02:00
func NewRegistry() registry.Registry {
m := &mockRegistry{Services: make(map[string][]*registry.Service)}
2016-02-26 02:09:06 +02:00
m.init()
return m
2015-12-09 21:23:16 +02:00
}