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
//! Unit tests for Kafka acknowledgment backend
//!
//! These tests validate the Kafka acknowledgment implementation without requiring
//! an actual Kafka broker connection. They focus on testing the logic, data structures,
//! offset management, and error handling of the acknowledgment system.

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

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

    #[test]
    fn test_kafka_ack_handle_creation() {
        let message_id = "test-message-123".to_string();
        let topic = "test-topic".to_string();
        let partition = 2;
        let offset = 12345;
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        let handle = KafkaAckHandle::new(
            message_id.clone(),
            topic.clone(),
            partition,
            offset,
            delivery_count,
            timestamp,
        );

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

    #[test]
    fn test_kafka_ack_handle_retry_detection() {
        let message_id = "test-message-retry".to_string();
        let topic = "test-topic".to_string();
        let partition = 0;
        let offset = 100;
        let timestamp = SystemTime::now();

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

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

        // Multiple retries
        let handle_multiple_retry = KafkaAckHandle::new(
            message_id,
            topic,
            partition,
            offset,
            5, // delivery_count = 5
            timestamp,
        );
        assert!(handle_multiple_retry.is_retry());
    }

    #[test]
    fn test_kafka_ack_handle_uniqueness() {
        let message_id = "test-message-unique".to_string();
        let topic = "test-topic".to_string();
        let partition = 1;
        let offset = 999;
        let delivery_count = 1;
        let timestamp = SystemTime::now();

        let handle1 = KafkaAckHandle::new(
            message_id.clone(),
            topic.clone(),
            partition,
            offset,
            delivery_count,
            timestamp,
        );

        let handle2 = KafkaAckHandle::new(
            message_id,
            topic,
            partition,
            offset,
            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_kafka_ack_handle_partition_offset_combinations() {
        let message_id = "test-message".to_string();
        let topic = "test-topic".to_string();
        let timestamp = SystemTime::now();

        let test_cases = vec![
            (0, 0),      // Partition 0, offset 0
            (0, 1000),   // Partition 0, high offset
            (5, 0),      // High partition, offset 0
            (10, 5000),  // High partition, high offset
            (255, u64::MAX), // Edge case: max values
        ];

        for (partition, offset) in test_cases {
            let handle = KafkaAckHandle::new(
                message_id.clone(),
                topic.clone(),
                partition,
                offset,
                1,
                timestamp,
            );

            assert_eq!(handle.partition(), partition);
            assert_eq!(handle.offset(), offset);
            assert_eq!(handle.topic(), topic);
        }
    }

    #[test]
    fn test_kafka_ack_handle_properties() {
        let message_id = "test-message-props".to_string();
        let topic = "test-topic-props".to_string();
        let partition = 3;
        let offset = 7777;
        let delivery_count = 4;
        let timestamp = SystemTime::now();

        let handle = KafkaAckHandle::new(
            message_id.clone(),
            topic.clone(),
            partition,
            offset,
            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 Kafka-specific methods
        assert_eq!(handle.partition(), partition);
        assert_eq!(handle.offset(), offset);
        assert!(!handle.handle_id().is_empty());
    }

    #[test]
    fn test_kafka_ack_handle_edge_cases() {
        let message_id = "".to_string(); // Empty message ID
        let topic = "".to_string(); // Empty topic
        let partition = 0;
        let offset = 0;
        let delivery_count = 0; // Zero delivery count
        let timestamp = SystemTime::now();

        let handle = KafkaAckHandle::new(
            message_id.clone(),
            topic.clone(),
            partition,
            offset,
            delivery_count,
            timestamp,
        );

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

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

    #[tokio::test]
    async fn test_kafka_ack_subscriber_creation() {
        let brokers = vec!["localhost:9092".to_string()];
        let group_id = "test-group".to_string();
        
        // Test subscriber creation (this should not fail even without connection)
        let result = KafkaAckSubscriber::new(brokers, group_id).await;
        
        // We expect this to fail since we don't have a Kafka 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("broker") ||
                error.to_string().contains("timeout") ||
                error.to_string().contains("resolve"));
    }

    #[tokio::test]
    async fn test_kafka_ack_subscriber_invalid_brokers() {
        let invalid_broker_configs = vec![
            (vec![], "empty-group".to_string()), // Empty brokers list
            (vec!["".to_string()], "test-group".to_string()), // Empty broker string
            (vec!["invalid-broker".to_string()], "test-group".to_string()), // Invalid broker format
            (vec!["localhost:99999".to_string()], "test-group".to_string()), // Invalid port
        ];

        for (brokers, group_id) in invalid_broker_configs {
            let result = KafkaAckSubscriber::new(brokers.clone(), group_id.clone()).await;
            assert!(result.is_err(), "Expected error for brokers: {:?}, group: {}", brokers, group_id);
        }
    }

    #[tokio::test]
    async fn test_kafka_ack_subscriber_invalid_group_id() {
        let brokers = vec!["localhost:9092".to_string()];
        let invalid_group_ids = vec![
            "".to_string(), // Empty group ID
            // Note: Kafka allows most characters in group IDs, so we mainly test empty
        ];

        for group_id in invalid_group_ids {
            let result = KafkaAckSubscriber::new(brokers.clone(), group_id.clone()).await;
            assert!(result.is_err(), "Expected error for group ID: {}", group_id);
        }
    }

    #[test]
    fn test_kafka_broker_string_validation() {
        // Test various broker string formats
        let valid_broker_strings = vec![
            "localhost:9092",
            "127.0.0.1:9092",
            "kafka.example.com:9092",
            "kafka1.cluster.local:9092",
            "10.0.0.1:9092",
        ];

        // These should be valid format-wise (though may not connect)
        for broker_string in valid_broker_strings {
            assert!(!broker_string.is_empty());
            assert!(broker_string.contains(':'));
            
            let parts: Vec<&str> = broker_string.split(':').collect();
            assert_eq!(parts.len(), 2);
            assert!(!parts[0].is_empty()); // Host part
            assert!(parts[1].parse::<u16>().is_ok()); // Port part should be valid number
        }
    }
}

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

    #[tokio::test]
    async fn test_kafka_connection_timeout() {
        // Test connection to a non-existent host (should timeout quickly)
        let brokers = vec!["192.0.2.1:9092".to_string()]; // RFC5737 test address
        let group_id = "timeout-test-group".to_string();
        
        let start_time = std::time::Instant::now();
        let result = KafkaAckSubscriber::new(brokers, group_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_kafka_malformed_broker_addresses() {
        let malformed_brokers = vec![
            vec!["not-a-broker".to_string()],
            vec!["localhost".to_string()], // Missing port
            vec![":9092".to_string()], // Missing host
            vec!["localhost:".to_string()], // Missing port number
            vec!["localhost:abc".to_string()], // Invalid port
        ];

        for brokers in malformed_brokers {
            let result = KafkaAckSubscriber::new(brokers.clone(), "test-group".to_string()).await;
            assert!(result.is_err(), "Expected error for malformed brokers: {:?}", brokers);
        }
    }
}

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

    #[test]
    fn test_message_to_kafka_handle_conversion() {
        // Test the logic that would convert a Kafka message to our handle
        let message = Message::new(b"test payload".to_vec())
            .with_metadata("partition", "2")
            .with_metadata("offset", "12345")
            .with_metadata("delivery_count", "1")
            .with_metadata("topic", "test-topic");

        // Simulate extracting Kafka-specific information
        let partition: i32 = message.metadata
            .get("partition")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        
        let offset: u64 = message.metadata
            .get("offset")
            .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 topic = message.metadata
            .get("topic")
            .unwrap_or(&"default".to_string())
            .clone();

        assert_eq!(partition, 2);
        assert_eq!(offset, 12345);
        assert_eq!(delivery_count, 1);
        assert_eq!(topic, "test-topic");

        // Create handle with extracted information
        let handle = KafkaAckHandle::new(
            message.uuid.clone(),
            topic,
            partition,
            offset,
            delivery_count,
            SystemTime::now(),
        );

        assert_eq!(handle.message_id(), message.uuid);
        assert_eq!(handle.partition(), partition);
        assert_eq!(handle.offset(), offset);
        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 Kafka offsets
        let handles = vec![
            KafkaAckHandle::new("msg1".to_string(), "topic".to_string(), 0, 100, 1, SystemTime::now()),
            KafkaAckHandle::new("msg2".to_string(), "topic".to_string(), 0, 101, 1, SystemTime::now()),
            KafkaAckHandle::new("msg3".to_string(), "topic".to_string(), 0, 102, 1, SystemTime::now()),
            KafkaAckHandle::new("msg4".to_string(), "topic".to_string(), 1, 200, 1, SystemTime::now()),
            KafkaAckHandle::new("msg5".to_string(), "topic".to_string(), 1, 201, 1, SystemTime::now()),
        ];

        // Group by partition and find highest offset per partition
        let mut partition_offsets = std::collections::HashMap::new();
        for handle in &handles {
            let current_max = partition_offsets.get(&handle.partition()).unwrap_or(&0);
            if handle.offset() > *current_max {
                partition_offsets.insert(handle.partition(), handle.offset());
            }
        }

        assert_eq!(partition_offsets.get(&0), Some(&102));
        assert_eq!(partition_offsets.get(&1), Some(&201));

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

    #[test]
    fn test_offset_management_logic() {
        // Test offset management for different scenarios
        let topic = "test-topic".to_string();
        let partition = 0;
        let timestamp = SystemTime::now();

        let test_cases = vec![
            // (offset, delivery_count, expected_commit_offset)
            (0, 1, 1),      // First message, commit offset 1
            (100, 1, 101),  // Message at offset 100, commit 101
            (999, 2, 1000), // Retry message, still commit next offset
        ];

        for (offset, delivery_count, expected_commit) in test_cases {
            let handle = KafkaAckHandle::new(
                format!("msg-{}", offset),
                topic.clone(),
                partition,
                offset,
                delivery_count,
                timestamp,
            );

            // The commit offset should be message offset + 1
            let commit_offset = handle.offset() + 1;
            assert_eq!(commit_offset, expected_commit);
        }
    }

    #[test]
    fn test_consumer_group_logic() {
        // Test consumer group related logic
        let group_ids = vec![
            "test-group-1".to_string(),
            "test-group-2".to_string(),
            "production-consumers".to_string(),
            "analytics-pipeline".to_string(),
        ];

        for group_id in group_ids {
            // Test that group IDs are preserved and valid
            assert!(!group_id.is_empty());
            assert!(!group_id.contains('\0')); // No null characters
            
            // Group IDs should be reasonable length
            assert!(group_id.len() < 256);
        }
    }

    #[test]
    fn test_partition_assignment_logic() {
        // Test partition assignment and handling logic
        let topic = "multi-partition-topic".to_string();
        let partitions = vec![0, 1, 2, 3, 4]; // 5 partitions
        let timestamp = SystemTime::now();

        for partition in partitions {
            let handle = KafkaAckHandle::new(
                format!("msg-p{}", partition),
                topic.clone(),
                partition,
                1000 + partition as u64, // Different offset per partition
                1,
                timestamp,
            );

            assert_eq!(handle.partition(), partition);
            assert_eq!(handle.offset(), 1000 + partition as u64);
            assert_eq!(handle.topic(), topic);
        }
    }

    #[test]
    fn test_delivery_count_tracking() {
        // Test delivery count tracking for retry logic
        let message_id = "retry-test".to_string();
        let topic = "test-topic".to_string();
        let partition = 0;
        let offset = 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 = KafkaAckHandle::new(
                message_id.clone(),
                topic.clone(),
                partition,
                offset,
                count,
                SystemTime::now(),
            );

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

#[cfg(test)]
mod kafka_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 = KafkaAckHandle::new(
                format!("message-{}", i),
                "test-topic".to_string(),
                (i % 5) as i32, // Distribute across 5 partitions
                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 = KafkaAckHandle::new(
            "perf-test".to_string(),
            "test-topic".to_string(),
            2,
            12345,
            3,
            SystemTime::now(),
        );

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

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

        let elapsed = start.elapsed();
        let per_call = elapsed / (iterations * 8); // 8 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_offset_calculation_performance() {
        let handles: Vec<KafkaAckHandle> = (0..1000)
            .map(|i| KafkaAckHandle::new(
                format!("message-{}", i),
                "test-topic".to_string(),
                (i % 10) as i32, // 10 partitions
                i as u64,
                1,
                SystemTime::now(),
            ))
            .collect();

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

        // Simulate batch offset calculation
        let mut partition_offsets = std::collections::HashMap::new();
        for handle in &handles {
            let current_max = partition_offsets.get(&handle.partition()).unwrap_or(&0);
            if handle.offset() > *current_max {
                partition_offsets.insert(handle.partition(), handle.offset());
            }
        }

        let elapsed = start.elapsed();

        // Offset calculation should be fast
        assert!(elapsed < Duration::from_millis(10), 
               "Offset calculation took too long: {:?}", elapsed);
        
        // Verify results
        assert_eq!(partition_offsets.len(), 10); // 10 partitions
        for i in 0..10 {
            let expected_max = ((999 / 10) * 10 + i) as u64; // Highest offset for partition i
            assert!(partition_offsets.get(&(i as i32)).unwrap() >= &expected_max);
        }
    }
}