camel-core 0.24.0

Core engine for rust-camel
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
//! Actor loop and supervision — the async task that processes route control commands.
//!
//! Extracted from a monolithic file. The command enum and handle live
//! in [`controller_actor_commands`](super::controller_actor_commands).

use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use std::time::Instant;

use camel_api::{MetricsCollector, RouteController, SupervisionConfig};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tracing::{debug, error, info, warn};

pub(crate) use super::controller_actor_commands::RouteControllerCommand;
pub use super::controller_actor_commands::RouteControllerHandle;
use super::route_controller::DefaultRouteController;
use super::route_helpers::CrashNotification;

pub fn spawn_controller_actor(
    controller: DefaultRouteController,
) -> (RouteControllerHandle, tokio::task::JoinHandle<()>) {
    let (tx, mut rx) = mpsc::channel::<RouteControllerCommand>(256);
    // Hold a clone of tx so the spawned task can send StartRoute back through
    // the same channel after the 100ms restart sleep, without moving the
    // original tx (which we still need to return as RouteControllerHandle).
    let tx_for_spawn = tx.clone();
    let handle = tokio::spawn(async move {
        let mut controller = controller;
        // Tracks routes currently restarting via spawned off-actor tasks.
        // Related to but separate from spawn_supervision_task's
        // currently_restarting set (that set tracks crash-recovery restarts;
        // this one tracks command-driven restarts).
        let restarting: Arc<Mutex<HashSet<String>>> = Arc::new(Mutex::new(HashSet::new()));
        while let Some(cmd) = rx.recv().await {
            match cmd {
                RouteControllerCommand::StartRoute { route_id, reply } => {
                    // allow-unwrap: Mutex cannot be poisoned in normal operation
                    if restarting
                        .lock()
                        .expect("restarting mutex poisoned") // allow-unwrap
                        .contains(&route_id)
                    {
                        let _ = reply.send(Err(camel_api::CamelError::RouteError(format!(
                            "route {} is restarting",
                            route_id
                        ))));
                        continue;
                    }
                    let _ = reply.send(controller.start_route(&route_id).await);
                }
                RouteControllerCommand::StopRoute { route_id, reply } => {
                    // allow-unwrap: Mutex cannot be poisoned in normal operation
                    if restarting
                        .lock()
                        .expect("restarting mutex poisoned") // allow-unwrap
                        .contains(&route_id)
                    {
                        let _ = reply.send(Err(camel_api::CamelError::RouteError(format!(
                            "route {} is restarting",
                            route_id
                        ))));
                        continue;
                    }
                    let _ = reply.send(controller.stop_route(&route_id).await);
                }
                RouteControllerCommand::RestartRoute { route_id, reply } => {
                    // Reject if already restarting.
                    // allow-unwrap: Mutex cannot be poisoned in normal operation
                    {
                        let mut guard = restarting.lock().expect("restarting mutex poisoned"); // allow-unwrap
                        if guard.contains(&route_id) {
                            let _ = reply.send(Err(camel_api::CamelError::RouteError(format!(
                                "route {} is restarting",
                                route_id
                            ))));
                            continue;
                        }
                        guard.insert(route_id.clone());
                    }

                    // Stop inline — actor owns the controller, so we cannot
                    // move it into a spawned task. The 100ms sleep + start
                    // happen off-actor by sending a StartRoute back through
                    // the same channel.
                    let stop_result = controller.stop_route(&route_id).await;
                    if let Err(ref e) = stop_result {
                        // allow-unwrap: Mutex cannot be poisoned in normal operation
                        restarting
                            .lock()
                            .expect("restarting mutex poisoned") // allow-unwrap
                            .remove(&route_id);
                        let _ = reply.send(Err(e.clone()));
                        continue;
                    }

                    // Spawn only the sleep + send StartRoute back through
                    // the SAME channel. The StartRoute is processed on the
                    // actor thread (correct ownership), and the reply moves
                    // into the spawned task to bridge the restart caller.
                    let tx_clone = tx_for_spawn.clone();
                    let restarting_clone = restarting.clone();
                    let route_id_for_start = route_id.clone();
                    let route_id_for_cleanup = route_id.clone();
                    tokio::spawn(async move {
                        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
                        // Remove from `restarting` BEFORE sending StartRoute: the
                        // actor's StartRoute arm rejects if the route is in the
                        // restarting set, so removing first lets the actor
                        // accept its own self-sent command.
                        // allow-unwrap: Mutex cannot be poisoned in normal operation
                        restarting_clone
                            .lock()
                            .expect("restarting mutex poisoned") // allow-unwrap
                            .remove(&route_id_for_cleanup);
                        if tx_clone
                            .send(RouteControllerCommand::StartRoute {
                                route_id: route_id_for_start,
                                reply,
                            })
                            .await
                            .is_err()
                        {
                            warn!(
                                "route {} restart: StartRoute send failed (actor shutting down)",
                                route_id_for_cleanup
                            );
                        }
                    });
                    // Actor returns to rx.recv() immediately — HoL eliminated.
                }
                RouteControllerCommand::SuspendRoute { route_id, reply } => {
                    // allow-unwrap: Mutex cannot be poisoned in normal operation
                    if restarting
                        .lock()
                        .expect("restarting mutex poisoned") // allow-unwrap
                        .contains(&route_id)
                    {
                        let _ = reply.send(Err(camel_api::CamelError::RouteError(format!(
                            "route {} is restarting",
                            route_id
                        ))));
                        continue;
                    }
                    let _ = reply.send(controller.suspend_route(&route_id).await);
                }
                RouteControllerCommand::ResumeRoute { route_id, reply } => {
                    // allow-unwrap: Mutex cannot be poisoned in normal operation
                    if restarting
                        .lock()
                        .expect("restarting mutex poisoned") // allow-unwrap
                        .contains(&route_id)
                    {
                        let _ = reply.send(Err(camel_api::CamelError::RouteError(format!(
                            "route {} is restarting",
                            route_id
                        ))));
                        continue;
                    }
                    let _ = reply.send(controller.resume_route(&route_id).await);
                }
                RouteControllerCommand::StartAllRoutes { reply } => {
                    let _ = reply.send(controller.start_all_routes().await);
                }
                RouteControllerCommand::StopAllRoutes { reply } => {
                    let _ = reply.send(controller.stop_all_routes().await);
                }
                RouteControllerCommand::AddRoute { definition, reply } => {
                    let _ = reply.send(controller.add_route(definition).await);
                }
                RouteControllerCommand::RemoveRoute { route_id, reply } => {
                    // allow-unwrap: Mutex cannot be poisoned in normal operation
                    if restarting
                        .lock()
                        .expect("restarting mutex poisoned") // allow-unwrap
                        .contains(&route_id)
                    {
                        let _ = reply.send(Err(camel_api::CamelError::RouteError(format!(
                            "route {} is restarting",
                            route_id
                        ))));
                        continue;
                    }
                    let _ = reply.send(controller.remove_route(&route_id).await);
                }
                RouteControllerCommand::SwapPipeline {
                    route_id,
                    pipeline,
                    reply,
                } => {
                    let _ = reply.send(controller.swap_pipeline(&route_id, pipeline));
                }
                RouteControllerCommand::SwapPipelineRaw {
                    route_id,
                    pipeline,
                    lifecycle,
                    reply,
                } => {
                    let _ =
                        reply.send(controller.swap_pipeline_raw(&route_id, pipeline, lifecycle));
                }
                RouteControllerCommand::CompileRouteDefinition { definition, reply } => {
                    let _ = reply.send(controller.compile_route_definition(definition));
                }
                RouteControllerCommand::CompileRouteDefinitionWithGeneration {
                    definition,
                    generation,
                    reply,
                } => {
                    let _ = reply.send(
                        controller.compile_route_definition_with_generation(definition, generation),
                    );
                }
                RouteControllerCommand::CompileRouteDefinitionPipeline {
                    definition,
                    generation,
                    reply,
                } => {
                    let _ = reply
                        .send(controller.compile_route_definition_pipeline(definition, generation));
                }
                RouteControllerCommand::CompileRouteDefinitionDryPipeline { definition, reply } => {
                    let _ =
                        reply.send(controller.compile_route_definition_dry_pipeline(definition));
                }
                RouteControllerCommand::PrepareRouteDefinitionWithGeneration {
                    definition,
                    generation,
                    reply,
                } => {
                    let _ = reply.send(
                        controller.prepare_route_definition_with_generation(definition, generation),
                    );
                }
                RouteControllerCommand::InsertPreparedRoute { prepared, reply } => {
                    let _ = reply.send(controller.insert_prepared_route(prepared));
                }
                RouteControllerCommand::RemoveRoutePreservingFunctions { route_id, reply } => {
                    // allow-unwrap: Mutex cannot be poisoned in normal operation
                    if restarting
                        .lock()
                        .expect("restarting mutex poisoned") // allow-unwrap
                        .contains(&route_id)
                    {
                        let _ = reply.send(Err(camel_api::CamelError::RouteError(format!(
                            "route {} is restarting",
                            route_id
                        ))));
                        continue;
                    }
                    let _ = reply.send(
                        controller
                            .remove_route_preserving_functions(&route_id)
                            .await,
                    );
                }
                RouteControllerCommand::RouteFromUri { route_id, reply } => {
                    let _ = reply.send(controller.route_from_uri(&route_id));
                }
                RouteControllerCommand::SetErrorHandler { config } => {
                    controller.set_error_handler(config);
                }
                RouteControllerCommand::SetTracerConfig { config } => {
                    controller.set_tracer_config(&config);
                }
                RouteControllerCommand::RouteCount { reply } => {
                    let _ = reply.send(controller.route_count());
                }
                RouteControllerCommand::InFlightCount { route_id, reply } => {
                    let _ = reply.send(controller.in_flight_count(&route_id));
                }
                RouteControllerCommand::RouteExists { route_id, reply } => {
                    let _ = reply.send(controller.route_exists(&route_id));
                }
                RouteControllerCommand::RouteIds { reply } => {
                    let _ = reply.send(controller.route_ids());
                }
                RouteControllerCommand::AutoStartupRouteIds { reply } => {
                    let _ = reply.send(controller.auto_startup_route_ids());
                }
                RouteControllerCommand::ShutdownRouteIds { reply } => {
                    let _ = reply.send(controller.shutdown_route_ids());
                }
                RouteControllerCommand::GetPipeline { route_id, reply } => {
                    let _ = reply.send(controller.get_pipeline(&route_id));
                }
                RouteControllerCommand::StartRouteReload { route_id, reply } => {
                    // allow-unwrap: Mutex cannot be poisoned in normal operation
                    if restarting
                        .lock()
                        .expect("restarting mutex poisoned") // allow-unwrap
                        .contains(&route_id)
                    {
                        let _ = reply.send(Err(camel_api::CamelError::RouteError(format!(
                            "route {} is restarting",
                            route_id
                        ))));
                        continue;
                    }
                    let _ = reply.send(controller.start_route_reload(&route_id).await);
                }
                RouteControllerCommand::StopRouteReload { route_id, reply } => {
                    // allow-unwrap: Mutex cannot be poisoned in normal operation
                    if restarting
                        .lock()
                        .expect("restarting mutex poisoned") // allow-unwrap
                        .contains(&route_id)
                    {
                        let _ = reply.send(Err(camel_api::CamelError::RouteError(format!(
                            "route {} is restarting",
                            route_id
                        ))));
                        continue;
                    }
                    let _ = reply.send(controller.stop_route_reload(&route_id).await);
                }
                RouteControllerCommand::SetRuntimeHandle { runtime } => {
                    controller.set_runtime_handle(runtime);
                }
                RouteControllerCommand::SetFunctionInvoker { invoker } => {
                    controller.set_function_invoker(invoker);
                }
                RouteControllerCommand::RouteSourceHash { route_id, reply } => {
                    let _ = reply.send(controller.route_source_hash(&route_id));
                }
                RouteControllerCommand::RouteHasLifecycle { route_id, reply } => {
                    let _ = reply.send(controller.route_has_lifecycle(&route_id));
                }
                RouteControllerCommand::Shutdown => {
                    break;
                }
            }
        }
    });
    (RouteControllerHandle { tx }, handle)
}

