Skip to content

Commit

Permalink
add discov tests (zeromicro#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Feb 7, 2021
1 parent 22a1fa6 commit 5b79ba2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/discov/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions core/discov/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions core/discov/subscriber_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package discov

import (
"sync/atomic"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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))
}

0 comments on commit 5b79ba2

Please sign in to comment.