Skip to content

Commit

Permalink
Complete topology tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda committed Nov 30, 2018
1 parent ee7ff1e commit 8dfde2b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
8 changes: 4 additions & 4 deletions gossip/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func (t *Topology) Get(kind member.Type) PeerList {
return t.m[kind]
}

func (t *Topology) Each(n int, p *PeerList) *PeerList {
func (t *Topology) Each(n int, exclude *PeerList) *PeerList {
var b PeerList

auditors := t.m[member.Auditor].Exclude(p).Shuffle()
monitors := t.m[member.Monitor].Exclude(p).Shuffle()
publishers := t.m[member.Publisher].Exclude(p).Shuffle()
auditors := t.m[member.Auditor].Exclude(exclude).Shuffle()
monitors := t.m[member.Monitor].Exclude(exclude).Shuffle()
publishers := t.m[member.Publisher].Exclude(exclude).Shuffle()

if len(auditors.L) > n {
auditors.L = auditors.L[:n]
Expand Down
84 changes: 84 additions & 0 deletions gossip/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ func setupPeerList(size int) *PeerList {
return &PeerList{peers}
}

func setupTopology(size int) *Topology {
topology := NewTopology()
for i := 0; i < size; i++ {
name := fmt.Sprintf("name%d", i)
port, _ := strconv.Atoi(fmt.Sprintf("900%d", i))
role := member.Type(i % int(member.Unknown))
peer := member.NewPeer(name, "127.0.0.1", uint16(port), role)
topology.Update(peer)
}
return topology
}

func TestFilterPeerList(t *testing.T) {
list := setupPeerList(10)

Expand Down Expand Up @@ -127,3 +139,75 @@ func TestDeleteNotIncludedPeerList(t *testing.T) {
require.Truef(t, 10 == list.Size(), "The new list should have the same size")

}

func TestShufflePeerList(t *testing.T) {
list := setupPeerList(10)

shuffled := list.Shuffle()

require.Truef(t, 10 == shuffled.Size(), "The new list should have the same size")
for _, e := range list.L {
require.Containsf(t, shuffled.L, e, "The element should remain in the list")
}
}

func TestUpdateAndDeleteTopology(t *testing.T) {
topology := NewTopology()

peer := member.NewPeer("auditor", "127.0.0.1", 9000, member.Auditor)
topology.Update(peer)

auditors := topology.Get(member.Auditor)
require.Truef(t, 1 == auditors.Size(), "The topology must include one auditor")

topology.Delete(peer)

auditors = topology.Get(member.Auditor)
require.Truef(t, 0 == auditors.Size(), "The topology must include zero auditor")

}

func TestEachWithoutExclusionsTopology(t *testing.T) {
topology := setupTopology(10)

each := topology.Each(1, nil)

require.Truef(t, 3 == each.Size(), "It must include only 3 elements")

auditors := each.Filter(func(m *member.Peer) bool {
return m.Meta.Role == member.Auditor
})
monitors := each.Filter(func(m *member.Peer) bool {
return m.Meta.Role == member.Monitor
})
publishers := each.Filter(func(m *member.Peer) bool {
return m.Meta.Role == member.Publisher
})

require.Truef(t, 1 == auditors.Size(), "It must include only one auditor")
require.Truef(t, 1 == monitors.Size(), "It must include only one monitor")
require.Truef(t, 1 == publishers.Size(), "It must include only one publisher")
}

func TestEachWithExclusionsTopology(t *testing.T) {
topology := setupTopology(10)

excluded := topology.Get(member.Auditor)
each := topology.Each(1, &excluded)

require.Truef(t, 2 == each.Size(), "It must include only 2 elements")

auditors := each.Filter(func(m *member.Peer) bool {
return m.Meta.Role == member.Auditor
})
monitors := each.Filter(func(m *member.Peer) bool {
return m.Meta.Role == member.Monitor
})
publishers := each.Filter(func(m *member.Peer) bool {
return m.Meta.Role == member.Publisher
})

require.Truef(t, 0 == auditors.Size(), "It must not include any auditor")
require.Truef(t, 1 == monitors.Size(), "It must include only one monitor")
require.Truef(t, 1 == publishers.Size(), "It must include only one publisher")
}

0 comments on commit 8dfde2b

Please sign in to comment.