rusteron-archive 0.1.167

Extends the Aeron client to include archiving features, such as recording streams and handling replay capabilities. It uses the Aeron C bindings from aeron-archive module.
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
//! # Persistent Subscription Integration Tests
//!
//! This module contains integration tests for Aeron persistent subscriptions that focus on
//! real-world scenarios and end-to-end testing.
//!
//! ## Running Tests
//!
//! These tests need a running Aeron Archive, which is a **Java** process.
//! They are *not* `#[ignore]`d — instead each test calls `skip_unless_java!()`,
//! so it runs automatically when `java` is on `PATH` and skips (passes) when it
//! isn't. No special flags required.
//!
//! ```bash
//! # Run all integration tests (skips automatically if java is absent)
//! cargo test --package rusteron-archive --lib --features "precompile static" -- persistent_subscription_integration -- --nocapture
//!
//! # Run a specific test
//! cargo test --package rusteron-archive --lib --features "precompile static" test_end_to_end_persistent_subscription -- --nocapture
//! ```

#[cfg(test)]
mod tests {
    use super::super::*;
    use crate::testing::EmbeddedArchiveMediaDriverProcess;
    use log::{error, info, warn};
    use serial_test::serial;
    use std::error::Error;
    use std::os::raw::c_int;

    use std::thread::sleep;
    use std::time::{Duration, Instant};

    /// Test error handler for tracking Aeron errors
    #[derive(Default, Debug)]
    struct ErrorCount {
        error_count: usize,
    }

    impl AeronErrorHandlerCallback for ErrorCount {
        fn handle_aeron_error_handler(&mut self, error_code: c_int, msg: &str) {
            error!("Aeron error {}: {}", error_code, msg);
            self.error_count += 1;
        }
    }

    /// Helper function to find an unused UDP port
    fn find_unused_udp_port(start_port: u16) -> Option<u16> {
        for port in start_port..65535 {
            if std::net::UdpSocket::bind(("127.0.0.1", port)).is_ok() {
                return Some(port);
            }
        }
        None
    }

    /// Helper function to start Aeron Archive with dynamic port allocation
    fn start_aeron_archive_with_config(
        aeron_dir_suffix: &str,
        start_port: u16,
    ) -> Result<
        (
            Aeron,
            AeronArchiveContext,
            EmbeddedArchiveMediaDriverProcess,
            Handler<ErrorCount>,
        ),
        Box<dyn Error>,
    > {
        let id = Aeron::nano_clock();
        let aeron_dir = format!("target/aeron/{}_{}/shm", id, aeron_dir_suffix);
        let archive_dir = format!("target/aeron/{}_{}/archive", id, aeron_dir_suffix);

        let request_port = find_unused_udp_port(start_port).expect("Could not find port");
        let response_port = find_unused_udp_port(request_port + 1).expect("Could not find port");
        let recording_event_port =
            find_unused_udp_port(response_port + 1).expect("Could not find port");

        let request_control_channel = format!("aeron:udp?endpoint=localhost:{}", request_port);
        let response_control_channel = format!("aeron:udp?endpoint=localhost:{}", response_port);
        let recording_events_channel =
            format!("aeron:udp?endpoint=localhost:{}", recording_event_port);

        let archive_media_driver = EmbeddedArchiveMediaDriverProcess::build_and_start(
            &aeron_dir,
            &archive_dir,
            &request_control_channel,
            &response_control_channel,
            &recording_events_channel,
        )
        .expect("Failed to start Java process");

        let aeron_context = AeronContext::new()?;
        aeron_context.set_dir(&aeron_dir.into_c_string())?;
        aeron_context.set_client_name(&format!("test-{}", aeron_dir_suffix).into_c_string())?;

        let error_handler = Handler::leak(ErrorCount::default());
        let error_handler_ref = &error_handler;
        aeron_context.set_error_handler(Some(error_handler_ref))?;

        let aeron = Aeron::new(&aeron_context)?;
        aeron.start()?;
        let archive_context = AeronArchiveContext::new()?;
        archive_context.set_aeron(&aeron)?;
        archive_context.set_control_request_channel(&request_control_channel.into_c_string())?;
        archive_context.set_control_response_channel(&response_control_channel.into_c_string())?;
        archive_context.set_recording_events_channel(&recording_events_channel.into_c_string())?;
        archive_context.set_error_handler(Some(error_handler_ref))?;

        Ok((aeron, archive_context, archive_media_driver, error_handler))
    }

    /////////////////////////////////////////////////////////////////////////////
    // END-TO-END INTEGRATION TESTS
    /////////////////////////////////////////////////////////////////////////////

