From 5b79ba2618628a8a602243d219c6a276028b5c09 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 7 Feb 2021 20:24:47 +0800 Subject: [PATCH] add discov tests (#448) --- core/discov/clients_test.go | 3 +++ core/discov/publisher_test.go | 15 +++++++++++++++ core/discov/subscriber_test.go | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/core/discov/clients_test.go b/core/discov/clients_test.go index 89eeb972..132a8824 100644 --- a/core/discov/clients_test.go +++ b/core/discov/clients_test.go @@ -28,6 +28,9 @@ func TestExtract(t *testing.T) { _, ok = extract("any", -1) assert.False(t, ok) + + _, ok = extract("any", 10) + assert.False(t, ok) } func TestMakeKey(t *testing.T) { diff --git a/core/discov/publisher_test.go b/core/discov/publisher_test.go index ab6d0aaa..e99ed2f3 100644 --- a/core/discov/publisher_test.go +++ b/core/discov/publisher_test.go @@ -4,10 +4,12 @@ import ( "errors" "sync" "testing" + "time" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/core/discov/internal" + "github.com/tal-tech/go-zero/core/lang" "github.com/tal-tech/go-zero/core/logx" "go.etcd.io/etcd/clientv3" ) @@ -152,3 +154,16 @@ func TestPublisher_keepAliveAsyncPause(t *testing.T) { pub.Pause() wg.Wait() } + +func TestPublisher_Resume(t *testing.T) { + publisher := new(Publisher) + publisher.resumeChan = make(chan lang.PlaceholderType) + go func() { + publisher.Resume() + }() + go func() { + time.Sleep(time.Minute) + t.Fail() + }() + <-publisher.resumeChan +} diff --git a/core/discov/subscriber_test.go b/core/discov/subscriber_test.go index b4f16b30..3d24cfb8 100644 --- a/core/discov/subscriber_test.go +++ b/core/discov/subscriber_test.go @@ -1,6 +1,7 @@ package discov import ( + "sync/atomic" "testing" "github.com/stretchr/testify/assert" @@ -198,3 +199,18 @@ func TestContainer(t *testing.T) { } } } + +func TestSubscriber(t *testing.T) { + var opt subOptions + Exclusive()(&opt) + + sub := new(Subscriber) + sub.items = newContainer(opt.exclusive) + var count int32 + sub.AddListener(func() { + atomic.AddInt32(&count, 1) + }) + sub.items.notifyChange() + assert.Empty(t, sub.Values()) + assert.Equal(t, int32(1), atomic.LoadInt32(&count)) +}