47 lines
1.5 KiB
Python
Raw Permalink Normal View History

from unittest import IsolatedAsyncioTestCase
2024-01-23 10:29:14 +09:00
2024-07-15 18:07:26 +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):
2024-07-15 18:07:26 +09:00
framework = asynciofw.Framework()
global_config, node_configs, _ = init_mixnet_config(10)
2024-07-15 18:07:26 +09:00
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):
2024-06-28 12:26:44 +09:00
try:
2024-07-15 18:07:26 +09:00
node.connect(
nodes[(i + 1) % len(nodes)],
LocalSimplexConnection(framework),
LocalSimplexConnection(framework),
)
2024-06-28 12:26:44 +09:00
except ValueError as e:
print(e)
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():
2024-07-15 18:07:26 +09:00
broadcasted_msgs.append(await node.broadcast_channel.get())
2024-06-26 15:55:00 +09:00
if len(broadcasted_msgs) == 0:
2024-07-15 18:07:26 +09:00
await framework.sleep(1)
2024-06-26 15:55:00 +09:00
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