Skip to content

Commit

Permalink
cluster_client: add handling for empty initial_nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarshgupta137 authored and djc committed Aug 31, 2022
1 parent 5f0c88d commit c1ab77f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions redis/src/cluster_client.rs
Expand Up @@ -42,15 +42,25 @@ impl ClusterClientBuilder {
pub fn build(self) -> RedisResult<ClusterClient> {
let initial_nodes = self.initial_nodes?;

let first_node = match initial_nodes.first() {
Some(node) => node,
None => {
return Err(RedisError::from((
ErrorKind::InvalidClientConfig,
"Initial nodes can't be empty.",
)))
}
};

let mut cluster_params = self.cluster_params;
let password = if cluster_params.password.is_none() {
cluster_params.password = initial_nodes[0].redis.password.clone();
cluster_params.password = first_node.redis.password.clone();
&cluster_params.password
} else {
&None
};
let username = if cluster_params.username.is_none() {
cluster_params.username = initial_nodes[0].redis.username.clone();
cluster_params.username = first_node.redis.username.clone();
&cluster_params.username
} else {
&None
Expand Down Expand Up @@ -253,4 +263,10 @@ mod tests {
assert_eq!(client.cluster_params.password, Some("pass".to_string()));
assert_eq!(client.cluster_params.username, Some("user1".to_string()));
}

#[test]
fn give_empty_initial_nodes() {
let client = ClusterClient::new(Vec::<String>::new());
assert!(client.is_err())
}
}

0 comments on commit c1ab77f

Please sign in to comment.