Files
allmende/internal/p2p/stats_test.go
T

45 lines
1.3 KiB
Go

package p2p
import (
"testing"
"github.com/libp2p/go-libp2p/core/network"
)
func TestStatsCountWhatTheGuardAndTheGateDo(t *testing.T) {
a := startNode(t, nil, "u1x")
b := startNode(t, nil, "u1x")
connect(t, a, b)
eventually(t, "a sees b", func() bool { return a.Stats().Peers == 1 && a.Stats().RegionPeers["u1x"] == 1 })
// Invalid messages and a ban.
for i := 0; i < maxBadPerWindow; i++ {
a.guard.bad(b.host.ID())
}
// A flood: messages over the rate are dropped.
for i := 0; i < maxPerWindow+5; i++ {
a.guard.admit(b.host.ID())
}
// A banned peer's sync request, and a connection from a full address.
a.guard.beginSync(b.host.ID())
a.gate.mu.Lock()
a.gate.counts["203.0.113.7"] = DefaultConnsPerIP
a.gate.mu.Unlock()
a.gate.InterceptAccept(fakeConnAddrs{mustAddr(t, "/ip4/203.0.113.7/tcp/1000")})
a.gate.InterceptSecured(network.DirInbound, b.host.ID(), nil)
s := a.Stats()
if s.MessagesInvalid != maxBadPerWindow || s.PeersBanned != 1 {
t.Errorf("invalid %d, banned %d", s.MessagesInvalid, s.PeersBanned)
}
if s.MessagesDropped != 5 {
t.Errorf("dropped %d, want 5", s.MessagesDropped)
}
if s.SyncRefused != 1 {
t.Errorf("sync refused %d, want 1", s.SyncRefused)
}
if s.ConnectionsFull != 1 || s.ConnectionsBanned != 1 {
t.Errorf("connections refused: full %d, banned %d", s.ConnectionsFull, s.ConnectionsBanned)
}
}