diff --git a/tests/plugins/misc_notifications.py b/tests/plugins/misc_notifications.py index bc0ab621e..d1b04f320 100755 --- a/tests/plugins/misc_notifications.py +++ b/tests/plugins/misc_notifications.py @@ -2,9 +2,10 @@ """Plugin to be used to test miscellaneous notifications. """ -from pyln.client import Plugin +from pyln.client import Plugin, RpcError from time import sleep import sys +import pytest plugin = Plugin() @@ -29,6 +30,23 @@ def channel_state_changed(plugin, channel_state_changed, **kwargs): @plugin.subscribe("shutdown") def shutdown(plugin, **kwargs): + plugin.log("received shutdown notification") + + # 'shutdown' notification can be called in two ways, from `plugin stop` or from + # lightningd's shutdown loop, we test which one by making `getinfo` call + try: + plugin.rpc.getinfo() + plugin.rpc.datastore(key='test', string='Allowed', mode="create-or-append") + plugin.log("datastore success") + except RpcError as e: + if e.error == {'code': -5, 'message': 'lightningd is shutting down'}: + # JSON RPC is disabled by now, but can do logging + with pytest.raises(RpcError, match=r'-5.*lightningd is shutting down'): + plugin.rpc.datastore(key='test', string='Not allowed', mode="create-or-append") + plugin.log("datastore failed") + else: + raise + plugin.log("delaying shutdown with 5s") sleep(5) sys.exit(0) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 5d054af6a..3141aaa4d 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -1086,7 +1086,7 @@ def test_htlc_accepted_hook_direct_restart(node_factory, executor): def test_htlc_accepted_hook_shutdown(node_factory, executor): """Hooks of important-plugins are never removed and these plugins are kept - alive until after subdaemons are shutdown. + alive until after subdaemons are shutdown. Also tests shutdown notification. """ l1, l2 = node_factory.line_graph(2, opts=[ {'may_reconnect': True, 'log-level': 'info'}, @@ -1095,6 +1095,10 @@ def test_htlc_accepted_hook_shutdown(node_factory, executor): 'important-plugin': [os.path.join(os.getcwd(), 'tests/plugins/fail_htlcs.py')]} ]) + l2.rpc.plugin_stop(os.path.join(os.getcwd(), 'tests/plugins/misc_notifications.py')) + l2.daemon.wait_for_log(r'datastore success') + l2.rpc.plugin_start(os.path.join(os.getcwd(), 'tests/plugins/misc_notifications.py')) + i1 = l2.rpc.invoice(msatoshi=1000, label="inv1", description="desc")['bolt11'] # fail_htlcs.py makes payment fail @@ -1107,6 +1111,8 @@ def test_htlc_accepted_hook_shutdown(node_factory, executor): # Should still fail htlc while shutting down with pytest.raises(RpcError): l1.rpc.pay(i1) + + l2.daemon.wait_for_log(r'datastore failed') f_stop.result()