From ab692ff59029e4b93047a9dbdba76fdd012b7d8c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 12 Jun 2019 17:20:06 +0300 Subject: [PATCH] remove mock data from memory registry memory registry can be used as fast inprocess registry, so mock data needs to be in tests only Signed-off-by: Vasiliy Tolstov --- broker/common_test.go | 51 +++++++++++++++++++++++ broker/http_broker_test.go | 2 +- client/common_test.go | 51 +++++++++++++++++++++++ client/rpc_client_test.go | 5 ++- registry/memory/data.go => common_test.go | 4 +- function_test.go | 2 +- registry/memory/memory.go | 2 + selector/common_test.go | 51 +++++++++++++++++++++++ selector/default_test.go | 3 +- service_test.go | 2 +- 10 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 broker/common_test.go create mode 100644 client/common_test.go rename registry/memory/data.go => common_test.go (92%) create mode 100644 selector/common_test.go diff --git a/broker/common_test.go b/broker/common_test.go new file mode 100644 index 00000000..af54db18 --- /dev/null +++ b/broker/common_test.go @@ -0,0 +1,51 @@ +package broker + +import ( + "github.com/micro/go-micro/registry" +) + +var ( + // mock data + testData = 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, + }, + }, + }, + { + Name: "foo", + Version: "1.0.1", + Nodes: []*registry.Node{ + { + Id: "foo-1.0.1-321", + Address: "localhost", + Port: 6666, + }, + }, + }, + { + Name: "foo", + Version: "1.0.3", + Nodes: []*registry.Node{ + { + Id: "foo-1.0.3-345", + Address: "localhost", + Port: 8888, + }, + }, + }, + }, + } +) diff --git a/broker/http_broker_test.go b/broker/http_broker_test.go index 69bad10e..83db509b 100644 --- a/broker/http_broker_test.go +++ b/broker/http_broker_test.go @@ -14,7 +14,7 @@ import ( func newTestRegistry() *memory.Registry { r := memory.NewRegistry() m := r.(*memory.Registry) - m.Setup() + m.Services = testData return m } diff --git a/client/common_test.go b/client/common_test.go new file mode 100644 index 00000000..d172812b --- /dev/null +++ b/client/common_test.go @@ -0,0 +1,51 @@ +package client + +import ( + "github.com/micro/go-micro/registry" +) + +var ( + // mock data + testData = 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, + }, + }, + }, + { + Name: "foo", + Version: "1.0.1", + Nodes: []*registry.Node{ + { + Id: "foo-1.0.1-321", + Address: "localhost", + Port: 6666, + }, + }, + }, + { + Name: "foo", + Version: "1.0.3", + Nodes: []*registry.Node{ + { + Id: "foo-1.0.3-345", + Address: "localhost", + Port: 8888, + }, + }, + }, + }, + } +) diff --git a/client/rpc_client_test.go b/client/rpc_client_test.go index 26fa1e77..219580cd 100644 --- a/client/rpc_client_test.go +++ b/client/rpc_client_test.go @@ -13,8 +13,9 @@ import ( func newTestRegistry() registry.Registry { r := memory.NewRegistry() - r.(*memory.Registry).Setup() - return r + reg := r.(*memory.Registry) + reg.Services = testData + return reg } func TestCallAddress(t *testing.T) { diff --git a/registry/memory/data.go b/common_test.go similarity index 92% rename from registry/memory/data.go rename to common_test.go index 7999fea8..612cdb37 100644 --- a/registry/memory/data.go +++ b/common_test.go @@ -1,4 +1,4 @@ -package memory +package micro import ( "github.com/micro/go-micro/registry" @@ -6,7 +6,7 @@ import ( var ( // mock data - Data = map[string][]*registry.Service{ + testData = map[string][]*registry.Service{ "foo": []*registry.Service{ { Name: "foo", diff --git a/function_test.go b/function_test.go index 49454929..dd85590f 100644 --- a/function_test.go +++ b/function_test.go @@ -14,7 +14,7 @@ func TestFunction(t *testing.T) { wg.Add(1) r := memory.NewRegistry() - r.(*memory.Registry).Setup() + r.(*memory.Registry).Services = testData // create service fn := NewFunction( diff --git a/registry/memory/memory.go b/registry/memory/memory.go index 64b7d4ca..e159ce61 100644 --- a/registry/memory/memory.go +++ b/registry/memory/memory.go @@ -22,6 +22,7 @@ var ( timeout = time.Millisecond * 10 ) +/* // Setup sets mock data func (m *Registry) Setup() { m.Lock() @@ -30,6 +31,7 @@ func (m *Registry) Setup() { // add some memory data m.Services = Data } +*/ func (m *Registry) watch(r *registry.Result) { var watchers []*Watcher diff --git a/selector/common_test.go b/selector/common_test.go new file mode 100644 index 00000000..aa8c15c7 --- /dev/null +++ b/selector/common_test.go @@ -0,0 +1,51 @@ +package selector + +import ( + "github.com/micro/go-micro/registry" +) + +var ( + // mock data + testData = 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, + }, + }, + }, + { + Name: "foo", + Version: "1.0.1", + Nodes: []*registry.Node{ + { + Id: "foo-1.0.1-321", + Address: "localhost", + Port: 6666, + }, + }, + }, + { + Name: "foo", + Version: "1.0.3", + Nodes: []*registry.Node{ + { + Id: "foo-1.0.3-345", + Address: "localhost", + Port: 8888, + }, + }, + }, + }, + } +) diff --git a/selector/default_test.go b/selector/default_test.go index 53f8859d..77075204 100644 --- a/selector/default_test.go +++ b/selector/default_test.go @@ -10,7 +10,8 @@ func TestRegistrySelector(t *testing.T) { counts := map[string]int{} r := memory.NewRegistry() - r.(*memory.Registry).Setup() + rg := r.(*memory.Registry) + rg.Services = testData cache := NewSelector(Registry(r)) next, err := cache.Select("foo") diff --git a/service_test.go b/service_test.go index 6d4cbbc0..8d4c4316 100644 --- a/service_test.go +++ b/service_test.go @@ -30,7 +30,7 @@ func testService(ctx context.Context, wg *sync.WaitGroup, name string) Service { wg.Add(1) r := memory.NewRegistry() - r.(*memory.Registry).Setup() + r.(*memory.Registry).Services = testData // create service return NewService(