Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(gossipsub): Part 2 Test cases covering JOIN and LEAVE Events #1205

Merged
merged 17 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion tests/pubsub/testgossipmembership.nim
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ suite "GossipSub Topic Membership Tests":
check topic notin gossipSub.mesh
check topic in gossipSub.gossipsub

# The topic should remain in gossipsub (for fanout)
# The topic should remain in gossipsub

await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop()
Expand Down Expand Up @@ -174,3 +174,79 @@ 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 topic and mesh is updated":
let topic = "test-join-topic"

# Initialize the GossipSub system and simulate peer connections
let (gossipSub, conns) = setupGossipSub(topic, 5)

# Simulate peer joining the topic
subscribeToTopics(gossipSub, @[topic])

# Check that peers are added to the mesh and the topic is tracked
check gossipSub.mesh[topic].len == 5
check gossipSub.topics.contains(topic)

await allFuturesThrowing(conns.mapIt(it.close()))
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 first
subscribeToTopics(gossipSub, @[topic])

# Now simulate peer leaving the topic
unsubscribeFromTopics(gossipSub, @[topic])

# 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 topic = "test-multi-join-leave"

# Initialize the GossipSub system and simulate peer connections for 6 peers
let (gossipSub, conns) = setupGossipSub(@[topic], 6)

# Ensure the topic is correctly initialized in mesh and gossipsub
doAssert gossipSub.mesh.contains(topic), "Topic not found in mesh"
doAssert gossipSub.gossipsub.contains(topic), "Topic not found in gossipsub"

# Simulate 6 peers joining the topic
subscribeToTopics(gossipSub, @[topic])

# Assert that 6 peers have joined the mesh
doAssert gossipSub.mesh[topic].len == 6, "Expected 6 peers to join the mesh"

# 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]
for peer in peersToUnsubscribe:
echo "Unsubscribing peer: ", peer.peerId
gossipSub.unsubscribe(topic, dummyHandler)

# Now assert that 6 peers still remain in the mesh because the mesh retains peers
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:
doAssert gossipSub.mesh[topic].contains(peer),
"Peer should still be in mesh even after unsubscription"

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