Skip to content

Commit 9230ecd

Browse files
PR review comment changes
1 parent eecc9aa commit 9230ecd

File tree

1 file changed

+57
-72
lines changed

1 file changed

+57
-72
lines changed

tests/pubsub/testgossipmembership.nim

+57-72
Original file line numberDiff line numberDiff line change
@@ -36,73 +36,72 @@ suite "GossipSub Topic Membership Tests":
3636

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

39-
# Simulate the `SUBSCRIBE` event and check proper handling in the mesh and gossipsub structures
40-
asyncTest "handle SUBSCRIBE event":
39+
# Generalized setup function to initialize one or more topics
40+
proc setupGossipSub(
41+
topics: seq[string], numPeers: int
42+
): (TestGossipSub, seq[Connection]) =
4143
let gossipSub = TestGossipSub.init(newStandardSwitch())
44+
var conns = newSeq[Connection]()
45+
46+
for topic in topics:
47+
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
48+
gossipSub.topicParams[topic] = TopicParams.init()
49+
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
50+
51+
for i in 0 ..< numPeers:
52+
let conn = TestBufferStream.new(noop)
53+
conns &= conn
54+
let peerId = randomPeerId()
55+
conn.peerId = peerId
56+
let peer = gossipSub.getPubSubPeer(peerId)
57+
peer.sendConn = conn
58+
gossipSub.gossipsub[topic].incl(peer)
4259

43-
# Ensure topic is correctly initialized
60+
return (gossipSub, conns)
61+
62+
# Wrapper function to initialize a single topic by converting it into a seq
63+
proc setupGossipSub(topic: string, numPeers: int): (TestGossipSub, seq[Connection]) =
64+
setupGossipSub(@[topic], numPeers)
65+
66+
# Helper function to subscribe to topics
67+
proc subscribeToTopics(gossipSub: TestGossipSub, topics: seq[string]) =
68+
for topic in topics:
69+
gossipSub.PubSub.subscribe(
70+
topic,
71+
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
72+
discard
73+
,
74+
)
75+
76+
# Helper function to unsubscribe to topics
77+
proc unsubscribeFromTopics(gossipSub: TestGossipSub, topics: seq[string]) =
78+
for topic in topics:
79+
gossipSub.PubSub.unsubscribeAll(topic)
80+
81+
# Simulate the `SUBSCRIBE` event and check proper handling in the mesh and gossipsub structures
82+
asyncTest "handle SUBSCRIBE event":
4483
let topic = "test-topic"
45-
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
46-
gossipSub.topicParams[topic] = TopicParams.init()
47-
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
48-
# Initialize gossipsub for the topic
84+
let (gossipSub, conns) = setupGossipSub(topic, 5)
4985

50-
var conns = newSeq[Connection]()
51-
for i in 0 ..< 5:
52-
let conn = TestBufferStream.new(noop)
53-
conns &= conn
54-
let peerId = randomPeerId()
55-
conn.peerId = peerId
56-
let peer = gossipSub.getPubSubPeer(peerId)
57-
peer.sendConn = conn
58-
gossipSub.gossipsub[topic].incl(peer) # Ensure the topic is added to gossipsub
59-
60-
# Subscribe to the topic
61-
gossipSub.PubSub.subscribe(
62-
topic,
63-
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
64-
discard
65-
,
66-
)
86+
# Subscribe to the topic (ensure `@[topic]` is passed as a sequence)
87+
subscribeToTopics(gossipSub, @[topic].toSeq()) # Pass topic as seq[string]
6788

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

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

74-
# This test will simulate an UNSUBSCRIBE event and check if the topic is removed from the relevant data structures but remains in gossipsub
95+
# Simulate an UNSUBSCRIBE event and check if the topic is removed from the relevant data structures but remains in gossipsub
7596
asyncTest "handle UNSUBSCRIBE event":
76-
let gossipSub = TestGossipSub.init(newStandardSwitch())
77-
78-
# Ensure topic is initialized properly in all relevant data structures
7997
let topic = "test-topic"
80-
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
81-
gossipSub.topicParams[topic] = TopicParams.init()
82-
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
83-
# Initialize gossipsub for the topic
84-
85-
var conns = newSeq[Connection]()
86-
for i in 0 ..< 5:
87-
let conn = TestBufferStream.new(noop)
88-
conns &= conn
89-
let peerId = randomPeerId()
90-
conn.peerId = peerId
91-
let peer = gossipSub.getPubSubPeer(peerId)
92-
peer.sendConn = conn
93-
gossipSub.gossipsub[topic].incl(peer)
94-
# Ensure peers are added to gossipsub for the topic
98+
let (gossipSub, conns) = setupGossipSub(topic, 5)
9599

96100
# Subscribe to the topic first
97-
gossipSub.PubSub.subscribe(
98-
topic,
99-
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
100-
discard
101-
,
102-
)
101+
subscribeToTopics(gossipSub, @[topic]) # Pass topic as seq[string]
103102

104103
# Now unsubscribe from the topic
105-
gossipSub.PubSub.unsubscribeAll(topic)
104+
unsubscribeFromTopics(gossipSub, @[topic]) # Pass topic as seq[string]
106105

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

116-
# This test ensures that multiple topics can be subscribed to and unsubscribed from, with proper initialization of the topic structures.
115+
# Test subscribing and unsubscribing multiple topics
117116
asyncTest "handle multiple SUBSCRIBE and UNSUBSCRIBE events":
118-
let gossipSub = TestGossipSub.init(newStandardSwitch())
119-
120-
let topics = ["topic1", "topic2", "topic3"]
121-
122-
var conns = newSeq[Connection]()
123-
for topic in topics:
124-
# Initialize all relevant structures before subscribing
125-
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
126-
gossipSub.topicParams[topic] = TopicParams.init()
127-
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
128-
# Initialize gossipsub for each topic
117+
let topics = ["topic1", "topic2", "topic3"].toSeq()
118+
let (gossipSub, conns) = setupGossipSub(topics, 5) # Initialize all topics
129119

130-
gossipSub.PubSub.subscribe(
131-
topic,
132-
proc(topic: string, data: seq[byte]): Future[void] {.async.} =
133-
discard
134-
,
135-
)
120+
# Subscribe to multiple topics
121+
subscribeToTopics(gossipSub, topics)
136122

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

142-
# Now unsubscribe from all topics
143-
for topic in topics:
144-
gossipSub.PubSub.unsubscribeAll(topic)
128+
# Unsubscribe from all topics
129+
unsubscribeFromTopics(gossipSub, topics)
145130

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

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

0 commit comments

Comments
 (0)