From 0da26933ac42cd70adbaa74f589e6f73e72aae30 Mon Sep 17 00:00:00 2001 From: Erik De Smedt Date: Wed, 3 Jan 2024 12:43:14 +0100 Subject: [PATCH] `cln_plugin` : Add basic test to `cln_plugin` --- plugins/src/lib.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/plugins/src/lib.rs b/plugins/src/lib.rs index 0db187b3e143..8e98087db0f4 100644 --- a/plugins/src/lib.rs +++ b/plugins/src/lib.rs @@ -718,4 +718,68 @@ mod test { let builder = Builder::new(tokio::io::stdin(), tokio::io::stdout()); let _ = builder.start(state); } + + #[tokio::test] + async fn logs_become_json_rpc_notifications() { + // The input and output for testing the plugin behavior + let (input, input_writer) = tokio::net::UnixStream::pair().unwrap(); + let (output, output_reader) = tokio::net::UnixStream::pair().unwrap(); + + let mut framed_reader = FramedRead::new(output_reader, JsonCodec::default()); + let mut input_writer = FramedWrite::new(input_writer, JsonCodec::default()); + + // Send the get_manifest method + let get_manifest = serde_json::json!({ + "jsonrpc" : "2.0", + "method" : "getmanifest", + "id" : 1, + "params" : {} + }); + let init = serde_json::json!({ + "jsonrpc" : "2.0", + "method" : "init", + "id" : 2, + "params" : { + "options" : {}, + "configuration" : { + "lightning-dir" : "/tmp/lightning-dir", + "rpc-file" : "rpc", + "startup" : true, + "network" : "testnet", + "feature_set" : {}, + } + } + }); + input_writer.send(get_manifest).await.unwrap(); + input_writer.send(init).await.unwrap(); + + // We create a scope here + // At the bottom of this scope the configured_plugin + // should be dropped and all logs should be flushed + { + // Configure the plugin and the logger + let builder: Builder<(), _, _> = Builder::new(input, output); + let _ = builder.configure().await.unwrap().unwrap(); + + // builder.configure() configures our logger + // Every LogEntry should now be written to `output`-stream + // as a json-rpc notification. + log::info!("I'm a log message"); + } + + // Check the output_reader the find the log message + let duration = std::time::Duration::from_millis(10); + let mut found_message = false; + while let Ok(Some(msg)) = tokio::time::timeout(duration, framed_reader.next()).await { + if msg.is_ok() { + let msg = msg.unwrap(); + if msg.get("method") == Some(&json!("log")) { + if msg["params"]["message"] == json!("I'm a log message") { + found_message = true + } + } + } + } + assert!(found_message) + } }