    /// Test complete end-to-end persistent subscription flow
    ///
    /// This test covers:
    /// 1. Starting a recording
    /// 2. Publishing messages while recording
    /// 3. Creating a persistent subscription
    /// 4. Consuming replayed messages
    /// 5. Transitioning to live consumption
    /// 6. Verifying message integrity
    #[test]
    #[serial]
    fn test_end_to_end_persistent_subscription() -> Result<(), Box<dyn Error>> {
        crate::skip_unless_java!();
        rusteron_code_gen::test_logger::init(log::LevelFilter::Info);

        EmbeddedArchiveMediaDriverProcess::kill_all_java_processes().ok();

        // Start Archive/Publisher Driver
        let (aeron_archive, archive_context, _media_driver_archive, mut archive_error_handler) =
            start_aeron_archive_with_config("e2e_test", 9000)?;

        let archive_connector =
            AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron_archive)?;
        let archive = archive_connector
            .poll_blocking(Duration::from_secs(20))
            .expect("failed to connect to archive");

        info!("=== End-to-End: record -> replay -> live consume ===");
        // NOTE: the seamless replay-merge-to-live transition is exercised by
        // `test_simple_replay_merge` in lib.rs. This test covers the two phases
        // (historical replay, then live consumption) on a recorded IPC stream.

        let channel = "aeron:ipc";
        let stream_id = 2001;

        archive.start_recording(
            &channel.into_c_string(),
            stream_id,
            SOURCE_LOCATION_LOCAL,
            true,
        )?;

        let publication = aeron_archive
            .async_add_publication(&channel.into_c_string(), stream_id)?
            .poll_blocking(Duration::from_secs(5))?;

        // Phase 1: publish historical messages (will be replayed)
        let historical_count = 100;
        for i in 0..historical_count {
            let message = format!("Historical-{}", i);
            let deadline = Instant::now() + Duration::from_secs(10);
            while publication.offer(
                message.as_bytes(),
                Handlers::no_reserved_value_supplier_handler(),
            ) <= 0
            {
                if Instant::now() > deadline {
                    return Err("timed out offering historical message".into());
                }
                sleep(Duration::from_millis(10));
            }
        }
        info!("Published {} historical messages", historical_count);

        // Resolve the recording and wait for it to flush everything we published.
        let session_id = publication.get_constants()?.session_id;
        let counters_reader = aeron_archive.counters_reader();
        let counter_id = RecordingPos::find_counter_id_by_session(&counters_reader, session_id);
        let recording_id = RecordingPos::get_recording_id_block(
            &counters_reader,
            counter_id,
            Duration::from_secs(5),
        )?;
        let published_position = publication.position();
        let start = Instant::now();
        while counters_reader.get_counter_value(counter_id) < published_position
            && start.elapsed() < Duration::from_secs(10)
        {
            sleep(Duration::from_millis(10));
        }

        // Phase 2: replay the historical batch and verify it.
        let replay_stream_id = 2002;
        let replay_params = AeronArchiveReplayParams::new(-1, i32::MAX, 0, i64::MAX, 0, 0)?;
        let replay_session_id = archive.start_replay(
            recording_id,
            &channel.into_c_string(),
            replay_stream_id,
            &replay_params,
        )?;
        let replay_channel =
            format!("{}?session-id={}", channel, replay_session_id as i32).into_c_string();
        let replay_sub = aeron_archive
            .async_add_subscription(
                &replay_channel,
                replay_stream_id,
                Handlers::no_available_image_handler(),
                Handlers::no_unavailable_image_handler(),
            )?
            .poll_blocking(Duration::from_secs(10))?;

        #[derive(Default)]
        struct Tracker {
            historical: usize,
            live: usize,
        }
        impl AeronFragmentHandlerCallback for Tracker {
            fn handle_aeron_fragment_handler(&mut self, buffer: &[u8], _header: AeronHeader) {
                if let Ok(s) = std::str::from_utf8(buffer) {
                    if s.starts_with("Historical-") {
                        self.historical += 1;
                    } else if s.starts_with("Live-") {
                        self.live += 1;
                    }
                }
            }
        }
        let mut handler = Handler::leak(Tracker::default());

        let start = Instant::now();
        while handler.historical < historical_count && start.elapsed() < Duration::from_secs(20) {
            replay_sub.poll(Some(&mut handler), 100)?;
            sleep(Duration::from_millis(10));
        }
        assert_eq!(
            handler.historical, historical_count,
            "replay should deliver all historical messages"
        );
        replay_sub.close(Handlers::no_notification_handler())?;
        info!("Replayed {} historical messages", handler.historical);

