diff --git a/contrib/plugins/fail/failtimeout.py b/contrib/plugins/fail/failtimeout.py new file mode 100755 index 000000000..9477c23f9 --- /dev/null +++ b/contrib/plugins/fail/failtimeout.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""An example plugin that fails to answer to `getmanifest` + +Used to test the `getmanifest` timeout. +""" +import json +import sys +import time + + +def json_getmanifest(request): + # Timeout is 10 seconds, so wait 11 + time.sleep(11) + return { + "options": [ + ], + "rpcmethods": [ + ] + } + + +methods = { + 'getmanifest': json_getmanifest, +} + + +partial = "" +for l in sys.stdin: + try: + partial += l + request = json.loads(partial) + except Exception: + continue + + result = None + method = methods[request['method']] + params = request['params'] + try: + if isinstance(params, dict): + result = method(request, **params) + else: + result = method(request, *params) + result = { + "jsonrpc": "2.0", + "result": result, + "id": request['id'] + } + except Exception as e: + result = { + "jsonrpc": "2.0", + "error": "Error while processing {}".format(request['method']), + "id": request['id'] + } + + json.dump(result, fp=sys.stdout) + sys.stdout.write('\n') + sys.stdout.flush() + partial = "" diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 3aa1ccb41..8b85269ad 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -77,3 +77,18 @@ def test_plugin_disable(node_factory): 'helloworld.py')])) with pytest.raises(RpcError): n.rpc.hello(name='Sun') + + +def test_failing_plugins(): + fail_plugins = [ + 'contrib/plugins/fail/failtimeout.py', + 'contrib/plugins/fail/doesnotexist.py', + ] + + for p in fail_plugins: + with pytest.raises(subprocess.CalledProcessError): + subprocess.check_output([ + 'lightningd/lightningd', + '--plugin={}'.format(p), + '--help', + ])