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 13 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
75 changes: 73 additions & 2 deletions tests/pubsub/testgossipmembership.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ suite "GossipSub Topic Membership Tests":
# Helper function to subscribe to topics
proc subscribeToTopics(gossipSub: TestGossipSub, topics: seq[string]) =
for topic in topics:
gossipSub.PubSub.subscribe(
gossipSub.subscribe(
topic,
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
discard
Expand Down 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,74 @@ 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 > 0
AlejandroCabeza marked this conversation as resolved.
Show resolved Hide resolved
check gossipSub.topics.contains(topic)

# Clean up by closing connections and stopping the gossipSub switch
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

# Clean up by closing connections and stopping the gossipSub switch
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])

check gossipSub.mesh[topic].len == 6

# Simulate 3 peers leaving the topic by unsubscribing them individually
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of running the excl method I'd suggest unsubscribing using the higher level unsubscribe, so we use the opposite (and public) methods.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used unsubscribe now.
Thanks done.

var peersToUnsubscribe = gossipSub.mesh[topic].toSeq()[0 .. 2]
for peer in peersToUnsubscribe:
echo "Unsubscribing peer: ", peer.peerId
gossipSub.mesh[topic].excl(peer)

# Now validate the state of the mesh and gossipsub. Ensure 3 peers are still subscribed
check gossipSub.mesh[topic].len == 3

for peer in peersToUnsubscribe:
# Ensure the first 3 peers are unsubscribed by checking if they are not in the mesh
check not gossipSub.mesh[topic].contains(peer)

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