-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_test.go
More file actions
97 lines (85 loc) · 2.19 KB
/
Copy pathview_test.go
File metadata and controls
97 lines (85 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package gossipProtocol
import (
"strconv"
"testing"
sll "github.com/emirpasic/gods/lists/singlylinkedlist"
"github.com/stretchr/testify/assert"
)
func mockView(hop int) View {
n := sll.New()
for i := 1; i <= 9; i++ {
n.Add(Peer{
UdpAddress: "120" + strconv.Itoa(i),
Hop: hop,
ProcessIdentifier: "process-" + strconv.Itoa(i),
})
}
return View{Nodes: n}
}
func TestMerge(t *testing.T) {
t.Parallel()
lowerHop, higherHop := 0, 2
v1 := mockView(lowerHop)
v2 := mockView(higherHop)
merged := mergeViews(toMap(v1, ""), toMap(v2, ""))
assert.Equal(t, merged.Nodes.Size(), v1.Nodes.Size())
merged.Nodes.Each(func(_ int, value interface{}) {
n := value.(Peer)
assert.Equal(t, n.Hop, lowerHop)
})
}
func TestViewNodes(t *testing.T) {
t.Parallel()
v1 := mockView(0)
assert.Equal(t, v1.headNode().UdpAddress, "1201")
assert.Equal(t, v1.tailNode().UdpAddress, "1209")
assert.NotNil(t, v1.randomNode())
}
func TestRandomView(t *testing.T) {
t.Parallel()
v := mockView(0)
v.RandomView()
assert.NotNil(t, v.randomNode())
assert.Equal(t, MaxNodesInView, v.Nodes.Size())
}
func TestHeadView(t *testing.T) {
t.Parallel()
v := mockView(0)
v.headView()
assert.Equal(t, MaxNodesInView, v.Nodes.Size())
assert.Equal(t, "1201", v.headNode().UdpAddress)
assert.Equal(t, "1206", v.tailNode().UdpAddress)
exists, hop, index := v.checkExists("1205")
assert.Equal(t, true, exists)
assert.Equal(t, 0, hop)
assert.Equal(t, 4, index)
}
func TestTailView(t *testing.T) {
t.Parallel()
v := mockView(0)
v.tailView()
v.sortByAddr()
assert.Equal(t, MaxNodesInView, v.Nodes.Size())
assert.Equal(t, "1204", v.headNode().UdpAddress)
assert.Equal(t, "1209", v.tailNode().UdpAddress)
exists, hop, index := v.checkExists("1205")
assert.Equal(t, true, exists)
assert.Equal(t, 0, hop)
assert.Equal(t, 1, index)
}
func TestSerialization(t *testing.T) {
t.Parallel()
v := mockView(4)
peer := Peer{
UdpAddress: "udp",
ProcessIdentifier: "id",
}
bytes := ViewToBytes(v, peer)
v2, from, e := BytesToView(bytes)
assert.NoError(t, e)
v2.Nodes.Each(func(_ int, value interface{}) {
n := value.(Peer)
assert.NotEqual(t, -1, v.Nodes.IndexOf(n))
})
assert.Equal(t, peer, from)
}