backbone-queue 2.7.35

Backbone Framework Queue Module - Redis, SQS, and RabbitMQ support
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
//! Deduplication Module Tests

use std::time::Duration;

use tokio::time::sleep;
use uuid::Uuid;

use crate::{
    deduplication::{
        MessageDeduplicator, DeduplicationConfig, DeduplicationStrategy, DeduplicationCache,
        ProcessingResult, ProcessingStatus,
    },
    QueueMessage,
};

#[tokio::test]
async fn test_memory_deduplication_basic() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::MessageId,
        deduplication_window_seconds: 60,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: false,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let mut message = QueueMessage::new("test".to_string(), serde_json::json!({"test": "data"}));
    message.message_deduplication_id = Some("test-dedup-1".to_string());

    // Should not be duplicate initially
    assert!(!deduplicator.is_duplicate(&message).await.unwrap());

    // Record message
    deduplicator.record_message(&message).await.unwrap();

    // Now should be duplicate
    assert!(deduplicator.is_duplicate(&message).await.unwrap());

    // Different message with same deduplication ID should be duplicate
    let mut message2 = QueueMessage::new("test2".to_string(), serde_json::json!({"different": "data"}));
    message2.message_deduplication_id = Some("test-dedup-1".to_string());
    assert!(deduplicator.is_duplicate(&message2).await.unwrap());

    // Different deduplication ID should not be duplicate
    let mut message3 = QueueMessage::new("test3".to_string(), serde_json::json!({"another": "data"}));
    message3.message_deduplication_id = Some("test-dedup-2".to_string());
    assert!(!deduplicator.is_duplicate(&message3).await.unwrap());
}

#[tokio::test]
async fn test_content_deduplication() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::Content,
        deduplication_window_seconds: 60,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: false,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let payload = serde_json::json!({"test": "data", "number": 42});
    let mut message = QueueMessage::new("test1".to_string(), payload.clone());

    // Should not be duplicate initially
    assert!(!deduplicator.is_duplicate(&message).await.unwrap());

    // Record message
    deduplicator.record_message(&message).await.unwrap();

    // Same content should be duplicate
    let mut message2 = QueueMessage::new("test2".to_string(), payload);
    assert!(deduplicator.is_duplicate(&message2).await.unwrap());

    // Different content should not be duplicate
    let different_payload = serde_json::json!({"test": "data", "number": 43});
    let mut message3 = QueueMessage::new("test3".to_string(), different_payload);
    assert!(!deduplicator.is_duplicate(&message3).await.unwrap());
}

#[tokio::test]
async fn test_both_deduplication_strategy() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::Both,
        deduplication_window_seconds: 60,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: false,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let payload = serde_json::json!({"test": "data"});
    let mut message = QueueMessage::new("test1".to_string(), payload.clone());
    message.message_deduplication_id = Some("dedup-1".to_string());

    // Record original message
    deduplicator.record_message(&message).await.unwrap();

    // Same deduplication ID should be duplicate
    let mut message2 = QueueMessage::new("test2".to_string(), serde_json::json!({"different": "data"}));
    message2.message_deduplication_id = Some("dedup-1".to_string());
    assert!(deduplicator.is_duplicate(&message2).await.unwrap());

    // Same content should be duplicate
    let mut message3 = QueueMessage::new("test3".to_string(), payload);
    message3.message_deduplication_id = Some("dedup-2".to_string());
    assert!(deduplicator.is_duplicate(&message3).await.unwrap());

    // Different both should not be duplicate
    let mut message4 = QueueMessage::new("test4".to_string(), serde_json::json!({"completely": "different"}));
    message4.message_deduplication_id = Some("dedup-3".to_string());
    assert!(!deduplicator.is_duplicate(&message4).await.unwrap());
}

#[tokio::test]
async fn test_none_deduplication_strategy() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::None,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: false,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let message = QueueMessage::new("test".to_string(), serde_json::json!({"test": "data"}));

    // Should never be duplicate with None strategy
    assert!(!deduplicator.is_duplicate(&message).await.unwrap());
    assert!(!deduplicator.is_duplicate(&message).await.unwrap());
    assert!(!deduplicator.is_duplicate(&message).await.unwrap());
}

#[tokio::test]
async fn test_exactly_once_processing() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::MessageId,
        deduplication_window_seconds: 60,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: true,
        exactly_once_window_seconds: 300,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let mut message = QueueMessage::new("test".to_string(), serde_json::json!({"test": "data"}));
    message.message_deduplication_id = Some("dedup-1".to_string());

    let processor_id = "test-processor";

    // Start processing
    let processing_id = deduplicator.start_processing(&message, processor_id).await.unwrap();

    // Second attempt to start processing should fail
    let result = deduplicator.start_processing(&message, processor_id).await;
    assert!(result.is_err());

    // Complete processing successfully
    deduplicator.complete_processing(&processing_id, ProcessingResult::Success, None)
        .await
        .unwrap();

    // Check statistics
    let stats = deduplicator.get_stats().await;
    assert_eq!(stats.exactly_once_violations_prevented, 1);
    assert_eq!(stats.completed_processing_records, 1);
}

#[tokio::test]
async fn test_exactly_once_processing_failure() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::MessageId,
        deduplication_window_seconds: 60,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: true,
        exactly_once_window_seconds: 300,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let mut message = QueueMessage::new("test".to_string(), serde_json::json!({"test": "data"}));
    message.message_deduplication_id = Some("dedup-1".to_string());

    let processor_id = "test-processor";

    // Start processing
    let processing_id = deduplicator.start_processing(&message, processor_id).await.unwrap();

    // Complete processing with failure
    deduplicator.complete_processing(
        &processing_id,
        ProcessingResult::Failed,
        Some("Test error".to_string()),
    )
    .await
    .unwrap();

    // Check statistics
    let stats = deduplicator.get_stats().await;
    assert_eq!(stats.failed_processing_records, 1);
}

