1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-01-05 10:20:53 +02:00

[fix] nats deregister issue (#2384)

This commit is contained in:
Johnson C 2021-12-10 19:32:21 +08:00 committed by GitHub
parent a40f6e8fae
commit 37de747d19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 14 deletions

View File

@ -8,9 +8,9 @@ import (
"sync" "sync"
"time" "time"
"github.com/nats-io/nats.go"
"go-micro.dev/v4/cmd" "go-micro.dev/v4/cmd"
"go-micro.dev/v4/registry" "go-micro.dev/v4/registry"
"github.com/nats-io/nats.go"
) )
type natsRegistry struct { type natsRegistry struct {
@ -81,6 +81,7 @@ func configure(n *natsRegistry, opts ...registry.Option) error {
return nil return nil
} }
func setAddrs(addrs []string) []string { func setAddrs(addrs []string) []string {
var cAddrs []string var cAddrs []string
for _, addr := range addrs { for _, addr := range addrs {
@ -205,8 +206,7 @@ func (n *natsRegistry) deregister(s *registry.Service) error {
n.Lock() n.Lock()
defer n.Unlock() defer n.Unlock()
// cache leftover service services := delServices(n.services[s.Name], cp([]*registry.Service{s}))
services := addServices(n.services[s.Name], cp([]*registry.Service{s}))
if len(services) > 0 { if len(services) > 0 {
n.services[s.Name] = services n.services[s.Name] = services
return nil return nil

View File

@ -21,18 +21,26 @@ func TestRegister(t *testing.T) {
} }
func TestDeregister(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(&service1))
services, err := e.registryOne.GetService(service1.Name)
assertNoError(t, e.registryOne.Register(&service))
assertNoError(t, e.registryOne.Deregister(&service))
services, err := e.registryOne.ListServices()
assertNoError(t, err) 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) assertNoError(t, err)
assertEqual(t, 0, len(services)) assertEqual(t, 0, len(services))
} }

View File

@ -8,8 +8,8 @@ import (
"testing" "testing"
"time" "time"
"go-micro.dev/v4/registry"
"github.com/nats-io/nats.go" "github.com/nats-io/nats.go"
"go-micro.dev/v4/registry"
) )
var addrTestCases = []struct { var addrTestCases = []struct {
@ -35,7 +35,8 @@ var addrTestCases = []struct {
"default", "default",
"check if default Address is set correctly", "check if default Address is set correctly",
map[string]string{ 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 // check if the same amount of addrs we set has actually been set
if len(natsRegistry.addrs) != len(tc.addrs) { 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", t.Errorf("Expected Addr count = %d, Actual Addr count = %d",
len(natsRegistry.addrs), len(tc.addrs)) len(natsRegistry.addrs), len(tc.addrs))
} }
@ -80,6 +83,8 @@ func TestInitAddrs(t *testing.T) {
for _, addr := range natsRegistry.addrs { for _, addr := range natsRegistry.addrs {
_, ok := tc.addrs[addr] _, ok := tc.addrs[addr]
if !ok { if !ok {
t.Errorf("Expected Addr = %v, Actual Addr = %v",
natsRegistry.addrs, tc.addrs)
t.Errorf("Expected '%s' has not been set", addr) t.Errorf("Expected '%s' has not been set", addr)
} }
} }