Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move mock_connection and connection tests to src folder. #106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ required-features = ["json", "serde/derive"]
name = "test_cluster_async"
required-features = ["cluster-async"]

[[test]]
name = "test_async_cluster_connections_logic"
required-features = ["cluster-async"]

[[bench]]
name = "bench_basic"
harness = false
Expand Down
49 changes: 47 additions & 2 deletions redis/src/cluster_async/connections_logic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "cluster-async")]
use super::{AsyncClusterNode, Connect};
use crate::{
aio::{get_socket_addrs, ConnectionLike},
Expand Down Expand Up @@ -53,8 +54,7 @@ where
}
}

#[doc(hidden)]
pub async fn connect_and_check<C>(
pub(crate) async fn connect_and_check<C>(
node: &str,
params: ClusterParams,
socket_addr: Option<SocketAddr>,
Expand Down Expand Up @@ -100,3 +100,48 @@ pub(crate) fn get_host_and_port_from_addr(addr: &str) -> Option<(&str, u16)> {
let port = parts.get(1).unwrap();
port.parse::<u16>().ok().map(|port| (*host, port))
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{
cluster_async::connections_logic::connect_and_check,
testing::mock_connection::{
modify_mock_connection_behavior, respond_startup, ConnectionIPReturnType,
MockConnection, MockConnectionBehavior,
},
};
use std::{
net::{IpAddr, Ipv4Addr},
sync::Arc,
};

#[tokio::test]
async fn test_connect_and_check_connect_successfully() {
// Test that upon refreshing all connections, if both connections were successful,
// the returned node contains both user and management connection
let name = "test_connect_and_check_connect_successfully";

let _handler = MockConnectionBehavior::register_new(
name,
Arc::new(move |cmd, _| {
respond_startup(name, cmd)?;
Ok(())
}),
);

let expected_ip = IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4));
modify_mock_connection_behavior(name, |behavior| {
behavior.returned_ip_type = ConnectionIPReturnType::Specified(expected_ip)
});

let (_conn, ip) = connect_and_check::<MockConnection>(
&format!("{name}:6379"),
ClusterParams::default(),
None,
)
.await
.unwrap();
assert_eq!(ip, Some(expected_ip));
}
}
3 changes: 1 addition & 2 deletions redis/src/cluster_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ impl RetryParams {

/// Redis cluster specific parameters.
#[derive(Default, Clone)]
#[doc(hidden)]
pub struct ClusterParams {
pub(crate) struct ClusterParams {
pub(crate) password: Option<String>,
pub(crate) username: Option<String>,
pub(crate) read_from_replicas: ReadFromReplicaStrategy,
Expand Down
5 changes: 2 additions & 3 deletions redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,8 @@ mod cluster_slotmap;
#[cfg(feature = "cluster")]
mod cluster_client;

// for testing purposes
#[cfg(feature = "cluster")]
pub use crate::cluster_client::ClusterParams;
/// Used exclusively for testing. This won't be stable, don't take a dependency on this module.
pub mod testing;

#[cfg(feature = "cluster")]
mod cluster_pipeline;
Expand Down
Loading
Loading