From 87a128ea7f02fd02b90772cfb4cc4d392f183a50 Mon Sep 17 00:00:00 2001 From: Victor Polevoy Date: Tue, 18 Jul 2023 08:49:34 +0200 Subject: [PATCH] Split the info example into two. This should help to confirm we still support the old way of using redis_module! macro too. --- Cargo.toml | 4 ++ examples/info_handler_macro.rs | 67 ++++++++++++++++++++++++++++++++++ examples/test_helper.rs | 3 +- tests/integration.rs | 28 ++++++++------ 4 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 examples/info_handler_macro.rs diff --git a/Cargo.toml b/Cargo.toml index 4d3b5522..1e5df1c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,6 +80,10 @@ crate-type = ["cdylib"] name = "test_helper" crate-type = ["cdylib"] +[[example]] +name = "info_handler_macro" +crate-type = ["cdylib"] + [[example]] name = "info" crate-type = ["cdylib"] diff --git a/examples/info_handler_macro.rs b/examples/info_handler_macro.rs new file mode 100644 index 00000000..f36a6fec --- /dev/null +++ b/examples/info_handler_macro.rs @@ -0,0 +1,67 @@ +use std::collections::HashMap; + +use redis_module::InfoContext; +use redis_module::{redis_module, Context, RedisError, RedisResult, RedisString}; +use redis_module_macros::info_command_handler; +use redis_module_macros::InfoSection; + +fn test_helper_version(ctx: &Context, _args: Vec) -> RedisResult { + let ver = ctx.get_redis_version()?; + let response: Vec = vec![ver.major.into(), ver.minor.into(), ver.patch.into()]; + + Ok(response.into()) +} + +fn test_helper_version_rm_call(ctx: &Context, _args: Vec) -> RedisResult { + let ver = ctx.get_redis_version_rm_call()?; + let response: Vec = vec![ver.major.into(), ver.minor.into(), ver.patch.into()]; + + Ok(response.into()) +} + +fn test_helper_command_name(ctx: &Context, _args: Vec) -> RedisResult { + Ok(ctx.current_command_name()?.into()) +} + +fn test_helper_err(ctx: &Context, args: Vec) -> RedisResult { + if args.len() < 1 { + return Err(RedisError::WrongArity); + } + + let msg = args.get(1).unwrap(); + + ctx.reply_error_string(msg.try_as_str().unwrap()); + Ok(().into()) +} + +#[derive(Debug, Clone, InfoSection)] +struct InfoData { + field: String, + dictionary: HashMap, +} + +#[info_command_handler] +fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> { + let mut dictionary = HashMap::new(); + dictionary.insert("key".to_owned(), "value".into()); + let data = InfoData { + field: "test_helper_value".to_owned(), + dictionary, + }; + ctx.build_one_section(data) +} + +////////////////////////////////////////////////////// + +redis_module! { + name: "test_helper", + version: 1, + allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc), + data_types: [], + commands: [ + ["test_helper.version", test_helper_version, "", 0, 0, 0], + ["test_helper._version_rm_call", test_helper_version_rm_call, "", 0, 0, 0], + ["test_helper.name", test_helper_command_name, "", 0, 0, 0], + ["test_helper.err", test_helper_err, "", 0, 0, 0], + ], +} diff --git a/examples/test_helper.rs b/examples/test_helper.rs index f36a6fec..3f776e68 100644 --- a/examples/test_helper.rs +++ b/examples/test_helper.rs @@ -2,7 +2,6 @@ use std::collections::HashMap; use redis_module::InfoContext; use redis_module::{redis_module, Context, RedisError, RedisResult, RedisString}; -use redis_module_macros::info_command_handler; use redis_module_macros::InfoSection; fn test_helper_version(ctx: &Context, _args: Vec) -> RedisResult { @@ -40,7 +39,6 @@ struct InfoData { dictionary: HashMap, } -#[info_command_handler] fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> { let mut dictionary = HashMap::new(); dictionary.insert("key".to_owned(), "value".into()); @@ -58,6 +56,7 @@ redis_module! { version: 1, allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc), data_types: [], + info: add_info, commands: [ ["test_helper.version", test_helper_version, "", 0, 0, 0], ["test_helper._version_rm_call", test_helper_version_rm_call, "", 0, 0, 0], diff --git a/tests/integration.rs b/tests/integration.rs index 412d44db..c3aa7141 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -113,20 +113,24 @@ fn test_command_name() -> Result<()> { #[test] fn test_helper_info() -> Result<()> { - let port: u16 = 6483; - let _guards = vec![start_redis_server_with_module("test_helper", port) - .with_context(|| "failed to start redis server")?]; - let mut con = - get_redis_connection(port).with_context(|| "failed to connect to redis server")?; + const MODULES: [&str; 2] = ["test_helper", "info_handler_macro"]; - let res: String = redis::cmd("INFO") - .arg("TEST_HELPER") - .query(&mut con) - .with_context(|| "failed to run INFO TEST_HELPER")?; - assert!(res.contains("test_helper_field:test_helper_value")); - assert!(res.contains("dictionary:key=value")); + MODULES.iter().try_for_each(|module| { + let port: u16 = 6483; + let _guards = vec![start_redis_server_with_module(module, port) + .with_context(|| "failed to start redis server")?]; + let mut con = + get_redis_connection(port).with_context(|| "failed to connect to redis server")?; - Ok(()) + let res: String = redis::cmd("INFO") + .arg("TEST_HELPER") + .query(&mut con) + .with_context(|| "failed to run INFO TEST_HELPER")?; + assert!(res.contains("test_helper_field:test_helper_value")); + assert!(res.contains("dictionary:key=value")); + + Ok(()) + }) } #[allow(unused_must_use)]