diff --git a/tests/pubsub/testgossipmembership.nim b/tests/pubsub/testgossipmembership.nim index c42388df8..b941fce32 100644 --- a/tests/pubsub/testgossipmembership.nim +++ b/tests/pubsub/testgossipmembership.nim @@ -216,88 +216,60 @@ suite "GossipSub Topic Membership Tests": await gossipSub.switch.stop() # Test for verifying peers leaving a topic using `LEAVE(topic)` - asyncTest "handle LEAVE topic and mesh is updated": - let topic = "test-leave-topic" - - # Initialize the GossipSub system and simulate peer connections - let (gossipSub, conns) = setupGossipSub(topic, 5) - - # Simulate peer joining the topic using commonSubscribe - commonSubscribe(@[gossipSub], topic, voidTopicHandler) - - # Now simulate peer leaving the topic using commonUnsubscribe - commonUnsubscribe(@[gossipSub], topic, voidTopicHandler) - - # Check that peers are removed from the mesh but the topic remains in gossipsub - check topic notin gossipSub.mesh - check topic in gossipSub.gossipsub - - await allFuturesThrowing(conns.mapIt(it.close())) - await gossipSub.switch.stop() - - # Test the behavior when multiple peers join and leave a topic simultaneously. asyncTest "multiple peers join and leave topic simultaneously": let numberOfNodes = 6 topic = "foobar" - nodes = generateNodes(numberOfNodes, gossip = true) - nodesFut = await allFinished(nodes.mapIt(it.switch.start())) + var nodes = newSeq[TestGossipSub]() - # Subscribe all nodes to the topic - await subscribeNodes(nodes) - for node in nodes: - node.subscribe(topic, voidTopicHandler) + # Initialize each node and start the switch + for i in 0 ..< numberOfNodes: + let (gossipSub, _) = setupGossipSub(topic, 5) + nodes.add(gossipSub) # Add the gossipSub instance to the sequence + await gossipSub.switch.start() + + # Subscribe all nodes to the topic using the commonSubscribe method + commonSubscribe(nodes, topic, voidTopicHandler) # Allow time for subscription propagation await sleepAsync(2 * DURATION_TIMEOUT) - # Ensure each node is subscribed by checking the gossipsub field + # Ensure that all nodes are subscribed to the topic for i in 0 ..< numberOfNodes: - let currentGossip = GossipSub(nodes[i]) # Rename to 'currentGossip' - doAssert currentGossip.gossipsub.hasKey(topic), - "Node is not subscribed to the topic" - - # Print mesh status before connecting nodes - echo "Initial mesh size:" - for i in 0 ..< numberOfNodes: - let currentGossip = GossipSub(nodes[i]) # Rename to 'currentGossip' - echo "Node ", i, " mesh size: ", currentGossip.mesh.getOrDefault(topic).len - - # Connect all nodes to each other and ensure subscription propagation - for x in 0 ..< numberOfNodes: - for y in 0 ..< numberOfNodes: - if x != y: - await waitSub(nodes[x], nodes[y], topic) + let currentGossip = nodes[i] + doAssert currentGossip.gossipsub.hasKey(topic), "Node is not subscribed to the topic" # Allow time for mesh stabilization await sleepAsync(2 * DURATION_TIMEOUT) - # Print mesh status after stabilization - echo "Mesh size after connecting:" + # Print the mesh size for all nodes before unsubscription for i in 0 ..< numberOfNodes: - let currentGossip = GossipSub(nodes[i]) # Rename to 'currentGossip' + let currentGossip = nodes[i] echo "Node ", i, " mesh size: ", currentGossip.mesh.getOrDefault(topic).len # Expected number of peers in the mesh let expectedNumberOfPeers = numberOfNodes - 1 for i in 0 ..< numberOfNodes: - let currentGossip = GossipSub(nodes[i]) # Rename to 'currentGossip' + let currentGossip = nodes[i] check: currentGossip.gossipsub[topic].len == expectedNumberOfPeers currentGossip.mesh[topic].len == expectedNumberOfPeers currentGossip.fanout.len == 0 # Simulate unsubscription of 3 peers - let firstNodeGossip = GossipSub(nodes[0]) # Initialize for the first node - let peersToUnsubscribe = firstNodeGossip.mesh[topic].toSeq()[0 .. 2] - for peer in peersToUnsubscribe: - firstNodeGossip.unsubscribe(topic, voidTopicHandler) - echo "Unsubscribing peer: ", peer.peerId + let peersToUnsubscribe = nodes[0].mesh[topic].toSeq()[0 .. 2] + + # Unsubscribe these peers from all the nodes + for node in nodes: + for peer in peersToUnsubscribe: + node.unsubscribe(topic, voidTopicHandler) + echo "Unsubscribing peer: ", peer.peerId # Allow time for heartbeat to adjust the mesh - await sleepAsync(3 * DURATION_TIMEOUT) # Increased delay for mesh stabilization + await sleepAsync(3 * DURATION_TIMEOUT) - # Check mesh status again, expecting remaining peers + # Check the mesh size for the first node after unsubscription + let firstNodeGossip = nodes[0] echo "Mesh size after unsubscription: ", firstNodeGossip.mesh.getOrDefault(topic).len doAssert firstNodeGossip.mesh.getOrDefault(topic).len == 3, @@ -305,8 +277,8 @@ suite "GossipSub Topic Membership Tests": # Assert that unsubscribed peers are no longer in the mesh for peer in peersToUnsubscribe: - doAssert not firstNodeGossip.mesh[topic].contains(peer), - "Unsubscribed peer should not be in the mesh" + for node in nodes: + doAssert not node.mesh[topic].contains(peer), + "Unsubscribed peer should not be in the mesh" - # Cleanup: stop all nodes await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop())))