From 854e3c530356b2f3e3c21caa1ce58526b336730f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 10 Aug 2019 20:18:24 +0930 Subject: [PATCH] pytest: wait for sync by default when starting lightningd. Otherwise we get some spurious test failures. Signed-off-by: Rusty Russell --- tests/test_misc.py | 6 +++--- tests/utils.py | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/test_misc.py b/tests/test_misc.py index 2480809d7..ed9ba8fd6 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -122,7 +122,7 @@ def test_bitcoin_ibd(node_factory, bitcoind): l1 = node_factory.get_node(start=False) l1.daemon.rpcproxy.mock_rpc('getblockchaininfo', info) - l1.start() + l1.start(wait_for_bitcoind_sync=False) # This happens before the Starting message start() waits for. assert l1.daemon.is_in_log('Waiting for initial block download') @@ -153,7 +153,7 @@ def test_lightningd_still_loading(node_factory, bitcoind, executor): } # Start it, establish channel, get extra funds. - l1, l2 = node_factory.line_graph(2, opts={'may_reconnect': True}) + l1, l2 = node_factory.line_graph(2, opts={'may_reconnect': True, 'wait_for_bitcoind_sync': False}) # Extra funds, for second channel attempt. bitcoind.rpc.sendtoaddress(l1.rpc.newaddr()['bech32'], 1.0) # Balance l1<->l2 channel @@ -173,7 +173,7 @@ def test_lightningd_still_loading(node_factory, bitcoind, executor): slow_blockid = bitcoind.rpc.getblockhash(bitcoind.rpc.getblockcount()) l1.daemon.rpcproxy.mock_rpc('getblock', mock_getblock) - l1.start() + l1.start(wait_for_bitcoind_sync=False) # It will warn about being out-of-sync. assert 'warning_bitcoind_sync' not in l1.rpc.getinfo() diff --git a/tests/utils.py b/tests/utils.py index b4f84d1e2..560a8762e 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -534,12 +534,19 @@ class LightningNode(object): c.close() db.close() - def start(self): + def is_synced_with_bitcoin(self, info=None): + if info is None: + info = self.rpc.getinfo() + return 'warning_bitcoind_sync' not in info and 'warning_lightningd_sync' not in info + + def start(self, wait_for_bitcoind_sync=True): self.daemon.start() # Cache `getinfo`, we'll be using it a lot self.info = self.rpc.getinfo() # This shortcut is sufficient for our simple tests. self.port = self.info['binding'][0]['port'] + if wait_for_bitcoind_sync and not self.is_synced_with_bitcoin(self.info): + wait_for(lambda: self.is_synced_with_bitcoin()) def stop(self, timeout=10): """ Attempt to do a clean shutdown, but kill if it hangs @@ -779,6 +786,7 @@ class NodeFactory(object): 'random_hsm', 'log_all_io', 'feerates', + 'wait_for_bitcoind_sync', ] node_opts = {k: v for k, v in opts.items() if k in node_opt_keys} cli_opts = {k: v for k, v in opts.items() if k not in node_opt_keys} @@ -821,7 +829,8 @@ class NodeFactory(object): def get_node(self, disconnect=None, options=None, may_fail=False, may_reconnect=False, random_hsm=False, feerates=(15000, 7500, 3750), start=True, log_all_io=False, - dbfile=None, node_id=None, allow_broken_log=False): + dbfile=None, node_id=None, allow_broken_log=False, + wait_for_bitcoind_sync=True): if not node_id: node_id = self.get_node_id() @@ -887,7 +896,7 @@ class NodeFactory(object): if start: try: - node.start() + node.start(wait_for_bitcoind_sync) except Exception: node.daemon.stop() raise