Skip to content

Commit

Permalink
PR review comment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankshampi committed Oct 1, 2024
1 parent eecc9aa commit 9230ecd
Showing 1 changed file with 57 additions and 72 deletions.
129 changes: 57 additions & 72 deletions tests/pubsub/testgossipmembership.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,73 +36,72 @@ suite "GossipSub Topic Membership Tests":

# Addition of Designed Test cases for 6. Topic Membership Tests: https://www.notion.so/Gossipsub-651e02d4d7894bb2ac1e4edb55f3192d

# Simulate the `SUBSCRIBE` event and check proper handling in the mesh and gossipsub structures
asyncTest "handle SUBSCRIBE event":
# Generalized setup function to initialize one or more topics
proc setupGossipSub(
topics: seq[string], numPeers: int
): (TestGossipSub, seq[Connection]) =
let gossipSub = TestGossipSub.init(newStandardSwitch())
var conns = newSeq[Connection]()

for topic in topics:
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
gossipSub.topicParams[topic] = TopicParams.init()
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()

for i in 0 ..< numPeers:
let conn = TestBufferStream.new(noop)
conns &= conn
let peerId = randomPeerId()
conn.peerId = peerId
let peer = gossipSub.getPubSubPeer(peerId)
peer.sendConn = conn
gossipSub.gossipsub[topic].incl(peer)

# Ensure topic is correctly initialized
return (gossipSub, conns)

# Wrapper function to initialize a single topic by converting it into a seq
proc setupGossipSub(topic: string, numPeers: int): (TestGossipSub, seq[Connection]) =
setupGossipSub(@[topic], numPeers)

# Helper function to subscribe to topics
proc subscribeToTopics(gossipSub: TestGossipSub, topics: seq[string]) =
for topic in topics:
gossipSub.PubSub.subscribe(
topic,
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
discard
,
)

# Helper function to unsubscribe to topics
proc unsubscribeFromTopics(gossipSub: TestGossipSub, topics: seq[string]) =
for topic in topics:
gossipSub.PubSub.unsubscribeAll(topic)

# Simulate the `SUBSCRIBE` event and check proper handling in the mesh and gossipsub structures
asyncTest "handle SUBSCRIBE event":
let topic = "test-topic"
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
gossipSub.topicParams[topic] = TopicParams.init()
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
# Initialize gossipsub for the topic
let (gossipSub, conns) = setupGossipSub(topic, 5)

var conns = newSeq[Connection]()
for i in 0 ..< 5:
let conn = TestBufferStream.new(noop)
conns &= conn
let peerId = randomPeerId()
conn.peerId = peerId
let peer = gossipSub.getPubSubPeer(peerId)
peer.sendConn = conn
gossipSub.gossipsub[topic].incl(peer) # Ensure the topic is added to gossipsub

# Subscribe to the topic
gossipSub.PubSub.subscribe(
topic,
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
discard
,
)
# Subscribe to the topic (ensure `@[topic]` is passed as a sequence)
subscribeToTopics(gossipSub, @[topic].toSeq()) # Pass topic as seq[string]

check gossipSub.topics.contains(topic) # Check if the topic is in topics
check gossipSub.gossipsub[topic].len() > 0 # Check if topic added to gossipsub

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

# This test will simulate an UNSUBSCRIBE event and check if the topic is removed from the relevant data structures but remains in gossipsub
# Simulate an UNSUBSCRIBE event and check if the topic is removed from the relevant data structures but remains in gossipsub
asyncTest "handle UNSUBSCRIBE event":
let gossipSub = TestGossipSub.init(newStandardSwitch())

# Ensure topic is initialized properly in all relevant data structures
let topic = "test-topic"
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
gossipSub.topicParams[topic] = TopicParams.init()
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
# Initialize gossipsub for the topic

var conns = newSeq[Connection]()
for i in 0 ..< 5:
let conn = TestBufferStream.new(noop)
conns &= conn
let peerId = randomPeerId()
conn.peerId = peerId
let peer = gossipSub.getPubSubPeer(peerId)
peer.sendConn = conn
gossipSub.gossipsub[topic].incl(peer)
# Ensure peers are added to gossipsub for the topic
let (gossipSub, conns) = setupGossipSub(topic, 5)

# Subscribe to the topic first
gossipSub.PubSub.subscribe(
topic,
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
discard
,
)
subscribeToTopics(gossipSub, @[topic]) # Pass topic as seq[string]

# Now unsubscribe from the topic
gossipSub.PubSub.unsubscribeAll(topic)
unsubscribeFromTopics(gossipSub, @[topic]) # Pass topic as seq[string]

# Verify the topic is removed from relevant structures
check topic notin gossipSub.topics # The topic should not be in topics
Expand All @@ -113,35 +112,21 @@ suite "GossipSub Topic Membership Tests":
await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop()

# This test ensures that multiple topics can be subscribed to and unsubscribed from, with proper initialization of the topic structures.
# Test subscribing and unsubscribing multiple topics
asyncTest "handle multiple SUBSCRIBE and UNSUBSCRIBE events":
let gossipSub = TestGossipSub.init(newStandardSwitch())

let topics = ["topic1", "topic2", "topic3"]

var conns = newSeq[Connection]()
for topic in topics:
# Initialize all relevant structures before subscribing
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
gossipSub.topicParams[topic] = TopicParams.init()
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
# Initialize gossipsub for each topic
let topics = ["topic1", "topic2", "topic3"].toSeq()
let (gossipSub, conns) = setupGossipSub(topics, 5) # Initialize all topics

gossipSub.PubSub.subscribe(
topic,
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
discard
,
)
# Subscribe to multiple topics
subscribeToTopics(gossipSub, topics)

# Verify that all topics are added to the topics and gossipsub
check gossipSub.topics.len == 3
for topic in topics:
check gossipSub.gossipsub[topic].len() >= 0

# Now unsubscribe from all topics
for topic in topics:
gossipSub.PubSub.unsubscribeAll(topic)
# Unsubscribe from all topics
unsubscribeFromTopics(gossipSub, topics)

# Ensure topics are removed from topics and mesh, but still present in gossipsub
for topic in topics:
Expand All @@ -152,7 +137,7 @@ suite "GossipSub Topic Membership Tests":
await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop()

# This test ensures that the number of subscriptions does not exceed the limit set in the GossipSub parameters
# Test ensuring that the number of subscriptions does not exceed the limit set in the GossipSub parameters
asyncTest "subscription limit test":
let gossipSub = TestGossipSub.init(newStandardSwitch())
gossipSub.topicsHigh = 10 # Set a limit for the number of subscriptions
Expand Down

0 comments on commit 9230ecd

Please sign in to comment.