#[cfg(test)]
mod test {
use bb8::Pool;
use sidekiq::{Processor, ProcessorConfig, RedisConnectionManager, RedisPool};
use std::time::Duration;
async fn new_pool() -> RedisPool {
let manager = RedisConnectionManager::new("redis://127.0.0.1/").unwrap();
Pool::builder().build(manager).await.unwrap()
}
async fn flushall(redis: &RedisPool) {
let mut conn = redis.get().await.unwrap();
let _: String = redis::cmd("FLUSHALL")
.query_async(conn.unnamespaced_borrow_mut())
.await
.unwrap();
}
async fn scard(redis: &RedisPool, key: &str) -> i64 {
let mut conn = redis.get().await.unwrap();
redis::cmd("SCARD")
.arg(key)
.query_async(conn.unnamespaced_borrow_mut())
.await
.unwrap_or(0)
}
#[tokio::test]
async fn graceful_shutdown_removes_process_from_processes_set() {
let redis = new_pool().await;
flushall(&redis).await;
let p = Processor::new(redis.clone(), vec!["default".to_string()])
.with_config(ProcessorConfig::default().num_workers(1));
let token = p.get_cancellation_token();
let handle = tokio::spawn(p.run());
tokio::time::sleep(Duration::from_secs(6)).await;
assert_eq!(
scard(&redis, "processes").await,
1,
"process should be registered in set after first heartbeat"
);
token.cancel();
handle.await.unwrap();
assert_eq!(
scard(&redis, "processes").await,
0,
"processes set must be empty after graceful shutdown"
);
}
#[tokio::test]
async fn early_shutdown_leaves_processes_set_empty() {
let redis = new_pool().await;
flushall(&redis).await;
let p = Processor::new(redis.clone(), vec!["default".to_string()]);
let token = p.get_cancellation_token();
let handle = tokio::spawn(p.run());
token.cancel();
handle.await.unwrap();
assert_eq!(
scard(&redis, "processes").await,
0,
"processes set must be empty when cancelled before first heartbeat"
);
}
}