From 29785d4990ff1007085718f6033fdb6fbfc2ab22 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 14 Aug 2017 16:52:19 +0200 Subject: [PATCH] pytest: Added a simple channel-persistence test This test opens a channel, stops the nodes and the restarts them to see if we can successfully reload the channel state from the database. --- tests/test_lightningd.py | 29 +++++++++++++++++++++++++++++ tests/utils.py | 4 ++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tests/test_lightningd.py b/tests/test_lightningd.py index bfb86121d..aa0301414 100644 --- a/tests/test_lightningd.py +++ b/tests/test_lightningd.py @@ -858,6 +858,35 @@ class LightningDTests(BaseLightningDTests): c.execute('SELECT COUNT(*) FROM outputs WHERE status=2') assert(c.fetchone()[0] == 2) + def test_channel_persistence(self): + # Start two nodes and open a channel (to remember) + l1, l2 = self.connect() + + # Neither node should have a channel open, they are just connected + for n in (l1, l2): + assert(n.db_query('SELECT COUNT(id) as count FROM channels;')[0]['count'] == 0) + + self.fund_channel(l1, l2, 100000) + + peers = l1.rpc.getpeers()['peers'] + assert(len(peers) == 1 and peers[0]['state'] == 'CHANNELD_NORMAL') + + # Both nodes should now have exactly one channel in the database + for n in (l1, l2): + assert(n.db_query('SELECT COUNT(id) as count FROM channels;')[0]['count'] == 1) + + l1.daemon.stop() + + # Let the other side notice, then stop it + wait_for(lambda: not l2.rpc.getpeers()['peers'][0]['connected']) + l2.daemon.stop() + + # Now restart l1 and it should reload peers/channels from the DB + l1.daemon.start() + + #wait_for(lambda: len(l1.rpc.getpeers()['peers']) == 1) + + class LegacyLightningDTests(BaseLightningDTests): def test_connect(self): diff --git a/tests/utils.py b/tests/utils.py index 645dd7af6..48e0e7490 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -42,8 +42,6 @@ class TailableProc(object): def __init__(self, outputDir=None): self.logs = [] self.logs_cond = threading.Condition(threading.RLock()) - self.thread = threading.Thread(target=self.tail) - self.thread.daemon = True self.cmd_line = None self.running = False self.proc = None @@ -55,6 +53,8 @@ class TailableProc(object): """ logging.debug("Starting '%s'", " ".join(self.cmd_line)) self.proc = subprocess.Popen(self.cmd_line, stdout=subprocess.PIPE) + self.thread = threading.Thread(target=self.tail) + self.thread.daemon = True self.thread.start() self.running = True