#[tokio::test]
async fn test_deduplication_expiration() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::MessageId,
        deduplication_window_seconds: 1, // 1 second
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: false,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let mut message = QueueMessage::new("test".to_string(), serde_json::json!({"test": "data"}));
    message.message_deduplication_id = Some("dedup-1".to_string());

    // Record message
    deduplicator.record_message(&message).await.unwrap();
    assert!(deduplicator.is_duplicate(&message).await.unwrap());

    // Wait for expiration
    sleep(Duration::from_secs(2)).await;

    // Clean up expired entries
    let cleaned = deduplicator.cleanup_expired().await.unwrap();
    assert!(cleaned > 0);

    // Should no longer be duplicate
    assert!(!deduplicator.is_duplicate(&message).await.unwrap());
}

#[tokio::test]
async fn test_cache_size_limits() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::MessageId,
        deduplication_window_seconds: 60,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 2, // Very small cache
        enable_exactly_once: false,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    // Add more entries than cache limit
    for i in 0..5 {
        let mut message = QueueMessage::new(format!("test-{}", i), serde_json::json!({"test": i}));
        message.message_deduplication_id = Some(format!("dedup-{}", i));
        deduplicator.record_message(&message).await.unwrap();
    }

    // Check that cache size is limited
    let stats = deduplicator.get_stats().await;
    assert!(stats.cache_size <= 2);
}

#[tokio::test]
async fn test_deduplication_statistics() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::MessageId,
        deduplication_window_seconds: 60,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: true,
        exactly_once_window_seconds: 300,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let mut message = QueueMessage::new("test".to_string(), serde_json::json!({"test": "data"}));
    message.message_deduplication_id = Some("dedup-1".to_string());

    // Initial stats
    let stats = deduplicator.get_stats().await;
    assert_eq!(stats.total_messages, 0);
    assert_eq!(stats.duplicates_detected, 0);
    assert_eq!(stats.cache_hits, 0);
    assert_eq!(stats.cache_misses, 0);

    // First check - should be cache miss
    assert!(!deduplicator.is_duplicate(&message).await.unwrap());
    let stats = deduplicator.get_stats().await;
    assert_eq!(stats.total_messages, 1);
    assert_eq!(stats.cache_misses, 1);

    // Record message
    deduplicator.record_message(&message).await.unwrap();

    // Second check - should be cache hit
    assert!(deduplicator.is_duplicate(&message).await.unwrap());
    let stats = deduplicator.get_stats().await;
    assert_eq!(stats.total_messages, 2);
    assert_eq!(stats.duplicates_detected, 1);
    assert_eq!(stats.cache_hits, 1);

    // Test duplicate rate calculation
    assert!(stats.duplicate_rate() > 0.0);
    assert!(stats.cache_hit_rate() > 0.0);
}

#[tokio::test]
async fn test_multiple_processors_exactly_once() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::MessageId,
        deduplication_window_seconds: 60,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: true,
        exactly_once_window_seconds: 300,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let mut message = QueueMessage::new("test".to_string(), serde_json::json!({"test": "data"}));
    message.message_deduplication_id = Some("dedup-1".to_string());

    let processor1 = "processor-1";
    let processor2 = "processor-2";

    // Processor 1 starts processing
    let processing_id_1 = deduplicator.start_processing(&message, processor1).await.unwrap();

    // Processor 2 should not be able to start processing the same message
    let result = deduplicator.start_processing(&message, processor2).await;
    assert!(result.is_err());

    // Complete processor 1
    deduplicator.complete_processing(&processing_id_1, ProcessingResult::Success, None)
        .await
        .unwrap();

    // Now processor 2 should be able to process (with a new message instance)
    let mut message2 = QueueMessage::new("test2".to_string(), serde_json::json!({"test": "data2"}));
    message2.message_deduplication_id = Some("dedup-1".to_string());

    let processing_id_2 = deduplicator.start_processing(&message2, processor2).await.unwrap();
    assert_ne!(processing_id_1, processing_id_2);
}

#[tokio::test]
async fn test_no_deduplication_id_error() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::MessageId,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: false,
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    // Message without deduplication ID should cause error when trying to record
    let message = QueueMessage::new("test".to_string(), serde_json::json!({"test": "data"}));

    let result = deduplicator.record_message(&message).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_exactly_once_disabled_error() {
    let config = DeduplicationConfig {
        strategy: DeduplicationStrategy::MessageId,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 1000,
        enable_exactly_once: false, // Disabled
        ..Default::default()
    };

    let deduplicator = MessageDeduplicator::new(config).unwrap();

    let message = QueueMessage::new("test".to_string(), serde_json::json!({"test": "data"}));

    // Should error when exactly-once is disabled
    let result = deduplicator.start_processing(&message, "processor").await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_config_validation() {
    // Test valid configs
    let valid_config = DeduplicationConfig::default();
    assert!(MessageDeduplicator::new(valid_config).is_ok());

    let config_with_custom_cache = DeduplicationConfig {
        strategy: DeduplicationStrategy::Content,
        cache_backend: DeduplicationCache::Memory,
        max_cache_entries: 5000,
        enable_exactly_once: true,
        ..Default::default()
    };
    assert!(MessageDeduplicator::new(config_with_custom_cache).is_ok());

    // Test invalid Redis cache (not implemented yet)
    let redis_config = DeduplicationConfig {
        cache_backend: DeduplicationCache::Redis(redis::Client::open("redis://localhost").unwrap()),
        ..Default::default()
    };
    assert!(MessageDeduplicator::new(redis_config).is_err());
}