qrusty 0.20.4

A trusty priority queue server built with Rust
Documentation
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// tests/integration_tests.rs

//! End-to-end integration tests
//!
//! These tests verify the complete Qrusty system functionality
//! including realistic usage scenarios and edge cases.

use axum::{
    body::Body,
    http::{Request, StatusCode},
};
use qrusty::{api::ApiServer, storage::Storage};
use serde_json::{json, Value};
use std::sync::Arc;
use tempfile::TempDir;
use tokio::time::{sleep, Duration};
use tower::ServiceExt;

/// Helper function to create a complete test system
async fn create_test_system() -> (ApiServer, TempDir) {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let storage =
        Storage::new(temp_dir.path().to_str().unwrap()).expect("Failed to create test storage");
    let api = ApiServer::new(Arc::new(storage));
    (api, temp_dir)
}

/// Helper function to make HTTP requests
async fn make_request(
    api: &ApiServer,
    method: &str,
    path: &str,
    body: Option<Value>,
) -> (StatusCode, Value) {
    let app = api.router();

    let request_builder = Request::builder()
        .method(method)
        .uri(path)
        .header("content-type", "application/json");

    let request = if let Some(body_json) = body {
        request_builder
            .body(Body::from(body_json.to_string()))
            .unwrap()
    } else {
        request_builder.body(Body::empty()).unwrap()
    };

    let response = app.oneshot(request).await.unwrap();
    let status = response.status();

    let body = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let body_str = String::from_utf8(body.to_vec()).unwrap();

    let json_body: Value = if body_str.is_empty() {
        Value::Null
    } else {
        serde_json::from_str(&body_str).unwrap_or(Value::String(body_str))
    };

    (status, json_body)
}

#[tokio::test]
async fn test_order_processing_workflow() {
    let (api, _temp_dir) = create_test_system().await;

    // Simulate an e-commerce order processing workflow

    // 1. High priority order comes in
    let urgent_order = json!({
        "queue": "orders",
        "priority": 1000,
        "payload": json!({
            "order_id": "ORD-001",
            "customer_id": "CUST-123",
            "items": [
                {"sku": "PHONE-X", "quantity": 1, "price": 999.99}
            ],
            "total": 999.99,
            "priority": "urgent",
            "created_at": "2025-08-19T10:00:00Z"
        }).to_string(),
        "max_retries": 5
    });

    // 2. Regular orders come in
    let regular_orders = vec![
        json!({
            "queue": "orders",
            "priority": 100,
            "payload": json!({
                "order_id": "ORD-002",
                "customer_id": "CUST-456",
                "items": [{"sku": "BOOK-A", "quantity": 2, "price": 29.99}],
                "total": 59.98
            }).to_string()
        }),
        json!({
            "queue": "orders",
            "priority": 100,
            "payload": json!({
                "order_id": "ORD-003",
                "customer_id": "CUST-789",
                "items": [{"sku": "SHIRT-B", "quantity": 1, "price": 39.99}],
                "total": 39.99
            }).to_string()
        }),
    ];

    // Publish all orders
    let (status, _) = make_request(&api, "POST", "/publish", Some(urgent_order)).await;
    assert_eq!(status, StatusCode::OK);

    for order in regular_orders {
        let (status, _) = make_request(&api, "POST", "/publish", Some(order)).await;
        assert_eq!(status, StatusCode::OK);
    }

    // 3. Order processor consumes messages (urgent should come first)
    let consume_request = json!({
        "consumer_id": "order-processor-1",
        "timeout_seconds": 60
    });

    let (status, body) = make_request(
        &api,
        "POST",
        "/consume/orders",
        Some(consume_request.clone()),
    )
    .await;
    assert_eq!(status, StatusCode::OK);

    let first_order: Value = serde_json::from_str(body["payload"].as_str().unwrap()).unwrap();
    assert_eq!(first_order["order_id"], "ORD-001"); // Urgent order should come first
    assert_eq!(first_order["priority"], "urgent");

    // 4. Successfully process the urgent order
    let ack_request = json!({"consumer_id": "order-processor-1"});
    let ack_path = format!("/ack/orders/{}", body["id"].as_str().unwrap());
    let (status, _) = make_request(&api, "POST", &ack_path, Some(ack_request)).await;
    assert_eq!(status, StatusCode::OK);

    // 5. Process remaining orders
    for _ in 0..2 {
        let (status, body) = make_request(
            &api,
            "POST",
            "/consume/orders",
            Some(consume_request.clone()),
        )
        .await;
        assert_eq!(status, StatusCode::OK);
        assert!(body.is_object());

        // Ack each order
        let ack_request = json!({"consumer_id": "order-processor-1"});
        let ack_path = format!("/ack/orders/{}", body["id"].as_str().unwrap());
        let (status, _) = make_request(&api, "POST", &ack_path, Some(ack_request)).await;
        assert_eq!(status, StatusCode::OK);
    }

    // 6. Queue should be empty now
    let (status, body) = make_request(&api, "POST", "/consume/orders", Some(consume_request)).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body, Value::Null);
}

