123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package discov
- import (
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/tal-tech/go-zero/core/discov/internal"
- )
- 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",
- },
- },
- expect: []string{"b"},
- },
- {
- name: "add two, delete two",
- do: []action{
- {
- act: actionAdd,
- key: "first",
- val: "a",
- },
- {
- act: actionAdd,
- key: "second",
- val: "b",
- },
- {
- act: actionDel,
- key: "first",
- },
- {
- act: actionDel,
- key: "second",
- },
- },
- expect: []string{},
- },
- {
- name: "add three, dup values, delete two",
- do: []action{
- {
- act: actionAdd,
- key: "first",
- val: "a",
- },
- {
- act: actionAdd,
- key: "second",
- val: "b",
- },
- {
- act: actionAdd,
- key: "third",
- val: "a",
- },
- {
- act: actionDel,
- key: "first",
- },
- {
- act: actionDel,
- key: "second",
- },
- },
- 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",
- val: "b",
- },
- {
- act: actionAdd,
- key: "third",
- val: "a",
- },
- {
- act: actionDel,
- key: "first",
- },
- {
- act: actionDel,
- key: "second",
- },
- {
- act: actionDel,
- key: "forth",
- },
- },
- expect: []string{"a"},
- },
- }
- exclusives := []bool{true, false}
- for _, test := range tests {
- for _, exclusive := range exclusives {
- t.Run(test.name, func(t *testing.T) {
- var changed bool
- c := newContainer(exclusive)
- c.addListener(func() {
- changed = true
- })
- assert.Nil(t, c.getValues())
- assert.False(t, changed)
- 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,
- })
- }
- }
- assert.True(t, changed)
- 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())
- })
- }
- }
- }
|