pytest: Minor cleanup

Now using assertRaisesRegex instead of try-except and added restart to
nodes.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2017-12-13 13:44:09 +01:00
committed by Rusty Russell
parent a8a6d1d669
commit e154f4a019
2 changed files with 31 additions and 33 deletions

View File

@@ -2168,52 +2168,36 @@ class LightningDTests(BaseLightningDTests):
l2 = self.node_factory.get_node(options=['--locktime-blocks={}'.format(max_locktime + 1)]) l2 = self.node_factory.get_node(options=['--locktime-blocks={}'.format(max_locktime + 1)])
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port']) l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
funds = 1000000
addr = l1.rpc.newaddr()['address'] addr = l1.rpc.newaddr()['address']
txid = l1.bitcoin.rpc.sendtoaddress(addr, 0.01) txid = l1.bitcoin.rpc.sendtoaddress(addr, funds / 10**8)
bitcoind.generate_block(1) bitcoind.generate_block(1)
# Wait for it to arrive. # Wait for it to arrive.
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) > 0) wait_for(lambda: len(l1.rpc.listfunds()['outputs']) > 0)
# Fail because l1 dislikes l2's huge locktime. # Fail because l1 dislikes l2's huge locktime.
try: self.assertRaisesRegex(ValueError, r'to_self_delay \d+ larger than \d+',
l1.rpc.fundchannel(l2.info['id'], 100000) l1.rpc.fundchannel, l2.info['id'], int(funds/10))
except ValueError as verr: assert l1.rpc.getpeers()['peers'][0]['connected']
str(verr).index('to_self_delay {} larger than {}' assert l2.rpc.getpeers()['peers'][0]['connected']
.format(max_locktime+1, max_locktime))
except Exception as err: # Restart l2 without ridiculous locktime.
self.fail("Unexpected exception {}".format(err)) l2.daemon.cmd_line.remove('--locktime-blocks={}'.format(max_locktime + 1))
else: l2.restart()
self.fail("huge locktime ignored?") l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
# We don't have enough left to cover fees if we try to spend it all. # We don't have enough left to cover fees if we try to spend it all.
try: self.assertRaisesRegex(ValueError, r'Cannot afford funding transaction',
l1.rpc.fundchannel(l2.info['id'], 1000000) l1.rpc.fundchannel, l2.info['id'], funds)
except ValueError as verr:
str(verr).index('Cannot afford funding transaction')
except Exception as err:
self.fail("Unexpected exception {}".format(err))
else:
self.fail("We somehow covered fees?")
# Should still be connected. # Should still be connected.
assert l1.rpc.getpeers()['peers'][0]['connected'] assert l1.rpc.getpeers()['peers'][0]['connected']
assert l2.rpc.getpeers()['peers'][0]['connected'] assert l2.rpc.getpeers()['peers'][0]['connected']
# Restart l2 without ridiculous locktime.
l2.daemon.proc.terminate()
l2.daemon.cmd_line.remove('--locktime-blocks={}'.format(max_locktime + 1))
# Wait for l1 to notice
wait_for(lambda: len(l1.rpc.getpeers()['peers']) == 0)
# Now restart l2, reconnect.
l2.daemon.start()
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
# This works. # This works.
l1.rpc.fundchannel(l2.info['id'], int(0.01 * 10**8 / 2)) l1.rpc.fundchannel(l2.info['id'], int(funds/10))
def test_addfunds_from_block(self): def test_addfunds_from_block(self):
"""Send funds to the daemon without telling it explicitly """Send funds to the daemon without telling it explicitly

View File

@@ -79,8 +79,8 @@ class TailableProc(object):
self.proc.wait() self.proc.wait()
self.thread.join() self.thread.join()
if failed: if self.proc.returncode:
raise(ValueError("Process '{}' did not cleanly shutdown".format(self.proc.pid))) raise ValueError("Process '{}' did not cleanly shutdown: return code {}".format(self.proc.pid, rc))
return self.proc.returncode return self.proc.returncode
@@ -364,3 +364,17 @@ class LightningNode(object):
raise ValueError("Node did not exit cleanly, rc={}".format(rc)) raise ValueError("Node did not exit cleanly, rc={}".format(rc))
else: else:
return rc return rc
def restart(self, timeout=10, clean=True):
"""Stop and restart the lightning node.
Keyword arguments:
timeout: number of seconds to wait for a shutdown
clean: whether to issue a `stop` RPC command before killing
"""
if clean:
self.stop(timeout)
else:
self.daemon.stop()
self.daemon.start()