Browse Source

pytest: Minor cleanup

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

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
committed by Rusty Russell
parent
commit
e154f4a019
  1. 44
      tests/test_lightningd.py
  2. 18
      tests/utils.py

44
tests/test_lightningd.py

@ -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:
str(verr).index('to_self_delay {} larger than {}'
.format(max_locktime+1, max_locktime))
except Exception as err:
self.fail("Unexpected exception {}".format(err))
else:
self.fail("huge locktime ignored?")
# We don't have enough left to cover fees if we try to spend it all.
try:
l1.rpc.fundchannel(l2.info['id'], 1000000)
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.
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. # Restart l2 without ridiculous locktime.
l2.daemon.proc.terminate()
l2.daemon.cmd_line.remove('--locktime-blocks={}'.format(max_locktime + 1)) l2.daemon.cmd_line.remove('--locktime-blocks={}'.format(max_locktime + 1))
l2.restart()
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
# Wait for l1 to notice # We don't have enough left to cover fees if we try to spend it all.
wait_for(lambda: len(l1.rpc.getpeers()['peers']) == 0) self.assertRaisesRegex(ValueError, r'Cannot afford funding transaction',
l1.rpc.fundchannel, l2.info['id'], funds)
# Now restart l2, reconnect. # Should still be connected.
l2.daemon.start() assert l1.rpc.getpeers()['peers'][0]['connected']
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port']) assert l2.rpc.getpeers()['peers'][0]['connected']
# 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

18
tests/utils.py

@ -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()

Loading…
Cancel
Save