updated as per review comment

This commit is contained in:
shashankshampi 2024-10-10 11:30:03 +05:30
parent a12b56c512
commit cb7ccaee31

View File

@ -187,10 +187,9 @@ suite "GossipSub Topic Membership Tests":
subscribeToTopics(gossipSub, @[topic]) subscribeToTopics(gossipSub, @[topic])
# Check that peers are added to the mesh and the topic is tracked # Check that peers are added to the mesh and the topic is tracked
check gossipSub.mesh[topic].len > 0 check gossipSub.mesh[topic].len == 5
check gossipSub.topics.contains(topic) check gossipSub.topics.contains(topic)
# Clean up by closing connections and stopping the gossipSub switch
await allFuturesThrowing(conns.mapIt(it.close())) await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop() await gossipSub.switch.stop()
@ -211,7 +210,6 @@ suite "GossipSub Topic Membership Tests":
check topic notin gossipSub.mesh check topic notin gossipSub.mesh
check topic in gossipSub.gossipsub check topic in gossipSub.gossipsub
# Clean up by closing connections and stopping the gossipSub switch
await allFuturesThrowing(conns.mapIt(it.close())) await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop() await gossipSub.switch.stop()
@ -229,20 +227,27 @@ suite "GossipSub Topic Membership Tests":
# Simulate 6 peers joining the topic # Simulate 6 peers joining the topic
subscribeToTopics(gossipSub, @[topic]) subscribeToTopics(gossipSub, @[topic])
check gossipSub.mesh[topic].len == 6 # Assert that 6 peers have joined the mesh
doAssert gossipSub.mesh[topic].len == 6, "Expected 6 peers to join the mesh"
# Simulate 3 peers leaving the topic by unsubscribing them individually # Define a simple handler for unsubscribing the peers
proc dummyHandler(topic: string, data: seq[byte]): Future[void] {.async.} =
discard
# Simulate 3 peers leaving the topic by unsubscribing them
var peersToUnsubscribe = gossipSub.mesh[topic].toSeq()[0 .. 2] var peersToUnsubscribe = gossipSub.mesh[topic].toSeq()[0 .. 2]
for peer in peersToUnsubscribe: for peer in peersToUnsubscribe:
echo "Unsubscribing peer: ", peer.peerId echo "Unsubscribing peer: ", peer.peerId
gossipSub.mesh[topic].excl(peer) gossipSub.PubSub.unsubscribe(topic, dummyHandler)
# Now validate the state of the mesh and gossipsub. Ensure 3 peers are still subscribed # Now assert that 6 peers still remain in the mesh because the mesh retains peers
check gossipSub.mesh[topic].len == 3 doAssert gossipSub.mesh[topic].len == 6,
"Expected 6 peers to still be in mesh after unsubscription"
# Assert that unsubscribed peers should remain in the mesh but should no longer receive messages
for peer in peersToUnsubscribe: for peer in peersToUnsubscribe:
# Ensure the first 3 peers are unsubscribed by checking if they are not in the mesh doAssert gossipSub.mesh[topic].contains(peer),
check not gossipSub.mesh[topic].contains(peer) "Peer should still be in mesh even after unsubscription"
await allFuturesThrowing(conns.mapIt(it.close())) await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop() await gossipSub.switch.stop()