forked from film42/sidekiq-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_middleware_test.rs
135 lines (114 loc) · 3.69 KB
/
server_middleware_test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#[cfg(test)]
mod test {
use async_trait::async_trait;
use bb8::Pool;
use sidekiq::{
ChainIter, Job, Processor, RedisConnectionManager, RedisPool, Result, ServerMiddleware,
WorkFetcher, Worker, WorkerRef,
};
use std::sync::{Arc, Mutex};
#[async_trait]
trait FlushAll {
async fn flushall(&self);
}
#[async_trait]
impl FlushAll for RedisPool {
async fn flushall(&self) {
let mut conn = self.get().await.unwrap();
let _: String = redis::cmd("FLUSHALL")
.query_async(conn.unnamespaced_borrow_mut())
.await
.unwrap();
}
}
async fn new_base_processor(queue: String) -> (Processor, RedisPool) {
// Redis
let manager = RedisConnectionManager::new("redis://127.0.0.1/").unwrap();
let redis = Pool::builder().build(manager).await.unwrap();
redis.flushall().await;
// Sidekiq server
let p = Processor::new(redis.clone(), vec![queue]);
(p, redis)
}
#[derive(Clone)]
struct TestWorker {
did_process: Arc<Mutex<bool>>,
}
#[async_trait]
impl Worker<()> for TestWorker {
async fn perform(&self, _args: ()) -> Result<()> {
let mut this = self.did_process.lock().unwrap();
*this = true;
Ok(())
}
}
#[derive(Clone)]
struct TestMiddleware {
should_halt: bool,
did_process: Arc<Mutex<bool>>,
}
#[async_trait]
impl ServerMiddleware for TestMiddleware {
async fn call(
&self,
chain: ChainIter,
job: &Job,
worker: Arc<WorkerRef>,
redis: RedisPool,
) -> Result<()> {
{
let mut this = self.did_process.lock().unwrap();
*this = true;
}
if self.should_halt {
return Ok(());
} else {
return chain.next(job, worker, redis).await;
}
}
}
#[tokio::test]
async fn can_process_job_with_middleware() {
let worker = TestWorker {
did_process: Arc::new(Mutex::new(false)),
};
let queue = "random123".to_string();
let (mut p, redis) = new_base_processor(queue.clone()).await;
let middleware = TestMiddleware {
should_halt: false,
did_process: Arc::new(Mutex::new(false)),
};
p.register(worker.clone());
p.using(middleware.clone()).await;
TestWorker::opts()
.queue(queue)
.perform_async(&redis, ())
.await
.unwrap();
assert_eq!(p.process_one_tick_once().await.unwrap(), WorkFetcher::Done);
assert!(*worker.did_process.lock().unwrap());
assert!(*middleware.did_process.lock().unwrap());
}
#[tokio::test]
async fn can_prevent_job_from_being_processed_with_halting_middleware() {
let worker = TestWorker {
did_process: Arc::new(Mutex::new(false)),
};
let queue = "random123".to_string();
let (mut p, mut redis) = new_base_processor(queue.clone()).await;
let middleware = TestMiddleware {
should_halt: true,
did_process: Arc::new(Mutex::new(false)),
};
p.register(worker.clone());
p.using(middleware.clone()).await;
TestWorker::opts()
.queue(queue)
.perform_async(&mut redis, ())
.await
.unwrap();
assert_eq!(p.process_one_tick_once().await.unwrap(), WorkFetcher::Done);
assert!(!*worker.did_process.lock().unwrap());
assert!(*middleware.did_process.lock().unwrap());
}
}