testing: test that RPC calls made in shutdown fail and receive error code -5

and the two conditions in which plugins can receive shutdown notification
This commit is contained in:
Simon Vrouwe 2021-11-10 10:31:06 +02:00 committed by Rusty Russell
parent 0388314ef8
commit 89cbdf4dce
2 changed files with 26 additions and 2 deletions

View File

@ -2,9 +2,10 @@
"""Plugin to be used to test miscellaneous notifications. """Plugin to be used to test miscellaneous notifications.
""" """
from pyln.client import Plugin from pyln.client import Plugin, RpcError
from time import sleep from time import sleep
import sys import sys
import pytest
plugin = Plugin() plugin = Plugin()
@ -29,6 +30,23 @@ def channel_state_changed(plugin, channel_state_changed, **kwargs):
@plugin.subscribe("shutdown") @plugin.subscribe("shutdown")
def shutdown(plugin, **kwargs): 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") plugin.log("delaying shutdown with 5s")
sleep(5) sleep(5)
sys.exit(0) sys.exit(0)

View File

@ -1086,7 +1086,7 @@ def test_htlc_accepted_hook_direct_restart(node_factory, executor):
def test_htlc_accepted_hook_shutdown(node_factory, executor): def test_htlc_accepted_hook_shutdown(node_factory, executor):
"""Hooks of important-plugins are never removed and these plugins are kept """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=[ l1, l2 = node_factory.line_graph(2, opts=[
{'may_reconnect': True, 'log-level': 'info'}, {'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')]} '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'] i1 = l2.rpc.invoice(msatoshi=1000, label="inv1", description="desc")['bolt11']
# fail_htlcs.py makes payment fail # 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 # Should still fail htlc while shutting down
with pytest.raises(RpcError): with pytest.raises(RpcError):
l1.rpc.pay(i1) l1.rpc.pay(i1)
l2.daemon.wait_for_log(r'datastore failed')
f_stop.result() f_stop.result()