pub fn spawn_supervision_task(
    controller: RouteControllerHandle,
    config: SupervisionConfig,
    _metrics: Option<Arc<dyn MetricsCollector>>,
    mut crash_rx: mpsc::Receiver<CrashNotification>,
) -> JoinHandle<()> {
    tokio::spawn(async move {
        let mut attempts: std::collections::HashMap<String, u32> = std::collections::HashMap::new();
        let mut last_restart_time: std::collections::HashMap<String, Instant> =
            std::collections::HashMap::new();
        let mut currently_restarting: std::collections::HashSet<String> =
            std::collections::HashSet::new();

        debug!("Supervision loop started");

        while let Some(notification) = crash_rx.recv().await {
            let route_id = notification.route_id;
            if currently_restarting.contains(&route_id) {
                continue;
            }

            if let Some(last_time) = last_restart_time.get(&route_id)
                && last_time.elapsed() >= config.initial_delay
            {
                attempts.insert(route_id.clone(), 0);
            }

            let current_attempt = attempts.entry(route_id.clone()).or_insert(0);
            *current_attempt += 1;

            if config
                .max_attempts
                .is_some_and(|max| *current_attempt > max)
            {
                // log-policy: system-broken
                error!(
                    route_id = %route_id,
                    attempts = *current_attempt,
                    "Route exceeded max restart attempts, giving up"
                );
                continue;
            }

            let delay = config.next_delay(*current_attempt);
            currently_restarting.insert(route_id.clone());
            tokio::time::sleep(delay).await;

            match controller.restart_route(route_id.clone()).await {
                Ok(()) => {
                    info!(route_id = %route_id, "Route restarted successfully");
                    last_restart_time.insert(route_id.clone(), Instant::now());
                }
                Err(err) => {
                    // log-policy: system-broken
                    error!(route_id = %route_id, error = %err, "Failed to restart route");
                }
            }

            currently_restarting.remove(&route_id);
        }

        debug!("Supervision loop ended");
    })
}

