mirror of
https://github.com/vacp2p/nim-libp2p.git
synced 2025-03-01 08:30:29 +00:00
added documentation as per request
This commit is contained in:
parent
df674d586a
commit
1bfdded523
@ -39,8 +39,9 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
checkTrackers()
|
checkTrackers()
|
||||||
# Addition of Designed Test cases for 6. Topic Membership Tests: https://www.notion.so/Gossipsub-651e02d4d7894bb2ac1e4edb55f3192d
|
# Addition of Designed Test cases for 6. Topic Membership Tests: https://www.notion.so/Gossipsub-651e02d4d7894bb2ac1e4edb55f3192d
|
||||||
|
|
||||||
# Simulate the `SUBSCRIBE` to the topic and check proper handling in the mesh and gossipsub structures
|
# Test for subscribing to a topic and verifying mesh and gossipsub structures
|
||||||
asyncTest "handle SUBSCRIBE to the topic":
|
asyncTest "handle SUBSCRIBE to the topic":
|
||||||
|
# Given 5 gossipsub nodes
|
||||||
let
|
let
|
||||||
numberOfNodes = 5
|
numberOfNodes = 5
|
||||||
topic = "test-topic"
|
topic = "test-topic"
|
||||||
@ -48,23 +49,25 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
await allFuturesThrowing(nodes.mapIt(it.switch.start()))
|
await allFuturesThrowing(nodes.mapIt(it.switch.start()))
|
||||||
|
|
||||||
|
# When all of them are connected and subscribed to the same topic
|
||||||
await subscribeNodes(nodes)
|
await subscribeNodes(nodes)
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
node.subscribe(topic, voidTopicHandler)
|
node.subscribe(topic, voidTopicHandler)
|
||||||
|
|
||||||
await sleepAsync(2 * DURATION_TIMEOUT)
|
await sleepAsync(2 * DURATION_TIMEOUT)
|
||||||
|
|
||||||
|
# Then their related attributes should reflect that
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
let currentGossip = GossipSub(node)
|
let currentGossip = GossipSub(node)
|
||||||
|
|
||||||
check currentGossip.topics.contains(topic)
|
check currentGossip.topics.contains(topic)
|
||||||
check currentGossip.gossipsub[topic].len() == numberOfNodes - 1
|
check currentGossip.gossipsub[topic].len() == numberOfNodes - 1
|
||||||
check currentGossip.mesh[topic].len() == numberOfNodes - 1
|
check currentGossip.mesh[topic].len() == numberOfNodes - 1
|
||||||
|
|
||||||
await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop())))
|
await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop())))
|
||||||
|
|
||||||
# Simulate an UNSUBSCRIBE to the topic and check if the topic is removed from all relevant data structures
|
# Test unsubscribing from a topic and verifying removal from relevant data structures
|
||||||
asyncTest "handle UNSUBSCRIBE to the topic":
|
asyncTest "handle UNSUBSCRIBE to the topic":
|
||||||
|
# Given 5 nodes subscribed to a topic
|
||||||
let
|
let
|
||||||
numberOfNodes = 5
|
numberOfNodes = 5
|
||||||
topic = "test-topic"
|
topic = "test-topic"
|
||||||
@ -78,21 +81,20 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
await sleepAsync(2 * DURATION_TIMEOUT)
|
await sleepAsync(2 * DURATION_TIMEOUT)
|
||||||
|
|
||||||
|
# When all nodes unsubscribe from the topic
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
node.unsubscribe(topic, voidTopicHandler)
|
node.unsubscribe(topic, voidTopicHandler)
|
||||||
|
|
||||||
await sleepAsync(2 * DURATION_TIMEOUT)
|
await sleepAsync(2 * DURATION_TIMEOUT)
|
||||||
|
|
||||||
|
# Then the topic should be removed from relevant data structures
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
let currentGossip = GossipSub(node)
|
let currentGossip = GossipSub(node)
|
||||||
|
|
||||||
check topic notin currentGossip.topics
|
check topic notin currentGossip.topics
|
||||||
|
|
||||||
if topic in currentGossip.mesh:
|
if topic in currentGossip.mesh:
|
||||||
check currentGossip.mesh[topic].len == 0
|
check currentGossip.mesh[topic].len == 0
|
||||||
else:
|
else:
|
||||||
check topic notin currentGossip.mesh
|
check topic notin currentGossip.mesh
|
||||||
|
|
||||||
if topic in currentGossip.gossipsub:
|
if topic in currentGossip.gossipsub:
|
||||||
check currentGossip.gossipsub[topic].len == 0
|
check currentGossip.gossipsub[topic].len == 0
|
||||||
else:
|
else:
|
||||||
@ -102,13 +104,15 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
# Test subscribing and unsubscribing multiple topics
|
# Test subscribing and unsubscribing multiple topics
|
||||||
asyncTest "handle SUBSCRIBE and UNSUBSCRIBE multiple topics":
|
asyncTest "handle SUBSCRIBE and UNSUBSCRIBE multiple topics":
|
||||||
|
# Given 3 nodes and multiple topics
|
||||||
let
|
let
|
||||||
numberOfNodes = 5
|
numberOfNodes = 3
|
||||||
topics = ["topic1", "topic2", "topic3"].toSeq()
|
topics = ["topic1", "topic2", "topic3"].toSeq()
|
||||||
nodes = generateNodes(numberOfNodes, gossip = true)
|
nodes = generateNodes(numberOfNodes, gossip = true)
|
||||||
|
|
||||||
await allFuturesThrowing(nodes.mapIt(it.switch.start()))
|
await allFuturesThrowing(nodes.mapIt(it.switch.start()))
|
||||||
|
|
||||||
|
# When nodes subscribe and then unsubscribe to multiple topics
|
||||||
await subscribeNodes(nodes)
|
await subscribeNodes(nodes)
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
for topic in topics:
|
for topic in topics:
|
||||||
@ -116,18 +120,21 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
await sleepAsync(2 * DURATION_TIMEOUT)
|
await sleepAsync(2 * DURATION_TIMEOUT)
|
||||||
|
|
||||||
|
# Then all nodes should be subscribed to the topics initially
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
let currentGossip = GossipSub(node)
|
let currentGossip = GossipSub(node)
|
||||||
check currentGossip.topics.len == topics.len
|
check currentGossip.topics.len == topics.len
|
||||||
for topic in topics:
|
for topic in topics:
|
||||||
check currentGossip.gossipsub[topic].len == numberOfNodes - 1
|
check currentGossip.gossipsub[topic].len == numberOfNodes - 1
|
||||||
|
|
||||||
|
# When they unsubscribe from all topics
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
for topic in topics:
|
for topic in topics:
|
||||||
node.unsubscribe(topic, voidTopicHandler)
|
node.unsubscribe(topic, voidTopicHandler)
|
||||||
|
|
||||||
await sleepAsync(2 * DURATION_TIMEOUT)
|
await sleepAsync(2 * DURATION_TIMEOUT)
|
||||||
|
|
||||||
|
# Then topics should be removed from mesh and gossipsub
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
let currentGossip = GossipSub(node)
|
let currentGossip = GossipSub(node)
|
||||||
for topic in topics:
|
for topic in topics:
|
||||||
@ -137,8 +144,9 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop())))
|
await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop())))
|
||||||
|
|
||||||
# Test ensuring that the number of subscriptions does not exceed the limit set in the GossipSub parameters
|
# Test ensuring the number of subscriptions does not exceed a set limit
|
||||||
asyncTest "subscription limit test":
|
asyncTest "subscription limit test":
|
||||||
|
# Given one node and a subscription limit of 10 topics
|
||||||
let
|
let
|
||||||
topicCount = 15
|
topicCount = 15
|
||||||
gossipSubParams = 10
|
gossipSubParams = 10
|
||||||
@ -148,6 +156,7 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
await allFuturesThrowing(nodes.mapIt(it.switch.start()))
|
await allFuturesThrowing(nodes.mapIt(it.switch.start()))
|
||||||
|
|
||||||
|
# When attempting to subscribe to 15 topics
|
||||||
let gossipSub = GossipSub(nodes[0])
|
let gossipSub = GossipSub(nodes[0])
|
||||||
gossipSub.topicsHigh = gossipSubParams
|
gossipSub.topicsHigh = gossipSubParams
|
||||||
|
|
||||||
@ -160,14 +169,16 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
,
|
,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
# Then the subscription count should not exceed the limit
|
||||||
check gossipSub.topics.len == gossipSub.topicsHigh
|
check gossipSub.topics.len == gossipSub.topicsHigh
|
||||||
|
|
||||||
check gossipSub.topics.len == gossipSub.topicsHigh
|
check gossipSub.topics.len == gossipSub.topicsHigh
|
||||||
|
|
||||||
await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop())))
|
await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop())))
|
||||||
|
|
||||||
# Test for verifying peers joining a topic using `JOIN(topic)`
|
# Test verifying peers joining a topic using `JOIN(topic)`
|
||||||
asyncTest "handle JOIN topic and mesh is updated":
|
asyncTest "handle JOIN topic and mesh is updated":
|
||||||
|
# Given 5 nodes and a join request to a topic
|
||||||
let
|
let
|
||||||
topic = "test-join-topic"
|
topic = "test-join-topic"
|
||||||
numberOfNodes = 5
|
numberOfNodes = 5
|
||||||
@ -175,12 +186,14 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
await allFuturesThrowing(nodes.mapIt(it.switch.start()))
|
await allFuturesThrowing(nodes.mapIt(it.switch.start()))
|
||||||
|
|
||||||
|
# When nodes join and subscribe to the topic
|
||||||
await subscribeNodes(nodes)
|
await subscribeNodes(nodes)
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
node.subscribe(topic, voidTopicHandler)
|
node.subscribe(topic, voidTopicHandler)
|
||||||
|
|
||||||
await sleepAsync(2 * DURATION_TIMEOUT)
|
await sleepAsync(2 * DURATION_TIMEOUT)
|
||||||
|
|
||||||
|
# Then each node's mesh should reflect this update
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
let currentGossip = GossipSub(node)
|
let currentGossip = GossipSub(node)
|
||||||
check currentGossip.mesh[topic].len == numberOfNodes - 1
|
check currentGossip.mesh[topic].len == numberOfNodes - 1
|
||||||
@ -188,8 +201,9 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop())))
|
await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop())))
|
||||||
|
|
||||||
# Test the behavior when multiple peers join and leave a topic simultaneously.
|
# Test the behavior when multiple peers join and leave a topic simultaneously
|
||||||
asyncTest "multiple peers join and leave topic simultaneously":
|
asyncTest "multiple peers join and leave topic simultaneously":
|
||||||
|
# Given 6 nodes and a shared topic
|
||||||
let
|
let
|
||||||
numberOfNodes = 6
|
numberOfNodes = 6
|
||||||
topic = "foobar"
|
topic = "foobar"
|
||||||
@ -202,12 +216,14 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
await sleepAsync(2 * DURATION_TIMEOUT)
|
await sleepAsync(2 * DURATION_TIMEOUT)
|
||||||
|
|
||||||
|
# When they all join the topic, their attributes should reflect this
|
||||||
for i in 0 ..< numberOfNodes:
|
for i in 0 ..< numberOfNodes:
|
||||||
let currentGossip = GossipSub(nodes[i])
|
let currentGossip = GossipSub(nodes[i])
|
||||||
check currentGossip.gossipsub.hasKey(topic)
|
check currentGossip.gossipsub.hasKey(topic)
|
||||||
check currentGossip.mesh.hasKey(topic)
|
check currentGossip.mesh.hasKey(topic)
|
||||||
check currentGossip.topics.contains(topic)
|
check currentGossip.topics.contains(topic)
|
||||||
|
|
||||||
|
# When peers subscribe to each other's topics
|
||||||
for x in 0 ..< numberOfNodes:
|
for x in 0 ..< numberOfNodes:
|
||||||
for y in 0 ..< numberOfNodes:
|
for y in 0 ..< numberOfNodes:
|
||||||
if x != y:
|
if x != y:
|
||||||
@ -223,6 +239,7 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
currentGossip.mesh[topic].len == expectedNumberOfPeers
|
currentGossip.mesh[topic].len == expectedNumberOfPeers
|
||||||
currentGossip.fanout.len == 0
|
currentGossip.fanout.len == 0
|
||||||
|
|
||||||
|
# When some peers unsubscribe
|
||||||
let firstNodeGossip = GossipSub(nodes[0])
|
let firstNodeGossip = GossipSub(nodes[0])
|
||||||
let peersToUnsubscribe = nodes[1 ..< 3]
|
let peersToUnsubscribe = nodes[1 ..< 3]
|
||||||
for peer in peersToUnsubscribe:
|
for peer in peersToUnsubscribe:
|
||||||
@ -230,6 +247,7 @@ suite "GossipSub Topic Membership Tests":
|
|||||||
|
|
||||||
await sleepAsync(3 * DURATION_TIMEOUT)
|
await sleepAsync(3 * DURATION_TIMEOUT)
|
||||||
|
|
||||||
|
# Then the mesh and gossipsub should reflect the updated peer count
|
||||||
check firstNodeGossip.mesh.getOrDefault(topic).len == 3
|
check firstNodeGossip.mesh.getOrDefault(topic).len == 3
|
||||||
check firstNodeGossip.gossipsub[topic].len == 3
|
check firstNodeGossip.gossipsub[topic].len == 3
|
||||||
check topic in firstNodeGossip.topics
|
check topic in firstNodeGossip.topics
|
||||||
|
Loading…
x
Reference in New Issue
Block a user