From ba897c8d409695ba1431243b7999b9f29544e3c1 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Wed, 23 Aug 2023 02:42:34 +0300 Subject: [PATCH] A short test for normal connection (with timeout in 5 min) --- minode/tests/test_network.py | 69 ++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 minode/tests/test_network.py diff --git a/minode/tests/test_network.py b/minode/tests/test_network.py new file mode 100644 index 0000000..6526aa1 --- /dev/null +++ b/minode/tests/test_network.py @@ -0,0 +1,69 @@ +"""Tests for network connections""" +import logging +import os +import random +import unittest +import tempfile +import time + +from minode import connection, main, shared +from minode.manager import Manager + + +logging.basicConfig( + level=logging.INFO, + format='[%(asctime)s] [%(levelname)s] %(message)s') + + +class TestNetwork(unittest.TestCase): + """Test case starting connections""" + + @classmethod + def setUpClass(cls): + shared.data_directory = tempfile.gettempdir() + + def setUp(self): + shared.core_nodes.clear() + shared.unchecked_node_pool.clear() + shared.objects = {} + try: + os.remove(os.path.join(shared.data_directory, 'objects.pickle')) + except FileNotFoundError: + pass + + def test_connection(self): + """Check a normal connection - should receive objects""" + Manager.load_data() + self.assertGreaterEqual(len(shared.core_nodes), 3) + + main.bootstrap_from_dns() + self.assertGreaterEqual(len(shared.unchecked_node_pool), 3) + + started = time.time() + nodes = list(shared.core_nodes.union(shared.unchecked_node_pool)) + random.shuffle(nodes) + + for node in nodes: + # unknown = node not in shared.node_pool + # self.assertTrue(unknown) + unknown = True + shared.node_pool.discard(node) + + c = connection.Connection(*node) + c.start() + connection_started = time.time() + while c.status not in ('disconnecting', 'disconnected', 'failed'): + # The addr of established connection is added to nodes pool + if unknown and c.status == 'fully_established': + unknown = False + self.assertIn(node, shared.node_pool) + if shared.objects or time.time() - connection_started > 90: + c.status = 'disconnecting' + if time.time() - started > 300: + c.status = 'disconnecting' + self.fail('Failed to receive an object in %s sec' % 300) + time.sleep(0.2) + if shared.objects: # got some objects + break + else: + self.fail('Failed to establish a proper connection')