-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsentinel.rs
280 lines (225 loc) · 8.56 KB
/
sentinel.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use crate::{
client::Client,
commands::{ConnectionCommands, SentinelCommands, StringCommands},
network::sleep,
tests::{get_sentinel_master_test_client, get_sentinel_test_client, log_try_init},
Result,
};
use serial_test::serial;
use std::{collections::HashMap, time::Duration};
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn unreachable() -> Result<()> {
log_try_init();
let result = Client::connect("redis+sentinel://127.0.0.1:1234,127.0.0.1:5678/myservice").await;
assert!(result.is_err());
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn unknown_service() -> Result<()> {
log_try_init();
let result = Client::connect("redis+sentinel://127.0.0.1:26379/unknown").await;
assert!(result.is_err());
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn connection() -> Result<()> {
let client = get_sentinel_master_test_client().await?;
client.hello(Default::default()).await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn connection_with_failures() -> Result<()> {
log_try_init();
let client =
Client::connect("redis+sentinel://127.0.0.1:1234,127.0.0.1:26379/myservice").await?;
client.hello(Default::default()).await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn config_get_set() -> Result<()> {
// connect to the sentinel instance directly for these commands
let client = get_sentinel_test_client().await?;
client.sentinel_config_set("sentinel-user", "user").await?;
client.sentinel_config_set("sentinel-pass", "pwd").await?;
let configs: HashMap<String, String> = client.sentinel_config_get("sentinel-*").await?;
assert_eq!(2, configs.len());
assert_eq!(Some(&"user".to_owned()), configs.get("sentinel-user"));
assert_eq!(Some(&"pwd".to_owned()), configs.get("sentinel-pass"));
client.sentinel_config_set("sentinel-user", "").await?;
client.sentinel_config_set("sentinel-pass", "").await?;
let configs: HashMap<String, String> = client.sentinel_config_get("toto").await?;
assert_eq!(0, configs.len());
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_ckquorum() -> Result<()> {
// connect to the sentinel instance directly for this command
let client = get_sentinel_test_client().await?;
client.sentinel_ckquorum("myservice").await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_flushconfig() -> Result<()> {
// connect to the sentinel instance directly for this command
let client = get_sentinel_test_client().await?;
client.sentinel_flushconfig().await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_info_cache() -> Result<()> {
// connect to the sentinel instance directly for this command
let client = get_sentinel_test_client().await?;
let result: HashMap<String, Vec<(u64, String)>> =
client.sentinel_info_cache("myservice").await?;
assert_eq!(1, result.len());
assert!(result.contains_key("myservice"));
assert!(result.get("myservice").unwrap().len() == 2); // 1 master & 1 replica
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_master() -> Result<()> {
// connect to the sentinel instance directly for this command
let client = get_sentinel_test_client().await?;
let result = client.sentinel_master("myservice").await?;
assert_eq!("master", result.flags);
//assert_eq!(2, result.num_other_sentinels);
assert_eq!(2, result.quorum);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_masters() -> Result<()> {
// connect to the sentinel instance directly for this command
let client = get_sentinel_test_client().await?;
let result = client.sentinel_masters().await?;
assert_eq!(1, result.len());
assert_eq!("master", result[0].flags);
//assert_eq!(2, result[0].num_other_sentinels);
assert_eq!(2, result[0].quorum);
Ok(())
}
// #[cfg_attr(feature = "tokio-runtime", tokio::test)]
// #[cfg_attr(feature = "async-std-runtime", async_std::test)]
// #[serial]
// async fn sentinel_remove_and_monitor() -> Result<()> {
// // connect to the sentinel instance directly for these commands
// let client = get_sentinel_test_client().await?;
// let master_info = client.sentinel_master("myservice").await?;
// client.sentinel_remove("myservice").await?;
// client
// .sentinel_monitor(
// "myservice",
// master_info.ip,
// master_info.port,
// master_info.quorum,
// )
// .await?;
// client.sentinel_reset("myservice").await?;
// Ok(())
// }
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_set() -> Result<()> {
// connect to the sentinel instance directly for this command
let client = get_sentinel_test_client().await?;
client
.sentinel_set(
"myservice",
[
("down-after-milliseconds", 1000),
("failover-timeout", 1000),
],
)
.await?;
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_myid() -> Result<()> {
// connect to the sentinel instance directly for this command
let client = get_sentinel_test_client().await?;
let id = client.sentinel_myid().await?;
assert!(!id.is_empty());
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_pending_scripts() -> Result<()> {
// connect to the sentinel instance directly for this command
let sentinel_client = get_sentinel_test_client().await?;
let result = sentinel_client.sentinel_pending_scripts().await?;
assert!(result.is_empty());
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_replicas() -> Result<()> {
// connect to the sentinel instance directly for this command
let sentinel_client = get_sentinel_test_client().await?;
let result = sentinel_client.sentinel_replicas("myservice").await?;
assert_eq!(1, result.len());
assert_eq!("slave", result[0].flags);
assert_eq!(6382, result[0].port);
assert_eq!(6381, result[0].master_port);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn sentinel_sentinels() -> Result<()> {
// connect to the sentinel instance directly for this command
let client = get_sentinel_test_client().await?;
let result = client.sentinel_sentinels("myservice").await?;
assert!(!result.is_empty());
assert!(result[0].flags.contains("sentinel"));
//assert_eq!(26379, result[0].port);
Ok(())
}
/// test reconnection to replica when master is stopped
/// master stop is not automated but must be done manually
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
#[ignore]
async fn get_loop() -> Result<()> {
let client = get_sentinel_master_test_client().await?;
client.set("key", "value").await?;
for _ in 0..1000 {
let _value: Result<String> = client.get("key").await;
sleep(Duration::from_secs(1)).await;
}
Ok(())
}
// #[cfg_attr(feature = "tokio-runtime", tokio::test)]
// #[cfg_attr(feature = "async-std-runtime", async_std::test)]
// #[serial]
// async fn sentinel_reset() -> Result<()> {
// // connect to the sentinel instance directly for this command
// let client = get_sentinel_test_client().await?;
// let num = client.sentinel_reset("myservice").await?;
// assert_eq!(1, num);
// Ok(())
// }