#[cfg(test)]
mod tests {
    use super::{
        RouteControllerCommand, RouteControllerHandle, spawn_controller_actor,
        spawn_supervision_task,
    };
    use crate::lifecycle::adapters::route_controller::DefaultRouteController;
    use crate::lifecycle::adapters::route_helpers::CrashNotification;
    use crate::lifecycle::application::route_definition::RouteDefinition;
    use crate::shared::components::domain::Registry;
    use crate::shared::observability::domain::TracerConfig;
    use camel_api::function::PrepareToken;
    use camel_api::{
        CamelError, ErrorHandlerConfig, Exchange, ExchangePatch, FunctionDefinition, FunctionDiff,
        FunctionId, FunctionInvocationError, FunctionInvoker, FunctionInvokerSync, RuntimeCommand,
        RuntimeCommandBus, RuntimeCommandResult, RuntimeQuery, RuntimeQueryBus, RuntimeQueryResult,
        SupervisionConfig,
    };
    use std::sync::Arc;
    use std::time::Duration;
    use tokio::sync::mpsc;
    use tokio::time::sleep;

    fn build_actor_with_components() -> (RouteControllerHandle, tokio::task::JoinHandle<()>) {
        let registry = Arc::new(std::sync::Mutex::new(Registry::new()));
        {
            let mut guard = registry.lock().expect("lock");
            guard.register(std::sync::Arc::new(
                camel_component_timer::TimerComponent::new(),
            ));
            guard.register(std::sync::Arc::new(
                camel_component_mock::MockComponent::new(),
            ));
        }
        let controller = DefaultRouteController::new(
            Arc::clone(&registry),
            Arc::new(camel_api::NoopPlatformService::default()),
        );
        spawn_controller_actor(controller)
    }

