@@ -36,73 +36,72 @@ suite "GossipSub Topic Membership Tests":
36
36
37
37
# Addition of Designed Test cases for 6. Topic Membership Tests: https://www.notion.so/Gossipsub-651e02d4d7894bb2ac1e4edb55f3192d
38
38
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]) =
41
43
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)
42
59
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":
44
83
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)
49
85
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 ]
67
88
68
89
check gossipSub.topics.contains(topic) # Check if the topic is in topics
69
90
check gossipSub.gossipsub[topic].len() > 0 # Check if topic added to gossipsub
70
91
71
92
await allFuturesThrowing(conns.mapIt(it.close()))
72
93
await gossipSub.switch.stop()
73
94
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
75
96
asyncTest "handle UNSUBSCRIBE event":
76
- let gossipSub = TestGossipSub.init(newStandardSwitch())
77
-
78
- # Ensure topic is initialized properly in all relevant data structures
79
97
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)
95
99
96
100
# 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 ]
103
102
104
103
# Now unsubscribe from the topic
105
- gossipSub.PubSub.unsubscribeAll( topic)
104
+ unsubscribeFromTopics(gossipSub, @[ topic]) # Pass topic as seq [ string ]
106
105
107
106
# Verify the topic is removed from relevant structures
108
107
check topic notin gossipSub.topics # The topic should not be in topics
@@ -113,35 +112,21 @@ suite "GossipSub Topic Membership Tests":
113
112
await allFuturesThrowing(conns.mapIt(it.close()))
114
113
await gossipSub.switch.stop()
115
114
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
117
116
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
129
119
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)
136
122
137
123
# Verify that all topics are added to the topics and gossipsub
138
124
check gossipSub.topics.len == 3
139
125
for topic in topics:
140
126
check gossipSub.gossipsub[topic].len() >= 0
141
127
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)
145
130
146
131
# Ensure topics are removed from topics and mesh, but still present in gossipsub
147
132
for topic in topics:
@@ -152,7 +137,7 @@ suite "GossipSub Topic Membership Tests":
152
137
await allFuturesThrowing(conns.mapIt(it.close()))
153
138
await gossipSub.switch.stop()
154
139
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
156
141
asyncTest " subscription limit test" :
157
142
let gossipSub = TestGossipSub.init(newStandardSwitch())
158
143
gossipSub.topicsHigh = 10 # Set a limit for the number of subscriptions
0 commit comments