#[tokio::test]
async fn test_failed_message_processing() {
    let (api, _temp_dir) = create_test_system().await;

    // Publish a message that will fail processing
    let problematic_message = json!({
        "queue": "processing",
        "priority": 100,
        "payload": json!({
            "task": "process_payment",
            "payment_id": "PAY-001",
            "amount": 100.00,
            "card_token": "invalid_token"
        }).to_string(),
        "max_retries": 2
    });

    let (status, publish_body) =
        make_request(&api, "POST", "/publish", Some(problematic_message)).await;
    assert_eq!(status, StatusCode::OK);
    let message_id = publish_body["id"].as_str().unwrap();

    // First processing attempt fails
    let consume_request = json!({
        "consumer_id": "payment-processor-1",
        "timeout_seconds": 30
    });

    let (status, consume_body) = make_request(
        &api,
        "POST",
        "/consume/processing",
        Some(consume_request.clone()),
    )
    .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(consume_body["retry_count"], 1);

    // Nack due to processing failure
    let nack_request = json!({"consumer_id": "payment-processor-1"});
    let nack_path = format!("/nack/processing/{}", message_id);
    let (status, _) = make_request(&api, "POST", &nack_path, Some(nack_request)).await;
    assert_eq!(status, StatusCode::OK);

    // Second attempt (retry)
    let (status, consume_body) = make_request(
        &api,
        "POST",
        "/consume/processing",
        Some(consume_request.clone()),
    )
    .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(consume_body["id"], message_id);
    assert_eq!(consume_body["retry_count"], 2);

    // Second attempt also fails
    let nack_request = json!({"consumer_id": "payment-processor-1"});
    let nack_path = format!("/nack/processing/{}", message_id);
    let (status, _) = make_request(&api, "POST", &nack_path, Some(nack_request)).await;
    assert_eq!(status, StatusCode::OK);

    // Message should be moved to dead letter queue (no longer available)
    let (status, body) =
        make_request(&api, "POST", "/consume/processing", Some(consume_request)).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body, Value::Null);
}