    fn build_empty_actor() -> (RouteControllerHandle, tokio::task::JoinHandle<()>) {
        let controller = DefaultRouteController::new(
            Arc::new(std::sync::Mutex::new(Registry::new())),
            Arc::new(camel_api::NoopPlatformService::default()),
        );
        spawn_controller_actor(controller)
    }

    fn route_def(route_id: &str, from_uri: &str) -> RouteDefinition {
        RouteDefinition::new(from_uri, vec![]).with_route_id(route_id)
    }

    struct NoopRuntime;
    struct NoopInvoker;

    #[async_trait::async_trait]
    impl RuntimeCommandBus for NoopRuntime {
        async fn execute(&self, _cmd: RuntimeCommand) -> Result<RuntimeCommandResult, CamelError> {
            Ok(RuntimeCommandResult::Accepted)
        }
    }

    #[async_trait::async_trait]
    impl RuntimeQueryBus for NoopRuntime {
        async fn ask(&self, query: RuntimeQuery) -> Result<RuntimeQueryResult, CamelError> {
            Ok(match query {
                RuntimeQuery::GetRouteStatus { route_id }
                | RuntimeQuery::InFlightCount { route_id } => {
                    RuntimeQueryResult::RouteNotFound { route_id }
                }
                RuntimeQuery::ListRoutes => RuntimeQueryResult::Routes {
                    route_ids: Vec::new(),
                },
            })
        }
    }

