-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathclient.rs
165 lines (126 loc) · 4.14 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::time::Duration;
use crate::{
client::{Client, IntoConfig},
commands::{
BlockingCommands, ClientKillOptions, ConnectionCommands, FlushingMode, LMoveWhere,
ListCommands, ServerCommands, StringCommands,
},
resp::cmd,
tests::{get_default_addr, get_test_client, log_try_init},
Error, Result,
};
use serial_test::serial;
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn send() -> Result<()> {
let client = get_test_client().await?;
client.send(cmd("PING"), None).await?;
client.close().await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn forget() -> Result<()> {
let client = get_test_client().await?;
client.send_and_forget(cmd("PING"), None)?;
client.send(cmd("PING"), None).await?;
client.close().await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn on_reconnect() -> Result<()> {
let client1 = get_test_client().await?;
let client2 = get_test_client().await?;
let mut receiver = client1.on_reconnect();
let result = receiver.try_recv();
assert!(result.is_err());
let client1_id = client1.client_id().await?;
client2
.client_kill(ClientKillOptions::default().id(client1_id))
.await?;
// send command to be sure that the reconnection has been done
client1.set("key", "value").retry_on_error(true).await?;
let result = receiver.try_recv();
assert!(result.is_ok());
client1.close().await?;
client2.close().await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn command_timeout() -> Result<()> {
log_try_init();
let client = get_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
// create an empty list
client.lpush("key", "value").await?;
let _result: Vec<String> = client.lpop("key", 1).await?;
client.close().await?;
let mut config = get_default_addr().into_config()?;
config.command_timeout = Duration::from_millis(10);
let client = Client::connect(config).await?;
// block for 5 seconds
// since the timeout is configured to 10ms, we should have a timeout error
let result: Result<Option<(String, Vec<String>)>> =
client.blmpop(5., "key", LMoveWhere::Left, 1).await;
assert!(matches!(result, Err(Error::Timeout(_))));
client.close().await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn connection_name() -> Result<()> {
log_try_init();
let mut config = get_default_addr().into_config()?;
"myconnection".clone_into(&mut config.connection_name);
let client = Client::connect(config).await?;
client.flushall(FlushingMode::Sync).await?;
let connection_name: Option<String> = client.client_getname().await?;
assert_eq!(Some("myconnection".to_owned()), connection_name);
client.close().await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn mget_mset() -> Result<()> {
let client = Client::connect("127.0.0.1:6379").await?;
client
.send(
cmd("MSET")
.arg("key1")
.arg("value1")
.arg("key2")
.arg("value2")
.arg("key3")
.arg("value3")
.arg("key4")
.arg("value4"),
None,
)
.await?
.to::<()>()?;
let values: Vec<String> = client
.send(
cmd("MGET").arg("key1").arg("key2").arg("key3").arg("key4"),
None,
)
.await?
.to()?;
assert_eq!(
vec![
"value1".to_owned(),
"value2".to_owned(),
"value3".to_owned(),
"value4".to_owned()
],
values
);
Ok(())
}