Skip to content

Commit 89cbdf4

Browse files
SimonVrouwerustyrussell
authored andcommitted
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
1 parent 0388314 commit 89cbdf4

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

tests/plugins/misc_notifications.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"""Plugin to be used to test miscellaneous notifications.
33
"""
44

5-
from pyln.client import Plugin
5+
from pyln.client import Plugin, RpcError
66
from time import sleep
77
import sys
8+
import pytest
89

910
plugin = Plugin()
1011

@@ -29,6 +30,23 @@ def channel_state_changed(plugin, channel_state_changed, **kwargs):
2930

3031
@plugin.subscribe("shutdown")
3132
def shutdown(plugin, **kwargs):
33+
plugin.log("received shutdown notification")
34+
35+
# 'shutdown' notification can be called in two ways, from `plugin stop` or from
36+
# lightningd's shutdown loop, we test which one by making `getinfo` call
37+
try:
38+
plugin.rpc.getinfo()
39+
plugin.rpc.datastore(key='test', string='Allowed', mode="create-or-append")
40+
plugin.log("datastore success")
41+
except RpcError as e:
42+
if e.error == {'code': -5, 'message': 'lightningd is shutting down'}:
43+
# JSON RPC is disabled by now, but can do logging
44+
with pytest.raises(RpcError, match=r'-5.*lightningd is shutting down'):
45+
plugin.rpc.datastore(key='test', string='Not allowed', mode="create-or-append")
46+
plugin.log("datastore failed")
47+
else:
48+
raise
49+
3250
plugin.log("delaying shutdown with 5s")
3351
sleep(5)
3452
sys.exit(0)

tests/test_plugin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ def test_htlc_accepted_hook_direct_restart(node_factory, executor):
10861086

10871087
def test_htlc_accepted_hook_shutdown(node_factory, executor):
10881088
"""Hooks of important-plugins are never removed and these plugins are kept
1089-
alive until after subdaemons are shutdown.
1089+
alive until after subdaemons are shutdown. Also tests shutdown notification.
10901090
"""
10911091
l1, l2 = node_factory.line_graph(2, opts=[
10921092
{'may_reconnect': True, 'log-level': 'info'},
@@ -1095,6 +1095,10 @@ def test_htlc_accepted_hook_shutdown(node_factory, executor):
10951095
'important-plugin': [os.path.join(os.getcwd(), 'tests/plugins/fail_htlcs.py')]}
10961096
])
10971097

1098+
l2.rpc.plugin_stop(os.path.join(os.getcwd(), 'tests/plugins/misc_notifications.py'))
1099+
l2.daemon.wait_for_log(r'datastore success')
1100+
l2.rpc.plugin_start(os.path.join(os.getcwd(), 'tests/plugins/misc_notifications.py'))
1101+
10981102
i1 = l2.rpc.invoice(msatoshi=1000, label="inv1", description="desc")['bolt11']
10991103

11001104
# fail_htlcs.py makes payment fail
@@ -1107,6 +1111,8 @@ def test_htlc_accepted_hook_shutdown(node_factory, executor):
11071111
# Should still fail htlc while shutting down
11081112
with pytest.raises(RpcError):
11091113
l1.rpc.pay(i1)
1114+
1115+
l2.daemon.wait_for_log(r'datastore failed')
11101116
f_stop.result()
11111117

11121118

0 commit comments

Comments
 (0)