Skip to content

Commit 6d2e297

Browse files
author
Adar Ovadia
committed
add version check with mutex to skip tests in runtime
Signed-off-by: Adar Ovadia <adarov@amazon.com>
1 parent 938a1ee commit 6d2e297

File tree

3 files changed

+99
-12
lines changed

3 files changed

+99
-12
lines changed

.github/workflows/redis-rs.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,7 @@ jobs:
6767
- name: Test
6868
# TODO remove the concurrency limit after we fix test flakyness.
6969
run: |
70-
smallest_version=$(printf "%s\n" "${{ matrix.engine.version }}" "8.0" | sort -V | head -n1)
71-
# Sort versions and get the smallest version between the current version and 8.0, if the version is small than 8.0, skip the test.
72-
if [[ "8.0" == "$smallest_version" ]]; then
73-
cargo test --release --features valkey-gte-8 -- --test-threads=1 | tee ../test-results.xml
74-
else
75-
cargo test --release -- --test-threads=1 | tee ../test-results.xml
76-
fi
77-
70+
cargo test --release -- --test-threads=1 | tee ../test-results.xml
7871
echo "### Tests passed :v:" >> $GITHUB_STEP_SUMMARY
7972
working-directory: ./glide-core/redis-rs/redis/src
8073

glide-core/redis-rs/redis/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ uuid = { version = "1.6.1", optional = true }
9999

100100
telemetrylib = { path = "../../telemetry" }
101101

102+
lazy_static = "1"
103+
102104
[features]
103105
default = [
104106
"acl",
@@ -153,7 +155,6 @@ bigdecimal = ["dep:bigdecimal"]
153155
num-bigint = []
154156
uuid = ["dep:uuid"]
155157
disable-client-setinfo = []
156-
valkey-gte-8 = []
157158

158159
# Deprecated features
159160
tls = ["tls-native-tls"] # use "tls-native-tls" instead

glide-core/redis-rs/redis/tests/test_cluster_async.rs

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,92 @@
22
#![cfg(feature = "cluster-async")]
33
mod support;
44

5+
use std::cell::Cell;
6+
use tokio::sync::Mutex;
7+
8+
use lazy_static::lazy_static;
9+
10+
lazy_static! {
11+
static ref CLUSTER_VERSION: Mutex<Cell<usize>> = Mutex::<Cell<usize>>::default();
12+
}
13+
14+
/// Check if the current cluster version is less than `min_version`.
15+
/// At first, the func check for the Valkey version and if none exists, then the Redis version is checked.
16+
async fn engine_version_less_than(min_version: &str) -> bool {
17+
let test_version = crate::get_cluster_version().await;
18+
let min_version_usize = crate::version_to_usize(min_version).unwrap();
19+
if test_version < min_version_usize {
20+
println!(
21+
"The engine version is {:?}, which is lower than {:?}",
22+
test_version, min_version
23+
);
24+
return true;
25+
}
26+
return false;
27+
}
28+
29+
/// Static function to get the engine version. When version looks like 8.0.0 -> 80000 and 12.0.1 -> 120001.
30+
async fn get_cluster_version() -> usize {
31+
let cluster_version = CLUSTER_VERSION.lock().await;
32+
if cluster_version.get() == 0 {
33+
let cluster = crate::support::TestClusterContext::new(3, 0);
34+
35+
let mut connection = cluster.async_connection(None).await;
36+
37+
let cmd = redis::cmd("INFO");
38+
let info = connection
39+
.route_command(
40+
&cmd,
41+
redis::cluster_routing::RoutingInfo::SingleNode(
42+
redis::cluster_routing::SingleNodeRoutingInfo::Random,
43+
),
44+
)
45+
.await
46+
.unwrap();
47+
48+
let info_result = redis::from_owned_redis_value::<String>(info).unwrap();
49+
50+
cluster_version.set(
51+
parse_version_from_info(info_result.clone())
52+
.expect(format!("Invalid version string in INFO : {info_result}").as_str()),
53+
);
54+
}
55+
return cluster_version.get();
56+
}
57+
58+
fn parse_version_from_info(info: String) -> Option<usize> {
59+
// check for valkey_version
60+
if let Some(version) = info
61+
.lines()
62+
.find_map(|line| line.strip_prefix("valkey_version:"))
63+
{
64+
return version_to_usize(version);
65+
}
66+
67+
// check for redis_version if no valkey_version was found
68+
if let Some(version) = info
69+
.lines()
70+
.find_map(|line| line.strip_prefix("redis_version:"))
71+
{
72+
return version_to_usize(version);
73+
}
74+
None
75+
}
76+
77+
/// Takes a version string (e.g., 8.2.1) and converts it to a usize (e.g., 80201)
78+
/// version 12.10.0 will became 121000
79+
fn version_to_usize(version: &str) -> Option<usize> {
80+
version
81+
.split('.')
82+
.enumerate()
83+
.map(|(index, part)| {
84+
part.parse::<usize>()
85+
.ok()
86+
.map(|num| num * 10_usize.pow(2 * (2 - index) as u32))
87+
})
88+
.sum()
89+
}
90+
591
#[cfg(test)]
692
mod cluster_async {
793
use std::{
@@ -35,7 +121,6 @@ mod cluster_async {
35121
};
36122

37123
use crate::support::*;
38-
39124
use tokio::sync::mpsc;
40125
fn broken_pipe_error() -> RedisError {
41126
RedisError::from(std::io::Error::new(
@@ -121,9 +206,13 @@ mod cluster_async {
121206
.unwrap();
122207
}
123208

124-
#[cfg(feature = "valkey-gte-8")]
125209
#[tokio::test]
126210
async fn test_routing_by_slot_to_replica_with_az_affinity_strategy_to_half_replicas() {
211+
// Skip test if version is less then Valkey 8.0
212+
if crate::engine_version_less_than("8.0").await {
213+
return;
214+
}
215+
127216
let replica_num: u16 = 4;
128217
let primaries_num: u16 = 3;
129218
let replicas_num_in_client_az = replica_num / 2;
@@ -213,9 +302,13 @@ mod cluster_async {
213302
);
214303
}
215304

216-
#[cfg(feature = "valkey-gte-8")]
217305
#[tokio::test]
218306
async fn test_routing_by_slot_to_replica_with_az_affinity_strategy_to_all_replicas() {
307+
// Skip test if version is less then Valkey 8.0
308+
if crate::engine_version_less_than("8.0").await {
309+
return;
310+
}
311+
219312
let replica_num: u16 = 4;
220313
let primaries_num: u16 = 3;
221314
let cluster =

0 commit comments

Comments
 (0)