Skip to content

Commit 87a128e

Browse files
committed
Split the info example into two.
This should help to confirm we still support the old way of using redis_module! macro too.
1 parent 6b87423 commit 87a128e

File tree

4 files changed

+88
-14
lines changed

4 files changed

+88
-14
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ crate-type = ["cdylib"]
8080
name = "test_helper"
8181
crate-type = ["cdylib"]
8282

83+
[[example]]
84+
name = "info_handler_macro"
85+
crate-type = ["cdylib"]
86+
8387
[[example]]
8488
name = "info"
8589
crate-type = ["cdylib"]

examples/info_handler_macro.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use std::collections::HashMap;
2+
3+
use redis_module::InfoContext;
4+
use redis_module::{redis_module, Context, RedisError, RedisResult, RedisString};
5+
use redis_module_macros::info_command_handler;
6+
use redis_module_macros::InfoSection;
7+
8+
fn test_helper_version(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
9+
let ver = ctx.get_redis_version()?;
10+
let response: Vec<i64> = vec![ver.major.into(), ver.minor.into(), ver.patch.into()];
11+
12+
Ok(response.into())
13+
}
14+
15+
fn test_helper_version_rm_call(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
16+
let ver = ctx.get_redis_version_rm_call()?;
17+
let response: Vec<i64> = vec![ver.major.into(), ver.minor.into(), ver.patch.into()];
18+
19+
Ok(response.into())
20+
}
21+
22+
fn test_helper_command_name(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
23+
Ok(ctx.current_command_name()?.into())
24+
}
25+
26+
fn test_helper_err(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
27+
if args.len() < 1 {
28+
return Err(RedisError::WrongArity);
29+
}
30+
31+
let msg = args.get(1).unwrap();
32+
33+
ctx.reply_error_string(msg.try_as_str().unwrap());
34+
Ok(().into())
35+
}
36+
37+
#[derive(Debug, Clone, InfoSection)]
38+
struct InfoData {
39+
field: String,
40+
dictionary: HashMap<String, String>,
41+
}
42+
43+
#[info_command_handler]
44+
fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
45+
let mut dictionary = HashMap::new();
46+
dictionary.insert("key".to_owned(), "value".into());
47+
let data = InfoData {
48+
field: "test_helper_value".to_owned(),
49+
dictionary,
50+
};
51+
ctx.build_one_section(data)
52+
}
53+
54+
//////////////////////////////////////////////////////
55+
56+
redis_module! {
57+
name: "test_helper",
58+
version: 1,
59+
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
60+
data_types: [],
61+
commands: [
62+
["test_helper.version", test_helper_version, "", 0, 0, 0],
63+
["test_helper._version_rm_call", test_helper_version_rm_call, "", 0, 0, 0],
64+
["test_helper.name", test_helper_command_name, "", 0, 0, 0],
65+
["test_helper.err", test_helper_err, "", 0, 0, 0],
66+
],
67+
}

examples/test_helper.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::collections::HashMap;
22

33
use redis_module::InfoContext;
44
use redis_module::{redis_module, Context, RedisError, RedisResult, RedisString};
5-
use redis_module_macros::info_command_handler;
65
use redis_module_macros::InfoSection;
76

87
fn test_helper_version(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
@@ -40,7 +39,6 @@ struct InfoData {
4039
dictionary: HashMap<String, String>,
4140
}
4241

43-
#[info_command_handler]
4442
fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
4543
let mut dictionary = HashMap::new();
4644
dictionary.insert("key".to_owned(), "value".into());
@@ -58,6 +56,7 @@ redis_module! {
5856
version: 1,
5957
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
6058
data_types: [],
59+
info: add_info,
6160
commands: [
6261
["test_helper.version", test_helper_version, "", 0, 0, 0],
6362
["test_helper._version_rm_call", test_helper_version_rm_call, "", 0, 0, 0],

tests/integration.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,24 @@ fn test_command_name() -> Result<()> {
113113

114114
#[test]
115115
fn test_helper_info() -> Result<()> {
116-
let port: u16 = 6483;
117-
let _guards = vec![start_redis_server_with_module("test_helper", port)
118-
.with_context(|| "failed to start redis server")?];
119-
let mut con =
120-
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
116+
const MODULES: [&str; 2] = ["test_helper", "info_handler_macro"];
121117

122-
let res: String = redis::cmd("INFO")
123-
.arg("TEST_HELPER")
124-
.query(&mut con)
125-
.with_context(|| "failed to run INFO TEST_HELPER")?;
126-
assert!(res.contains("test_helper_field:test_helper_value"));
127-
assert!(res.contains("dictionary:key=value"));
118+
MODULES.iter().try_for_each(|module| {
119+
let port: u16 = 6483;
120+
let _guards = vec![start_redis_server_with_module(module, port)
121+
.with_context(|| "failed to start redis server")?];
122+
let mut con =
123+
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
128124

129-
Ok(())
125+
let res: String = redis::cmd("INFO")
126+
.arg("TEST_HELPER")
127+
.query(&mut con)
128+
.with_context(|| "failed to run INFO TEST_HELPER")?;
129+
assert!(res.contains("test_helper_field:test_helper_value"));
130+
assert!(res.contains("dictionary:key=value"));
131+
132+
Ok(())
133+
})
130134
}
131135

132136
#[allow(unused_must_use)]

0 commit comments

Comments
 (0)