From f6557251fe71e659830c6e118cfd1b6bbdf14c44 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 13 Dec 2018 17:35:01 +0100 Subject: [PATCH] pytest: Add a test for the event subscription and notification Signed-off-by: Christian Decker --- contrib/plugins/helloworld.py | 10 ++++++++++ tests/test_plugin.py | 12 ++++++++++++ tests/utils.py | 10 ++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/contrib/plugins/helloworld.py b/contrib/plugins/helloworld.py index 214ac74e9..dcc329ca6 100755 --- a/contrib/plugins/helloworld.py +++ b/contrib/plugins/helloworld.py @@ -24,5 +24,15 @@ def init(options, configuration, plugin): plugin.log("Plugin helloworld.py initialized") +@plugin.subscribe("connect") +def on_connect(id, address, plugin): + plugin.log("Received connect event for peer {}".format(id)) + + +@plugin.subscribe("disconnect") +def on_disconnect(id, plugin): + plugin.log("Received disconnect event for peer {}".format(id)) + + plugin.add_option('greeting', 'Hello', 'The greeting I should use.') plugin.run() diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 6b9d15360..d76a563ab 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -83,6 +83,18 @@ def test_plugin_disable(node_factory): n.rpc.hello(name='Sun') +def test_plugin_notifications(node_factory): + l1, l2 = node_factory.get_nodes(2, opts={'plugin': 'contrib/plugins/helloworld.py'}) + + l1.connect(l2) + l1.daemon.wait_for_log(r'Received connect event') + l2.daemon.wait_for_log(r'Received connect event') + + l2.rpc.disconnect(l1.info['id']) + l1.daemon.wait_for_log(r'Received disconnect event') + l2.daemon.wait_for_log(r'Received disconnect event') + + def test_failing_plugins(): fail_plugins = [ 'contrib/plugins/fail/failtimeout.py', diff --git a/tests/utils.py b/tests/utils.py index 057e48070..bcc751674 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -385,11 +385,17 @@ class LightningNode(object): self.may_fail = may_fail self.may_reconnect = may_reconnect + def connect(self, remote_node): + self.rpc.connect(remote_node.info['id'], '127.0.0.1', remote_node.daemon.port) + + def is_connected(self, remote_node): + return remote_node.info['id'] in [p['id'] for p in self.rpc.listpeers()['peers']] + def openchannel(self, remote_node, capacity, addrtype="p2sh-segwit", confirm=True, wait_for_announce=True, connect=True): addr, wallettxid = self.fundwallet(10 * capacity, addrtype) - if connect and remote_node.info['id'] not in [p['id'] for p in self.rpc.listpeers()['peers']]: - self.rpc.connect(remote_node.info['id'], '127.0.0.1', remote_node.daemon.port) + if connect and not self.is_connected(remote_node): + self.connect(remote_node) fundingtx = self.rpc.fundchannel(remote_node.info['id'], capacity)