-
Notifications
You must be signed in to change notification settings - Fork 19
/
shardpreftable_test.go
109 lines (102 loc) · 3.91 KB
/
shardpreftable_test.go
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
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"testing"
peer "github.com/libp2p/go-libp2p-peer"
)
func TestListeningShards(t *testing.T) {
ls := NewListeningShards()
lsSlice := ls.getShards()
if len(lsSlice) != 0 {
t.Error("Should not have subscribed to any shard")
}
ls.setShard(1)
lsSlice = ls.getShards()
if (len(lsSlice) != 1) || lsSlice[0] != ShardIDType(1) {
t.Error("Should only subscribe to shard 1")
}
ls.setShard(42)
if len(ls.getShards()) != 2 {
t.Error("Should subscribe to exactly two shards")
}
// test `toBytes` and `fromBytes`
bytes := ls.toBytes()
lsNew := ls.fromBytes(bytes)
if len(ls.getShards()) != len(lsNew.getShards()) {
t.Error("Failed to convert listening shards between toBytes and fromBytes")
}
lsNewSlice := lsNew.getShards()
for index, value := range ls.getShards() {
if value != lsNewSlice[index] {
t.Error("Shards in the new listerning shard do not match the ones in the old listening shard")
}
}
}
func TestShardPrefTable(t *testing.T) {
shardPrefTable := NewShardPrefTable()
arbitraryPeerID := peer.ID("123456")
var testingShardID ShardIDType = 42
if len(shardPrefTable.GetPeerListeningShardSlice(arbitraryPeerID)) != 0 {
t.Errorf("Peer %v should not be listening to any shard", arbitraryPeerID)
}
if shardPrefTable.IsPeerListeningShard(arbitraryPeerID, testingShardID) {
t.Errorf("Peer %v should not be listening to shard %v", arbitraryPeerID, testingShardID)
}
shardPrefTable.AddPeerListeningShard(arbitraryPeerID, testingShardID)
if !shardPrefTable.IsPeerListeningShard(arbitraryPeerID, testingShardID) {
t.Errorf("Peer %v should be listening to shard %v", arbitraryPeerID, testingShardID)
}
shardPrefTable.AddPeerListeningShard(arbitraryPeerID, numShards)
if shardPrefTable.IsPeerListeningShard(arbitraryPeerID, numShards) {
t.Errorf(
"Peer %v should not be able to listen to shardID bigger than %v",
arbitraryPeerID,
numShards,
)
}
// listen to multiple shards
anotherShardID := testingShardID + 1 // notice that it should be less than `numShards`
shardPrefTable.AddPeerListeningShard(arbitraryPeerID, anotherShardID)
if !shardPrefTable.IsPeerListeningShard(arbitraryPeerID, anotherShardID) {
t.Errorf("Peer %v should be listening to shard %v", arbitraryPeerID, testingShardID)
}
if len(shardPrefTable.GetPeerListeningShardSlice(arbitraryPeerID)) != 2 {
t.Errorf(
"Peer %v should be listening to %v shards, not %v",
arbitraryPeerID,
2,
len(shardPrefTable.GetPeerListeningShardSlice(arbitraryPeerID)),
)
}
shardPrefTable.RemovePeerListeningShard(arbitraryPeerID, anotherShardID)
if shardPrefTable.IsPeerListeningShard(arbitraryPeerID, anotherShardID) {
t.Errorf("Peer %v should be listening to shard %v", arbitraryPeerID, testingShardID)
}
if len(shardPrefTable.GetPeerListeningShardSlice(arbitraryPeerID)) != 1 {
t.Errorf(
"Peer %v should be only listening to %v shards, not %v",
arbitraryPeerID,
1,
len(shardPrefTable.GetPeerListeningShardSlice(arbitraryPeerID)),
)
}
// see if it is still correct with multiple peers
anotherPeerID := peer.ID("9547")
if len(shardPrefTable.GetPeerListeningShardSlice(anotherPeerID)) != 0 {
t.Errorf("Peer %v should not be listening to any shard", anotherPeerID)
}
shardPrefTable.AddPeerListeningShard(anotherPeerID, testingShardID)
if len(shardPrefTable.GetPeerListeningShardSlice(anotherPeerID)) != 1 {
t.Errorf("Peer %v should be listening to 1 shard", anotherPeerID)
}
// make sure not affect other peers
if len(shardPrefTable.GetPeerListeningShardSlice(arbitraryPeerID)) != 1 {
t.Errorf("Peer %v should be listening to 1 shard", arbitraryPeerID)
}
shardPrefTable.RemovePeerListeningShard(anotherPeerID, testingShardID)
if len(shardPrefTable.GetPeerListeningShardSlice(anotherPeerID)) != 0 {
t.Errorf("Peer %v should be listening to 0 shard", anotherPeerID)
}
if len(shardPrefTable.GetPeerListeningShardSlice(arbitraryPeerID)) != 1 {
t.Errorf("Peer %v should be listening to 1 shard", arbitraryPeerID)
}
}