Skip to content

Commit

Permalink
pytest: add notifications to tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Oct 23, 2020
1 parent 41290a4 commit 44f6b8a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/plugins/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

from pyln.client import Plugin
import time

plugin = Plugin()


@plugin.method("make_notify")
def make_notify(plugin, request, **kwargs):
plugin.notify_message(request, "Beginning stage 1")
for i in range(100):
plugin.notify_progress(request, i, 100, stage=0, stage_total=2)
time.sleep(0.01)
plugin.notify_message(request, "Beginning stage 2", level='debug')
for i in range(10):
plugin.notify_progress(request, i, 10, stage=1, stage_total=2)
time.sleep(0.1)
return "This worked"


plugin.run()
16 changes: 16 additions & 0 deletions tests/plugins/notify2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3

from pyln.client import Plugin

plugin = Plugin()


@plugin.method("call_make_notify")
def call_make_notify(plugin, request, **kwargs):
plugin.notify_message(request, "Starting notification", level='debug')
plugin.notify_progress(request, 0, 2)
plugin.notify_progress(request, 1, 2)
return plugin.rpc.call('make_notify')


plugin.run()
1 change: 1 addition & 0 deletions tests/plugins/test_libplugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static struct command_result *json_helloworld(struct command *cmd,
NULL))
return command_param_failed();

plugin_notify_message(cmd, LOG_INFORM, "Notification from %s", "json_helloworld");
if (!name)
name = name_option ? name_option : tal_strdup(tmpctx, "world");

Expand Down
62 changes: 62 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,68 @@ def test_htlc_accepted_hook_crash(node_factory, executor):
f.result(10)


def test_notify(node_factory):
"""Test that notifications from plugins get ignored"""
plugins = [os.path.join(os.getcwd(), 'tests/plugins/notify.py'),
os.path.join(os.getcwd(), 'tests/plugins/notify2.py')]
l1 = node_factory.get_node(options={'plugin': plugins})

assert l1.rpc.call('make_notify') == 'This worked'
assert l1.rpc.call('call_make_notify') == 'This worked'

out = subprocess.check_output(['cli/lightning-cli',
'--network={}'.format(TEST_NETWORK),
'--lightning-dir={}'
.format(l1.daemon.lightning_dir),
'make_notify']).decode('utf-8').splitlines(keepends=True)
assert out[0] == '# Beginning stage 1\n'
assert out[1] == '\r'
for i in range(100):
assert out[2 + i].startswith("# Stage 1/2 {:>3}/100 |".format(1 + i))
if i == 99:
assert out[2 + i].endswith("|\n")
else:
assert out[2 + i].endswith("|\r")
assert out[102] == '\r'
for i in range(10):
assert out[103 + i].startswith("# Stage 2/2 {:>2}/10 |".format(1 + i))
if i == 9:
assert out[103 + i].endswith("|\n")
else:
assert out[103 + i].endswith("|\r")
assert out[113] == '"This worked"\n'
assert len(out) == 114

# At debug level, we get the second prompt.
out = subprocess.check_output(['cli/lightning-cli',
'--network={}'.format(TEST_NETWORK),
'--lightning-dir={}'
.format(l1.daemon.lightning_dir),
'-N', 'debug',
'make_notify']).decode('utf-8').splitlines()
assert out[0] == '# Beginning stage 1'
assert out[1] == ''
for i in range(100):
assert out[2 + i].startswith("# Stage 1/2 {:>3}/100 |".format(1 + i))
assert out[2 + i].endswith("|")
assert out[102] == '# Beginning stage 2'
assert out[103] == ''
for i in range(10):
assert out[104 + i].startswith("# Stage 2/2 {:>2}/10 |".format(1 + i))
assert out[104 + i].endswith("|")
assert out[114] == '"This worked"'
assert len(out) == 115

# none suppresses
out = subprocess.check_output(['cli/lightning-cli',
'--network={}'.format(TEST_NETWORK),
'--lightning-dir={}'
.format(l1.daemon.lightning_dir),
'--notifications=none',
'make_notify']).decode('utf-8').splitlines()
assert out == ['"This worked"']


def test_htlc_accepted_hook_failcodes(node_factory):
plugin = os.path.join(os.path.dirname(__file__), 'plugins/htlc_accepted-failcode.py')
l1, l2 = node_factory.line_graph(2, opts=[{}, {'plugin': plugin}])
Expand Down

0 comments on commit 44f6b8a

Please sign in to comment.