Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "rslock"
version = "0.7.4"
version = "0.8.0"
authors = [
"Jan-Erik Rediger <badboy@archlinux.us>",
"Romain Boces <bocesr@gmail.com>",
Expand All @@ -19,20 +19,20 @@ readme = "README.md"
edition = "2021"

[features]
async-std-comp = ["redis/async-std-rustls-comp"]
async-std-comp = ["redis/smol-rustls-comp"]
tokio-comp = ["redis/tokio-rustls-comp"]
default = ["async-std-comp"]

[dependencies]
redis = { version = "0.32.7" }
tokio = { version = "1.49.0", features = ["rt", "time"] }
rand = "0.9.2"
futures = "0.3.31"
thiserror = "2.0.18"
redis = { version = "1.5.0" }
tokio = { version = "1.53.1", features = ["rt", "time"] }
rand = "0.10.2"
futures = "0.3.33"
thiserror = "2.0.19"

[dev-dependencies]
once_cell = "^1.21.3"
testcontainers = "^0.23.3"
anyhow = "^1.0.101"
tokio = { version = "^1.49.0", features = ["macros", "rt-multi-thread"] }
tokio-test = "^0.4.5"
once_cell = "1.21.4"
testcontainers = "0.27.3"
anyhow = "1.0.104"
tokio = { version = "1.53.1", features = ["macros", "rt-multi-thread"] }
tokio-test = "0.4.5"
2 changes: 1 addition & 1 deletion examples/from_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;
#[tokio::main]
async fn main() {
// Create Redis clients
let uris = vec![
let uris = [
"redis://127.0.0.1:6380/",
"redis://127.0.0.1:6381/",
"redis://127.0.0.1:6382/",
Expand Down
60 changes: 16 additions & 44 deletions src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use std::time::{Duration, Instant};

use futures::future::join_all;
use rand::{rng, Rng, RngCore};
use rand::{rng, Rng, RngExt};
use redis::aio::MultiplexedConnection;
use redis::Value::Okay;
use redis::{Client, IntoConnectionInfo, RedisError, RedisResult, Value};
Expand Down Expand Up @@ -903,7 +903,7 @@ mod tests {
}
.await;

if let Ok(_) = rl2.lock(&key, Duration::from_millis(10_000)).await {
if rl2.lock(&key, Duration::from_millis(10_000)).await.is_ok() {
panic!("Lock couldn't be acquired");
}

Expand Down Expand Up @@ -971,11 +971,8 @@ mod tests {
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;

// Assert rl2 can lock with the key now
match rl2.lock(&key, Duration::from_millis(10_000)).await {
Err(_) => {
panic!("Unexpected error when trying to claim free lock after extend expired")
}
_ => (),
if rl2.lock(&key, Duration::from_millis(10_000)).await.is_err() {
panic!("Unexpected error when trying to claim free lock after extend expired")
}

// Also assert rl1 can't reuse lock1
Expand Down Expand Up @@ -1025,9 +1022,8 @@ mod tests {

// Too big Duration, fails - technical limit is from_millis(u64::MAX)
let ttl = Duration::from_secs(u64::MAX);
match rl.lock(&key, ttl).await {
Ok(_) => panic!("Expected LockError::TtlTooLarge"),
Err(_) => (), // Test passes
if rl.lock(&key, ttl).await.is_ok() {
panic!("Expected LockError::TtlTooLarge");
}
}

Expand Down Expand Up @@ -1083,9 +1079,7 @@ mod tests {

match rl.is_freed(&lock1).await {
Ok(freed) => assert!(freed, "Lock should be freed after unlock"),
Err(LockError::RedisKeyNotFound) => {
assert!(true, "RedisKeyNotFound is expected if key is missing")
}
Err(LockError::RedisKeyNotFound) => {}
Err(e) => panic!("Unexpected error: {:?}", e),
};

Expand All @@ -1097,9 +1091,7 @@ mod tests {

match rl.is_freed(&lock2).await {
Ok(freed) => assert!(freed, "Lock should be freed after unlock"),
Err(LockError::RedisKeyNotFound) => {
assert!(true, "RedisKeyNotFound is expected if key is missing")
}
Err(LockError::RedisKeyNotFound) => {}
Err(e) => panic!("Unexpected error: {:?}", e),
};
}
Expand Down Expand Up @@ -1172,9 +1164,7 @@ mod tests {

match rl.is_freed(&lock).await {
Ok(freed) => assert!(freed, "Lock should be freed after unlock"),
Err(LockError::RedisKeyNotFound) => {
assert!(true, "RedisKeyNotFound is expected if key is missing")
}
Err(LockError::RedisKeyNotFound) => {}
Err(e) => panic!("Unexpected error: {:?}", e),
};
}
Expand Down Expand Up @@ -1205,10 +1195,7 @@ mod tests {
freed,
"Lock should be marked as freed when key is missing in Redis"
),
Err(LockError::RedisKeyNotFound) => assert!(
true,
"RedisKeyNotFound is expected when key is missing in Redis"
),
Err(LockError::RedisKeyNotFound) => {}
Err(e) => panic!("Unexpected error: {:?}", e),
};
}
Expand All @@ -1227,14 +1214,11 @@ mod tests {
// Since there are no clients, any check with Redis will fail
match rl.is_freed(&lock).await {
Ok(freed) => panic!("Expected failure due to Redis connection, but got Ok with freed status: {}", freed),
Err(LockError::RedisConnectionFailed) => assert!(true, "Expected RedisConnectionFailed when all Redis connections fail"),
Err(LockError::RedisConnectionFailed) => {}
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
Err(LockError::Unavailable) => {
// Expected error, the test should pass in this scenario
assert!(true);
}
Err(LockError::Unavailable) => {}
Err(e) => panic!("Unexpected error while acquiring lock: {:?}", e),
}
}
Expand All @@ -1250,17 +1234,11 @@ mod tests {

match lock_result {
Ok(lock) => match rl.is_freed(&lock).await {
Err(LockError::RedisConnectionFailed) => assert!(
true,
"Expected RedisConnectionFailed when all Redis connections fail"
),
Err(LockError::RedisConnectionFailed) => {}
Ok(_) => panic!("Expected RedisConnectionFailed, but got Ok"),
Err(e) => panic!("Unexpected error: {:?}", e),
},
Err(LockError::Unavailable) => {
// Expected error, the test should pass in this scenario
assert!(true);
}
Err(LockError::Unavailable) => {}
Err(e) => panic!("Unexpected error while acquiring lock: {:?}", e),
}
}
Expand Down Expand Up @@ -1289,10 +1267,7 @@ mod tests {

// Now check if is_freed identifies the mismatch correctly
match rl.is_freed(&lock).await {
Err(LockError::RedisKeyMismatch) => assert!(
true,
"Expected RedisKeyMismatch when key value does not match the lock value"
),
Err(LockError::RedisKeyMismatch) => {}
Ok(_) => panic!("Expected RedisKeyMismatch, but got Ok"),
Err(e) => panic!("Unexpected error: {:?}", e),
}
Expand All @@ -1319,10 +1294,7 @@ mod tests {
.unwrap();

match rl.is_freed(&lock).await {
Err(LockError::RedisKeyNotFound) => assert!(
true,
"Expected RedisKeyNotFound when key is missing in Redis"
),
Err(LockError::RedisKeyNotFound) => {}
Ok(_) => panic!("Expected RedisKeyNotFound, but got Ok"),
Err(e) => panic!("Unexpected error: {:?}", e),
}
Expand Down
Loading