update tests

This commit is contained in:
Jack Robison 2021-01-21 16:20:01 -05:00
parent 20efdc70b3
commit eff2fe7a1b
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
3 changed files with 39 additions and 29 deletions

View file

@ -6,6 +6,7 @@ from unittest.mock import Mock
from lbry.wallet.network import Network from lbry.wallet.network import Network
from lbry.wallet.orchstr8.node import SPVNode from lbry.wallet.orchstr8.node import SPVNode
from lbry.wallet.rpc import RPCSession from lbry.wallet.rpc import RPCSession
from lbry.wallet.server.udp import StatusServer
from lbry.testcase import IntegrationTestCase, AsyncioTestCase from lbry.testcase import IntegrationTestCase, AsyncioTestCase
@ -64,22 +65,21 @@ class ReconnectTests(IntegrationTestCase):
async def test_multiple_servers(self): async def test_multiple_servers(self):
# we have a secondary node that connects later, so # we have a secondary node that connects later, so
node2 = SPVNode(self.conductor.spv_module, node_number=2) node2 = SPVNode(self.conductor.spv_module, node_number=2)
self.ledger.network.config['default_servers'].append((node2.hostname, node2.port))
await asyncio.wait_for(self.ledger.stop(), timeout=1)
await asyncio.wait_for(self.ledger.start(), timeout=1)
self.ledger.network.session_pool.new_connection_event.clear()
await node2.start(self.blockchain) await node2.start(self.blockchain)
# this is only to speed up the test as retrying would take 4+ seconds
for session in self.ledger.network.session_pool.sessions: self.ledger.network.config['default_servers'].append((node2.hostname, node2.port))
session.trigger_urgent_reconnect.set() self.ledger.network.config['default_servers'].reverse()
await asyncio.wait_for(self.ledger.network.session_pool.new_connection_event.wait(), timeout=1) self.assertEqual(50002, self.ledger.network.client.server[1])
self.assertEqual(2, len(list(self.ledger.network.session_pool.available_sessions))) await self.ledger.stop()
await self.ledger.start()
self.assertTrue(self.ledger.network.is_connected) self.assertTrue(self.ledger.network.is_connected)
switch_event = self.ledger.network.on_connected.first self.assertEqual(50003, self.ledger.network.client.server[1])
await node2.stop(True)
self.assertFalse(self.ledger.network.is_connected)
await self.ledger.resolve([], ['derp'])
self.assertEqual(50002, self.ledger.network.client.server[1])
await node2.stop(True) await node2.stop(True)
# secondary down, but primary is ok, do not switch! (switches trigger new on_connected events)
with self.assertRaises(asyncio.TimeoutError):
await asyncio.wait_for(switch_event, timeout=1)
async def test_direct_sync(self): async def test_direct_sync(self):
await self.ledger.stop() await self.ledger.stop()
@ -97,10 +97,13 @@ class ReconnectTests(IntegrationTestCase):
async def test_connection_drop_still_receives_events_after_reconnected(self): async def test_connection_drop_still_receives_events_after_reconnected(self):
address1 = await self.account.receiving.get_or_create_usable_address() address1 = await self.account.receiving.get_or_create_usable_address()
# disconnect and send a new tx, should reconnect and get it # disconnect and send a new tx, should reconnect and get it
self.ledger.network.client.connection_lost(Exception()) self.ledger.network.client.transport.close()
self.assertFalse(self.ledger.network.is_connected) self.assertFalse(self.ledger.network.is_connected)
await self.ledger.resolve([], 'derp')
sendtxid = await self.blockchain.send_to_address(address1, 1.1337) sendtxid = await self.blockchain.send_to_address(address1, 1.1337)
await asyncio.wait_for(self.on_transaction_id(sendtxid), 1.0) # mempool # await self.ledger.resolve([], 'derp')
# self.assertTrue(self.ledger.network.is_connected)
await asyncio.wait_for(self.on_transaction_id(sendtxid), 10.0) # mempool
await self.blockchain.generate(1) await self.blockchain.generate(1)
await self.on_transaction_id(sendtxid) # confirmed await self.on_transaction_id(sendtxid) # confirmed
self.assertLess(self.ledger.network.client.response_time, 1) # response time properly set lower, we are fine self.assertLess(self.ledger.network.client.response_time, 1) # response time properly set lower, we are fine
@ -122,7 +125,7 @@ class ReconnectTests(IntegrationTestCase):
await self.blockchain.generate(1) await self.blockchain.generate(1)
# (this is just so the test doesn't hang forever if it doesn't reconnect) # (this is just so the test doesn't hang forever if it doesn't reconnect)
if not self.ledger.network.is_connected: if not self.ledger.network.is_connected:
await asyncio.wait_for(self.ledger.network.on_connected.first, timeout=1.0) await asyncio.wait_for(self.ledger.network.on_connected.first, timeout=10.0)
# omg, the burned cable still works! torba is fire proof! # omg, the burned cable still works! torba is fire proof!
await self.ledger.network.get_transaction(sendtxid) await self.ledger.network.get_transaction(sendtxid)
@ -135,15 +138,19 @@ class ReconnectTests(IntegrationTestCase):
await self.ledger.network.on_connected.first await self.ledger.network.on_connected.first
self.assertTrue(self.ledger.network.is_connected) self.assertTrue(self.ledger.network.is_connected)
async def test_online_but_still_unavailable(self): # async def test_online_but_still_unavailable(self):
# Edge case. See issue #2445 for context # # Edge case. See issue #2445 for context
self.assertIsNotNone(self.ledger.network.session_pool.fastest_session) # self.assertIsNotNone(self.ledger.network.session_pool.fastest_session)
for session in self.ledger.network.session_pool.sessions: # for session in self.ledger.network.session_pool.sessions:
session.response_time = None # session.response_time = None
self.assertIsNone(self.ledger.network.session_pool.fastest_session) # self.assertIsNone(self.ledger.network.session_pool.fastest_session)
class ServerPickingTestCase(AsyncioTestCase): class ServerPickingTestCase(AsyncioTestCase):
async def _make_udp_server(self, port):
s = StatusServer()
await s.start(0, b'\x00' * 32, '127.0.0.1', port)
self.addCleanup(s.stop)
async def _make_fake_server(self, latency=1.0, port=1): async def _make_fake_server(self, latency=1.0, port=1):
# local fake server with artificial latency # local fake server with artificial latency
@ -155,6 +162,7 @@ class ServerPickingTestCase(AsyncioTestCase):
return {'height': 1} return {'height': 1}
server = await self.loop.create_server(lambda: FakeSession(), host='127.0.0.1', port=port) server = await self.loop.create_server(lambda: FakeSession(), host='127.0.0.1', port=port)
self.addCleanup(server.close) self.addCleanup(server.close)
await self._make_udp_server(port)
return '127.0.0.1', port return '127.0.0.1', port
async def _make_bad_server(self, port=42420): async def _make_bad_server(self, port=42420):
@ -163,9 +171,10 @@ class ServerPickingTestCase(AsyncioTestCase):
writer.write(await reader.read()) writer.write(await reader.read())
server = await asyncio.start_server(echo, host='127.0.0.1', port=port) server = await asyncio.start_server(echo, host='127.0.0.1', port=port)
self.addCleanup(server.close) self.addCleanup(server.close)
await self._make_udp_server(port)
return '127.0.0.1', port return '127.0.0.1', port
async def test_pick_fastest(self): async def _test_pick_fastest(self):
ledger = Mock(config={ ledger = Mock(config={
'default_servers': [ 'default_servers': [
# fast but unhealthy, should be discarded # fast but unhealthy, should be discarded
@ -181,8 +190,8 @@ class ServerPickingTestCase(AsyncioTestCase):
network = Network(ledger) network = Network(ledger)
self.addCleanup(network.stop) self.addCleanup(network.stop)
asyncio.ensure_future(network.start()) await network.start()
await asyncio.wait_for(network.on_connected.first, timeout=1) await asyncio.wait_for(network.on_connected.first, timeout=10)
self.assertTrue(network.is_connected) self.assertTrue(network.is_connected)
self.assertTupleEqual(network.client.server, ('127.0.0.1', 1337)) self.assertTupleEqual(network.client.server, ('127.0.0.1', 1337))
self.assertTrue(all([not session.is_closing() for session in network.session_pool.available_sessions])) self.assertTrue(all([not session.is_closing() for session in network.session_pool.available_sessions]))

View file

@ -43,12 +43,14 @@ class WalletCommands(CommandTestCase):
) )
async def test_wallet_reconnect(self): async def test_wallet_reconnect(self):
status = await self.daemon.jsonrpc_status()
self.assertEqual(len(status['wallet']['servers']), 1)
self.assertEqual(status['wallet']['servers'][0]['port'], 50002)
await self.conductor.spv_node.stop(True) await self.conductor.spv_node.stop(True)
self.conductor.spv_node.port = 54320 self.conductor.spv_node.port = 54320
await self.conductor.spv_node.start(self.conductor.blockchain_node) await self.conductor.spv_node.start(self.conductor.blockchain_node)
status = await self.daemon.jsonrpc_status() status = await self.daemon.jsonrpc_status()
self.assertEqual(len(status['wallet']['servers']), 1) self.assertEqual(len(status['wallet']['servers']), 0)
self.assertEqual(status['wallet']['servers'][0]['port'], 50002)
self.daemon.jsonrpc_settings_set('lbryum_servers', ['localhost:54320']) self.daemon.jsonrpc_settings_set('lbryum_servers', ['localhost:54320'])
await self.daemon.jsonrpc_wallet_reconnect() await self.daemon.jsonrpc_wallet_reconnect()
status = await self.daemon.jsonrpc_status() status = await self.daemon.jsonrpc_status()

View file

@ -68,8 +68,7 @@ class TestUsagePayment(CommandTestCase):
await node.start(self.blockchain, extraconf={"PAYMENT_ADDRESS": address, "DAILY_FEE": "1.1"}) await node.start(self.blockchain, extraconf={"PAYMENT_ADDRESS": address, "DAILY_FEE": "1.1"})
self.addCleanup(node.stop) self.addCleanup(node.stop)
self.daemon.jsonrpc_settings_set('lbryum_servers', [f"{node.hostname}:{node.port}"]) self.daemon.jsonrpc_settings_set('lbryum_servers', [f"{node.hostname}:{node.port}"])
await self.daemon.jsonrpc_wallet_reconnect() # await self.daemon.jsonrpc_wallet_reconnect()
LBRYElectrumX.set_server_features(node.server.env) LBRYElectrumX.set_server_features(node.server.env)
features = await self.ledger.network.get_server_features() features = await self.ledger.network.get_server_features()
self.assertEqual(features["payment_address"], address) self.assertEqual(features["payment_address"], address)