Wait for routing table to clear with timeout

This commit is contained in:
Miroslav Kovar 2019-11-24 14:20:29 +01:00 committed by Jack Robison
parent 6bff298d1e
commit 36101db500
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2

View file

@ -91,6 +91,15 @@ class TestNodePingQueueDiscover(AsyncioTestCase):
class TestTemporarilyLosingConnection(AsyncioTestCase):
async def test_losing_connection(self):
async def wait_for(check_ok, insist, timeout=20):
start = loop.time()
while loop.time() - start < timeout:
if check_ok():
break
await asyncio.sleep(0)
else:
insist()
loop = self.loop
loop.set_debug(False)
@ -134,12 +143,18 @@ class TestTemporarilyLosingConnection(AsyncioTestCase):
# The peers are cleared on refresh from RT and storage
await advance(4000)
self.assertListEqual([], node.protocol.routing_table.get_peers())
self.assertListEqual([], await node._storage.get_persisted_kademlia_peers())
await wait_for(
lambda: len(node.protocol.routing_table.get_peers()) == 0,
lambda: self.assertListEqual(node.protocol.routing_table.get_peers(), [])
)
# Reconnect
dht_network[(node.protocol.external_ip, node.protocol.udp_port)] = node.protocol
# Check that node reconnects at least to them
await advance(1000)
self.assertTrue(len(node.protocol.routing_table.get_peers()) >= num_seeds)
await wait_for(
lambda: len(node.protocol.routing_table.get_peers()) >= num_seeds,
lambda: self.assertTrue(len(node.protocol.routing_table.get_peers()) >= num_seeds)
)