kincir 0.2.0

A Rust message streaming library inspired by Watermill
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
//! Unit tests for RabbitMQ acknowledgment backend
//!
//! These tests validate the RabbitMQ acknowledgment implementation without requiring
//! an actual RabbitMQ broker connection. They focus on testing the logic, data structures,
//! and error handling of the acknowledgment system.

use super::ack::{RabbitMQAckHandle, RabbitMQAckSubscriber};
use crate::ack::{AckHandle, AckSubscriber};
use crate::Message;
use std::time::SystemTime;
use tokio::time::Duration;

#[cfg(test)]
mod rabbitmq_ack_handle_tests {
    use super::*;

    #[test]
    fn test_rabbitmq_ack_handle_creation() {
        let message_id = "test-message-123".to_string();
        let topic = "test-queue".to_string();
        let delivery_tag = 42;
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        let handle = RabbitMQAckHandle::new(
            message_id.clone(),
            topic.clone(),
            delivery_tag,
            delivery_count,
            timestamp,
        );

        assert_eq!(handle.message_id(), message_id);
        assert_eq!(handle.topic(), topic);
        assert_eq!(handle.delivery_tag(), delivery_tag);
        assert_eq!(handle.delivery_count(), delivery_count);
        assert_eq!(handle.timestamp(), timestamp);
        assert!(!handle.handle_id().is_empty());
    }

    #[test]
    fn test_rabbitmq_ack_handle_retry_detection() {
        let message_id = "test-message-retry".to_string();
        let topic = "test-queue".to_string();
        let delivery_tag = 1;
        let timestamp = SystemTime::now();

        // First delivery (not a retry)
        let handle_first = RabbitMQAckHandle::new(
            message_id.clone(),
            topic.clone(),
            delivery_tag,
            1, // delivery_count = 1
            timestamp,
        );
        assert!(!handle_first.is_retry());

        // Second delivery (is a retry)
        let handle_retry = RabbitMQAckHandle::new(
            message_id.clone(),
            topic.clone(),
            delivery_tag,
            2, // delivery_count = 2
            timestamp,
        );
        assert!(handle_retry.is_retry());

        // Third delivery (is a retry)
        let handle_multiple_retry = RabbitMQAckHandle::new(
            message_id,
            topic,
            delivery_tag,
            5, // delivery_count = 5
            timestamp,
        );
        assert!(handle_multiple_retry.is_retry());
    }

    #[test]
    fn test_rabbitmq_ack_handle_uniqueness() {
        let message_id = "test-message-unique".to_string();
        let topic = "test-queue".to_string();
        let delivery_tag = 100;
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        let handle1 = RabbitMQAckHandle::new(
            message_id.clone(),
            topic.clone(),
            delivery_tag,
            delivery_count,
            timestamp,
        );

        let handle2 = RabbitMQAckHandle::new(
            message_id,
            topic,
            delivery_tag,
            delivery_count,
            timestamp,
        );

        // Each handle should have a unique ID even with same parameters
        assert_ne!(handle1.handle_id(), handle2.handle_id());
    }

    #[test]
    fn test_rabbitmq_ack_handle_properties() {
        let message_id = "test-message-props".to_string();
        let topic = "test-queue-props".to_string();
        let delivery_tag = 999;
        let delivery_count = 3;
        let timestamp = SystemTime::now();

        let handle = RabbitMQAckHandle::new(
            message_id.clone(),
            topic.clone(),
            delivery_tag,
            delivery_count,
            timestamp,
        );

        // Test all AckHandle trait methods
        assert_eq!(handle.message_id(), message_id);
        assert_eq!(handle.topic(), topic);
        assert_eq!(handle.delivery_count(), delivery_count);
        assert!(handle.is_retry()); // delivery_count > 1
        assert_eq!(handle.timestamp(), timestamp);

        // Test RabbitMQ-specific methods
        assert_eq!(handle.delivery_tag(), delivery_tag);
        assert!(!handle.handle_id().is_empty());
    }

    #[test]
    fn test_rabbitmq_ack_handle_edge_cases() {
        let message_id = "".to_string(); // Empty message ID
        let topic = "test-queue".to_string();
        let delivery_tag = 0; // Minimum delivery tag
        let delivery_count = 0; // Zero delivery count (edge case)
        let timestamp = SystemTime::now();

        let handle = RabbitMQAckHandle::new(
            message_id.clone(),
            topic.clone(),
            delivery_tag,
            delivery_count,
            timestamp,
        );

        assert_eq!(handle.message_id(), message_id);
        assert_eq!(handle.topic(), topic);
        assert_eq!(handle.delivery_tag(), delivery_tag);
        assert_eq!(handle.delivery_count(), delivery_count);
        assert!(!handle.is_retry()); // delivery_count <= 1
    }
}

