nomos-specs/mixnet/test_node.py

45 lines
1.4 KiB
Python
Raw Normal View History

2024-01-25 18:04:55 +09:00
import asyncio
from unittest import IsolatedAsyncioTestCase
2024-01-23 10:29:14 +09:00
import mixnet.framework.asyncio as asynciofw
from mixnet.connection import LocalSimplexConnection
2024-06-26 15:55:00 +09:00
from mixnet.node import Node
from mixnet.test_utils import (
init_mixnet_config,
)
2024-01-23 10:29:14 +09:00
2024-06-26 15:55:00 +09:00
class TestNode(IsolatedAsyncioTestCase):
async def test_node(self):
framework = asynciofw.Framework()
global_config, node_configs, _ = init_mixnet_config(10)
nodes = [
Node(framework, node_config, global_config) for node_config in node_configs
]
2024-06-26 15:55:00 +09:00
for i, node in enumerate(nodes):
node.connect(
nodes[(i + 1) % len(nodes)],
LocalSimplexConnection(framework),
LocalSimplexConnection(framework),
)
2024-06-26 15:55:00 +09:00
await nodes[0].send_message(b"block selection")
timeout = 15
for _ in range(timeout):
broadcasted_msgs = []
for node in nodes:
if not node.broadcast_channel.empty():
broadcasted_msgs.append(await node.broadcast_channel.get())
2024-06-26 15:55:00 +09:00
if len(broadcasted_msgs) == 0:
await asyncio.sleep(1)
else:
# We expect only one node to broadcast the message.
assert len(broadcasted_msgs) == 1
self.assertEqual(b"block selection", broadcasted_msgs[0])
return
self.fail("timeout")
# TODO: check noise