go-libp2p-pubsub/peer_gater_test.go

123 lines
2.2 KiB
Go
Raw Normal View History

2020-09-02 19:09:37 +00:00
package pubsub
import (
2020-09-03 08:59:38 +00:00
"context"
2020-09-02 19:09:37 +00:00
"testing"
"time"
"github.com/libp2p/go-libp2p-core/peer"
)
func TestPeerGater(t *testing.T) {
2020-09-03 08:59:38 +00:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2020-09-02 19:09:37 +00:00
peerA := peer.ID("A")
2020-09-03 08:59:38 +00:00
peerAip := "1.2.3.4"
pg := newPeerGater(ctx, nil, .1, .9)
pg.getIP = func(p peer.ID) string {
switch p {
case peerA:
return peerAip
default:
return "<wtf>"
}
}
2020-09-02 19:09:37 +00:00
pg.AddPeer(peerA, "")
status := pg.AcceptFrom(peerA)
if status != AcceptAll {
t.Fatal("expected AcceptAll")
}
msg := &Message{ReceivedFrom: peerA}
pg.ValidateMessage(msg)
status = pg.AcceptFrom(peerA)
if status != AcceptAll {
t.Fatal("expected AcceptAll")
}
pg.RejectMessage(msg, rejectValidationQueueFull)
status = pg.AcceptFrom(peerA)
if status != AcceptAll {
t.Fatal("expected AcceptAll")
}
pg.RejectMessage(msg, rejectValidationThrottled)
status = pg.AcceptFrom(peerA)
if status != AcceptAll {
t.Fatal("expected AcceptAll")
}
2020-09-02 22:33:03 +00:00
for i := 0; i < 100; i++ {
pg.RejectMessage(msg, rejectValidationIgnored)
pg.RejectMessage(msg, rejectValidationFailed)
2020-09-02 19:09:37 +00:00
}
2020-09-02 22:33:03 +00:00
accepted := false
for i := 0; !accepted && i < 1000; i++ {
status = pg.AcceptFrom(peerA)
if status == AcceptControl {
accepted = true
}
}
if !accepted {
2020-09-02 19:09:37 +00:00
t.Fatal("expected AcceptControl")
}
for i := 0; i < 100; i++ {
pg.DeliverMessage(msg)
}
2020-09-02 22:33:03 +00:00
accepted = false
2020-09-02 19:09:37 +00:00
for i := 0; !accepted && i < 1000; i++ {
status = pg.AcceptFrom(peerA)
if status == AcceptAll {
accepted = true
}
}
if !accepted {
t.Fatal("expected to accept at least once")
}
for i := 0; i < 100; i++ {
pg.decayStats()
}
status = pg.AcceptFrom(peerA)
if status != AcceptAll {
t.Fatal("expected AcceptAll")
}
pg.RemovePeer(peerA)
2020-09-03 09:34:20 +00:00
pg.Lock()
2020-09-03 08:59:38 +00:00
_, ok := pg.peerStats[peerA]
2020-09-03 09:34:20 +00:00
pg.Unlock()
2020-09-03 08:59:38 +00:00
if ok {
t.Fatal("still have a stat record for peerA")
}
2020-09-02 19:09:37 +00:00
2020-09-03 09:34:20 +00:00
pg.Lock()
2020-09-03 08:59:38 +00:00
_, ok = pg.ipStats[peerAip]
2020-09-03 09:34:20 +00:00
pg.Unlock()
2020-09-03 08:59:38 +00:00
if !ok {
t.Fatal("expected to still have a stat record for peerA's ip")
}
2020-09-02 19:09:37 +00:00
2020-09-03 09:34:20 +00:00
pg.Lock()
2020-09-03 08:59:38 +00:00
pg.ipStats[peerAip].expire = time.Now()
2020-09-03 09:34:20 +00:00
pg.Unlock()
2020-09-02 19:09:37 +00:00
2020-09-03 08:59:38 +00:00
time.Sleep(2 * time.Second)
2020-09-03 09:34:20 +00:00
pg.Lock()
2020-09-03 08:59:38 +00:00
_, ok = pg.ipStats["1.2.3.4"]
2020-09-03 09:34:20 +00:00
pg.Unlock()
2020-09-02 19:09:37 +00:00
if ok {
2020-09-03 08:59:38 +00:00
t.Fatal("still have a stat record for peerA's ip")
2020-09-02 19:09:37 +00:00
}
}