forked from libp2p/go-libp2p-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossip_tracer_test.go
98 lines (77 loc) · 2.25 KB
/
gossip_tracer_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
package pubsub
import (
"testing"
"time"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
"github.com/libp2p/go-libp2p-core/peer"
)
func TestBrokenPromises(t *testing.T) {
// tests that unfullfilled promises are tracked correctly
gt := newGossipTracer()
gt.followUpTime = 100 * time.Millisecond
peerA := peer.ID("A")
peerB := peer.ID("B")
peerC := peer.ID("C")
var mids []string
for i := 0; i < 100; i++ {
m := makeTestMessage(i)
m.From = []byte(peerA)
mid := DefaultMsgIdFn(m)
mids = append(mids, mid)
}
gt.AddPromise(peerA, mids)
gt.AddPromise(peerB, mids)
gt.AddPromise(peerC, mids)
// no broken promises yet
brokenPromises := gt.GetBrokenPromises()
if brokenPromises != nil {
t.Fatal("expected no broken promises")
}
// throttle one of the peers to save his promises
gt.ThrottlePeer(peerC)
// make promises break
time.Sleep(GossipSubIWantFollowupTime + 10*time.Millisecond)
brokenPromises = gt.GetBrokenPromises()
if len(brokenPromises) != 2 {
t.Fatalf("expected 2 broken prmises, got %d", len(brokenPromises))
}
brokenPromisesA := brokenPromises[peerA]
if brokenPromisesA != 1 {
t.Fatalf("expected 1 broken promise from A, got %d", brokenPromisesA)
}
brokenPromisesB := brokenPromises[peerB]
if brokenPromisesB != 1 {
t.Fatalf("expected 1 broken promise from A, got %d", brokenPromisesB)
}
}
func TestNoBrokenPromises(t *testing.T) {
// like above, but this time we deliver messages to fullfil the promises
originalGossipSubIWantFollowupTime := GossipSubIWantFollowupTime
GossipSubIWantFollowupTime = 100 * time.Millisecond
defer func() {
GossipSubIWantFollowupTime = originalGossipSubIWantFollowupTime
}()
gt := newGossipTracer()
peerA := peer.ID("A")
peerB := peer.ID("B")
var msgs []*pb.Message
var mids []string
for i := 0; i < 100; i++ {
m := makeTestMessage(i)
m.From = []byte(peerA)
msgs = append(msgs, m)
mid := DefaultMsgIdFn(m)
mids = append(mids, mid)
}
gt.AddPromise(peerA, mids)
gt.AddPromise(peerB, mids)
for _, m := range msgs {
gt.DeliverMessage(&Message{Message: m})
}
time.Sleep(GossipSubIWantFollowupTime + 10*time.Millisecond)
// there should be no broken promises
brokenPromises := gt.GetBrokenPromises()
if brokenPromises != nil {
t.Fatal("expected no broken promises")
}
}