Browse Source

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 <rusty@rustcorp.com.au>
trytravis
Rusty Russell 6 years ago
committed by Christian Decker
parent
commit
3f5487e247
  1. 44
      lightningd/jsonrpc.c
  2. 6
      lightningd/test/run-jsonrpc.c

44
lightningd/jsonrpc.c

@ -11,6 +11,7 @@
#include <ccan/tal/str/str.h>
#include <common/bech32.h>
#include <common/memleak.h>
#include <common/timeout.h>
#include <common/version.h>
#include <common/wallet_tx.h>
#include <common/wireaddr.h>
@ -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)
{

6
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, ...)

Loading…
Cancel
Save