Skip to content

Commit

Permalink
Merge pull request #623 from cupen/dev
Browse files Browse the repository at this point in the history
fix: testcase for memberlist
  • Loading branch information
rogeralsing authored Apr 14, 2022
2 parents 0aedd1c + bac5251 commit e90d282
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 61 deletions.
98 changes: 38 additions & 60 deletions cluster/member_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,119 +56,97 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
}

func TestMemberList_UpdateClusterTopology(t *testing.T) {
t.Skip("Skipping test, test is wrong, not the code")
c := newClusterForTest("test-UpdateClusterTopology", nil)

obj := NewMemberList(c)
dumpMembers := func(list Members) {
t.Logf("membersByMemberId=%d", len(list))

for _, m := range list {
t.Logf("\t%s", m.Address())
}
}

empty := make([]*Member, 0)

_ = dumpMembers
_sorted := func(tpl *ClusterTopology) {
_sortMembers := func(list Members) {
sort.Slice(list, func(i, j int) bool {
return (list)[i].Port < (list)[j].Port
})
}
// dumpMembers(tpl.MemberSet)
_sortMembers(tpl.Members)
// dumpMembers(tpl.MemberSet)
_sortMembers(tpl.Left)
_sortMembers(tpl.Joined)
}

t.Run("init", func(t *testing.T) {
a := assert.New(t)
members := _newTopologyEventForTest(2)
changes, _, _, _, _ := obj.getTopologyChanges(members)
_sorted(changes)
assert := assert.New(t)
members := newMembersForTest(2)
changes, unchanged, actives, _, _ := obj.getTopologyChanges(members)
assert.False(unchanged)
expected := &ClusterTopology{TopologyHash: TopologyHash(members), Members: members, Joined: members, Left: empty}
a.Equal(expected.TopologyHash, changes.TopologyHash)
assert.Equal(expected.TopologyHash, changes.TopologyHash)

var m1, m2 *MemberSet
m1 = NewMemberSet(expected.Members)
m2 = NewMemberSet(changes.Members)
a.Equal(m1, m2)
assert.Equal(m1, m2)

m1 = NewMemberSet(expected.Joined)
m2 = NewMemberSet(changes.Joined)
a.Equal(m1, m2)
assert.Equal(m1, m2)

m1 = NewMemberSet(expected.Left)
m2 = NewMemberSet(changes.Left)
a.Equal(m1, m2)
assert.Equal(m1, m2)

// current members
obj.members = actives
})

t.Run("join", func(t *testing.T) {
a := assert.New(t)
members := _newTopologyEventForTest(4)
changes, _, _, _, _ := obj.getTopologyChanges(members)
_sorted(changes)
assert := assert.New(t)
assert.Equal(2, obj.members.Len())
members := newMembersForTest(4)
changes, unchanged, actives, _, _ := obj.getTopologyChanges(members)
assert.False(unchanged)
// _sorted(changes)
expected := &ClusterTopology{TopologyHash: TopologyHash(members), Members: members, Joined: members[2:4], Left: empty}
a.Equal(expected.TopologyHash, changes.TopologyHash)
assert.Equal(expected.TopologyHash, changes.TopologyHash)

var m1, m2 *MemberSet
m1 = NewMemberSet(expected.Members)
m2 = NewMemberSet(changes.Members)
a.Equal(m1, m2)
assert.Equal(m1, m2)

m1 = NewMemberSet(expected.Joined)
m2 = NewMemberSet(changes.Joined)
a.Equal(m1, m2)
assert.Equal(m1, m2)

m1 = NewMemberSet(expected.Left)
m2 = NewMemberSet(changes.Left)
a.Equal(m1, m2)
assert.Equal(m1, m2)

obj.members = actives
})

t.Run("left", func(t *testing.T) {

a := assert.New(t)
members := _newTopologyEventForTest(4)
assert := assert.New(t)
assert.Equal(4, obj.members.Len())
members := newMembersForTest(4)
changes, _, _, _, _ := obj.getTopologyChanges(members[2:4])
_sorted(changes)
expected := &ClusterTopology{TopologyHash: TopologyHash(members), Members: members[2:4], Joined: empty, Left: members[0:2]}
a.Equal(expected.TopologyHash, changes.TopologyHash)
expected := &ClusterTopology{TopologyHash: TopologyHash(members[2:4]), Members: members[2:4], Joined: empty, Left: members[0:2]}
assert.Equal(expected.TopologyHash, changes.TopologyHash)

var m1, m2 *MemberSet
m1 = NewMemberSet(expected.Members)
m2 = NewMemberSet(changes.Members)
a.Equal(m1, m2)
assert.Equal(m1, m2)

m1 = NewMemberSet(expected.Joined)
m2 = NewMemberSet(changes.Joined)
a.Equal(m1, m2)
assert.Equal(m1, m2)

m1 = NewMemberSet(expected.Left)
m2 = NewMemberSet(changes.Left)
a.Equal(m1, m2)
assert.Equal(m1, m2)
})
}

func _newTopologyEventForTest(membersCount int, kinds ...string) Members {
func newMembersForTest(count int, kinds ...string) Members {
if len(kinds) == 0 {
kinds = append(kinds, "kind")
}

members := make(Members, membersCount)

for i := 0; i < membersCount; i++ {
memberID := fmt.Sprintf("memberId-%d", i)
members := make(Members, count)
for i := 0; i < count; i++ {
members[i] = &Member{
Id: memberID,
Id: fmt.Sprintf("memberId-%d", i),
Host: "127.0.0.1",
Port: int32(i),
Kinds: kinds,
}
}

return members
}

Expand Down Expand Up @@ -199,7 +177,7 @@ func TestMemberList_UpdateClusterTopology2(t *testing.T) {
}

a := assert.New(t)
members := _newTopologyEventForTest(2)
members := newMembersForTest(2)
changes, _, _, _, _ := obj.getTopologyChanges(members) //nolint:dogsled
_sorted(changes)

Expand Down Expand Up @@ -229,7 +207,7 @@ func TestMemberList_getPartitionMember(t *testing.T) {
obj := NewMemberList(c)

for _, v := range []int{1, 2, 10, 100, 1000} {
members := _newTopologyEventForTest(v)
members := newMembersForTest(v)
obj.UpdateClusterTopology(members)

testName := fmt.Sprintf("member*%d", v)
Expand Down Expand Up @@ -298,7 +276,7 @@ func TestMemberList_newMemberStrategies(t *testing.T) {
obj := NewMemberList(c)

for _, v := range []int{1, 10, 100, 1000} {
members := _newTopologyEventForTest(v, "kind1", "kind2")
members := newMembersForTest(v, "kind1", "kind2")
obj.UpdateClusterTopology(members)
a.Equal(2, len(obj.memberStrategyByKind))
a.Contains(obj.memberStrategyByKind, "kind1")
Expand Down
2 changes: 1 addition & 1 deletion cluster/rendezvous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func Benchmark_Rendezvous_Get(b *testing.B) {
SetLogLevel(log.ErrorLevel)
for _, v := range []int{1, 2, 3, 5, 10, 100, 1000, 2000} {
members := _newTopologyEventForTest(v)
members := newMembersForTest(v)
ms := newDefaultMemberStrategy(nil, "kind").(*simpleMemberStrategy)
for _, member := range members {
ms.AddMember(member)
Expand Down

0 comments on commit e90d282

Please sign in to comment.