diff --git a/babushka-core/tests/test_cluster_client.rs b/babushka-core/tests/test_cluster_client.rs index 1c92dac78f..864db0d9df 100644 --- a/babushka-core/tests/test_cluster_client.rs +++ b/babushka-core/tests/test_cluster_client.rs @@ -2,6 +2,8 @@ mod utilities; #[cfg(test)] mod cluster_client_tests { + use std::collections::HashMap; + use super::*; use babushka::connection_request::ReadFrom; use redis::cluster_routing::{ @@ -11,17 +13,22 @@ mod cluster_client_tests { use utilities::cluster::{setup_test_basics_internal, SHORT_CLUSTER_TEST_TIMEOUT}; use utilities::*; - fn count_primaries_and_replicas(info_replication: Vec>) -> (u16, u16) { + fn count_primary_or_replica(value: &str) -> (u16, u16) { + if value.contains("role:master") { + (1, 0) + } else if value.contains("role:slave") { + (0, 1) + } else { + (0, 0) + } + } + + fn count_primaries_and_replicas(info_replication: HashMap) -> (u16, u16) { info_replication .into_iter() - .fold((0, 0), |acc, internal_vec| { - if internal_vec.iter().any(|str| str.contains("role:master")) { - (acc.0 + 1, acc.1) - } else if internal_vec.iter().any(|str| str.contains("role:slave")) { - (acc.0, acc.1 + 1) - } else { - (acc.0, acc.1) - } + .fold((0, 0), |acc, (_, value)| { + let count = count_primary_or_replica(&value); + (acc.0 + count.0, acc.1 + count.1) }) } @@ -43,7 +50,7 @@ mod cluster_client_tests { .req_packed_command(&cmd, None) .await .unwrap(); - let info = redis::from_redis_value::>>(&info).unwrap(); + let info = redis::from_redis_value::>(&info).unwrap(); let (primaries, replicas) = count_primaries_and_replicas(info); assert_eq!(primaries, 3); assert_eq!(replicas, 0); @@ -74,7 +81,7 @@ mod cluster_client_tests { ) .await .unwrap(); - let info = redis::from_redis_value::>>(&info).unwrap(); + let info = redis::from_redis_value::>(&info).unwrap(); let (primaries, replicas) = count_primaries_and_replicas(info); assert_eq!(primaries, 3); assert_eq!(replicas, 0); @@ -105,7 +112,7 @@ mod cluster_client_tests { ) .await .unwrap(); - let info = redis::from_redis_value::>>(&info).unwrap(); + let info = redis::from_redis_value::>(&info).unwrap(); let (primaries, replicas) = count_primaries_and_replicas(info); assert_eq!(primaries, 3); assert_eq!(replicas, 3); @@ -135,8 +142,8 @@ mod cluster_client_tests { ) .await .unwrap(); - let info = redis::from_redis_value::>>(&info).unwrap(); - let (primaries, replicas) = count_primaries_and_replicas(info); + let info = redis::from_redis_value::(&info).unwrap(); + let (primaries, replicas) = count_primary_or_replica(&info); assert_eq!(primaries, 1); assert_eq!(replicas, 0); }); @@ -169,8 +176,8 @@ mod cluster_client_tests { ) .await .unwrap(); - let info = redis::from_redis_value::>>(&info).unwrap(); - let (primaries, replicas) = count_primaries_and_replicas(info); + let info = redis::from_redis_value::(&info).unwrap(); + let (primaries, replicas) = count_primary_or_replica(&info); assert_eq!(primaries, 0); assert_eq!(replicas, 1); }); @@ -203,8 +210,8 @@ mod cluster_client_tests { ) .await .unwrap(); - let info = redis::from_redis_value::>>(&info).unwrap(); - let (primaries, replicas) = count_primaries_and_replicas(info); + let info = redis::from_redis_value::(&info).unwrap(); + let (primaries, replicas) = count_primary_or_replica(&info); assert_eq!(primaries, 0); assert_eq!(replicas, 1); }); diff --git a/babushka-core/tests/test_socket_listener.rs b/babushka-core/tests/test_socket_listener.rs index b001f91eec..b6bdd49a2f 100644 --- a/babushka-core/tests/test_socket_listener.rs +++ b/babushka-core/tests/test_socket_listener.rs @@ -617,16 +617,12 @@ mod socket_listener { }; let pointer = pointer as *mut Value; let received_value = unsafe { Box::from_raw(pointer) }; - let Value::Array(values) = *received_value else { + let Value::Map(values) = *received_value else { panic!("Unexpected value {:?}", received_value); }; assert_eq!(values.len(), 3); for i in 0..3 { - let Value::Array(nested_values) = values.get(i).unwrap() else { - panic!("Unexpected value {:?}", values[i]); - }; - assert_eq!(nested_values.len(), 2); - assert_eq!(nested_values[1], Value::BulkString(b"foo".to_vec())); + assert_eq!(values.get(i).unwrap().1, Value::BulkString(b"foo".to_vec())); } } diff --git a/node/src/RedisClusterClient.ts b/node/src/RedisClusterClient.ts index 14a78a6cb2..e420709be4 100644 --- a/node/src/RedisClusterClient.ts +++ b/node/src/RedisClusterClient.ts @@ -122,25 +122,6 @@ function toProtobufRoute( } } -/** Convert the multi-node response from a list of [address, nodeResponse] pairs to - * a dictionary where each address is the key and its corresponding node response is the value. - * - * @param response - A list of lists, where each inner list contains an address (string) - * and the corresponding node response (of type T). Or a single node response (of type T). - * @param isSingleResponse - Predicate that checks if `response` is single node response. - * @returns `response` if response is single node response, - * otherwise a dictionary where each address is the key and its corresponding node response is the value. - */ -export function convertMultiNodeResponseToDict( - response: T | [string, T][], - isSingleResponse: (res: T | [string, T][]) => boolean -): T | Record { - if (isSingleResponse(response)) { - return response as T; - } - return Object.fromEntries(response as [string, T][]); -} - export class RedisClusterClient extends BaseClient { /** * @internal @@ -241,16 +222,10 @@ export class RedisClusterClient extends BaseClient { options?: InfoOptions[], route?: Routes ): Promise> { - const result = this.createWritePromise( + return this.createWritePromise>( createInfo(options), toProtobufRoute(route) ); - return result.then((res) => { - return convertMultiNodeResponseToDict( - res, - (response) => typeof response == "string" - ); - }); } /** Get the name of the current connection. @@ -266,16 +241,10 @@ export class RedisClusterClient extends BaseClient { public clientGetName( route?: Routes ): Promise> { - const result = this.createWritePromise( + return this.createWritePromise>( createClientGetName(), toProtobufRoute(route) ); - return result.then((res) => { - return convertMultiNodeResponseToDict( - res, - (response) => typeof response == "string" || response == null - ); - }); } /** Rewrite the configuration file with the current configuration. @@ -317,16 +286,10 @@ export class RedisClusterClient extends BaseClient { * it returns a dictionary where each address is the key and its corresponding node response is the value. */ public clientId(route?: Routes): Promise> { - const result = this.createWritePromise( + return this.createWritePromise>( createClientId(), toProtobufRoute(route) ); - return result.then((res) => { - return convertMultiNodeResponseToDict( - res, - (response) => typeof response == "number" - ); - }); } /** Reads the configuration parameters of a running Redis server. @@ -344,18 +307,10 @@ export class RedisClusterClient extends BaseClient { parameters: string[], route?: Routes ): Promise> { - const result = this.createWritePromise( + return this.createWritePromise>( createConfigGet(parameters), toProtobufRoute(route) ); - return result.then((res) => { - return convertMultiNodeResponseToDict( - res, - (response: (string | [string, string[]])[]) => - Array.isArray(response) && - response.every((item) => typeof item === "string") - ); - }); } /** Set configuration parameters to the specified values. diff --git a/node/tests/RedisClusterClient.test.ts b/node/tests/RedisClusterClient.test.ts index 25ee7e39a4..5eafef6678 100644 --- a/node/tests/RedisClusterClient.test.ts +++ b/node/tests/RedisClusterClient.test.ts @@ -11,9 +11,8 @@ import { BaseClientConfiguration, ClusterTransaction, InfoOptions, - RedisClusterClient + RedisClusterClient, } from "../"; -import { convertMultiNodeResponseToDict } from "../src/RedisClusterClient"; import { runBaseTests } from "./SharedTests"; import { flushallOnPort, transactionTest } from "./TestUtilities"; @@ -222,49 +221,4 @@ describe("RedisClusterClient", () => { }, TIMEOUT ); - - it( - "convertMultiNodeResponseToDict function test", - async () => { - const param1 = "This is a string value"; - const param2 = ["value", "value"]; - const param3 = [ - ["value1", ["value"]], - ["value2", ["value"]], - ] as [string, string[]][]; - const result = { value1: ["value"], value2: ["value"] }; - - const isString = (response: string | [string, string][]) => - typeof response == "string"; - - const isNull = (response: null | [string, null][]) => - response == null; - - const isStringArray = ( - response: (string | [string, string[]])[] - ): boolean => { - return ( - Array.isArray(response) && - response.every((item) => typeof item === "string") - ); - }; - - expect( - convertMultiNodeResponseToDict(param1, isString) - ).toEqual(param1); - - expect( - convertMultiNodeResponseToDict(param2, isStringArray) - ).toEqual(param2); - - expect( - convertMultiNodeResponseToDict(param3, isStringArray) - ).toEqual(result); - - expect( - convertMultiNodeResponseToDict(null, isNull) - ).toBeNull(); - }, - TIMEOUT - ); });