diff --git a/tests/plugins/notify.py b/tests/plugins/notify.py new file mode 100755 index 000000000..fd4ad7f50 --- /dev/null +++ b/tests/plugins/notify.py @@ -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() diff --git a/tests/plugins/notify2.py b/tests/plugins/notify2.py new file mode 100755 index 000000000..94e0f6636 --- /dev/null +++ b/tests/plugins/notify2.py @@ -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() diff --git a/tests/plugins/test_libplugin.c b/tests/plugins/test_libplugin.c index 51a32d3d7..073565696 100644 --- a/tests/plugins/test_libplugin.c +++ b/tests/plugins/test_libplugin.c @@ -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"); diff --git a/tests/test_plugin.py b/tests/test_plugin.py index cc5dacdad..598833f83 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -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}])