-
Notifications
You must be signed in to change notification settings - Fork 30
/
realistic.rs
51 lines (42 loc) · 1.61 KB
/
realistic.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
/*
* Copyright 2017-2022 Ben Ashford
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
use std::env;
use futures_util::future;
// use futures::{future, Future};
use redis_async::{client, resp_array};
/// An artificial "realistic" non-trivial example to demonstrate usage
#[tokio::main]
async fn main() {
// Create some completely arbitrary "test data"
let test_data_size = 10;
let addr = env::args()
.nth(1)
.unwrap_or_else(|| "127.0.0.1".to_string());
let connection = client::paired_connect(addr, 6379)
.await
.expect("Cannot open connection");
let futures = (0..test_data_size).map(|x| (x, x.to_string())).map(|data| {
let connection_inner = connection.clone();
let incr_f = connection.send(resp_array!["INCR", "realistic_test_ctr"]);
async move {
let ctr: String = incr_f.await.expect("Cannot increment");
let key = format!("rt_{}", ctr);
let d_val = data.0.to_string();
connection_inner.send_and_forget(resp_array!["SET", &key, d_val]);
connection_inner
.send(resp_array!["SET", data.1, key])
.await
.expect("Cannot set")
}
});
let result: Vec<String> = future::join_all(futures).await;
println!("RESULT: {:?}", result);
assert_eq!(result.len(), test_data_size);
}