go-zero/core/discov/subscriber_test.go

217 lines
3.3 KiB
Go
Raw Normal View History

2020-07-30 22:56:39 +08:00
package discov
import (
2021-02-07 20:24:47 +08:00
"sync/atomic"
2020-07-30 22:56:39 +08:00
"testing"
"github.com/stretchr/testify/assert"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/discov/internal"
2020-07-30 22:56:39 +08:00
)
const (
actionAdd = iota
actionDel
)
func TestContainer(t *testing.T) {
type action struct {
act int
key string
val string
}
tests := []struct {
name string
do []action
expect []string
}{
{
name: "add one",
do: []action{
{
act: actionAdd,
key: "first",
val: "a",
},
},
expect: []string{
"a",
},
},
{
name: "add two",
do: []action{
{
act: actionAdd,
key: "first",
val: "a",
},
{
act: actionAdd,
key: "second",
val: "b",
},
},
expect: []string{
"a",
"b",
},
},
{
name: "add two, delete one",
do: []action{
{
act: actionAdd,
key: "first",
val: "a",
},
{
act: actionAdd,
key: "second",
val: "b",
},
{
act: actionDel,
key: "first",
},
},
2020-07-31 10:47:20 +08:00
expect: []string{"b"},
2020-07-30 22:56:39 +08:00
},
{
name: "add two, delete two",
do: []action{
{
act: actionAdd,
key: "first",
val: "a",
},
{
act: actionAdd,
key: "second",
val: "b",
},
{
act: actionDel,
key: "first",
2020-07-31 10:47:20 +08:00
},
{
act: actionDel,
key: "second",
},
},
expect: []string{},
},
{
name: "add three, dup values, delete two",
do: []action{
{
act: actionAdd,
key: "first",
2020-07-30 22:56:39 +08:00
val: "a",
},
2020-07-31 10:47:20 +08:00
{
act: actionAdd,
key: "second",
val: "b",
},
{
act: actionAdd,
key: "third",
val: "a",
},
{
act: actionDel,
key: "first",
},
2020-07-30 22:56:39 +08:00
{
act: actionDel,
key: "second",
2020-07-31 10:47:20 +08:00
},
},
expect: []string{"a"},
},
{
name: "add three, dup values, delete two, delete not added",
do: []action{
{
act: actionAdd,
key: "first",
val: "a",
},
{
act: actionAdd,
key: "second",
2020-07-30 22:56:39 +08:00
val: "b",
},
2020-07-31 10:47:20 +08:00
{
act: actionAdd,
key: "third",
val: "a",
},
{
act: actionDel,
key: "first",
},
{
act: actionDel,
key: "second",
},
{
act: actionDel,
key: "forth",
},
2020-07-30 22:56:39 +08:00
},
2020-07-31 10:47:20 +08:00
expect: []string{"a"},
2020-07-30 22:56:39 +08:00
},
}
2020-07-31 10:47:20 +08:00
exclusives := []bool{true, false}
2020-07-30 22:56:39 +08:00
for _, test := range tests {
2020-07-31 10:47:20 +08:00
for _, exclusive := range exclusives {
t.Run(test.name, func(t *testing.T) {
2020-07-31 16:20:13 +08:00
var changed bool
2020-07-31 10:47:20 +08:00
c := newContainer(exclusive)
2020-07-31 16:20:13 +08:00
c.addListener(func() {
changed = true
})
2020-07-31 10:58:30 +08:00
assert.Nil(t, c.getValues())
2020-07-31 16:20:13 +08:00
assert.False(t, changed)
2020-07-31 10:58:30 +08:00
2020-07-31 10:47:20 +08:00
for _, order := range test.do {
if order.act == actionAdd {
c.OnAdd(internal.KV{
Key: order.key,
Val: order.val,
})
} else {
c.OnDelete(internal.KV{
Key: order.key,
Val: order.val,
})
}
2020-07-30 22:56:39 +08:00
}
2020-07-31 11:14:48 +08:00
2020-07-31 16:20:13 +08:00
assert.True(t, changed)
2020-07-31 10:47:20 +08:00
assert.True(t, c.dirty.True())
assert.ElementsMatch(t, test.expect, c.getValues())
assert.False(t, c.dirty.True())
assert.ElementsMatch(t, test.expect, c.getValues())
})
}
2020-07-30 22:56:39 +08:00
}
}
2021-02-07 20:24:47 +08:00
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))
}