#[cfg(test)]
mod rabbitmq_ack_subscriber_tests {
    use super::*;

    #[tokio::test]
    async fn test_rabbitmq_ack_subscriber_creation() {
        let connection_string = "amqp://localhost:5672";
        
        // Test subscriber creation (this should not fail even without connection)
        let result = RabbitMQAckSubscriber::new(connection_string).await;
        
        // We expect this to fail since we don't have a RabbitMQ broker running
        // but we're testing that the error handling works correctly
        assert!(result.is_err());
        
        // The error should be a connection error, not a panic or other issue
        let error = result.unwrap_err();
        assert!(error.to_string().contains("connection") || 
                error.to_string().contains("Connection") ||
                error.to_string().contains("refused") ||
                error.to_string().contains("timeout"));
    }

    #[tokio::test]
    async fn test_rabbitmq_ack_subscriber_invalid_connection_string() {
        let invalid_connection_strings = vec![
            "",
            "invalid://connection",
            "amqp://",
            "not-a-url",
            "amqp://invalid-host:99999",
        ];

        for connection_string in invalid_connection_strings {
            let result = RabbitMQAckSubscriber::new(connection_string).await;
            assert!(result.is_err(), "Expected error for connection string: {}", connection_string);
        }
    }

    #[test]
    fn test_rabbitmq_connection_string_validation() {
        // Test various connection string formats
        let valid_connection_strings = vec![
            "amqp://localhost:5672",
            "amqp://user:pass@localhost:5672",
            "amqp://user:pass@localhost:5672/vhost",
            "amqps://secure.rabbitmq.com:5671",
            "amqp://guest:guest@127.0.0.1:5672/%2F",
        ];

        // These should be valid format-wise (though may not connect)
        for connection_string in valid_connection_strings {
            // We're just testing that the format is acceptable
            // The actual connection test is in the async test above
            assert!(!connection_string.is_empty());
            assert!(connection_string.starts_with("amqp"));
        }
    }
}

#[cfg(test)]
mod rabbitmq_error_handling_tests {
    use super::*;

    #[tokio::test]
    async fn test_rabbitmq_connection_timeout() {
        // Test connection to a non-existent host (should timeout quickly)
        let connection_string = "amqp://192.0.2.1:5672"; // RFC5737 test address
        
        let start_time = std::time::Instant::now();
        let result = RabbitMQAckSubscriber::new(connection_string).await;
        let elapsed = start_time.elapsed();
        
        // Should fail relatively quickly (within 30 seconds)
        assert!(result.is_err());
        assert!(elapsed < Duration::from_secs(30));
    }

    #[tokio::test]
    async fn test_rabbitmq_malformed_url() {
        let malformed_urls = vec![
            "not-a-url",
            "http://localhost:5672", // Wrong protocol
            "amqp://", // Incomplete
            "amqp:///", // Missing host
        ];

        for url in malformed_urls {
            let result = RabbitMQAckSubscriber::new(url).await;
            assert!(result.is_err(), "Expected error for malformed URL: {}", url);
        }
    }
}

#[cfg(test)]
mod rabbitmq_integration_unit_tests {
    use super::*;

    #[test]
    fn test_message_to_rabbitmq_handle_conversion() {
        // Test the logic that would convert a RabbitMQ message to our handle
        let message = Message::new(b"test payload".to_vec())
            .with_metadata("delivery_tag", "123")
            .with_metadata("delivery_count", "2")
            .with_metadata("queue_name", "test-queue");

        // Simulate extracting RabbitMQ-specific information
        let delivery_tag: u64 = message.metadata
            .get("delivery_tag")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        
        let delivery_count: u32 = message.metadata
            .get("delivery_count")
            .and_then(|s| s.parse().ok())
            .unwrap_or(1);
        
        let queue_name = message.metadata
            .get("queue_name")
            .unwrap_or(&"default".to_string())
            .clone();

        assert_eq!(delivery_tag, 123);
        assert_eq!(delivery_count, 2);
        assert_eq!(queue_name, "test-queue");

        // Create handle with extracted information
        let handle = RabbitMQAckHandle::new(message.uuid.clone(), queue_name, SystemTime::now(, delivery_count, delivery_tag),
        );

        assert_eq!(handle.message_id(), message.uuid);
        assert_eq!(handle.delivery_tag(), delivery_tag);
        assert_eq!(handle.delivery_count(), delivery_count);
        assert!(handle.is_retry()); // delivery_count > 1
    }

