cln-plugin: Add test for cln-plugin send_custom_notification

Also fix Makefile for rust plugin examples

Also add in a missing assert in the test_plugin_start test
This commit is contained in:
Chris Guida
2023-09-14 17:34:13 -06:00
committed by Peter Neuroth
parent 80ba3a573d
commit c6262189b7
3 changed files with 36 additions and 4 deletions

View File

@@ -11,10 +11,10 @@ MSGGEN_GENALL += $(CLN_RPC_GENALL)
target/${RUST_PROFILE}/examples/cln-rpc-getinfo: $(shell find cln-rpc -name *.rs)
cargo build ${CARGO_OPTS} --example cln-rpc-getinfo
target/${RUST_PROFILE}/examples/cln-plugin-startup: $(shell find cln-rpc -name *.rs)
target/${RUST_PROFILE}/examples/cln-plugin-startup: plugins/examples/cln-plugin-startup.rs
cargo build ${CARGO_OPTS} --example cln-plugin-startup
target/${RUST_PROFILE}/examples/cln-plugin-reentrant: $(shell find plugins/examples -name *.rs)
target/${RUST_PROFILE}/examples/cln-plugin-reentrant: plugins/examples/cln-plugin-reentrant.rs
cargo build ${CARGO_OPTS} --example cln-plugin-reentrant

View File

@@ -2,8 +2,11 @@
//! plugins using the Rust API against Core Lightning.
#[macro_use]
extern crate serde_json;
use cln_plugin::{options, Builder, Error, Plugin};
use cln_plugin::{messages, options, Builder, Error, Plugin};
use tokio;
const TEST_NOTIF_TAG: &str = "test_custom_notification";
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let state = ();
@@ -25,8 +28,15 @@ async fn main() -> Result<(), anyhow::Error> {
"Retrieve options from this plugin",
testoptions,
)
.rpcmethod(
"test-custom-notification",
"send a test_custom_notification event",
test_send_custom_notification,
)
.subscribe("connect", connect_handler)
.subscribe("test_custom_notification", test_receive_custom_notification)
.hook("peer_connected", peer_connected_handler)
.notification(messages::NotificationTopic::new(TEST_NOTIF_TAG))
.start(state)
.await?
{
@@ -46,6 +56,26 @@ async fn testmethod(_p: Plugin<()>, _v: serde_json::Value) -> Result<serde_json:
Ok(json!("Hello"))
}
async fn test_send_custom_notification(
p: Plugin<()>,
_v: serde_json::Value,
) -> Result<serde_json::Value, Error> {
let custom_notification = json!({
"test": "test",
});
p.send_custom_notification(TEST_NOTIF_TAG.to_string(), custom_notification)
.await?;
Ok(json!("Notification sent"))
}
async fn test_receive_custom_notification(
_p: Plugin<()>,
v: serde_json::Value,
) -> Result<(), Error> {
log::info!("Received a test_custom_notification: {}", v);
Ok(())
}
async fn connect_handler(_p: Plugin<()>, v: serde_json::Value) -> Result<(), Error> {
log::info!("Got a connect notification: {}", v);
Ok(())

View File

@@ -46,7 +46,7 @@ def test_plugin_start(node_factory):
assert str(bin_path) in l1.rpc.listconfigs()['configs']['plugin']['values_str']
# Now check that the `testmethod was registered ok
l1.rpc.help("testmethod") == {
assert l1.rpc.help("testmethod") == {
'help': [
{
'command': 'testmethod ',
@@ -59,6 +59,8 @@ def test_plugin_start(node_factory):
}
assert l1.rpc.testmethod() == "Hello"
assert l1.rpc.test_custom_notification() == "Notification sent"
l1.daemon.wait_for_log(r'Received a test_custom_notification')
l1.connect(l2)
l1.daemon.wait_for_log(r'Got a connect hook call')