Skip to content

Commit e154f4a

Browse files
cdeckerrustyrussell
authored andcommitted
pytest: Minor cleanup
Now using assertRaisesRegex instead of try-except and added restart to nodes. Signed-off-by: Christian Decker <[email protected]>
1 parent a8a6d1d commit e154f4a

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

tests/test_lightningd.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,52 +2168,36 @@ def test_funding_fail(self):
21682168
l2 = self.node_factory.get_node(options=['--locktime-blocks={}'.format(max_locktime + 1)])
21692169
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
21702170

2171+
funds = 1000000
2172+
21712173
addr = l1.rpc.newaddr()['address']
2172-
txid = l1.bitcoin.rpc.sendtoaddress(addr, 0.01)
2174+
txid = l1.bitcoin.rpc.sendtoaddress(addr, funds / 10**8)
21732175
bitcoind.generate_block(1)
21742176

21752177
# Wait for it to arrive.
21762178
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) > 0)
21772179

21782180
# Fail because l1 dislikes l2's huge locktime.
2179-
try:
2180-
l1.rpc.fundchannel(l2.info['id'], 100000)
2181-
except ValueError as verr:
2182-
str(verr).index('to_self_delay {} larger than {}'
2183-
.format(max_locktime+1, max_locktime))
2184-
except Exception as err:
2185-
self.fail("Unexpected exception {}".format(err))
2186-
else:
2187-
self.fail("huge locktime ignored?")
2188-
2189-
# We don't have enough left to cover fees if we try to spend it all.
2190-
try:
2191-
l1.rpc.fundchannel(l2.info['id'], 1000000)
2192-
except ValueError as verr:
2193-
str(verr).index('Cannot afford funding transaction')
2194-
except Exception as err:
2195-
self.fail("Unexpected exception {}".format(err))
2196-
else:
2197-
self.fail("We somehow covered fees?")
2198-
2199-
# Should still be connected.
2181+
self.assertRaisesRegex(ValueError, r'to_self_delay \d+ larger than \d+',
2182+
l1.rpc.fundchannel, l2.info['id'], int(funds/10))
22002183
assert l1.rpc.getpeers()['peers'][0]['connected']
22012184
assert l2.rpc.getpeers()['peers'][0]['connected']
22022185

22032186
# Restart l2 without ridiculous locktime.
2204-
l2.daemon.proc.terminate()
2205-
22062187
l2.daemon.cmd_line.remove('--locktime-blocks={}'.format(max_locktime + 1))
2188+
l2.restart()
2189+
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
22072190

2208-
# Wait for l1 to notice
2209-
wait_for(lambda: len(l1.rpc.getpeers()['peers']) == 0)
2191+
# We don't have enough left to cover fees if we try to spend it all.
2192+
self.assertRaisesRegex(ValueError, r'Cannot afford funding transaction',
2193+
l1.rpc.fundchannel, l2.info['id'], funds)
22102194

2211-
# Now restart l2, reconnect.
2212-
l2.daemon.start()
2213-
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
2195+
# Should still be connected.
2196+
assert l1.rpc.getpeers()['peers'][0]['connected']
2197+
assert l2.rpc.getpeers()['peers'][0]['connected']
22142198

22152199
# This works.
2216-
l1.rpc.fundchannel(l2.info['id'], int(0.01 * 10**8 / 2))
2200+
l1.rpc.fundchannel(l2.info['id'], int(funds/10))
22172201

22182202
def test_addfunds_from_block(self):
22192203
"""Send funds to the daemon without telling it explicitly

tests/utils.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def stop(self, timeout=10):
7979
self.proc.wait()
8080
self.thread.join()
8181

82-
if failed:
83-
raise(ValueError("Process '{}' did not cleanly shutdown".format(self.proc.pid)))
82+
if self.proc.returncode:
83+
raise ValueError("Process '{}' did not cleanly shutdown: return code {}".format(self.proc.pid, rc))
8484

8585
return self.proc.returncode
8686

@@ -364,3 +364,17 @@ def stop(self, timeout=10):
364364
raise ValueError("Node did not exit cleanly, rc={}".format(rc))
365365
else:
366366
return rc
367+
368+
def restart(self, timeout=10, clean=True):
369+
"""Stop and restart the lightning node.
370+
371+
Keyword arguments:
372+
timeout: number of seconds to wait for a shutdown
373+
clean: whether to issue a `stop` RPC command before killing
374+
"""
375+
if clean:
376+
self.stop(timeout)
377+
else:
378+
self.daemon.stop()
379+
380+
self.daemon.start()

0 commit comments

Comments
 (0)