From 453ce2fcbe049c106ca3fc49c54ee3202f163e83 Mon Sep 17 00:00:00 2001 From: "xinfei.wu" Date: Wed, 9 Jan 2019 14:24:12 +0800 Subject: [PATCH 1/2] add locker --- registry/mock/mock.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/registry/mock/mock.go b/registry/mock/mock.go index da690893..11a145af 100644 --- a/registry/mock/mock.go +++ b/registry/mock/mock.go @@ -1,11 +1,13 @@ -// Package mock provides a mock registry for testing package mock import ( + "sync" + "github.com/micro/go-micro/registry" ) type mockRegistry struct { + sync.RWMutex Services map[string][]*registry.Service } @@ -55,11 +57,17 @@ var ( ) func (m *mockRegistry) init() { + m.Lock() + defer m.Unlock() + // add some mock data m.Services = mockData } func (m *mockRegistry) GetService(service string) ([]*registry.Service, error) { + m.Lock() + defer m.Unlock() + s, ok := m.Services[service] if !ok || len(s) == 0 { return nil, registry.ErrNotFound @@ -69,6 +77,9 @@ func (m *mockRegistry) GetService(service string) ([]*registry.Service, error) { } func (m *mockRegistry) ListServices() ([]*registry.Service, error) { + m.Lock() + defer m.Unlock() + var services []*registry.Service for _, service := range m.Services { services = append(services, service...) @@ -77,12 +88,18 @@ func (m *mockRegistry) ListServices() ([]*registry.Service, error) { } func (m *mockRegistry) Register(s *registry.Service, opts ...registry.RegisterOption) error { + m.Lock() + defer m.Unlock() + services := addServices(m.Services[s.Name], []*registry.Service{s}) m.Services[s.Name] = services return nil } func (m *mockRegistry) Deregister(s *registry.Service) error { + m.Lock() + defer m.Unlock() + services := delServices(m.Services[s.Name], []*registry.Service{s}) m.Services[s.Name] = services return nil From eec1726f1d2475bc34c5f5162b825b7ccb563be7 Mon Sep 17 00:00:00 2001 From: "xinfei.wu" Date: Wed, 9 Jan 2019 16:31:23 +0800 Subject: [PATCH 2/2] add package comment --- registry/mock/mock.go | 1 + 1 file changed, 1 insertion(+) diff --git a/registry/mock/mock.go b/registry/mock/mock.go index 11a145af..6b193ad6 100644 --- a/registry/mock/mock.go +++ b/registry/mock/mock.go @@ -1,3 +1,4 @@ +// Package mock provides a mock registry for testing package mock import (