forked from libp2p/go-libp2p-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcache_test.go
165 lines (136 loc) · 2.94 KB
/
mcache_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package pubsub
import (
"encoding/binary"
"fmt"
"testing"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
)
func TestMessageCache(t *testing.T) {
mcache := NewMessageCache(3, 5)
msgs := make([]*pb.Message, 60)
for i := range msgs {
msgs[i] = makeTestMessage(i)
}
for i := 0; i < 10; i++ {
mcache.Put(msgs[i])
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[i])
m, ok := mcache.Get(mid)
if !ok {
t.Fatalf("Message %d not in cache", i)
}
if m != msgs[i] {
t.Fatalf("Message %d does not match cache", i)
}
}
gids := mcache.GetGossipIDs("test")
if len(gids) != 10 {
t.Fatalf("Expected 10 gossip IDs; got %d", len(gids))
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[i])
if mid != gids[i] {
t.Fatalf("GossipID mismatch for message %d", i)
}
}
mcache.Shift()
for i := 10; i < 20; i++ {
mcache.Put(msgs[i])
}
for i := 0; i < 20; i++ {
mid := msgID(msgs[i])
m, ok := mcache.Get(mid)
if !ok {
t.Fatalf("Message %d not in cache", i)
}
if m != msgs[i] {
t.Fatalf("Message %d does not match cache", i)
}
}
gids = mcache.GetGossipIDs("test")
if len(gids) != 20 {
t.Fatalf("Expected 20 gossip IDs; got %d", len(gids))
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[i])
if mid != gids[10+i] {
t.Fatalf("GossipID mismatch for message %d", i)
}
}
for i := 10; i < 20; i++ {
mid := msgID(msgs[i])
if mid != gids[i-10] {
t.Fatalf("GossipID mismatch for message %d", i)
}
}
mcache.Shift()
for i := 20; i < 30; i++ {
mcache.Put(msgs[i])
}
mcache.Shift()
for i := 30; i < 40; i++ {
mcache.Put(msgs[i])
}
mcache.Shift()
for i := 40; i < 50; i++ {
mcache.Put(msgs[i])
}
mcache.Shift()
for i := 50; i < 60; i++ {
mcache.Put(msgs[i])
}
if len(mcache.msgs) != 50 {
t.Fatalf("Expected 50 messages in the cache; got %d", len(mcache.msgs))
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[i])
_, ok := mcache.Get(mid)
if ok {
t.Fatalf("Message %d still in cache", i)
}
}
for i := 10; i < 60; i++ {
mid := msgID(msgs[i])
m, ok := mcache.Get(mid)
if !ok {
t.Fatalf("Message %d not in cache", i)
}
if m != msgs[i] {
t.Fatalf("Message %d does not match cache", i)
}
}
gids = mcache.GetGossipIDs("test")
if len(gids) != 30 {
t.Fatalf("Expected 30 gossip IDs; got %d", len(gids))
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[50+i])
if mid != gids[i] {
t.Fatalf("GossipID mismatch for message %d", i)
}
}
for i := 10; i < 20; i++ {
mid := msgID(msgs[30+i])
if mid != gids[i] {
t.Fatalf("GossipID mismatch for message %d", i)
}
}
for i := 20; i < 30; i++ {
mid := msgID(msgs[10+i])
if mid != gids[i] {
t.Fatalf("GossipID mismatch for message %d", i)
}
}
}
func makeTestMessage(n int) *pb.Message {
seqno := make([]byte, 8)
binary.BigEndian.PutUint64(seqno, uint64(n))
data := []byte(fmt.Sprintf("%d", n))
return &pb.Message{
Data: data,
TopicIDs: []string{"test"},
From: []byte("test"),
Seqno: seqno,
}
}