    impl FunctionInvokerSync for NoopInvoker {
        fn stage_pending(
            &self,
            _def: FunctionDefinition,
            _route_id: Option<&str>,
            _generation: u64,
        ) {
        }
        fn discard_staging(&self, _generation: u64) {}
        fn begin_reload(&self) -> u64 {
            1
        }
        fn function_refs_for_route(&self, _route_id: &str) -> Vec<(FunctionId, Option<String>)> {
            vec![]
        }
        fn staged_refs_for_route(
            &self,
            _route_id: &str,
            _generation: u64,
        ) -> Vec<(FunctionId, Option<String>)> {
            vec![]
        }
        fn staged_defs_for_route(
            &self,
            _route_id: &str,
            _generation: u64,
        ) -> Vec<(FunctionDefinition, Option<String>)> {
            vec![]
        }
    }

    #[async_trait::async_trait]
    impl FunctionInvoker for NoopInvoker {
        async fn register(
            &self,
            _def: FunctionDefinition,
            _route_id: Option<&str>,
        ) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
        async fn unregister(
            &self,
            _id: &FunctionId,
            _route_id: Option<&str>,
        ) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
        async fn invoke(
            &self,
            _id: &FunctionId,
            _exchange: &Exchange,
        ) -> Result<ExchangePatch, FunctionInvocationError> {
            Ok(ExchangePatch::default())
        }
        async fn prepare_reload(
            &self,
            _diff: FunctionDiff,
            _generation: u64,
        ) -> Result<PrepareToken, FunctionInvocationError> {
            Ok(PrepareToken::default())
        }
        async fn finalize_reload(
            &self,
            _diff: &FunctionDiff,
            _generation: u64,
        ) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
        async fn rollback_reload(
            &self,
            _token: PrepareToken,
            _generation: u64,
        ) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
        async fn commit_staged(&self) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
    }

