Skip to content

Commit

Permalink
Added tests, slight change in the sync client.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Oct 17, 2022
1 parent 53b01bb commit ba5134d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
22 changes: 22 additions & 0 deletions limitador/src/storage/redis/redis_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,25 @@ impl AsyncRedisStorage {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::storage::redis::AsyncRedisStorage;
use redis::ErrorKind;

#[tokio::test]
async fn errs_on_bad_url() {
let result = AsyncRedisStorage::new("cassandra://127.0.0.1:6379").await;
assert!(result.is_err());
assert_eq!(result.err().unwrap().kind(), ErrorKind::InvalidClientConfig);
}

#[tokio::test]
async fn errs_on_connection_issue() {
let result = AsyncRedisStorage::new("redis://127.0.0.1:21").await;
assert!(result.is_err());
let error = result.err().unwrap();
assert_eq!(error.kind(), ErrorKind::IoError);
assert!(error.is_connection_refusal())
}
}
22 changes: 22 additions & 0 deletions limitador/src/storage/redis/redis_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,25 @@ impl CachedRedisStorageBuilder {
.await
}
}

#[cfg(test)]
mod tests {
use crate::storage::redis::CachedRedisStorage;
use redis::ErrorKind;

#[tokio::test]
async fn errs_on_bad_url() {
let result = CachedRedisStorage::new("cassandra://127.0.0.1:6379").await;
assert!(result.is_err());
assert_eq!(result.err().unwrap().kind(), ErrorKind::InvalidClientConfig);
}

#[tokio::test]
async fn errs_on_connection_issue() {
let result = CachedRedisStorage::new("redis://127.0.0.1:21").await;
assert!(result.is_err());
let error = result.err().unwrap();
assert_eq!(error.kind(), ErrorKind::IoError);
assert!(error.is_connection_refusal())
}
}
38 changes: 30 additions & 8 deletions limitador/src/storage/redis/redis_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,21 @@ impl CounterStorage for RedisStorage {
}

impl RedisStorage {
pub fn new(redis_url: &str) -> Result<Self, RedisError> {
let conn_manager = RedisConnectionManager::new(redis_url)?;
let conn_pool = Pool::builder()
pub fn new(redis_url: &str) -> Result<Self, String> {
let conn_manager = match RedisConnectionManager::new(redis_url) {
Ok(conn_manager) => conn_manager,
Err(err) => {
return Err(err.to_string());
}
};
match Pool::builder()
.connection_timeout(Duration::from_secs(3))
.max_size(MAX_REDIS_CONNS)
.build(conn_manager)
.unwrap();

Ok(Self { conn_pool })
{
Ok(conn_pool) => Ok(Self { conn_pool }),
Err(err) => Err(err.to_string()),
}
}
}

Expand Down Expand Up @@ -199,8 +205,24 @@ mod test {
use crate::storage::redis::RedisStorage;

#[test]
fn create_default() {
let _ = RedisStorage::default();
fn errs_on_bad_url() {
let result = RedisStorage::new("cassandra://127.0.0.1:6379");
assert!(result.is_err());
assert_eq!(result.err().unwrap(), "Redis URL did not parse".to_string())
}

#[test]
fn errs_on_connection_issue() {
// this panic!s And I really don't see how to bubble the redis error back up:
// r2d2 consumes it
// RedisError are not publicly constructable
// So using String as error type… sad
let result = RedisStorage::new("redis://127.0.0.1:21");
assert!(result.is_err());
assert_eq!(
result.err().unwrap(),
"timed out waiting for connection: Connection refused (os error 61)".to_string()
)
}

#[test]
Expand Down

0 comments on commit ba5134d

Please sign in to comment.