diff --git a/contrib/plugins/helloworld.py b/contrib/plugins/helloworld.py index 987ab4cd9..19e07fc22 100755 --- a/contrib/plugins/helloworld.py +++ b/contrib/plugins/helloworld.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from lightning import Plugin - +import time plugin = Plugin() @@ -34,5 +34,12 @@ def on_disconnect(plugin, id): plugin.log("Received disconnect event for peer {}".format(id)) +@plugin.hook("htlc_accepted") +def on_htlc_accepted(plugin): + plugin.log('on_htlc_accepted called') + time.sleep(20) + return None + + 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 8d1faf1cf..32d043938 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -107,6 +107,26 @@ def test_plugin_disable(node_factory): n.rpc.hello(name='Sun') +def test_plugin_hook(node_factory, executor): + """The helloworld plugin registers a htlc_accepted hook. + + The hook will sleep for a few seconds and log a + message. `lightningd` should wait for the response and only then + complete the payment. + + """ + l1, l2 = node_factory.line_graph(2, opts={'plugin': 'contrib/plugins/helloworld.py'}) + start_time = time.time() + f = executor.submit(l1.pay, l2, 100000) + l2.daemon.wait_for_log(r'on_htlc_accepted called') + + # The hook will sleep for 20 seconds before answering, so `f` + # should take at least that long. + f.result() + end_time = time.time() + assert(end_time >= start_time + 20) + + def test_plugin_notifications(node_factory): l1, l2 = node_factory.get_nodes(2, opts={'plugin': 'contrib/plugins/helloworld.py'})