#[tokio::test]
async fn test_multi_consumer_load_balancing() {
    let (api, _temp_dir) = create_test_system().await;

    // Publish multiple tasks
    let task_count = 20;
    for i in 0..task_count {
        let task = json!({
            "queue": "tasks",
            "priority": 100 - (i % 5), // Vary priorities slightly
            "payload": json!({
                "task_id": format!("TASK-{:03}", i),
                "type": "data_processing",
                "data": format!("data_chunk_{}", i)
            }).to_string()
        });

        let (status, _) = make_request(&api, "POST", "/publish", Some(task)).await;
        assert_eq!(status, StatusCode::OK);
    }

    // Simulate multiple workers processing tasks concurrently
    let worker_count = 4;
    let mut handles = vec![];

    for worker_id in 0..worker_count {
        let api_clone = api.clone();
        let handle = tokio::spawn(async move {
            let mut processed_tasks = vec![];
            let consumer_id = format!("worker-{}", worker_id);

            // Each worker tries to consume tasks until queue is empty
            loop {
                let consume_request = json!({
                    "consumer_id": consumer_id,
                    "timeout_seconds": 30
                });

                let (status, body) =
                    make_request(&api_clone, "POST", "/consume/tasks", Some(consume_request)).await;
                if status != StatusCode::OK || body == Value::Null {
                    break;
                }

                let task_payload: Value =
                    serde_json::from_str(body["payload"].as_str().unwrap()).unwrap();
                processed_tasks.push(task_payload["task_id"].as_str().unwrap().to_string());

                // Simulate processing time
                sleep(Duration::from_millis(10)).await;

                // Ack the task
                let ack_request = json!({"consumer_id": consumer_id});
                let ack_path = format!("/ack/tasks/{}", body["id"].as_str().unwrap());
                let (status, _) =
                    make_request(&api_clone, "POST", &ack_path, Some(ack_request)).await;
                assert_eq!(status, StatusCode::OK);
            }

            processed_tasks
        });
        handles.push(handle);
    }

    // Collect results from all workers
    let mut all_processed_tasks = vec![];
    for handle in handles {
        let worker_tasks = handle.await.unwrap();
        all_processed_tasks.extend(worker_tasks);
    }

    // Verify all tasks were processed exactly once
    assert_eq!(all_processed_tasks.len(), task_count);
    all_processed_tasks.sort();

    for i in 0..task_count {
        let expected_task_id = format!("TASK-{:03}", i);
        assert!(all_processed_tasks.contains(&expected_task_id));
    }

    // Verify queue is empty
    let consume_request = json!({
        "consumer_id": "final-check",
        "timeout_seconds": 1
    });
    let (status, body) = make_request(&api, "POST", "/consume/tasks", Some(consume_request)).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body, Value::Null);
}

#[tokio::test]
async fn test_mixed_queue_operations() {
    let (api, _temp_dir) = create_test_system().await;

    // Test scenario with multiple queues, priorities, and operations

    // Email queue - high frequency, lower priority
    for i in 0..10 {
        let email = json!({
            "queue": "emails",
            "priority": 10,
            "payload": json!({
                "type": "welcome_email",
                "user_id": format!("USER-{}", i),
                "template": "welcome_template"
            }).to_string()
        });
        make_request(&api, "POST", "/publish", Some(email)).await;
    }

    // SMS queue - medium priority, time sensitive
    for i in 0..5 {
        let sms = json!({
            "queue": "sms",
            "priority": 50,
            "payload": json!({
                "type": "verification_code",
                "phone": format!("+1555000{:04}", i),
                "code": format!("{:06}", i * 123456)
            }).to_string()
        });
        make_request(&api, "POST", "/publish", Some(sms)).await;
    }

    // Critical alerts - highest priority
    for i in 0..3 {
        let alert = json!({
            "queue": "alerts",
            "priority": 1000,
            "payload": json!({
                "type": "security_breach",
                "severity": "critical",
                "incident_id": format!("INC-{}", i)
            }).to_string()
        });
        make_request(&api, "POST", "/publish", Some(alert)).await;
    }

    // Process alerts first (highest priority)
    for _ in 0..3 {
        let consume_request = json!({
            "consumer_id": "alert-handler",
            "timeout_seconds": 30
        });

        let (status, body) =
            make_request(&api, "POST", "/consume/alerts", Some(consume_request)).await;
        assert_eq!(status, StatusCode::OK);

        let payload: Value = serde_json::from_str(body["payload"].as_str().unwrap()).unwrap();
        assert_eq!(payload["type"], "security_breach");

        let ack_request = json!({"consumer_id": "alert-handler"});
        let ack_path = format!("/ack/alerts/{}", body["id"].as_str().unwrap());
        make_request(&api, "POST", &ack_path, Some(ack_request)).await;
    }

    // Process SMS messages (medium priority)
    for _ in 0..5 {
        let consume_request = json!({
            "consumer_id": "sms-sender",
            "timeout_seconds": 30
        });

        let (status, body) =
            make_request(&api, "POST", "/consume/sms", Some(consume_request)).await;
        assert_eq!(status, StatusCode::OK);

        let payload: Value = serde_json::from_str(body["payload"].as_str().unwrap()).unwrap();
        assert_eq!(payload["type"], "verification_code");

        let ack_request = json!({"consumer_id": "sms-sender"});
        let ack_path = format!("/ack/sms/{}", body["id"].as_str().unwrap());
        make_request(&api, "POST", &ack_path, Some(ack_request)).await;
    }

    // Process emails (lowest priority)
    for _ in 0..10 {
        let consume_request = json!({
            "consumer_id": "email-sender",
            "timeout_seconds": 30
        });

        let (status, body) =
            make_request(&api, "POST", "/consume/emails", Some(consume_request)).await;
        assert_eq!(status, StatusCode::OK);

        let payload: Value = serde_json::from_str(body["payload"].as_str().unwrap()).unwrap();
        assert_eq!(payload["type"], "welcome_email");

        let ack_request = json!({"consumer_id": "email-sender"});
        let ack_path = format!("/ack/emails/{}", body["id"].as_str().unwrap());
        make_request(&api, "POST", &ack_path, Some(ack_request)).await;
    }

    // All queues should now be empty
    for queue in ["alerts", "sms", "emails"] {
        let consume_request = json!({
            "consumer_id": "final-check",
            "timeout_seconds": 1
        });

        let (status, body) = make_request(
            &api,
            "POST",
            &format!("/consume/{}", queue),
            Some(consume_request),
        )
        .await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(body, Value::Null);
    }
}

