Skip to content

Commit

Permalink
Split the info example into two.
Browse files Browse the repository at this point in the history
This should help to confirm we still support the old way of using
redis_module! macro too.
  • Loading branch information
iddm committed Jul 18, 2023
1 parent 6b87423 commit 87a128e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
67 changes: 67 additions & 0 deletions examples/info_handler_macro.rs
Original file line number Diff line number Diff line change
@@ -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<RedisString>) -> RedisResult {
let ver = ctx.get_redis_version()?;
let response: Vec<i64> = vec![ver.major.into(), ver.minor.into(), ver.patch.into()];

Ok(response.into())
}

fn test_helper_version_rm_call(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
let ver = ctx.get_redis_version_rm_call()?;
let response: Vec<i64> = vec![ver.major.into(), ver.minor.into(), ver.patch.into()];

Ok(response.into())
}

fn test_helper_command_name(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(ctx.current_command_name()?.into())
}

fn test_helper_err(ctx: &Context, args: Vec<RedisString>) -> 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<String, String>,
}

#[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],
],
}
3 changes: 1 addition & 2 deletions examples/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RedisString>) -> RedisResult {
Expand Down Expand Up @@ -40,7 +39,6 @@ struct InfoData {
dictionary: HashMap<String, String>,
}

#[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());
Expand All @@ -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],
Expand Down
28 changes: 16 additions & 12 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 87a128e

Please sign in to comment.