From 68e46cf6b3b64d7331fc3e86c3dd702e8a4a9784 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 31 Jul 2020 10:47:20 +0800 Subject: [PATCH] add more tests --- core/discov/subscriber_test.go | 108 +++++++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 24 deletions(-) diff --git a/core/discov/subscriber_test.go b/core/discov/subscriber_test.go index 64e3c2a2..b88b11f7 100644 --- a/core/discov/subscriber_test.go +++ b/core/discov/subscriber_test.go @@ -72,12 +72,9 @@ func TestContainer(t *testing.T) { { act: actionDel, key: "first", - val: "a", }, }, - expect: []string{ - "b", - }, + expect: []string{"b"}, }, { name: "add two, delete two", @@ -95,38 +92,101 @@ func TestContainer(t *testing.T) { { 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{}, + expect: []string{"a"}, }, } + exclusives := []bool{true, false} for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - c := newContainer(false) - 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, - }) + for _, exclusive := range exclusives { + t.Run(test.name, func(t *testing.T) { + c := newContainer(exclusive) + 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, c.dirty.True()) - assert.ElementsMatch(t, test.expect, c.getValues()) - assert.False(t, c.dirty.True()) - assert.ElementsMatch(t, test.expect, c.getValues()) - }) + 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()) + }) + } } }