Skip to content

Commit

Permalink
Handle metric not being reset between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Oct 4, 2023
1 parent 904fd00 commit 5ff1fe4
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tests/pubsub/testgossipsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,12 @@ suite "GossipSub":

return (nodes, gossip0, gossip1)

proc getCurrentDisconnections(): float64 =
try:
libp2p_gossipsub_peers_rate_limit_disconnections.valueByName("libp2p_gossipsub_peers_rate_limit_disconnections_total", @["nim-libp2p"])
except KeyError:
0

asyncTest "e2e - GossipSub should process valid messages":
let (nodes, gossip0, gossip1) = await initializeGossipTest()

Expand All @@ -962,23 +968,25 @@ suite "GossipSub":
)
await sleepAsync(300.millis)

expect(system.KeyError):
check libp2p_gossipsub_peers_rate_limit_disconnections.valueByName("libp2p_gossipsub_peers_rate_limit_disconnections_total", @["nim-libp2p"]) == 0
check getCurrentDisconnections() == 0

await stopNodes(nodes)

asyncTest "e2e - GossipSub should rate limit undecodable messages above what is allowed":
let disconnections = getCurrentDisconnections()

let (nodes, gossip0, gossip1) = await initializeGossipTest()

# Simulate sending an undecodable message
await gossip0.peers[gossip1.switch.peerInfo.peerId].sendEncoded(newSeqWith[byte](30, 1.byte))
await sleepAsync(300.millis)

check libp2p_gossipsub_peers_rate_limit_disconnections.valueByName("libp2p_gossipsub_peers_rate_limit_disconnections_total", @["nim-libp2p"]) == 1
check getCurrentDisconnections() == disconnections + 1

await stopNodes(nodes)

asyncTest "e2e - GossipSub should rate limit messages with excessive useless data":
let disconnections = getCurrentDisconnections()
let (nodes, gossip0, gossip1) = await initializeGossipTest()

gossip0.broadcast(gossip1.mesh["foobar"], RPCMsg(control: some(ControlMessage(prune: @[
Expand All @@ -990,6 +998,6 @@ suite "GossipSub":
]))))
await sleepAsync(300.millis)

check libp2p_gossipsub_peers_rate_limit_disconnections.valueByName("libp2p_gossipsub_peers_rate_limit_disconnections_total", @["nim-libp2p"]) == 1
check getCurrentDisconnections() == disconnections + 1

await stopNodes(nodes)

0 comments on commit 5ff1fe4

Please sign in to comment.