    #[tokio::test]
    async fn start_route_sends_command_and_returns_reply() {
        let (tx, mut rx) = mpsc::channel(1);
        let handle = RouteControllerHandle { tx };

        let task = tokio::spawn(async move { handle.start_route("route-a").await });

        let command = rx.recv().await.expect("command should be received");
        match command {
            RouteControllerCommand::StartRoute { route_id, reply } => {
                assert_eq!(route_id, "route-a");
                let _ = reply.send(Ok(()));
            }
            _ => panic!("unexpected command variant"),
        }

        let result = task.await.expect("join should succeed");
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn start_route_returns_error_when_actor_stops() {
        let (tx, rx) = mpsc::channel(1);
        drop(rx);

        let handle = RouteControllerHandle { tx };
        let result = handle.start_route("route-a").await;

        assert!(matches!(result, Err(CamelError::ProcessorError(_))));
    }

    #[tokio::test]
    async fn spawn_controller_actor_processes_commands_and_shutdown() {
        let controller = DefaultRouteController::new(
            Arc::new(std::sync::Mutex::new(Registry::new())),
            Arc::new(camel_api::NoopPlatformService::default()),
        );
        let (handle, join_handle) = spawn_controller_actor(controller);

        assert_eq!(handle.route_count().await.expect("route_count"), 0);
        assert_eq!(
            handle.route_ids().await.expect("route_ids"),
            Vec::<String>::new()
        );

        handle.shutdown().await.expect("shutdown send");
        join_handle.await.expect("actor join");
    }

    #[tokio::test]
    async fn actor_handle_introspection_and_mutation_commands() {
        let (handle, join_handle) = build_actor_with_components();
        let definition = route_def("h-1", "timer:tick?period=100");

        handle.add_route(definition).await.expect("add route");
        assert!(handle.route_exists("h-1").await.expect("route exists h-1"));
        assert!(
            !handle
                .route_exists("no-such")
                .await
                .expect("route exists no-such")
        );

        let from_uri = handle.route_from_uri("h-1").await.expect("route_from_uri");
        assert_eq!(from_uri.as_deref(), Some("timer:tick?period=100"));
        assert_eq!(handle.route_count().await.expect("route_count"), 1);

        let auto_ids = handle
            .auto_startup_route_ids()
            .await
            .expect("auto_startup_route_ids");
        assert!(auto_ids.iter().any(|id| id == "h-1"));

        let shutdown_ids = handle
            .shutdown_route_ids()
            .await
            .expect("shutdown_route_ids");
        assert!(shutdown_ids.iter().any(|id| id == "h-1"));

        let compiled = handle
            .compile_route_definition(route_def("h-1", "timer:tick?period=100"))
            .await
            .expect("compile_route_definition");

        assert!(
            handle
                .get_pipeline("h-1")
                .await
                .expect("get_pipeline")
                .is_some()
        );
        handle
            .swap_pipeline("h-1", compiled)
            .await
            .expect("swap_pipeline");

        let _ = handle
            .in_flight_count("h-1")
            .await
            .expect("in_flight_count");
        let _ = handle.route_source_hash("h-1").await;

        handle
            .set_error_handler(ErrorHandlerConfig::dead_letter_channel("log:dlq"))
            .await
            .expect("set_error_handler");
        handle
            .set_tracer_config(TracerConfig::default())
            .await
            .expect("set_tracer_config");
        handle
            .set_runtime_handle(Arc::new(NoopRuntime))
            .await
            .expect("set_runtime_handle");

        handle.remove_route("h-1").await.expect("remove_route");
        assert_eq!(
            handle
                .route_count()
                .await
                .expect("route_count after remove"),
            0
        );
        handle
            .stop_all_routes()
            .await
            .expect("stop_all_routes on empty");

        handle.shutdown().await.expect("shutdown send");
        join_handle.await.expect("actor join");
    }

    #[tokio::test]
    async fn actor_handle_lifecycle_start_stop_restart_suspend_resume() {
        let (handle, join_handle) = build_actor_with_components();
        handle
            .add_route(route_def("lc-1", "timer:tick?period=50"))
            .await
            .expect("add route lc-1");

        handle.start_route("lc-1").await.expect("start_route");
        sleep(Duration::from_millis(20)).await;

        handle.restart_route("lc-1").await.expect("restart_route");
        sleep(Duration::from_millis(20)).await;

        handle.suspend_route("lc-1").await.expect("suspend_route");
        handle.resume_route("lc-1").await.expect("resume_route");
        sleep(Duration::from_millis(20)).await;

        handle.stop_route("lc-1").await.expect("stop_route");
        handle.start_all_routes().await.expect("start_all_routes");
        sleep(Duration::from_millis(20)).await;
        handle.stop_all_routes().await.expect("stop_all_routes");

        handle
            .start_route_reload("lc-1")
            .await
            .expect("start_route_reload");
        handle
            .stop_route_reload("lc-1")
            .await
            .expect("stop_route_reload");

        handle.shutdown().await.expect("shutdown send");
        join_handle.await.expect("actor join");
    }

    #[tokio::test]
    async fn spawn_supervision_restarts_route_on_crash() {
        let (handle, join_handle) = build_actor_with_components();
        handle
            .add_route(route_def("sup-1", "timer:tick?period=100"))
            .await
            .expect("add route sup-1");
        handle
            .start_route("sup-1")
            .await
            .expect("start_route sup-1");

        let (crash_tx, crash_rx) = mpsc::channel(8);
        let supervision = spawn_supervision_task(
            handle.clone(),
            SupervisionConfig {
                initial_delay: Duration::from_millis(10),
                max_attempts: Some(2),
                ..SupervisionConfig::default()
            },
            None,
            crash_rx,
        );

        crash_tx
            .send(CrashNotification {
                route_id: "sup-1".to_string(),
                error: "simulated".to_string(),
            })
            .await
            .expect("send crash notification");

        sleep(Duration::from_millis(150)).await;
        drop(crash_tx);
        supervision.await.expect("supervision join");

        handle.shutdown().await.expect("shutdown send");
        join_handle.await.expect("actor join");
    }

    #[tokio::test]
    async fn supervision_skips_duplicate_and_gives_up_after_max_attempts() {
        let (handle, join_handle) = build_actor_with_components();
        handle
            .add_route(route_def("sup-2", "timer:tick?period=100"))
            .await
            .expect("add route sup-2");
        handle
            .start_route("sup-2")
            .await
            .expect("start_route sup-2");

        let (crash_tx, crash_rx) = mpsc::channel(8);
        let supervision = spawn_supervision_task(
            handle.clone(),
            SupervisionConfig {
                initial_delay: Duration::from_millis(10),
                max_attempts: Some(1),
                ..SupervisionConfig::default()
            },
            None,
            crash_rx,
        );

        crash_tx
            .send(CrashNotification {
                route_id: "sup-2".to_string(),
                error: "attempt-1".to_string(),
            })
            .await
            .expect("send crash attempt-1");
        crash_tx
            .send(CrashNotification {
                route_id: "sup-2".to_string(),
                error: "attempt-2".to_string(),
            })
            .await
            .expect("send crash attempt-2");

        sleep(Duration::from_millis(200)).await;
        drop(crash_tx);
        supervision.await.expect("supervision join");

        handle.shutdown().await.expect("shutdown send");
        join_handle.await.expect("actor join");
    }

    #[tokio::test]
    async fn try_set_runtime_handle_succeeds_on_fresh_actor() {
        let (handle, join_handle) = build_empty_actor();

        handle
            .try_set_runtime_handle(Arc::new(NoopRuntime))
            .expect("try_set_runtime_handle should succeed");

        handle.shutdown().await.expect("shutdown send");
        join_handle.await.expect("actor join");
    }

    #[tokio::test]
    async fn shutdown_returns_error_when_actor_stopped() {
        let (tx, rx) = mpsc::channel(1);
        drop(rx);

        let handle = RouteControllerHandle { tx };
        let result = handle.shutdown().await;

        assert!(matches!(result, Err(CamelError::ProcessorError(_))));
    }

    #[tokio::test]
    async fn handle_methods_send_expected_commands_and_receive_replies() {
        let (tx, mut rx) = mpsc::channel(16);
        let handle = RouteControllerHandle { tx };

        let stop_task = tokio::spawn({
            let h = handle.clone();
            async move { h.stop_route("r-1").await }
        });
        let cmd = rx.recv().await.expect("stop command");
        match cmd {
            RouteControllerCommand::StopRoute { route_id, reply } => {
                assert_eq!(route_id, "r-1");
                let _ = reply.send(Ok(()));
            }
            _ => panic!("unexpected command"),
        }
        assert!(stop_task.await.expect("join").is_ok());

        let exists_task = tokio::spawn({
            let h = handle.clone();
            async move { h.route_exists("r-2").await }
        });
        let cmd = rx.recv().await.expect("exists command");
        match cmd {
            RouteControllerCommand::RouteExists { route_id, reply } => {
                assert_eq!(route_id, "r-2");
                let _ = reply.send(true);
            }
            _ => panic!("unexpected command"),
        }
        assert!(exists_task.await.expect("join").expect("ok"));

        let hash_task = tokio::spawn({
            let h = handle.clone();
            async move { h.route_source_hash("r-3").await }
        });
        let cmd = rx.recv().await.expect("hash command");
        match cmd {
            RouteControllerCommand::RouteSourceHash { route_id, reply } => {
                assert_eq!(route_id, "r-3");
                let _ = reply.send(Some(77));
            }
            _ => panic!("unexpected command"),
        }
        assert_eq!(hash_task.await.expect("join"), Some(77));
    }

    #[tokio::test]
    async fn handle_methods_error_on_dropped_reply_channel() {
        let (tx, mut rx) = mpsc::channel(16);
        let handle = RouteControllerHandle { tx };

        let count_task = tokio::spawn({
            let h = handle.clone();
            async move { h.route_count().await }
        });
        let cmd = rx.recv().await.expect("route_count command");
        match cmd {
            RouteControllerCommand::RouteCount { reply } => drop(reply),
            _ => panic!("unexpected command"),
        }
        assert!(matches!(
            count_task.await.expect("join"),
            Err(CamelError::ProcessorError(_))
        ));

        let stop_task = tokio::spawn({
            let h = handle.clone();
            async move { h.stop_route("x").await }
        });
        let cmd = rx.recv().await.expect("stop command");
        match cmd {
            RouteControllerCommand::StopRoute { reply, .. } => drop(reply),
            _ => panic!("unexpected command"),
        }
        assert!(matches!(
            stop_task.await.expect("join"),
            Err(CamelError::ProcessorError(_))
        ));

        let maybe_hash = tokio::spawn({
            let h = handle.clone();
            async move { h.route_source_hash("x").await }
        });
        let cmd = rx.recv().await.expect("hash command");
        match cmd {
            RouteControllerCommand::RouteSourceHash { reply, .. } => drop(reply),
            _ => panic!("unexpected command"),
        }
        assert_eq!(maybe_hash.await.expect("join"), None);
    }

    #[test]
    fn try_set_function_invoker_returns_mailbox_full() {
        let (tx, mut rx) = mpsc::channel(1);
        tx.try_send(RouteControllerCommand::Shutdown)
            .expect("fill mailbox");
        let handle = RouteControllerHandle { tx };

        let result = handle.try_set_function_invoker(Arc::new(NoopInvoker));
        assert!(matches!(result, Err(CamelError::ProcessorError(_))));

        rx.try_recv().expect("mailbox still has first message");
    }

    #[tokio::test]
    async fn test_restart_does_not_block_other_routes() {
        // D-L6: a route's restart (stop + 100ms sleep + start) must not block
        // commands for other routes. Without the fix, start_c would wait
        // behind restart's 100ms sleep in the actor mailbox.
        let (handle, join_handle) = build_actor_with_components();
        handle
            .add_route(route_def("route-a", "timer:tick?period=100"))
            .await
            .expect("add route-a");
        handle
            .add_route(route_def("route-b", "timer:tick?period=100"))
            .await
            .expect("add route-b");
        handle
            .add_route(route_def("route-c", "timer:tick?period=100"))
            .await
            .expect("add route-c");

        handle.start_route("route-a").await.expect("start route-a");
        handle.start_route("route-b").await.expect("start route-b");

        // Spawn restart on its own task so its RestartRoute command is
        // guaranteed to land in the mailbox before start_c is enqueued.
        let restart_handle = handle.clone();
        let restart_fut =
            tokio::spawn(async move { restart_handle.restart_route("route-a").await });

        // Yield once so restart's send completes and RestartRoute is queued first.
        tokio::task::yield_now().await;

        // Now issue start_c — its StartRoute command is queued behind RestartRoute.
        let start_c_t0 = std::time::Instant::now();
        let start_result = handle.start_route("route-c").await;
        let start_c_elapsed = start_c_t0.elapsed();

        assert!(start_result.is_ok(), "route-c start should succeed");
        assert!(
            start_c_elapsed < Duration::from_millis(80),
            "route-c start blocked by route-a restart (took {start_c_elapsed:?}); \
             expected to complete before restart's 100ms sleep"
        );

        let restart_result = restart_fut.await.expect("restart join");
        assert!(restart_result.is_ok(), "route-a restart should succeed");

        handle.shutdown().await.expect("shutdown send");
        join_handle.await.expect("actor join");
    }

    #[tokio::test]
    async fn test_command_to_restarting_route_is_rejected() {
        // D-L6: while a route is restarting, mutating commands for that route
        // must be rejected.
        let (handle, join_handle) = build_actor_with_components();
        handle
            .add_route(route_def("route-a", "timer:tick?period=100"))
            .await
            .expect("add route-a");
        handle.start_route("route-a").await.expect("start route-a");

        // Spawn restart on its own task so the RestartRoute command is
        // guaranteed to land in the mailbox.
        let restart_handle = handle.clone();
        let restart_fut =
            tokio::spawn(async move { restart_handle.restart_route("route-a").await });

        // Give the actor time to process the Restart and enter the 100ms sleep.
        // After the inline stop completes, the actor returns to rx.recv() and
        // the spawned task is sleeping — route-a is in the restarting set.
        tokio::time::sleep(Duration::from_millis(20)).await;

        // While restarting, try to stop route-a — must be rejected.
        let stop_result = handle.stop_route("route-a").await;
        assert!(
            stop_result.is_err(),
            "stop during restart should be rejected"
        );
        assert!(
            stop_result.unwrap_err().to_string().contains("restarting"),
            "error should mention restarting"
        );

        // Now the restart completes.
        let restart_result = restart_fut.await.expect("restart join");
        assert!(restart_result.is_ok(), "route-a restart should succeed");

        handle.shutdown().await.expect("shutdown send");
        join_handle.await.expect("actor join");
    }
}