From d5acd12ac9af484dd5b8f790389a7397666607ea Mon Sep 17 00:00:00 2001 From: Shoham Elias Date: Sun, 11 Aug 2024 16:43:53 +0000 Subject: [PATCH] add tests Signed-off-by: Shoham Elias --- glide-core/src/client/value_conversion.rs | 143 +++++++++++++++++++++- 1 file changed, 142 insertions(+), 1 deletion(-) diff --git a/glide-core/src/client/value_conversion.rs b/glide-core/src/client/value_conversion.rs index 7a0a6c3b77..b8ca830936 100644 --- a/glide-core/src/client/value_conversion.rs +++ b/glide-core/src/client/value_conversion.rs @@ -589,8 +589,11 @@ pub(crate) fn convert_to_expected_type( for inner_item in outer_array { if let Value::Map(map) = converted_map.clone() { - if let Some((key, running_script_value)) = map.get(0) { + if let Some((key, running_script_value)) = map.first() { if key == &Value::BulkString(b"running_script".into()) && running_script_value != &Value::Nil { + // we have found a response with an active running script + // the server can only run a single script / function at a time + // we can return this nodes response break; } } @@ -2464,6 +2467,144 @@ mod tests { ); } + #[test] + fn convert_function_stats_standalone_response() { + assert!(matches!( + expected_type_for_cmd(redis::cmd("FUNCTION").arg("STATS")), + Some(ExpectedReturnType::FunctionStatsReturnType) + )); + + let resp2_response = Value::Array(vec![ + Value::Array(vec![ + Value::BulkString(b"running_script".into()), + Value::Array(vec![ + Value::BulkString(b"name".into()), + Value::BulkString(b"".into()), + Value::BulkString(b"command".into()), + Value::Array(vec![ + Value::BulkString(b"fcall".into()), + Value::BulkString(b"".into()), + Value::BulkString(b"... rest `fcall` args ...".into()), + ]), + Value::BulkString(b"duration_ms".into()), + Value::Int(24529), + ]), + Value::BulkString(b"engines".into()), + Value::Array(vec![ + Value::BulkString(b"LUA".into()), + Value::Array(vec![ + Value::BulkString(b"libraries_count".into()), + Value::Int(3), + Value::BulkString(b"functions_count".into()), + Value::Int(5), + ]), + ]), + ]), + Value::Array(vec![ + Value::BulkString(b"running_script".into()), + Value::Nil, + Value::BulkString(b"engines".into()), + Value::Array(vec![ + Value::BulkString(b"LUA".into()), + Value::Array(vec![ + Value::BulkString(b"libraries_count".into()), + Value::Int(3), + Value::BulkString(b"functions_count".into()), + Value::Int(5), + ]), + ]), + ]), + ]); + + let resp3_response = Value::Map(vec![ + ( + Value::BulkString(b"running_script".into()), + Value::Map(vec![ + ( + Value::BulkString(b"name".into()), + Value::BulkString(b"".into()), + ), + ( + Value::BulkString(b"command".into()), + Value::Array(vec![ + Value::BulkString(b"fcall".into()), + Value::BulkString(b"".into()), + Value::BulkString(b"... rest `fcall` args ...".into()), + ]), + ), + (Value::BulkString(b"duration_ms".into()), Value::Int(24529)), + ]), + ), + ( + Value::BulkString(b"engines".into()), + Value::Map(vec![( + Value::BulkString(b"LUA".into()), + Value::Map(vec![ + (Value::BulkString(b"libraries_count".into()), Value::Int(3)), + (Value::BulkString(b"functions_count".into()), Value::Int(5)), + ]), + )]), + ), + ]); + + let conversion_type = Some(ExpectedReturnType::FunctionStatsReturnType); + assert_eq!( + convert_to_expected_type(resp2_response.clone(), conversion_type), + Ok(resp3_response.clone()) + ); + + let resp3_map_response = Value::Array(vec![ + Value::Map(vec![ + (Value::BulkString(b"running_script".into()), Value::Nil), + ( + Value::BulkString(b"engines".into()), + Value::Map(vec![( + Value::BulkString(b"LUA".into()), + Value::Map(vec![ + (Value::BulkString(b"libraries_count".into()), Value::Int(3)), + (Value::BulkString(b"functions_count".into()), Value::Int(5)), + ]), + )]), + ), + ]), + Value::Map(vec![ + ( + Value::BulkString(b"running_script".into()), + Value::Map(vec![ + ( + Value::BulkString(b"name".into()), + Value::BulkString(b"".into()), + ), + ( + Value::BulkString(b"command".into()), + Value::Array(vec![ + Value::BulkString(b"fcall".into()), + Value::BulkString(b"".into()), + Value::BulkString(b"... rest `fcall` args ...".into()), + ]), + ), + (Value::BulkString(b"duration_ms".into()), Value::Int(24529)), + ]), + ), + ( + Value::BulkString(b"engines".into()), + Value::Map(vec![( + Value::BulkString(b"LUA".into()), + Value::Map(vec![ + (Value::BulkString(b"libraries_count".into()), Value::Int(3)), + (Value::BulkString(b"functions_count".into()), Value::Int(5)), + ]), + )]), + ), + ]), + ]); + + assert_eq!( + convert_to_expected_type(resp3_map_response.clone(), conversion_type), + Ok(resp3_response.clone()) + ); + } + #[test] fn convert_smismember() { assert!(matches!(