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 12 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
76 changes: 74 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,75 @@ 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 that 6 peers have joined the mesh
check gossipSub.mesh[topic].len == 6 # Mesh should have 6 peers

# Simulate 3 peers leaving the topic by unsubscribing them
var peersToUnsubscribe = gossipSub.mesh[topic].toSeq()[0 .. 2]
# Select the first 3 peers to unsubscribe
for peer in peersToUnsubscribe:
gossipSub.PubSub.unsubscribeAll(topic) # Unsubscribing from the topic

# Validate that 3 peers are still subscribed and 3 peers have been unsubscribed
check gossipSub.mesh[topic].len == 3 # Ensure 3 peers are still subscribed
for peer in peersToUnsubscribe:
check not gossipSub.mesh[topic].contains(peer)
# Ensure the first 3 peers are unsubscribed

# Clean up by closing connections and stopping the gossipSub switch
await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop()
Loading