#[tokio::test]
async fn test_system_health_monitoring() {
    let (api, _temp_dir) = create_test_system().await;

    // Health check should always be responsive
    for _ in 0..10 {
        let (status, body) = make_request(&api, "GET", "/health", None).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(body["status"], Value::String("ok".to_string()));
    }

    // Health check should work even under load
    // Publish many messages
    for i in 0..100 {
        let message = json!({
            "queue": "load_test",
            "priority": i,
            "payload": format!("{{\"load_test\": {}}}", i)
        });
        make_request(&api, "POST", "/publish", Some(message)).await;
    }

    // Health check should still be responsive
    let (status, body) = make_request(&api, "GET", "/health", None).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body["status"], Value::String("ok".to_string()));

    // Stats endpoint should also be available
    let (status, body) = make_request(&api, "GET", "/stats", None).await;
    assert_eq!(status, StatusCode::OK);
    assert!(body.is_object());
    assert!(body.get("queues").is_some());
    assert!(body.get("summary").is_some());
}

#[tokio::test]
async fn test_data_persistence_across_restarts() {
    let temp_dir = TempDir::new().unwrap();
    let data_path = temp_dir.path().to_str().unwrap();

    let persistent_messages = vec![
        ("queue1", 100, "message1"),
        ("queue1", 200, "message2"),
        ("queue2", 150, "message3"),
    ];

    // First system instance
    {
        let storage = Storage::new(data_path).unwrap();
        let api = ApiServer::new(Arc::new(storage));

        // Publish messages
        for (queue, priority, payload) in &persistent_messages {
            let message = json!({
                "queue": queue,
                "priority": priority,
                "payload": json!({"data": payload}).to_string()
            });

            let (status, _) = make_request(&api, "POST", "/publish", Some(message)).await;
            assert_eq!(status, StatusCode::OK);
        }
    }

    // Second system instance (simulating restart)
    {
        let storage = Storage::new(data_path).unwrap();
        let api = ApiServer::new(Arc::new(storage));

        // Messages should still be available
        let consume_request = json!({
            "consumer_id": "persistence-test",
            "timeout_seconds": 30
        });

        // Should get highest priority message from queue1 first
        let (status, body) = make_request(
            &api,
            "POST",
            "/consume/queue1",
            Some(consume_request.clone()),
        )
        .await;
        assert_eq!(status, StatusCode::OK);
        let payload: Value = serde_json::from_str(body["payload"].as_str().unwrap()).unwrap();
        assert_eq!(payload["data"], "message2"); // Priority 200

        // Then lower priority message from queue1
        let (status, body) = make_request(
            &api,
            "POST",
            "/consume/queue1",
            Some(consume_request.clone()),
        )
        .await;
        assert_eq!(status, StatusCode::OK);
        let payload: Value = serde_json::from_str(body["payload"].as_str().unwrap()).unwrap();
        assert_eq!(payload["data"], "message1"); // Priority 100

        // And message from queue2
        let (status, body) =
            make_request(&api, "POST", "/consume/queue2", Some(consume_request)).await;
        assert_eq!(status, StatusCode::OK);
        let payload: Value = serde_json::from_str(body["payload"].as_str().unwrap()).unwrap();
        assert_eq!(payload["data"], "message3"); // Priority 150
    }
}