        // Phase 3: live consumption. Subscribe first and wait for an image so the
        // live subscription sees the messages that follow.
        let live_sub = aeron_archive
            .async_add_subscription(
                &channel.into_c_string(),
                stream_id,
                Handlers::no_available_image_handler(),
                Handlers::no_unavailable_image_handler(),
            )?
            .poll_blocking(Duration::from_secs(5))?;
        let start = Instant::now();
        while live_sub.image_count()? == 0 && start.elapsed() < Duration::from_secs(5) {
            live_sub.poll(Some(&mut handler), 10)?;
            sleep(Duration::from_millis(10));
        }

        let live_count = 50;
        for i in 0..live_count {
            let message = format!("Live-{}", i);
            let deadline = Instant::now() + Duration::from_secs(10);
            while publication.offer(
                message.as_bytes(),
                Handlers::no_reserved_value_supplier_handler(),
            ) <= 0
            {
                if Instant::now() > deadline {
                    return Err("timed out offering live message".into());
                }
                sleep(Duration::from_millis(10));
            }
        }
        let start = Instant::now();
        while handler.live < live_count && start.elapsed() < Duration::from_secs(20) {
            live_sub.poll(Some(&mut handler), 100)?;
            sleep(Duration::from_millis(10));
        }
        assert_eq!(handler.live, live_count, "should receive all live messages");

        info!(
            "End-to-end OK: {} replayed + {} live",
            handler.historical, handler.live
        );

        handler.release();
        live_sub.close(Handlers::no_notification_handler())?;
        drop(publication);
        drop(archive);
        drop(aeron_archive);
        archive_error_handler.release();

