-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdelay.rs
More file actions
447 lines (362 loc) · 13.9 KB
/
delay.rs
File metadata and controls
447 lines (362 loc) · 13.9 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// tests/delay.rs
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use redis::AsyncCommands;
use serde::{Deserialize, Serialize};
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
mod fixtures;
use fixtures::TestJobErrorData;
use twmq::{
DurableExecution, Queue, SuccessHookData,
hooks::TransactionContext,
job::{BorrowedJob, DelayOptions, JobResult, JobStatus, RequeuePosition},
queue::QueueOptions,
redis::aio::ConnectionManager,
};
const REDIS_URL: &str = "redis://127.0.0.1:6379/";
// Helper to clean up Redis keys
async fn cleanup_redis_keys(conn_manager: &ConnectionManager, queue_name: &str) {
let mut conn = conn_manager.clone();
// twmq queue keys are hash-tagged for Redis Cluster compatibility
let keys_pattern = format!("twmq:{{{queue_name}}}:*");
let keys: Vec<String> = redis::cmd("KEYS")
.arg(&keys_pattern)
.query_async(&mut conn)
.await
.unwrap_or_default();
if !keys.is_empty() {
redis::cmd("DEL")
.arg(keys)
.query_async::<()>(&mut conn)
.await
.unwrap_or_default();
}
tracing::info!("Cleaned up keys for pattern: {}", keys_pattern);
}
// Job that tests delay functionality
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DelayTestJobData {
pub expected_delay_seconds: u64,
pub test_id: String, // Unique per test to avoid conflicts
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct DelayTestOutput {
pub actual_delay_seconds: u64,
pub message: String,
pub test_id: String,
}
struct DelayTestJobHandler;
impl DurableExecution for DelayTestJobHandler {
type Output = DelayTestOutput;
type ErrorData = TestJobErrorData;
type JobData = DelayTestJobData;
async fn process(
&self,
job: &BorrowedJob<Self::JobData>,
) -> JobResult<Self::Output, Self::ErrorData> {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let actual_delay = now - job.job.created_at;
tracing::info!(
"DELAY_JOB: Processing job {}, expected delay: {}s, actual delay: {}s",
job.job.id,
job.job.data.expected_delay_seconds,
actual_delay
);
Ok(DelayTestOutput {
actual_delay_seconds: actual_delay,
message: format!("Job {} processed after {}s delay", job.job.id, actual_delay),
test_id: job.job.data.test_id.clone(),
})
}
async fn on_success(
&self,
job: &BorrowedJob<Self::JobData>,
d: SuccessHookData<'_, Self::Output>,
tx: &mut TransactionContext<'_>,
) {
tracing::info!("DELAY_JOB: on_success hook - {}", d.result.message);
// Store processing order in Redis using test-specific key
let order_key = format!("test:{}:processing_order", job.job.data.test_id);
// Use pipeline to add to processing order list
tx.pipeline().rpush(&order_key, &job.job.id);
tx.pipeline().expire(&order_key, 300); // Expire after 5 minutes
}
}
type DelayTestQueue = Queue<DelayTestJobHandler>;
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_job_delay_basic() {
tracing_subscriber::registry()
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| "twmq=debug".into()))
.with(tracing_subscriber::fmt::layer())
.init();
let test_id = nanoid::nanoid!();
let queue_name = format!("test_delay_{test_id}");
let job_id = "delay_job_001";
let delay_duration = Duration::from_secs(2);
tracing::info!(
"Creating delay test queue: {} (test_id: {})",
queue_name,
test_id
);
tracing::info!("Job should be delayed by: {:?}", delay_duration);
let queue_options = QueueOptions {
local_concurrency: 1,
polling_interval: Duration::from_millis(100),
always_poll: true,
..Default::default()
};
// Create Redis connection for the execution context
let redis_client = redis::Client::open(REDIS_URL).unwrap();
let redis_conn = Arc::new(redis_client.get_connection_manager().await.unwrap());
let handler = DelayTestJobHandler;
let queue = Arc::new(
DelayTestQueue::new(REDIS_URL, &queue_name, Some(queue_options), handler)
.await
.expect("Failed to create delay test queue"),
);
cleanup_redis_keys(&redis_conn, &queue_name).await;
// Create job with delay
let delay_job = DelayTestJobData {
expected_delay_seconds: delay_duration.as_secs(),
test_id: test_id.clone(),
};
tracing::info!("Pushing delayed job with ID: {}", job_id);
let delay_options = DelayOptions {
delay: delay_duration,
position: RequeuePosition::Last,
};
queue
.clone()
.job(delay_job)
.with_id(job_id)
.with_delay(delay_options)
.push()
.await
.expect("Failed to push delayed job");
// Verify job starts in delayed queue, not pending
let initial_pending = queue.count(JobStatus::Pending).await.unwrap();
let initial_delayed = queue.count(JobStatus::Delayed).await.unwrap();
let initial_active = queue.count(JobStatus::Active).await.unwrap();
tracing::info!(
"Initial state - Pending: {}, Delayed: {}, Active: {}",
initial_pending,
initial_delayed,
initial_active
);
assert_eq!(
initial_pending, 0,
"Job should not be in pending queue initially"
);
assert_eq!(initial_delayed, 1, "Job should be in delayed queue");
assert_eq!(initial_active, 0, "No jobs should be active initially");
// Start worker
tracing::info!("Starting worker");
let worker = queue.work();
// Job should not be processed immediately (still delayed)
tokio::time::sleep(Duration::from_millis(500)).await;
let early_success_count = queue.count(JobStatus::Success).await.unwrap();
assert_eq!(
early_success_count, 0,
"Job should not be processed before delay expires"
);
let mid_pending = queue.count(JobStatus::Pending).await.unwrap();
let mid_delayed = queue.count(JobStatus::Delayed).await.unwrap();
tracing::info!(
"Mid-delay state - Pending: {}, Delayed: {}",
mid_pending,
mid_delayed
);
// Wait for delay to expire and job to be processed
let total_wait = delay_duration + Duration::from_secs(2);
tracing::info!(
"Waiting {:?} for delay to expire and job to process...",
total_wait
);
let mut job_processed = false;
let start_waiting = SystemTime::now();
while start_waiting.elapsed().unwrap() < total_wait {
let success_count = queue.count(JobStatus::Success).await.unwrap();
if success_count > 0 {
job_processed = true;
tracing::info!(
"Job processed after waiting {:?}",
start_waiting.elapsed().unwrap()
);
break;
}
tokio::time::sleep(Duration::from_millis(200)).await;
}
assert!(job_processed, "Job should be processed after delay expires");
// Give a moment for final processing
tokio::time::sleep(Duration::from_millis(300)).await;
// Verify final state
let final_pending = queue.count(JobStatus::Pending).await.unwrap();
let final_delayed = queue.count(JobStatus::Delayed).await.unwrap();
let final_active = queue.count(JobStatus::Active).await.unwrap();
let final_success = queue.count(JobStatus::Success).await.unwrap();
tracing::info!(
"Final state - Pending: {}, Delayed: {}, Active: {}, Success: {}",
final_pending,
final_delayed,
final_active,
final_success
);
assert_eq!(final_delayed, 0, "No jobs should remain in delayed queue");
assert_eq!(final_active, 0, "No jobs should be active");
assert_eq!(final_success, 1, "Job should be in success queue");
// Verify the job result shows correct delay timing
let mut redis_conn_direct = redis_conn.as_ref().clone();
let result_json: Option<String> = redis_conn_direct
.hget(queue.job_result_hash_name(), job_id)
.await
.expect("Failed to get job result");
assert!(result_json.is_some(), "Job result should be stored");
let job_output: DelayTestOutput =
serde_json::from_str(&result_json.unwrap()).expect("Failed to deserialize job result");
tracing::info!("Job output: {:?}", job_output);
// Allow some tolerance for timing (±1 second)
let expected_delay = delay_duration.as_secs();
let actual_delay = job_output.actual_delay_seconds;
assert!(
actual_delay >= expected_delay && actual_delay <= expected_delay + 2,
"Actual delay ({actual_delay}) should be close to expected delay ({expected_delay})"
);
tracing::info!("✅ Basic delay mechanism works correctly!");
worker.shutdown().await.unwrap();
cleanup_redis_keys(&redis_conn, &queue_name).await;
// Clean up test-specific keys
let _: () = redis_conn_direct
.del(format!("test:{test_id}:processing_order"))
.await
.unwrap_or(());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_delay_position_ordering() {
// Test that delayed jobs respect RequeuePosition when they expire
let test_id = nanoid::nanoid!();
let queue_name = format!("test_delay_order_{test_id}");
tracing::info!(
"\n=== Testing delay position ordering (test_id: {}) ===",
test_id
);
let queue_options = QueueOptions {
local_concurrency: 1, // Process one job at a time to see clear ordering
polling_interval: Duration::from_millis(50),
always_poll: true,
..Default::default()
};
// Create Redis connection for the execution context
let redis_client = redis::Client::open(REDIS_URL).unwrap();
let redis_conn = Arc::new(redis_client.get_connection_manager().await.unwrap());
let handler = DelayTestJobHandler;
let queue = Arc::new(
DelayTestQueue::new(REDIS_URL, &queue_name, Some(queue_options), handler)
.await
.expect("Failed to create delay order queue"),
);
cleanup_redis_keys(&redis_conn, &queue_name).await;
let short_delay = Duration::from_secs(1);
// Push jobs in specific order:
// 1. Normal job (no delay) - should process first
// 2. Delayed job with "First" position - should process second when delay expires
// 3. Delayed job with "Last" position - should process third when delay expires
// Job 1: Normal job (immediate)
let immediate_job = DelayTestJobData {
expected_delay_seconds: 0,
test_id: test_id.clone(),
};
queue
.clone()
.job(immediate_job)
.with_id("immediate")
.push()
.await
.unwrap();
// Job 2: Delayed job with First position
let delayed_first_job = DelayTestJobData {
expected_delay_seconds: short_delay.as_secs(),
test_id: test_id.clone(),
};
let first_delay_options = DelayOptions {
delay: short_delay,
position: RequeuePosition::First,
};
queue
.clone()
.job(delayed_first_job)
.with_id("delayed_first")
.with_delay(first_delay_options)
.push()
.await
.unwrap();
// Job 3: Delayed job with Last position
let delayed_last_job = DelayTestJobData {
expected_delay_seconds: short_delay.as_secs(),
test_id: test_id.clone(),
};
let last_delay_options = DelayOptions {
delay: short_delay,
position: RequeuePosition::Last,
};
queue
.clone()
.job(delayed_last_job)
.with_id("delayed_last")
.with_delay(last_delay_options)
.push()
.await
.unwrap();
// Verify initial state
let pending_count = queue.count(JobStatus::Pending).await.unwrap();
let delayed_count = queue.count(JobStatus::Delayed).await.unwrap();
tracing::info!(
"Initial state - Pending: {}, Delayed: {}",
pending_count,
delayed_count
);
assert_eq!(pending_count, 1, "Should have 1 immediate job pending");
assert_eq!(delayed_count, 2, "Should have 2 delayed jobs");
// Start worker
let worker = queue.work();
// Wait for all jobs to complete
let max_wait = short_delay + Duration::from_secs(3);
let start_time = SystemTime::now();
while start_time.elapsed().unwrap() < max_wait {
let success_count = queue.count(JobStatus::Success).await.unwrap();
if success_count >= 3 {
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
tokio::time::sleep(Duration::from_millis(200)).await;
// Check processing order from Redis
let order_key = format!("test:{test_id}:processing_order");
let mut redis_conn_direct = redis_conn.as_ref().clone();
let processing_order: Vec<String> = redis_conn_direct
.lrange(&order_key, 0, -1)
.await
.unwrap_or_default();
tracing::info!("Processing order: {:?}", processing_order);
assert_eq!(processing_order.len(), 3, "All 3 jobs should be processed");
assert_eq!(
processing_order[0], "immediate",
"Immediate job should process first"
);
// The delayed jobs should process in position order: First comes before Last
assert_eq!(
processing_order[1], "delayed_first",
"Delayed job with First position should process before Last position"
);
assert_eq!(
processing_order[2], "delayed_last",
"Delayed job with Last position should process last"
);
tracing::info!("✅ Delay position ordering works correctly!");
worker.shutdown().await.unwrap();
cleanup_redis_keys(&redis_conn, &queue_name).await;
// Clean up test-specific keys
let _: () = redis_conn_direct.del(&order_key).await.unwrap_or(());
}