Skip to content

Commit 220c6a9

Browse files
authored
Fix test to run all failovers. (#937)
1 parent 163f5c2 commit 220c6a9

File tree

4 files changed

+64
-87
lines changed

4 files changed

+64
-87
lines changed

redis/src/aio/connection.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ use ::tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
1414
#[cfg(feature = "tokio-comp")]
1515
use ::tokio::net::lookup_host;
1616
use combine::{parser::combinator::AnySendSyncPartialState, stream::PointerOffset};
17+
use futures_util::future::select_ok;
1718
use futures_util::{
1819
future::FutureExt,
19-
stream::{self, Stream, StreamExt},
20+
stream::{Stream, StreamExt},
2021
};
2122
use std::net::SocketAddr;
2223
use std::pin::Pin;
@@ -386,20 +387,7 @@ pub(crate) async fn connect_simple<T: RedisRuntime>(
386387
Ok(match connection_info.addr {
387388
ConnectionAddr::Tcp(ref host, port) => {
388389
let socket_addrs = get_socket_addrs(host, port).await?;
389-
stream::iter(socket_addrs)
390-
.fold(
391-
Err(RedisError::from((
392-
ErrorKind::InvalidClientConfig,
393-
"No address found for host",
394-
))),
395-
|acc, socket_addr| async move {
396-
match acc {
397-
ok @ Ok(_) => ok,
398-
Err(_) => <T>::connect_tcp(socket_addr).await,
399-
}
400-
},
401-
)
402-
.await?
390+
select_ok(socket_addrs.map(<T>::connect_tcp)).await?.0
403391
}
404392

405393
#[cfg(any(feature = "tls-native-tls", feature = "tls-rustls"))]
@@ -409,20 +397,11 @@ pub(crate) async fn connect_simple<T: RedisRuntime>(
409397
insecure,
410398
} => {
411399
let socket_addrs = get_socket_addrs(host, port).await?;
412-
stream::iter(socket_addrs)
413-
.fold(
414-
Err(RedisError::from((
415-
ErrorKind::InvalidClientConfig,
416-
"No address found for host",
417-
))),
418-
|acc, socket_addr| async move {
419-
match acc {
420-
ok @ Ok(_) => ok,
421-
Err(_) => <T>::connect_tcp_tls(host, socket_addr, insecure).await,
422-
}
423-
},
424-
)
425-
.await?
400+
select_ok(
401+
socket_addrs.map(|socket_addr| <T>::connect_tcp_tls(host, socket_addr, insecure)),
402+
)
403+
.await?
404+
.0
426405
}
427406

428407
#[cfg(not(any(feature = "tls-native-tls", feature = "tls-rustls")))]

redis/src/cluster_routing.rs

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,53 @@ pub(crate) fn aggregate(values: Vec<Value>, op: AggregateOp) -> RedisResult<Valu
5959
AggregateOp::Min => i64::MAX,
6060
AggregateOp::Sum => 0,
6161
};
62-
let result = values
63-
.into_iter()
64-
.fold(RedisResult::Ok(initial_value), |acc, curr| {
65-
let mut acc = acc?;
66-
let int = match curr {
67-
Value::Int(int) => int,
68-
_ => {
69-
return Err((
62+
let result = values.into_iter().try_fold(initial_value, |acc, curr| {
63+
let int = match curr {
64+
Value::Int(int) => int,
65+
_ => {
66+
return RedisResult::Err(
67+
(
7068
ErrorKind::TypeError,
7169
"expected array of integers as response",
7270
)
73-
.into());
74-
}
75-
};
76-
acc = match op {
77-
AggregateOp::Min => min(acc, int),
78-
AggregateOp::Sum => acc + int,
79-
};
80-
Ok(acc)
81-
})?;
71+
.into(),
72+
);
73+
}
74+
};
75+
let acc = match op {
76+
AggregateOp::Min => min(acc, int),
77+
AggregateOp::Sum => acc + int,
78+
};
79+
Ok(acc)
80+
})?;
8281
Ok(Value::Int(result))
8382
}
8483

8584
pub(crate) fn logical_aggregate(values: Vec<Value>, op: LogicalAggregateOp) -> RedisResult<Value> {
8685
let initial_value = match op {
8786
LogicalAggregateOp::And => true,
8887
};
89-
let results = values
90-
.into_iter()
91-
.fold(RedisResult::Ok(Vec::new()), |acc, curr| {
92-
let acc = acc?;
93-
let values = match curr {
94-
Value::Bulk(values) => values,
88+
let results = values.into_iter().try_fold(Vec::new(), |acc, curr| {
89+
let values = match curr {
90+
Value::Bulk(values) => values,
91+
_ => {
92+
return RedisResult::Err(
93+
(
94+
ErrorKind::TypeError,
95+
"expected array of integers as response",
96+
)
97+
.into(),
98+
);
99+
}
100+
};
101+
let mut acc = if acc.is_empty() {
102+
vec![initial_value; values.len()]
103+
} else {
104+
acc
105+
};
106+
for (index, value) in values.into_iter().enumerate() {
107+
let int = match value {
108+
Value::Int(int) => int,
95109
_ => {
96110
return Err((
97111
ErrorKind::TypeError,
@@ -100,28 +114,12 @@ pub(crate) fn logical_aggregate(values: Vec<Value>, op: LogicalAggregateOp) -> R
100114
.into());
101115
}
102116
};
103-
let mut acc = if acc.is_empty() {
104-
vec![initial_value; values.len()]
105-
} else {
106-
acc
117+
acc[index] = match op {
118+
LogicalAggregateOp::And => acc[index] && (int > 0),
107119
};
108-
for (index, value) in values.into_iter().enumerate() {
109-
let int = match value {
110-
Value::Int(int) => int,
111-
_ => {
112-
return Err((
113-
ErrorKind::TypeError,
114-
"expected array of integers as response",
115-
)
116-
.into());
117-
}
118-
};
119-
acc[index] = match op {
120-
LogicalAggregateOp::And => acc[index] && (int > 0),
121-
};
122-
}
123-
Ok(acc)
124-
})?;
120+
}
121+
Ok(acc)
122+
})?;
125123
Ok(Value::Bulk(
126124
results
127125
.into_iter()
@@ -636,14 +634,14 @@ mod tests {
636634
);
637635
}
638636

639-
for cmd in vec![
637+
for cmd in [
640638
cmd("EVAL").arg(r#"redis.call("PING");"#).arg(0),
641639
cmd("EVALSHA").arg(r#"redis.call("PING");"#).arg(0),
642640
] {
643641
assert_eq!(RoutingInfo::for_routable(cmd), Some(RoutingInfo::Random));
644642
}
645643

646-
for (cmd, expected) in vec![
644+
for (cmd, expected) in [
647645
(
648646
cmd("EVAL")
649647
.arg(r#"redis.call("GET, KEYS[1]");"#)

redis/tests/test_acl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn test_acl_cat() {
125125
assert!(res.contains(*cat), "Category `{cat}` does not exist");
126126
}
127127

128-
let expects = vec!["pfmerge", "pfcount", "pfselftest", "pfadd"];
128+
let expects = ["pfmerge", "pfcount", "pfselftest", "pfadd"];
129129
let res: HashSet<String> = con
130130
.acl_cat_categoryname("hyperloglog")
131131
.expect("Got commands of a category");

redis/tests/test_cluster_async.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ async fn test_failover(env: &TestClusterContext, requests: i32, value: i32) {
171171
async move {
172172
if i == requests / 2 {
173173
// Failover all the nodes, error only if all the failover requests error
174-
node_conns
175-
.iter_mut()
176-
.map(do_failover)
177-
.collect::<stream::FuturesUnordered<_>>()
178-
.fold(
179-
Err(anyhow::anyhow!("None")),
180-
|acc: Result<(), _>, result: Result<(), _>| async move {
181-
acc.or(result)
182-
},
183-
)
184-
.await
174+
let mut results = future::join_all(
175+
node_conns
176+
.iter_mut()
177+
.map(|conn| Box::pin(do_failover(conn))),
178+
)
179+
.await;
180+
if results.iter().all(|res| res.is_err()) {
181+
results.pop().unwrap()
182+
} else {
183+
Ok::<_, anyhow::Error>(())
184+
}
185185
} else {
186186
let key = format!("test-{value}-{i}");
187187
cmd("SET")

0 commit comments

Comments
 (0)