use crate::{
Error, RedisError, RedisErrorKind, Result,
commands::{ClientKillOptions, ConnectionCommands, StringCommands},
resp::cmd,
tests::{get_default_config, get_test_client, get_test_client_with_config},
};
use serial_test::serial;
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn unknown_command() -> Result<()> {
let client = get_test_client().await?;
let result = client.send::<()>(cmd("UNKNOWN").arg("arg"), None).await;
assert!(matches!(
result,
Err(Error::Redis(RedisError {
kind: RedisErrorKind::Err,
description
})) if description.starts_with("unknown command 'UNKNOWN'")
));
Ok(())
}
#[test]
fn moved_error() {
let raw_error = b"MOVED 3999 127.0.0.1:6381";
let error = RedisError::try_from(&raw_error[..]);
println!("error: {error:?}");
assert!(matches!(
error,
Ok(RedisError {
kind: RedisErrorKind::Moved { hash_slot: 3999, address: (host, 6381) },
description
}) if description.is_empty() && host == "127.0.0.1"
));
}
#[test]
fn ask_error() {
let raw_error = b"ASK 3999 127.0.0.1:6381";
let error = RedisError::try_from(&raw_error[..]);
assert!(matches!(
error,
Ok(RedisError {
kind: RedisErrorKind::Ask { hash_slot: 3999, address: (host, 6381) },
description
}) if description.is_empty() && host == "127.0.0.1"
));
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn reconnection() -> Result<()> {
let mut config = get_default_config()?;
config.connection_name = "regular".to_string();
let regular_client = get_test_client_with_config(config).await?;
let mut config = get_default_config()?;
config.connection_name = "killer".to_string();
let killer_client = get_test_client_with_config(config).await?;
let client_id = regular_client.client_id().await?;
killer_client
.client_kill(ClientKillOptions::default().id(client_id))
.await?;
let result = regular_client.set("key", "value").await;
assert!(result.is_err());
Ok(())
}
#[cfg(debug_assertions)]
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn kill_on_write() -> Result<()> {
use crate::client::ReconnectionConfig;
let mut config = get_default_config()?;
config.reconnection = ReconnectionConfig::new_constant(0, 100);
let client = get_test_client_with_config(config).await?;
let result = client
.send::<()>(
cmd("SET")
.arg("key1")
.arg("value1")
.kill_connection_on_write(3),
Some(true),
)
.await;
assert!(result.is_ok());
let result = client
.send::<()>(
cmd("SET")
.arg("key2")
.arg("value2")
.kill_connection_on_write(2),
Some(true),
)
.await;
assert!(result.is_ok());
let result = client
.send::<()>(
cmd("SET")
.arg("key3")
.arg("value3")
.kill_connection_on_write(2),
Some(false),
)
.await;
assert!(result.is_err());
Ok(())
}