From 571fb44d205d939a6169a6d294dc7e20f27cdd0f Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 22 Jan 2019 23:23:34 +0100 Subject: [PATCH] pylightning: Add a small test for async rpcmethods Signed-off-by: Christian Decker --- tests/plugins/asynctest.py | 30 ++++++++++++++++++++++++++++++ tests/test_plugin.py | 25 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100755 tests/plugins/asynctest.py diff --git a/tests/plugins/asynctest.py b/tests/plugins/asynctest.py new file mode 100755 index 000000000..9e8c849ab --- /dev/null +++ b/tests/plugins/asynctest.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +"""This plugin is used to check that async method calls are working correctly. + +The plugin registers a method `callme` with an argument. All calls are +stashed away, and are only resolved on the fifth invocation. All calls +will then return the argument of the fifth call. + +""" +from lightning import Plugin + +plugin = Plugin() + + +@plugin.init() +def init(configuration, options, plugin): + plugin.requests = [] + + +@plugin.method('asyncqueue', sync=False) +def async_queue(request, plugin): + plugin.requests.append(request) + + +@plugin.method('asyncflush') +def async_flush(res, plugin): + for r in plugin.requests: + r.set_result(res) + + +plugin.run() diff --git a/tests/test_plugin.py b/tests/test_plugin.py index a21adb0f7..9706c3c60 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -5,6 +5,7 @@ from utils import only_one import pytest import subprocess +import time def test_option_passthrough(node_factory): @@ -146,3 +147,27 @@ def test_plugin_connected_hook(node_factory): peer = l1.rpc.listpeers(l3.info['id'])['peers'] assert(peer == [] or not peer[0]['connected']) + + +def test_async_rpcmethod(node_factory, executor): + """This tests the async rpcmethods. + + It works in conjunction with the `asynctest` plugin which stashes + requests and then resolves all of them on the fifth call. + """ + l1 = node_factory.get_node(options={'plugin': 'tests/plugins/asynctest.py'}) + + results = [] + for i in range(10): + results.append(executor.submit(l1.rpc.asyncqueue)) + + time.sleep(3) + + # None of these should have returned yet + assert len([r for r in results if r.done()]) == 0 + + # This last one triggers the release and all results should be 42, + # since the last number is returned for all + l1.rpc.asyncflush(42) + + assert [r.result() for r in results] == [42] * len(results)