From 37de747d195c32bd2deaea06d0064bf5e2c89cfa Mon Sep 17 00:00:00 2001 From: Johnson C Date: Fri, 10 Dec 2021 19:32:21 +0800 Subject: [PATCH] [fix] nats deregister issue (#2384) --- plugins/registry/nats/nats.go | 6 +++--- plugins/registry/nats/nats_test.go | 26 +++++++++++++++++--------- plugins/registry/nats/options_test.go | 9 +++++++-- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/plugins/registry/nats/nats.go b/plugins/registry/nats/nats.go index 86b28fbb..221490a3 100644 --- a/plugins/registry/nats/nats.go +++ b/plugins/registry/nats/nats.go @@ -8,9 +8,9 @@ import ( "sync" "time" + "github.com/nats-io/nats.go" "go-micro.dev/v4/cmd" "go-micro.dev/v4/registry" - "github.com/nats-io/nats.go" ) type natsRegistry struct { @@ -81,6 +81,7 @@ func configure(n *natsRegistry, opts ...registry.Option) error { return nil } + func setAddrs(addrs []string) []string { var cAddrs []string for _, addr := range addrs { @@ -205,8 +206,7 @@ func (n *natsRegistry) deregister(s *registry.Service) error { n.Lock() defer n.Unlock() - // cache leftover service - services := addServices(n.services[s.Name], cp([]*registry.Service{s})) + services := delServices(n.services[s.Name], cp([]*registry.Service{s})) if len(services) > 0 { n.services[s.Name] = services return nil diff --git a/plugins/registry/nats/nats_test.go b/plugins/registry/nats/nats_test.go index 344c1411..75d0d118 100644 --- a/plugins/registry/nats/nats_test.go +++ b/plugins/registry/nats/nats_test.go @@ -21,18 +21,26 @@ func TestRegister(t *testing.T) { } func TestDeregister(t *testing.T) { - t.Skip("not properly implemented") + service1 := registry.Service{Name: "test-deregister", Version: "v1"} + service2 := registry.Service{Name: "test-deregister", Version: "v2"} - service := registry.Service{Name: "test"} - - assertNoError(t, e.registryOne.Register(&service)) - assertNoError(t, e.registryOne.Deregister(&service)) - - services, err := e.registryOne.ListServices() + assertNoError(t, e.registryOne.Register(&service1)) + services, err := e.registryOne.GetService(service1.Name) assertNoError(t, err) - assertEqual(t, 0, len(services)) + assertEqual(t, 1, len(services)) - services, err = e.registryTwo.ListServices() + assertNoError(t, e.registryOne.Register(&service2)) + services, err = e.registryOne.GetService(service2.Name) + assertNoError(t, err) + assertEqual(t, 2, len(services)) + + assertNoError(t, e.registryOne.Deregister(&service1)) + services, err = e.registryOne.GetService(service1.Name) + assertNoError(t, err) + assertEqual(t, 1, len(services)) + + assertNoError(t, e.registryOne.Deregister(&service2)) + services, err = e.registryOne.GetService(service1.Name) assertNoError(t, err) assertEqual(t, 0, len(services)) } diff --git a/plugins/registry/nats/options_test.go b/plugins/registry/nats/options_test.go index 468900d2..57f173ee 100644 --- a/plugins/registry/nats/options_test.go +++ b/plugins/registry/nats/options_test.go @@ -8,8 +8,8 @@ import ( "testing" "time" - "go-micro.dev/v4/registry" "github.com/nats-io/nats.go" + "go-micro.dev/v4/registry" ) var addrTestCases = []struct { @@ -35,7 +35,8 @@ var addrTestCases = []struct { "default", "check if default Address is set correctly", map[string]string{ - "nats://localhost:4222": ""}, + nats.DefaultURL: "", + }, }, } @@ -73,6 +74,8 @@ func TestInitAddrs(t *testing.T) { } // check if the same amount of addrs we set has actually been set if len(natsRegistry.addrs) != len(tc.addrs) { + t.Errorf("Expected Addr = %v, Actual Addr = %v", + natsRegistry.addrs, tc.addrs) t.Errorf("Expected Addr count = %d, Actual Addr count = %d", len(natsRegistry.addrs), len(tc.addrs)) } @@ -80,6 +83,8 @@ func TestInitAddrs(t *testing.T) { for _, addr := range natsRegistry.addrs { _, ok := tc.addrs[addr] if !ok { + t.Errorf("Expected Addr = %v, Actual Addr = %v", + natsRegistry.addrs, tc.addrs) t.Errorf("Expected '%s' has not been set", addr) } }