Browse Source

pytest: Add a simple test for the hooks

This uses the `htlc_accepted` hook to delay payment acceptance.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
htlc_accepted_hook
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
b54577041a
  1. 9
      contrib/plugins/helloworld.py
  2. 20
      tests/test_plugin.py

9
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()

20
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'})

Loading…
Cancel
Save