From 3f5487e247af134e852d8f3482d3ffbbfe3ed08b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 20 Nov 2018 12:16:32 +1030 Subject: [PATCH] jsonrpc: dev_slowcmd, a command which starts output then delays. This lets us explicitly test that our JSON outputs don't intermingle. Signed-off-by: Rusty Russell --- lightningd/jsonrpc.c | 44 +++++++++++++++++++++++++++++++++++ lightningd/test/run-jsonrpc.c | 6 +++++ 2 files changed, 50 insertions(+) diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index d0adad463..3bfa2fe49 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -175,6 +176,49 @@ static const struct json_command dev_rhash_command = { }; AUTODATA(json_command, &dev_rhash_command); +struct slowcmd { + struct command *cmd; + unsigned *msec; + struct json_stream *js; +}; + +static void slowcmd_finish(struct slowcmd *sc) +{ + json_object_start(sc->js, NULL); + json_add_num(sc->js, "msec", *sc->msec); + json_object_end(sc->js); + command_success(sc->cmd, sc->js); +} + +static void slowcmd_start(struct slowcmd *sc) +{ + sc->js = json_stream_success(sc->cmd); + new_reltimer(&sc->cmd->ld->timers, sc, time_from_msec(*sc->msec), + slowcmd_finish, sc); +} + +static void json_slowcmd(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + struct slowcmd *sc = tal(cmd, struct slowcmd); + + sc->cmd = cmd; + if (!param(cmd, buffer, params, + p_opt_def("msec", json_tok_number, &sc->msec, 1000), + NULL)) + return; + + new_reltimer(&cmd->ld->timers, sc, time_from_msec(0), slowcmd_start, sc); + command_still_pending(cmd); +} + +static const struct json_command dev_slowcmd_command = { + "dev-slowcmd", + json_slowcmd, + "Torture test for slow commands, optional {msec}" +}; +AUTODATA(json_command, &dev_slowcmd_command); + static void json_crash(struct command *cmd UNUSED, const char *buffer UNUSED, const jsmntok_t *params UNUSED) { diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index ad15a5315..66f799263 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -46,6 +46,12 @@ const char *log_prefix(const struct log *log UNNEEDED) /* Generated stub for new_log */ struct log *new_log(const tal_t *ctx UNNEEDED, struct log_book *record UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "new_log called!\n"); abort(); } +/* Generated stub for new_reltimer_ */ +struct oneshot *new_reltimer_(struct timers *timers UNNEEDED, + const tal_t *ctx UNNEEDED, + struct timerel expire UNNEEDED, + void (*cb)(void *) UNNEEDED, void *arg UNNEEDED) +{ fprintf(stderr, "new_reltimer_ called!\n"); abort(); } /* Generated stub for param */ bool param(struct command *cmd UNNEEDED, const char *buffer UNNEEDED, const jsmntok_t params[] UNNEEDED, ...)