    #[test]
    fn test_batch_acknowledgment_logic() {
        // Test the logic for batch acknowledgment with RabbitMQ delivery tags
        let handles = vec![
            RabbitMQAckHandle::new("msg1".to_string(), "queue".to_string(), SystemTime::now(), 1, 100),
            RabbitMQAckHandle::new("msg2".to_string(), "queue".to_string(), SystemTime::now(), 1, 101),
            RabbitMQAckHandle::new("msg3".to_string(), "queue".to_string(), SystemTime::now(), 1, 102),
        ];

        // Find the highest delivery tag for batch acknowledgment
        let max_delivery_tag = handles.iter()
            .map(|h| h.delivery_tag())
            .max()
            .unwrap_or(0);

        assert_eq!(max_delivery_tag, 102);

        // Verify all handles are from the same queue (required for batch ack)
        let all_same_queue = handles.iter()
            .all(|h| h.topic() == "queue");
        
        assert!(all_same_queue);
    }

    #[test]
    fn test_delivery_count_tracking() {
        // Test delivery count tracking for retry logic
        let message_id = "retry-test".to_string();
        let queue = "test-queue".to_string();
        let delivery_tag = 50;

        let deliveries = vec![
            (1, false), // First delivery - not a retry
            (2, true),  // Second delivery - is a retry
            (3, true),  // Third delivery - is a retry
            (5, true),  // Fifth delivery - is a retry
        ];

        for (count, expected_retry) in deliveries {
            let handle = RabbitMQAckHandle::new(message_id.clone(), queue.clone(), SystemTime::now(, count, delivery_tag),
            );

            assert_eq!(handle.is_retry(), expected_retry, 
                      "Delivery count {} should have retry={}", count, expected_retry);
            assert_eq!(handle.delivery_count(), count);
        }
    }

    #[test]
    fn test_handle_metadata_consistency() {
        // Test that handle metadata remains consistent
        let message_id = "consistency-test".to_string();
        let queue = "test-queue".to_string();
        let delivery_tag = 777;
        let delivery_count = 3;
        let timestamp = SystemTime::now();

        let handle = RabbitMQAckHandle::new(
            message_id.clone(),
            queue.clone(),
            delivery_tag,
            delivery_count,
            timestamp,
        );

        // Multiple calls should return the same values
        for _ in 0..5 {
            assert_eq!(handle.message_id(), message_id);
            assert_eq!(handle.topic(), queue);
            assert_eq!(handle.delivery_tag(), delivery_tag);
            assert_eq!(handle.delivery_count(), delivery_count);
            assert_eq!(handle.timestamp(), timestamp);
            assert!(handle.is_retry());
        }

        // Handle ID should be consistent but unique
        let handle_id = handle.handle_id();
        assert_eq!(handle.handle_id(), handle_id);
        assert!(!handle_id.is_empty());
    }
}

#[cfg(test)]
mod rabbitmq_performance_unit_tests {
    use super::*;

    #[test]
    fn test_handle_creation_performance() {
        let start = std::time::Instant::now();
        let count = 1000;

        for i in 0..count {
            let _handle = RabbitMQAckHandle::new(
                format!("message-{}", i),
                "test-queue".to_string(),
                i as u64,
                1,
                SystemTime::now(),
            );
        }

        let elapsed = start.elapsed();
        let per_handle = elapsed / count;

        // Handle creation should be very fast (< 1ms per handle)
        assert!(per_handle < Duration::from_millis(1), 
               "Handle creation took too long: {:?} per handle", per_handle);
    }

    #[test]
    fn test_handle_method_call_performance() {
        let handle = RabbitMQAckHandle::new("perf-test".to_string(), "test-queue".to_string(), SystemTime::now(), 2, 12345,
        );

        let start = std::time::Instant::now();
        let iterations = 10000;

        for _ in 0..iterations {
            let _ = handle.message_id();
            let _ = handle.topic();
            let _ = handle.delivery_tag();
            let _ = handle.delivery_count();
            let _ = handle.is_retry();
            let _ = handle.timestamp();
            let _ = handle.handle_id();
        }

        let elapsed = start.elapsed();
        let per_call = elapsed / (iterations * 7); // 7 method calls per iteration

        // Method calls should be extremely fast (< 1µs per call)
        assert!(per_call < Duration::from_micros(1), 
               "Method calls took too long: {:?} per call", per_call);
    }

    #[test]
    fn test_memory_usage_estimation() {
        // Test memory usage of handles
        let handles: Vec<RabbitMQAckHandle> = (0..100)
            .map(|i| RabbitMQAckHandle::new(
                format!("message-{}", i),
                "test-queue".to_string(),
                i as u64,
                1,
                SystemTime::now(),
            ))
            .collect();

        // Each handle should be relatively small in memory
        // This is more of a documentation test than a strict requirement
        assert_eq!(handles.len(), 100);
        
        // Verify all handles are properly created
        for (i, handle) in handles.iter().enumerate() {
            assert_eq!(handle.message_id(), format!("message-{}", i));
            assert_eq!(handle.delivery_tag(), i as u64);
        }
    }
}