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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! Unit tests for MQTT acknowledgment backend
//!
//! These tests validate the MQTT acknowledgment implementation without requiring
//! an actual MQTT broker connection. They focus on testing the logic, data structures,
//! QoS handling, and error handling of the acknowledgment system.

use super::ack::{MQTTAckHandle, MQTTAckSubscriber};
use crate::ack::{AckHandle, AckSubscriber};
use crate::Message;
use rumqttc::QoS;
use std::time::SystemTime;
use tokio::time::Duration;

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

    #[test]
    fn test_mqtt_ack_handle_creation() {
        let message_id = "test-message-123".to_string();
        let topic = "sensors/temperature".to_string();
        let qos = QoS::AtLeastOnce;
        let packet_id = Some(42);
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        let handle = MQTTAckHandle::new(
            message_id.clone(),
            topic.clone(),
            timestamp,
            delivery_count,
            qos,
            packet_id,
        );

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

    #[test]
    fn test_mqtt_ack_handle_qos_levels() {
        let message_id = "qos-test".to_string();
        let topic = "test/topic".to_string();
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        let qos_test_cases = vec![
            (QoS::AtMostOnce, None, false),      // QoS 0 - no packet ID, no ack required
            (QoS::AtLeastOnce, Some(100), true), // QoS 1 - has packet ID, ack required
            (QoS::ExactlyOnce, Some(200), true), // QoS 2 - has packet ID, ack required
        ];

        for (qos, packet_id, requires_ack) in qos_test_cases {
            let handle = MQTTAckHandle::new(
                message_id.clone(),
                topic.clone(),
                qos,
                packet_id,
                delivery_count,
                timestamp,
            );

            assert_eq!(handle.qos(), qos);
            assert_eq!(handle.packet_id(), packet_id);
            assert_eq!(handle.requires_ack(), requires_ack);
        }
    }

    #[test]
    fn test_mqtt_ack_handle_retry_detection() {
        let message_id = "retry-test".to_string();
        let topic = "test/retry".to_string();
        let qos = QoS::AtLeastOnce;
        let packet_id = Some(50);
        let timestamp = SystemTime::now();

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

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

        // Multiple retries
        let handle_multiple_retry = MQTTAckHandle::new(
            message_id,
            topic,
            qos,
            packet_id,
            5, // delivery_count = 5
            timestamp,
        );
        assert!(handle_multiple_retry.is_retry());
    }

    #[test]
    fn test_mqtt_ack_handle_uniqueness() {
        let message_id = "unique-test".to_string();
        let topic = "test/unique".to_string();
        let qos = QoS::ExactlyOnce;
        let packet_id = Some(999);
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        let handle1 = MQTTAckHandle::new(
            message_id.clone(),
            topic.clone(),
            qos,
            packet_id,
            delivery_count,
            timestamp,
        );

        let handle2 = MQTTAckHandle::new(
            message_id,
            topic,
            qos,
            packet_id,
            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_mqtt_ack_handle_requires_ack_logic() {
        let message_id = "ack-logic-test".to_string();
        let topic = "test/ack".to_string();
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        // QoS 0 - no acknowledgment required
        let handle_qos0 = MQTTAckHandle::new(
            message_id.clone(),
            topic.clone(),
            timestamp,
            delivery_count,
            QoS::AtMostOnce,
            None, // No packet ID for QoS 0
        );
        assert!(!handle_qos0.requires_ack());

        // QoS 1 - acknowledgment required
        let handle_qos1 = MQTTAckHandle::new(
            message_id.clone(),
            topic.clone(),
            timestamp,
            delivery_count,
            QoS::AtLeastOnce,
            Some(123),
        );
        assert!(handle_qos1.requires_ack());

        // QoS 2 - acknowledgment required
        let handle_qos2 = MQTTAckHandle::new(
            message_id,
            topic,
            timestamp,
            delivery_count,
            QoS::ExactlyOnce,
            Some(456),
        );
        assert!(handle_qos2.requires_ack());
    }

    #[test]
    fn test_mqtt_ack_handle_packet_id_consistency() {
        let message_id = "packet-id-test".to_string();
        let topic = "test/packet".to_string();
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        // Test various packet ID scenarios
        let packet_id_cases = vec![
            (QoS::AtMostOnce, None),        // QoS 0 should have no packet ID
            (QoS::AtLeastOnce, Some(1)),    // QoS 1 minimum packet ID
            (QoS::AtLeastOnce, Some(65535)), // QoS 1 maximum packet ID
            (QoS::ExactlyOnce, Some(32768)), // QoS 2 mid-range packet ID
        ];

        for (qos, expected_packet_id) in packet_id_cases {
            let handle = MQTTAckHandle::new(
                message_id.clone(),
                topic.clone(),
                timestamp,
                delivery_count,
                qos,
                expected_packet_id,
            );

            assert_eq!(handle.packet_id(), expected_packet_id);
            assert_eq!(handle.qos(), qos);
        }
    }

    #[test]
    fn test_mqtt_ack_handle_edge_cases() {
        let timestamp = SystemTime::now();

        // Empty strings
        let handle_empty = MQTTAckHandle::new(
            "".to_string(),
            "".to_string(),
            QoS::AtMostOnce,
            None,
            0, // Zero delivery count
            timestamp,
        );

        assert_eq!(handle_empty.message_id(), "");
        assert_eq!(handle_empty.topic(), "");
        assert_eq!(handle_empty.delivery_count(), 0);
        assert!(!handle_empty.is_retry()); // delivery_count <= 1

        // Very long topic name
        let long_topic = "a".repeat(1000);
        let handle_long = MQTTAckHandle::new(
            "long-topic-test".to_string(),
            long_topic.clone(),
            QoS::AtLeastOnce,
            Some(1),
            1,
            timestamp,
        );

        assert_eq!(handle_long.topic(), long_topic);
    }
}

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

    #[tokio::test]
    async fn test_mqtt_ack_subscriber_creation() {
        let broker_url = "127.0.0.1";
        let client_id = Some("test-client".to_string());
        
        // Test subscriber creation (this should not fail even without connection)
        let result = MQTTAckSubscriber::new(broker_url, client_id).await;
        
        // We expect this to fail since we don't have an MQTT 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") ||
                error.to_string().contains("resolve"));
    }

    #[tokio::test]
    async fn test_mqtt_ack_subscriber_invalid_broker_url() {
        let invalid_broker_urls = vec![
            "",
            "invalid-host",
            "256.256.256.256", // Invalid IP
            "localhost:99999",  // Invalid port
        ];

        for broker_url in invalid_broker_urls {
            let result = MQTTAckSubscriber::new(broker_url, Some("test-client".to_string())).await;
            assert!(result.is_err(), "Expected error for broker URL: {}", broker_url);
        }
    }

    #[tokio::test]
    async fn test_mqtt_ack_subscriber_client_id_handling() {
        let broker_url = "127.0.0.1";
        
        let client_id_cases = vec![
            Some("valid-client-id".to_string()),
            Some("".to_string()), // Empty client ID (should be auto-generated)
            None, // No client ID (should be auto-generated)
        ];

        for client_id in client_id_cases {
            let result = MQTTAckSubscriber::new(broker_url, client_id.clone()).await;
            // All should fail due to no broker, but shouldn't panic on client ID handling
            assert!(result.is_err(), "Expected connection error for client ID: {:?}", client_id);
        }
    }

    #[test]
    fn test_mqtt_broker_url_validation() {
        // Test various broker URL formats
        let valid_broker_urls = vec![
            "localhost",
            "127.0.0.1",
            "mqtt.example.com",
            "192.168.1.100",
            "broker.hivemq.com",
        ];

        // These should be valid format-wise (though may not connect)
        for broker_url in valid_broker_urls {
            assert!(!broker_url.is_empty());
            // Basic format validation - should not contain invalid characters
            assert!(!broker_url.contains('\0'));
            assert!(!broker_url.contains('\n'));
        }
    }

    #[test]
    fn test_mqtt_client_id_generation() {
        // Test client ID generation logic
        let client_ids = vec![
            Some("custom-client-123".to_string()),
            Some("device-sensor-01".to_string()),
            Some("analytics-consumer".to_string()),
        ];

        for client_id in client_ids {
            if let Some(id) = &client_id {
                // Client ID should be reasonable length and format
                assert!(!id.is_empty() || id.is_empty()); // Allow empty for auto-generation
                assert!(id.len() < 256); // Reasonable length limit
                assert!(!id.contains('\0')); // No null characters
            }
        }
    }
}

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

    #[tokio::test]
    async fn test_mqtt_connection_timeout() {
        // Test connection to a non-existent host (should timeout quickly)
        let broker_url = "192.0.2.1"; // RFC5737 test address
        let client_id = Some("timeout-test".to_string());
        
        let start_time = std::time::Instant::now();
        let result = MQTTAckSubscriber::new(broker_url, client_id).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_mqtt_invalid_configurations() {
        let invalid_configs = vec![
            ("", Some("client".to_string())), // Empty broker URL
            ("invalid\0host", Some("client".to_string())), // Null character in host
        ];

        for (broker_url, client_id) in invalid_configs {
            let result = MQTTAckSubscriber::new(broker_url, client_id.clone()).await;
            assert!(result.is_err(), "Expected error for broker: {}, client: {:?}", broker_url, client_id);
        }
    }
}

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

    #[test]
    fn test_message_to_mqtt_handle_conversion() {
        // Test the logic that would convert an MQTT message to our handle
        let message = Message::new(b"sensor data".to_vec())
            .with_metadata("qos", "1")
            .with_metadata("packet_id", "123")
            .with_metadata("delivery_count", "1")
            .with_metadata("topic", "sensors/temperature");

        // Simulate extracting MQTT-specific information
        let qos_level: u8 = message.metadata
            .get("qos")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        
        let qos = match qos_level {
            0 => QoS::AtMostOnce,
            1 => QoS::AtLeastOnce,
            2 => QoS::ExactlyOnce,
            _ => QoS::AtMostOnce,
        };
        
        let packet_id: Option<u16> = message.metadata
            .get("packet_id")
            .and_then(|s| s.parse().ok());
        
        let delivery_count: u32 = message.metadata
            .get("delivery_count")
            .and_then(|s| s.parse().ok())
            .unwrap_or(1);
        
        let topic = message.metadata
            .get("topic")
            .unwrap_or(&"default".to_string())
            .clone();

        assert_eq!(qos, QoS::AtLeastOnce);
        assert_eq!(packet_id, Some(123));
        assert_eq!(delivery_count, 1);
        assert_eq!(topic, "sensors/temperature");

        // Create handle with extracted information
        let handle = MQTTAckHandle::new(
            message.uuid.clone(),
            topic,
            qos,
            packet_id,
            delivery_count,
            SystemTime::now(),
        );

        assert_eq!(handle.message_id(), message.uuid);
        assert_eq!(handle.qos(), qos);
        assert_eq!(handle.packet_id(), packet_id);
        assert_eq!(handle.delivery_count(), delivery_count);
        assert!(!handle.is_retry()); // delivery_count == 1
        assert!(handle.requires_ack()); // QoS 1 requires ack
    }

    #[test]
    fn test_qos_acknowledgment_requirements() {
        let message_id = "qos-ack-test".to_string();
        let topic = "test/qos".to_string();
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        let qos_ack_cases = vec![
            (QoS::AtMostOnce, None, false),      // QoS 0 - fire and forget
            (QoS::AtLeastOnce, Some(100), true), // QoS 1 - at least once
            (QoS::ExactlyOnce, Some(200), true), // QoS 2 - exactly once
        ];

        for (qos, packet_id, should_ack) in qos_ack_cases {
            let handle = MQTTAckHandle::new(
                message_id.clone(),
                topic.clone(),
                qos,
                packet_id,
                delivery_count,
                timestamp,
            );

            assert_eq!(handle.requires_ack(), should_ack);
            
            if should_ack {
                assert!(packet_id.is_some(), "QoS {} should have packet ID", qos as u8);
            } else {
                assert!(packet_id.is_none(), "QoS {} should not have packet ID", qos as u8);
            }
        }
    }

    #[test]
    fn test_topic_pattern_handling() {
        // Test various MQTT topic patterns
        let topic_patterns = vec![
            "sensors/temperature",
            "home/+/temperature",      // Single-level wildcard
            "sensors/#",               // Multi-level wildcard
            "building/floor1/room2/sensor3",
            "$SYS/broker/uptime",      // System topic
        ];

        for topic in topic_patterns {
            let handle = MQTTAckHandle::new("topic-test".to_string(), topic.to_string(), SystemTime::now(), 1, QoS::AtLeastOnce, Some(1),
            );

            assert_eq!(handle.topic(), topic);
            assert!(!handle.topic().is_empty());
        }
    }

    #[test]
    fn test_packet_id_range_handling() {
        // Test packet ID range (1-65535 for MQTT)
        let message_id = "packet-range-test".to_string();
        let topic = "test/packet".to_string();
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        let packet_id_cases = vec![
            1,      // Minimum valid packet ID
            100,    // Low range
            32768,  // Mid range
            65535,  // Maximum valid packet ID
        ];

        for packet_id in packet_id_cases {
            let handle = MQTTAckHandle::new(
                message_id.clone(),
                topic.clone(),
                QoS::AtLeastOnce,
                Some(packet_id),
                delivery_count,
                timestamp,
            );

            assert_eq!(handle.packet_id(), Some(packet_id));
            assert!(handle.requires_ack());
        }
    }

    #[test]
    fn test_delivery_count_tracking() {
        // Test delivery count tracking for retry logic
        let message_id = "delivery-count-test".to_string();
        let topic = "test/delivery".to_string();
        let qos = QoS::AtLeastOnce;
        let packet_id = Some(500);

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

        for (count, expected_retry) in deliveries {
            let handle = MQTTAckHandle::new(
                message_id.clone(),
                topic.clone(),
                qos,
                packet_id,
                count,
                SystemTime::now(),
            );

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

    #[test]
    fn test_persistent_session_logic() {
        // Test logic related to persistent sessions
        let client_ids = vec![
            Some("persistent-client-1".to_string()),
            Some("temp-client-2".to_string()),
            None, // Auto-generated client ID
        ];

        for client_id in client_ids {
            // Test client ID handling for persistent sessions
            if let Some(id) = &client_id {
                assert!(!id.is_empty() || id.is_empty()); // Allow empty for auto-gen
                assert!(id.len() < 256); // Reasonable length
            }
            
            // In a real implementation, we would test:
            // - Clean session flag handling
            // - Message persistence across reconnections
            // - Subscription persistence
        }
    }
}

#[cfg(test)]
mod mqtt_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 qos = match i % 3 {
                0 => QoS::AtMostOnce,
                1 => QoS::AtLeastOnce,
                2 => QoS::ExactlyOnce,
                _ => QoS::AtMostOnce,
            };
            
            let packet_id = if qos != QoS::AtMostOnce {
                Some((i % 65535 + 1) as u16)
            } else {
                None
            };

            let _handle = MQTTAckHandle::new(
                format!("message-{}", i),
                format!("topic/{}", i % 10),
                qos,
                packet_id,
                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 = MQTTAckHandle::new("perf-test".to_string(), "test/performance".to_string(), SystemTime::now(), 3, QoS::ExactlyOnce, Some(12345),
        );

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

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

        let elapsed = start.elapsed();
        let per_call = elapsed / (iterations * 9); // 9 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_qos_determination_performance() {
        let qos_levels = vec![
            QoS::AtMostOnce,
            QoS::AtLeastOnce,
            QoS::ExactlyOnce,
        ];

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

        for i in 0..iterations {
            let qos = &qos_levels[i % 3];
            
            // Simulate QoS-based logic
            let requires_ack = match qos {
                QoS::AtMostOnce => false,
                QoS::AtLeastOnce | QoS::ExactlyOnce => true,
            };
            
            let packet_id = if requires_ack {
                Some((i % 65535 + 1) as u16)
            } else {
                None
            };

            // Use the values to prevent optimization
            assert!(packet_id.is_some() == requires_ack);
        }

        let elapsed = start.elapsed();
        let per_iteration = elapsed / iterations as u32;

        // QoS determination should be extremely fast
        assert!(per_iteration < Duration::from_nanos(100), 
               "QoS determination took too long: {:?} per iteration", per_iteration);
    }
}