diff --git a/lbry/lbry/dht/node.py b/lbry/lbry/dht/node.py index bf8afba61..9f439e25d 100644 --- a/lbry/lbry/dht/node.py +++ b/lbry/lbry/dht/node.py @@ -162,7 +162,7 @@ class Node: for address, udp_port in known_node_urls or [] ])) except socket.gaierror: - await asyncio.sleep(30) + await asyncio.sleep(30, loop=self.loop) continue self.protocol.peer_manager.reset() diff --git a/lbry/lbry/extras/daemon/storage.py b/lbry/lbry/extras/daemon/storage.py index 3afd7ab11..0c8eeb02f 100644 --- a/lbry/lbry/extras/daemon/storage.py +++ b/lbry/lbry/extras/daemon/storage.py @@ -823,10 +823,7 @@ class SQLiteStorage(SQLiteMixin): def _save_kademlia_peers(transaction: sqlite3.Connection): transaction.execute('delete from peer').fetchall() transaction.executemany( - 'insert into peer(node_id, address, udp_port, tcp_port) values (?, ?, ?, ?)', ( - tuple( - [(binascii.hexlify(p.node_id), p.address, p.udp_port, p.tcp_port) for p in peers] - ) - ) + 'insert into peer(node_id, address, udp_port, tcp_port) values (?, ?, ?, ?)', + tuple([(binascii.hexlify(p.node_id), p.address, p.udp_port, p.tcp_port) for p in peers]) ).fetchall() return await self.db.run(_save_kademlia_peers) diff --git a/lbry/tests/integration/test_dht.py b/lbry/tests/integration/test_dht.py index 3e75acb91..761a3e315 100644 --- a/lbry/tests/integration/test_dht.py +++ b/lbry/tests/integration/test_dht.py @@ -31,7 +31,6 @@ class DHTIntegrationTest(AsyncioTestCase): self.addCleanup(node.stop) node.protocol.rpc_timeout = .5 node.protocol.ping_queue._default_delay = .5 - node._peer_search_timeout = .5 return node async def setup_network(self, size: int, start_port=40000, seed_nodes=1, external_ip='127.0.0.1'): diff --git a/lbry/tests/unit/dht/test_node.py b/lbry/tests/unit/dht/test_node.py index 28bd19bbe..7bb0463e0 100644 --- a/lbry/tests/unit/dht/test_node.py +++ b/lbry/tests/unit/dht/test_node.py @@ -88,14 +88,14 @@ class TestNodePingQueueDiscover(AsyncioTestCase): n.stop() -class TestTemporarilyLosingConnetction(AsyncioTestCase): +class TestTemporarilyLosingConnection(AsyncioTestCase): async def test_losing_connection(self): - loop = asyncio.get_event_loop() + loop = self.loop loop.set_debug(False) peer_addresses = [ - (f'127.0.0.1', 40000+i) for i in range(10) + ('127.0.0.1', 40000+i) for i in range(10) ] node_ids = [constants.generate_id(i) for i in range(10)] @@ -112,17 +112,20 @@ class TestTemporarilyLosingConnetction(AsyncioTestCase): with dht_mocks.mock_network_loop(loop, dht_network): for i, n in enumerate(nodes): await n._storage.open() + self.addCleanup(n.stop) n.start(peer_addresses[i][0], peer_addresses[:num_seeds]) + await asyncio.gather(*[n.joined.wait() for n in nodes]) node = nodes[-1] advance = dht_mocks.get_time_accelerator(loop, loop.time()) - await advance(1000) + await advance(500) # Join the network, assert that at least the known peers are in RT self.assertTrue(node.joined.is_set()) self.assertTrue(len(node.protocol.routing_table.get_peers()) >= num_seeds) # Refresh, so that the peers are persisted + self.assertFalse(len(await node._storage.get_persisted_kademlia_peers()) > num_seeds) await advance(4000) self.assertTrue(len(await node._storage.get_persisted_kademlia_peers()) > num_seeds) @@ -133,14 +136,14 @@ class TestTemporarilyLosingConnetction(AsyncioTestCase): # The peers are cleared on refresh from RT and storage await advance(4000) - self.assertFalse(node.protocol.routing_table.get_peers()) - self.assertFalse(await node._storage.get_persisted_kademlia_peers()) + self.assertListEqual([], node.protocol.routing_table.get_peers()) + self.assertListEqual([], await node._storage.get_persisted_kademlia_peers()) # Reconnect some of the previously stored - node shouldn't connect for peer_address, protocol in zip(peer_addresses[num_seeds+1:-2], popped_protocols[num_seeds+1:-2]): dht_network[peer_address] = protocol await advance(1000) - self.assertEqual(0, len(node.protocol.routing_table.get_peers())) + self.assertListEqual([], node.protocol.routing_table.get_peers()) # Reconnect some of the seed nodes for peer_address, protocol in zip(peer_addresses[:num_seeds], popped_protocols[:num_seeds]):