diff --git a/tests/plugins/test_libplugin.c b/tests/plugins/test_libplugin.c index 9630d10be..a432fae9b 100644 --- a/tests/plugins/test_libplugin.c +++ b/tests/plugins/test_libplugin.c @@ -22,6 +22,37 @@ static struct command_result *json_helloworld(struct command *cmd, return command_success_str(cmd, tal_fmt(tmpctx, "hello %s", name)); } +static struct command_result * +json_peer_connected(struct command *cmd, + const char *buf, + const jsmntok_t *params) +{ + const jsmntok_t *peertok, *idtok; + struct json_stream *response; + + peertok = json_get_member(buf, params, "peer"); + assert(peertok); + idtok = json_get_member(buf, peertok, "id"); + assert(idtok); + plugin_log(cmd->plugin, LOG_INFORM, "%s peer_connected", + json_strdup(tmpctx, buf, idtok)); + + response = jsonrpc_stream_success(cmd); + json_add_string(response, "result", "continue"); + + return command_finished(cmd, response); +} + +static void json_connected(struct command *cmd, + const char *buf, + const jsmntok_t *params) +{ + const jsmntok_t *idtok = json_get_member(buf, params, "id"); + assert(idtok); + plugin_log(cmd->plugin, LOG_INFORM, "%s connected", + json_strdup(tmpctx, buf, idtok)); +} + static void init(struct plugin *p, const char *buf UNUSED, const jsmntok_t *config UNUSED) { @@ -39,11 +70,23 @@ static const struct plugin_command commands[] = { { } }; +static const struct plugin_hook hooks[] = { { + "peer_connected", + json_peer_connected, + } +}; + +static const struct plugin_notification notifs[] = { { + "connect", + json_connected, + } +}; + int main(int argc, char *argv[]) { setup_locale(); plugin_main(argv, init, PLUGIN_RESTARTABLE, commands, ARRAY_SIZE(commands), - NULL, 0, NULL, 0, + notifs, ARRAY_SIZE(notifs), hooks, ARRAY_SIZE(hooks), plugin_option("name", "string", "Who to say hello to.", diff --git a/tests/test_plugin.py b/tests/test_plugin.py index e69ba0890..832940c66 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -813,3 +813,9 @@ def test_libplugin(node_factory): assert l1.rpc.call("helloworld") == "hello test_opt" # But param takes over! assert l1.rpc.call("helloworld", {"name": "test"}) == "hello test" + + # Test hooks and notifications + l2 = node_factory.get_node() + l2.connect(l1) + assert l1.daemon.is_in_log("{} peer_connected".format(l2.info["id"])) + l1.daemon.wait_for_log("{} connected".format(l2.info["id"]))