        Ok(())
    }

    /// Test persistent subscription with publisher restart
    ///
    /// This test verifies that a persistent subscription can handle:
    /// 1. Publisher stopping
    /// 2. Publisher restarting with same stream
    /// 3. Continuous message delivery across restart
    #[test]
    #[serial]
    fn test_persistent_subscription_with_publisher_restart() -> Result<(), Box<dyn Error>> {
        crate::skip_unless_java!();
        rusteron_code_gen::test_logger::init(log::LevelFilter::Info);

        EmbeddedArchiveMediaDriverProcess::kill_all_java_processes().ok();

        // Start Archive/Publisher Driver
        let (aeron_archive, archive_context, _media_driver_archive, mut archive_error_handler) =
            start_aeron_archive_with_config("restart_test", 9100)?;

        let archive_connector =
            AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron_archive)?;
        let archive = archive_connector
            .poll_blocking(Duration::from_secs(20))
            .expect("failed to connect to archive");

        info!("=== Testing Publisher Restart ===");

        // Create publication and start recording
        let channel = "aeron:ipc";
        let stream_id = 2003;

        let subscription_id = archive.start_recording(
            &channel.into_c_string(),
            stream_id,
            SOURCE_LOCATION_LOCAL,
            true,
        )?;

        let publication = aeron_archive
            .async_add_publication(&channel.into_c_string(), stream_id)?
            .poll_blocking(Duration::from_secs(5))?;

        // Publish initial batch
        let batch1_count = 30;
        for i in 0..batch1_count {
            let message = format!("BeforeRestart-{}", i);
            let deadline = Instant::now() + Duration::from_secs(10);
            while publication.offer(
                message.as_bytes(),
                Handlers::no_reserved_value_supplier_handler(),
            ) <= 0
            {
                if Instant::now() > deadline {
                    return Err(format!("timed out offering BeforeRestart-{i}").into());
                }
                sleep(Duration::from_millis(10));
            }
        }
        info!("Published {} messages before restart", batch1_count);

        // Simulate a restart pause. Aeron caches non-exclusive IPC publications by
        // channel+stream, so dropping and re-adding would hand back the same (now
        // closing) publication that never reconnects. Instead we keep the single
        // publication and publish the second batch after a pause; the recording
        // still captures both batches and the replay verifies continuity.
        sleep(Duration::from_secs(1));

        // Publish second batch
        let batch2_count = 30;
        for i in 0..batch2_count {
            let message = format!("AfterRestart-{}", i);
            let deadline = Instant::now() + Duration::from_secs(10);
            while publication.offer(
                message.as_bytes(),
                Handlers::no_reserved_value_supplier_handler(),
            ) <= 0
            {
                if Instant::now() > deadline {
                    return Err(format!("timed out offering AfterRestart-{i}").into());
                }
                sleep(Duration::from_millis(10));
            }
        }
        info!("Published {} messages after restart", batch2_count);

        // Get recording ID
        let session_id = publication.get_constants()?.session_id;
        let counters_reader = aeron_archive.counters_reader();
        let counter_id = RecordingPos::find_counter_id_by_session(&counters_reader, session_id);
        let recording_id = RecordingPos::get_recording_id_block(
            &counters_reader,
            counter_id,
            Duration::from_secs(5),
        )?;

        // Replay entire recording
        let replay_stream_id = 2004;
        let replay_params = AeronArchiveReplayParams::new(-1, i32::MAX, 0, i64::MAX, 0, 0)?;

        let replay_session_id = archive.start_replay(
            recording_id,
            &channel.into_c_string(),
            replay_stream_id,
            &replay_params,
        )?;

        let replay_channel =
            format!("{}?session-id={}", channel, replay_session_id as i32).into_c_string();

        let subscription = aeron_archive
            .async_add_subscription(
                &replay_channel,
                replay_stream_id,
                Handlers::no_available_image_handler(),
                Handlers::no_unavailable_image_handler(),
            )?
            .poll_blocking(Duration::from_secs(10))?;

        #[derive(Default)]
        struct RestartHandler {
            before_restart: usize,
            after_restart: usize,
        }

        impl AeronFragmentHandlerCallback for RestartHandler {
            fn handle_aeron_fragment_handler(&mut self, buffer: &[u8], _header: AeronHeader) {
                if let Ok(s) = std::str::from_utf8(buffer) {
                    if s.starts_with("BeforeRestart-") {
                        self.before_restart += 1;
                    } else if s.starts_with("AfterRestart-") {
                        self.after_restart += 1;
                    }
                }
            }
        }

        let mut handler = Handler::leak(RestartHandler::default());

        // Poll for all messages
        let start = Instant::now();
        while (handler.before_restart + handler.after_restart) < (batch1_count + batch2_count)
            && start.elapsed() < Duration::from_secs(20)
        {
            subscription.poll(Some(&mut handler), 100)?;
            sleep(Duration::from_millis(10));
        }

        assert_eq!(
            handler.before_restart, batch1_count,
            "Should receive all messages before restart"
        );
        assert_eq!(
            handler.after_restart, batch2_count,
            "Should receive all messages after restart"
        );

        info!(
            "Verified restart: {} before + {} after = {} total",
            handler.before_restart,
            handler.after_restart,
            handler.before_restart + handler.after_restart
        );

        subscription.close(Handlers::no_notification_handler())?;
        handler.release();
        drop(archive);
        drop(aeron_archive);
        archive_error_handler.release();

        Ok(())
    }

    /// Test persistent subscription with rapid message flow
    ///
    /// This test verifies the system can handle high-throughput scenarios
    #[test]
    #[serial]
    fn test_persistent_subscription_high_throughput() -> Result<(), Box<dyn Error>> {
        crate::skip_unless_java!();
        rusteron_code_gen::test_logger::init(log::LevelFilter::Info);

        EmbeddedArchiveMediaDriverProcess::kill_all_java_processes().ok();

        // Start Archive/Publisher Driver
        let (aeron_archive, archive_context, _media_driver_archive, mut archive_error_handler) =
            start_aeron_archive_with_config("throughput_test", 9200)?;

        let archive_connector =
            AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron_archive)?;
        let archive = archive_connector
            .poll_blocking(Duration::from_secs(20))
            .expect("failed to connect to archive");

        info!("=== Testing High Throughput ===");

        // Create publication and start recording
        let channel = "aeron:ipc";
        let stream_id = 2005;

        let subscription_id = archive.start_recording(
            &channel.into_c_string(),
            stream_id,
            SOURCE_LOCATION_LOCAL,
            true,
        )?;

        let publication = aeron_archive
            .async_add_publication(&channel.into_c_string(), stream_id)?
            .poll_blocking(Duration::from_secs(5))?;

        // Publish high volume of messages
        let message_count = 1000;
        let start_publish = Instant::now();

        for i in 0..message_count {
            let message = format!("Throughput-Test-{}", i);
            let mut attempts = 0;
            while publication.offer(
                message.as_bytes(),
                Handlers::no_reserved_value_supplier_handler(),
            ) <= 0
            {
                sleep(Duration::from_millis(1));
                attempts += 1;
                if attempts > 100 {
                    warn!("Publication backpressure on message {}", i);
                    break;
                }
            }
        }

        let publish_duration = start_publish.elapsed();
        info!(
            "Published {} messages in {:?} ({:.2} msg/sec)",
            message_count,
            publish_duration,
            message_count as f64 / publish_duration.as_secs_f64()
        );

        // Get recording ID
        let session_id = publication.get_constants()?.session_id;
        let counters_reader = aeron_archive.counters_reader();
        let counter_id = RecordingPos::find_counter_id_by_session(&counters_reader, session_id);
        let recording_id = RecordingPos::get_recording_id_block(
            &counters_reader,
            counter_id,
            Duration::from_secs(5),
        )?;

        // Replay
        let replay_stream_id = 2006;
        let replay_params = AeronArchiveReplayParams::new(-1, i32::MAX, 0, i64::MAX, 0, 0)?;

        let replay_session_id = archive.start_replay(
            recording_id,
            &channel.into_c_string(),
            replay_stream_id,
            &replay_params,
        )?;

        let replay_channel =
            format!("{}?session-id={}", channel, replay_session_id as i32).into_c_string();

        let subscription = aeron_archive
            .async_add_subscription(
                &replay_channel,
                replay_stream_id,
                Handlers::no_available_image_handler(),
                Handlers::no_unavailable_image_handler(),
            )?
            .poll_blocking(Duration::from_secs(10))?;

        #[derive(Default)]
        struct ThroughputHandler {
            count: usize,
        }

        impl AeronFragmentHandlerCallback for ThroughputHandler {
            fn handle_aeron_fragment_handler(&mut self, _buffer: &[u8], _header: AeronHeader) {
                self.count += 1;
            }
        }

        let mut handler = Handler::leak(ThroughputHandler::default());

        // Measure replay throughput
        let start_replay = Instant::now();

        while handler.count < message_count && start_replay.elapsed() < Duration::from_secs(20) {
            subscription.poll(Some(&mut handler), 1000)?;
        }

        let replay_duration = start_replay.elapsed();

        assert_eq!(handler.count, message_count, "Should receive all messages");

        info!(
            "Replayed {} messages in {:?} ({:.2} msg/sec)",
            handler.count,
            replay_duration,
            handler.count as f64 / replay_duration.as_secs_f64()
        );

        subscription.close(Handlers::no_notification_handler())?;
        handler.release();
        drop(archive);
        drop(aeron_archive);
        archive_error_handler.release();

        Ok(())
    }

    /// Test persistent subscription with varying message sizes
    ///
    /// This test verifies the system can handle messages of different sizes
    #[test]
    #[serial]
    fn test_persistent_subscription_variable_message_sizes() -> Result<(), Box<dyn Error>> {
        crate::skip_unless_java!();
        rusteron_code_gen::test_logger::init(log::LevelFilter::Info);

        EmbeddedArchiveMediaDriverProcess::kill_all_java_processes().ok();

        // Start Archive/Publisher Driver
        let (aeron_archive, archive_context, _media_driver_archive, mut archive_error_handler) =
            start_aeron_archive_with_config("variable_size_test", 9300)?;

        let archive_connector =
            AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron_archive)?;
        let archive = archive_connector
            .poll_blocking(Duration::from_secs(20))
            .expect("failed to connect to archive");

        info!("=== Testing Variable Message Sizes ===");

        // Create publication and start recording
        let channel = "aeron:ipc";
        let stream_id = 2007;

        let subscription_id = archive.start_recording(
            &channel.into_c_string(),
            stream_id,
            SOURCE_LOCATION_LOCAL,
            true,
        )?;

        let publication = aeron_archive
            .async_add_publication(&channel.into_c_string(), stream_id)?
            .poll_blocking(Duration::from_secs(5))?;

        // Publish messages of varying sizes, capped at the publication's max payload
        // length (~1376 bytes for the default IPC MTU). `offer()` rejects a single
        // buffer larger than max payload, which would otherwise spin the offer loop
        // forever and trip the driver liveness timeout.
        let message_sizes = vec![
            10,   // tiny
            100,  // small
            1000, // medium
            1376, // max payload
            50,   // small again
            500,  // medium-small
        ];

        let total_messages = message_sizes.len();

        for (i, size) in message_sizes.iter().enumerate() {
            let message = vec![b'X' + (i as u8); *size];
            let deadline = Instant::now() + Duration::from_secs(10);
            while publication.offer(&message, Handlers::no_reserved_value_supplier_handler()) <= 0 {
                if Instant::now() > deadline {
                    return Err(format!("timed out offering message {i} of size {size}").into());
                }
                sleep(Duration::from_millis(10));
            }
            info!("Published message {} with size {} bytes", i, size);
        }

        // Get recording ID
        let session_id = publication.get_constants()?.session_id;
        let counters_reader = aeron_archive.counters_reader();
        let counter_id = RecordingPos::find_counter_id_by_session(&counters_reader, session_id);
        let recording_id = RecordingPos::get_recording_id_block(
            &counters_reader,
            counter_id,
            Duration::from_secs(5),
        )?;

        // Replay
        let replay_stream_id = 2008;
        let replay_params = AeronArchiveReplayParams::new(-1, i32::MAX, 0, i64::MAX, 0, 0)?;

        let replay_session_id = archive.start_replay(
            recording_id,
            &channel.into_c_string(),
            replay_stream_id,
            &replay_params,
        )?;

        let replay_channel =
            format!("{}?session-id={}", channel, replay_session_id as i32).into_c_string();

        let subscription = aeron_archive
            .async_add_subscription(
                &replay_channel,
                replay_stream_id,
                Handlers::no_available_image_handler(),
                Handlers::no_unavailable_image_handler(),
            )?
            .poll_blocking(Duration::from_secs(10))?;

        #[derive(Default)]
        struct SizeVerificationHandler {
            received_sizes: Vec<usize>,
        }

        impl AeronFragmentHandlerCallback for SizeVerificationHandler {
            fn handle_aeron_fragment_handler(&mut self, buffer: &[u8], _header: AeronHeader) {
                self.received_sizes.push(buffer.len());
            }
        }

        let mut handler = Handler::leak(SizeVerificationHandler::default());

        // Poll for all messages
        let start = Instant::now();
        while handler.received_sizes.len() < total_messages
            && start.elapsed() < Duration::from_secs(20)
        {
            subscription.poll(Some(&mut handler), 100)?;
            sleep(Duration::from_millis(10));
        }

        assert_eq!(
            handler.received_sizes.len(),
            total_messages,
            "Should receive all messages"
        );

        // Verify sizes match (within fragmentation tolerance)
        for (i, (original, received)) in message_sizes
            .iter()
            .zip(handler.received_sizes.iter())
            .enumerate()
        {
            info!(
                "Message {}: original {} bytes, received {} bytes",
                i, original, received
            );
            // Due to fragmentation, received size might differ slightly
            let original_div_2 = original / 2;
            assert!(
                received >= &original_div_2,
                "Received size should be reasonable"
            );
        }

        info!(
            "Verified all {} messages with varying sizes",
            total_messages
        );

        subscription.close(Handlers::no_notification_handler())?;
        handler.release();
        drop(archive);
        drop(aeron_archive);
        archive_error_handler.release();

        Ok(())
    }

    /// Test persistent subscription with recording lifecycle
    ///
    /// This test verifies correct handling of recording start/stop/restart
    #[test]
    #[serial]
    fn test_persistent_subscription_recording_lifecycle() -> Result<(), Box<dyn Error>> {
        crate::skip_unless_java!();
        rusteron_code_gen::test_logger::init(log::LevelFilter::Info);

        EmbeddedArchiveMediaDriverProcess::kill_all_java_processes().ok();

        // Start Archive/Publisher Driver
        let (aeron_archive, archive_context, _media_driver_archive, mut archive_error_handler) =
            start_aeron_archive_with_config("lifecycle_test", 9400)?;

        let archive_connector =
            AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron_archive)?;
        let archive = archive_connector
            .poll_blocking(Duration::from_secs(20))
            .expect("failed to connect to archive");

        info!("=== Testing Recording Lifecycle ===");

        // Create publication and start recording
        let channel = "aeron:ipc";
        let stream_id = 2009;

        let subscription_id = archive.start_recording(
            &channel.into_c_string(),
            stream_id,
            SOURCE_LOCATION_LOCAL,
            true,
        )?;
        info!("Started recording with subscription_id={}", subscription_id);

        let publication = aeron_archive
            .async_add_publication(&channel.into_c_string(), stream_id)?
            .poll_blocking(Duration::from_secs(5))?;

        // Phase 1: Record initial messages
        let phase1_count = 20;
        for i in 0..phase1_count {
            let message = format!("Phase1-{}", i);
            while publication.offer(
                message.as_bytes(),
                Handlers::no_reserved_value_supplier_handler(),
            ) <= 0
            {
                sleep(Duration::from_millis(10));
            }
        }
        info!("Phase 1: Published {} messages", phase1_count);

        // Stop recording
        archive.stop_recording_channel_and_stream(&channel.into_c_string(), stream_id)?;
        info!("Stopped recording");

        sleep(Duration::from_millis(500));

        // Restart recording
        let subscription_id2 = archive.start_recording(
            &channel.into_c_string(),
            stream_id,
            SOURCE_LOCATION_LOCAL,
            true,
        )?;
        info!(
            "Restarted recording with subscription_id={}",
            subscription_id2
        );

        // Phase 2: Record more messages
        let phase2_count = 20;
        for i in 0..phase2_count {
            let message = format!("Phase2-{}", i);
            while publication.offer(
                message.as_bytes(),
                Handlers::no_reserved_value_supplier_handler(),
            ) <= 0
            {
                sleep(Duration::from_millis(10));
            }
        }
        info!("Phase 2: Published {} messages", phase2_count);

        // List recordings to verify state
        let mut count = 0;
        let recording_found = Cell::new(false);
        archive.list_recordings_once(&mut count, 0, i32::MAX, |desc| {
            if desc.stream_id() == stream_id {
                recording_found.set(true);
                info!(
                    "Recording: id={}, start_position={}, stop_position={}",
                    desc.recording_id(),
                    desc.start_position(),
                    desc.stop_position()
                );
            }
        })?;

        assert!(
            recording_found.get(),
            "Should find recording for our stream"
        );

        drop(archive);
        drop(aeron_archive);
        archive_error_handler.release();

        Ok(())
    }

    /// Test persistent subscription with multiple streams
    ///
    /// This test verifies handling multiple persistent subscriptions on different streams
    #[test]
    #[serial]
    fn test_persistent_subscription_multiple_streams() -> Result<(), Box<dyn Error>> {
        crate::skip_unless_java!();
        rusteron_code_gen::test_logger::init(log::LevelFilter::Info);

        EmbeddedArchiveMediaDriverProcess::kill_all_java_processes().ok();

        // Start Archive/Publisher Driver
        let (aeron_archive, archive_context, _media_driver_archive, mut archive_error_handler) =
            start_aeron_archive_with_config("multi_stream_test", 9500)?;

        let archive_connector =
            AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron_archive)?;
        let archive = archive_connector
            .poll_blocking(Duration::from_secs(20))
            .expect("failed to connect to archive");

        info!("=== Testing Multiple Streams ===");

        let num_streams = 3;
        let stream_ids: Vec<i32> = (3000..3000 + num_streams as i32).collect();
        let mut publications = Vec::new();
        let mut recording_ids = Vec::new();

        // Setup multiple streams
        for stream_id in &stream_ids {
            let subscription_id = archive.start_recording(
                &"aeron:ipc".into_c_string(),
                *stream_id,
                SOURCE_LOCATION_LOCAL,
                true,
            )?;

            let publication = aeron_archive
                .async_add_publication(&"aeron:ipc".into_c_string(), *stream_id)?
                .poll_blocking(Duration::from_secs(5))?;

            // Publish some messages
            for i in 0..10 {
                let message = format!("Stream-{}-Message-{}", stream_id, i);
                while publication.offer(
                    message.as_bytes(),
                    Handlers::no_reserved_value_supplier_handler(),
                ) <= 0
                {
                    sleep(Duration::from_millis(10));
                }
            }

            // Get recording ID
            let session_id = publication.get_constants()?.session_id;
            let counters_reader = aeron_archive.counters_reader();
            let counter_id = RecordingPos::find_counter_id_by_session(&counters_reader, session_id);
            let recording_id = RecordingPos::get_recording_id_block(
                &counters_reader,
                counter_id,
                Duration::from_secs(5),
            )?;

            publications.push(publication);
            recording_ids.push((*stream_id, recording_id));
        }

        info!("Setup {} streams with recordings", num_streams);

        // Verify we have multiple recordings
        let mut count = 0;
        archive.list_recordings_once(&mut count, 0, i32::MAX, |desc| {
            info!(
                "Found recording: stream_id={}, recording_id={}",
                desc.stream_id(),
                desc.recording_id()
            );
        })?;

        assert!(
            count >= num_streams,
            "Should have at least as many recordings as streams"
        );

        // Cleanup
        for pub_item in publications {
            drop(pub_item);
        }

        drop(archive);
        drop(aeron_archive);
        archive_error_handler.release();

        Ok(())
    }

    /// Test persistent subscription error recovery
    ///
    /// This test verifies the system can recover from various error conditions
    #[test]
    #[serial]
    fn test_persistent_subscription_error_recovery() -> Result<(), Box<dyn Error>> {
        crate::skip_unless_java!();
        rusteron_code_gen::test_logger::init(log::LevelFilter::Info);

        EmbeddedArchiveMediaDriverProcess::kill_all_java_processes().ok();

        // Start Archive/Publisher Driver
        let (aeron_archive, archive_context, _media_driver_archive, mut archive_error_handler) =
            start_aeron_archive_with_config("error_recovery_test", 9600)?;

        let archive_connector =
            AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron_archive)?;
        let archive = archive_connector
            .poll_blocking(Duration::from_secs(20))
            .expect("failed to connect to archive");

        info!("=== Testing Error Recovery ===");

        // Test 1: Attempt to replay non-existent recording
        let fake_recording_id = 999999;
        let replay_params = AeronArchiveReplayParams::new(-1, i32::MAX, 0, 100, 0, 0)?;

        let result = archive.start_replay(
            fake_recording_id,
            &"aeron:ipc".into_c_string(),
            4001,
            &replay_params,
        );

        assert!(
            result.is_err(),
            "Should fail to replay non-existent recording"
        );
        info!("Correctly handled non-existent recording error");

        // Test 2: Attempt to get recording position for invalid counter
        let invalid_counter_id = 9999;
        let result =
            RecordingPos::get_recording_id(&aeron_archive.counters_reader(), invalid_counter_id);
        assert!(
            result.is_err(),
            "Should fail to get recording ID for invalid counter"
        );
        info!("Correctly handled invalid counter ID");

        // Test 3: Valid operation after errors
        let channel = "aeron:ipc";
        let stream_id = 4002;

        let subscription_id = archive.start_recording(
            &channel.into_c_string(),
            stream_id,
            SOURCE_LOCATION_LOCAL,
            true,
        )?;

        let publication = aeron_archive
            .async_add_publication(&channel.into_c_string(), stream_id)?
            .poll_blocking(Duration::from_secs(5))?;

        // Publish messages
        for i in 0..5 {
            let message = format!("Recovery-Test-{}", i);
            while publication.offer(
                message.as_bytes(),
                Handlers::no_reserved_value_supplier_handler(),
            ) <= 0
            {
                sleep(Duration::from_millis(10));
            }
        }

        info!("System recovered and functioning normally after errors");

        drop(archive);
        drop(aeron_archive);
        archive_error_handler.release();

        Ok(())
    }

    /// Truncate and purge a recording — ports the purge exercise in Aeron's
    /// `RecordedBasicPublisher`. Archive RPCs are synchronous (Ok = the archive
    /// accepted the request), so we assert each step rather than ignore the result.
    /// Truncate requires a *stopped* recording, so we stop recording first.
    #[test]
    #[serial]
    fn test_truncate_and_purge_recording() -> Result<(), Box<dyn Error>> {
        crate::skip_unless_java!();
        rusteron_code_gen::test_logger::init(log::LevelFilter::Info);

        EmbeddedArchiveMediaDriverProcess::kill_all_java_processes().ok();

        let (aeron_archive, archive_context, _media_driver_archive, mut archive_error_handler) =
            start_aeron_archive_with_config("purge_test", 9900)?;

        let archive = AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron_archive)?
            .poll_blocking(Duration::from_secs(20))
            .expect("failed to connect to archive");

        let channel = "aeron:ipc";
        let stream_id = 5001;
        archive.start_recording(
            &channel.into_c_string(),
            stream_id,
            SOURCE_LOCATION_LOCAL,
            true,
        )?;

        let publication = aeron_archive
            .async_add_publication(&channel.into_c_string(), stream_id)?
            .poll_blocking(Duration::from_secs(5))?;
        for i in 0..100 {
            let m = format!("msg-{i}");
            while publication.offer(m.as_bytes(), Handlers::no_reserved_value_supplier_handler())
                <= 0
            {
                sleep(Duration::from_millis(1));
            }
        }

        // Resolve the recording id and wait for the recorder to flush what we published.
        let session_id = publication.get_constants()?.session_id;
        let counters = aeron_archive.counters_reader();
        let counter_id = RecordingPos::find_counter_id_by_session(&counters, session_id);
        let recording_id =
            RecordingPos::get_recording_id_block(&counters, counter_id, Duration::from_secs(5))?;
        let published_position = publication.position();
        let deadline = Instant::now() + Duration::from_secs(5);
        while counters.get_counter_value(counter_id) < published_position
            && Instant::now() < deadline
        {
            sleep(Duration::from_millis(10));
        }
        info!("recording_id={recording_id} position={published_position}");

        // Truncate/purge operate on a *stopped* recording. Close the stream, stop recording,
        // then wait for the stop to take effect (stop_position becomes non-null) before
        // truncating — otherwise the archive rejects it with "cannot truncate active recording".
        drop(publication);
        archive.stop_recording_channel_and_stream(&channel.into_c_string(), stream_id)?;
        let deadline = Instant::now() + Duration::from_secs(5);
        let mut stopped = false;
        while !stopped && Instant::now() < deadline {
            let mut count = 0;
            archive.list_recordings_once(&mut count, recording_id, 1, |desc| {
                if desc.recording_id() == recording_id && desc.stop_position() > 0 {
                    stopped = true;
                }
            })?;
            if !stopped {
                sleep(Duration::from_millis(10));
            }
        }
        assert!(stopped, "recording {recording_id} never stopped");

        let halfway = published_position / 2;
        archive.truncate_recording(recording_id, halfway)?;
        info!("truncated {recording_id} to {halfway}");

        // Purge deletes the recording entirely.
        archive.purge_recording(recording_id)?;
        info!("purged recording {recording_id}");

        drop(archive);
        drop(aeron_archive);
        archive_error_handler.release();
        Ok(())
    }
}