Skip to content

Commit 25ca49c

Browse files
committed
pytest: Add a test for the event subscription and notification
Signed-off-by: Christian Decker <decker.christian@gmail.com>
1 parent 5338d31 commit 25ca49c

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

contrib/plugins/helloworld.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,15 @@ def init(options, configuration, plugin):
2424
plugin.log("Plugin helloworld.py initialized")
2525

2626

27+
@plugin.subscribe("connect")
28+
def on_connect(id, address, plugin):
29+
plugin.log("Received connect event for peer {}".format(id))
30+
31+
32+
@plugin.subscribe("disconnect")
33+
def on_disconnect(id, plugin):
34+
plugin.log("Received disconnect event for peer {}".format(id))
35+
36+
2737
plugin.add_option('greeting', 'Hello', 'The greeting I should use.')
2838
plugin.run()

tests/test_plugin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ def test_plugin_disable(node_factory):
8383
n.rpc.hello(name='Sun')
8484

8585

86+
def test_plugin_notifications(node_factory):
87+
l1, l2 = node_factory.get_nodes(2, opts={'plugin': 'contrib/plugins/helloworld.py'})
88+
89+
l1.connect(l2)
90+
l1.daemon.wait_for_log(r'Received connect event')
91+
l2.daemon.wait_for_log(r'Received connect event')
92+
93+
l2.rpc.disconnect(l1.info['id'])
94+
l1.daemon.wait_for_log(r'Received disconnect event')
95+
l2.daemon.wait_for_log(r'Received disconnect event')
96+
97+
8698
def test_failing_plugins():
8799
fail_plugins = [
88100
'contrib/plugins/fail/failtimeout.py',

tests/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,17 @@ def __init__(self, daemon, rpc, btc, executor, may_fail=False, may_reconnect=Fal
385385
self.may_fail = may_fail
386386
self.may_reconnect = may_reconnect
387387

388+
def connect(self, remote_node):
389+
self.rpc.connect(remote_node.info['id'], '127.0.0.1', remote_node.daemon.port)
390+
391+
def is_connected(self, remote_node):
392+
return remote_node.info['id'] in [p['id'] for p in self.rpc.listpeers()['peers']]
393+
388394
def openchannel(self, remote_node, capacity, addrtype="p2sh-segwit", confirm=True, wait_for_announce=True, connect=True):
389395
addr, wallettxid = self.fundwallet(10 * capacity, addrtype)
390396

391-
if connect and remote_node.info['id'] not in [p['id'] for p in self.rpc.listpeers()['peers']]:
392-
self.rpc.connect(remote_node.info['id'], '127.0.0.1', remote_node.daemon.port)
397+
if connect and not self.is_connected(remote_node):
398+
self.connect(remote_node)
393399

394400
fundingtx = self.rpc.fundchannel(remote_node.info['id'], capacity)
395401

0 commit comments

Comments
 (0)