Skip to content

Commit

Permalink
added test for membership for join and leave topic
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankshampi committed Sep 30, 2024
1 parent 1c2e221 commit 66774dc
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/pubsub/testgossipmembership.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ import ../../libp2p/muxers/muxer
import ../../libp2p/protocols/pubsub/rpc/protobuf
import utils
import chronos
import unittest2, chronos, stew/byteutils, ../../libp2p/protocols/pubsub/gossipsub
import ../helpers

import sequtils, options, tables, sets, sugar
import chronos, chronicles # Added chronicles for logging (trace)
import stew/byteutils
import chronos/ratelimit
import metrics

import ../../libp2p/protocols/pubsub/errors as pubsub_errors
import ../helpers

proc noop(data: seq[byte]) {.async: (raises: [CancelledError, LPStreamError]).} =
Expand Down Expand Up @@ -182,3 +191,78 @@ suite "GossipSub Topic Membership Tests":

await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop()

# Test for verifying peers joining a topic using `JOIN(topic)`
asyncTest "handle JOIN event":
let gossipSub = TestGossipSub.init(newStandardSwitch())

let topic = "test-join-topic"

# Initialize relevant data structures
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
gossipSub.topicParams[topic] = TopicParams.init()
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()

var conns = newSeq[Connection]()

for i in 0 ..< 5:
let conn = TestBufferStream.new(noop)
conns &= conn
let peerId = randomPeerId()
conn.peerId = peerId
let peer = gossipSub.getPubSubPeer(peerId)
peer.sendConn = conn
gossipSub.gossipsub[topic].incl(peer)

# Simulate the peer joining the topic
gossipSub.PubSub.subscribe(
topic,
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
discard
,
)

check gossipSub.mesh[topic].len > 0 # Ensure the peer is added to the mesh
check gossipSub.topics.contains(topic) # Ensure the topic is in `topics`

await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop()

# Test for verifying peers leaving a topic using `LEAVE(topic)`
asyncTest "handle LEAVE event":
let gossipSub = TestGossipSub.init(newStandardSwitch())

let topic = "test-leave-topic"

# Initialize relevant data structures
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
gossipSub.topicParams[topic] = TopicParams.init()
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()

var conns = newSeq[Connection]()

for i in 0 ..< 5:
let conn = TestBufferStream.new(noop)
conns &= conn
let peerId = randomPeerId()
conn.peerId = peerId
let peer = gossipSub.getPubSubPeer(peerId)
peer.sendConn = conn
gossipSub.gossipsub[topic].incl(peer)

# Simulate peer joining the topic first
gossipSub.PubSub.subscribe(
topic,
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
discard
,
)

# Now simulate peer leaving the topic
gossipSub.PubSub.unsubscribeAll(topic)

check topic notin gossipSub.mesh # Ensure the peer is removed from the mesh
check topic in gossipSub.gossipsub # Ensure the topic remains in `gossipsub`

await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop()

0 comments on commit 66774dc

Please sign in to comment.