forked from libp2p/go-libp2p-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pubsub_test.go
49 lines (36 loc) · 1.13 KB
/
pubsub_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
package pubsub
import (
"context"
"testing"
"time"
)
// See https://github.com/libp2p/go-libp2p-pubsub/issues/426
func TestPubSubRemovesBlacklistedPeer(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
hosts := getNetHosts(t, ctx, 2)
bl := NewMapBlacklist()
psubs0 := getPubsub(ctx, hosts[0])
psubs1 := getPubsub(ctx, hosts[1], WithBlacklist(bl))
connect(t, hosts[0], hosts[1])
// Bad peer is blacklisted after it has connected.
// Calling p.BlacklistPeer directly does the right thing but we should also clean
// up the peer if it has been added the the blacklist by another means.
bl.Add(hosts[0].ID())
_, err := psubs0.Subscribe("test")
if err != nil {
t.Fatal(err)
}
sub1, err := psubs1.Subscribe("test")
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 100)
psubs0.Publish("test", []byte("message"))
wctx, cancel2 := context.WithTimeout(ctx, 1*time.Second)
defer cancel2()
_, _ = sub1.Next(wctx)
// Explicitly cancel context so PubSub cleans up peer channels.
// Issue 426 reports a panic due to a peer channel being closed twice.
cancel()
time.Sleep(time.Millisecond * 100)
}