camel-component-grpc 0.18.0

gRPC component for rust-camel (dynamic producer and consumer)
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
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex, OnceLock};
use std::task::{Context, Poll};

use camel_api::CamelError;
use futures::StreamExt;
use hyper::server::conn::http2;
use hyper::service::service_fn;
use hyper_util::rt::TokioIo;
use tokio::sync::{OnceCell, RwLock, mpsc};
use tonic::body::Body as TonicBody;
use tonic::codec::Streaming;
use tonic::{Request, Response, Status};
use tower::Service;
use tracing::{debug, error};

use camel_component_api::RuntimeObservability;

use crate::codec::RawBytesCodec;
use crate::config::GrpcServerConfig;
use crate::consumer::{GrpcReply, GrpcRequestEnvelope, GrpcStreamItem};
use crate::mode::GrpcMode;

pub(crate) type GrpcDispatchEntry = (
    mpsc::Sender<GrpcRequestEnvelope>,
    GrpcMode,
    Option<Arc<dyn camel_auth::TokenAuthenticator>>,
);

pub(crate) type GrpcDispatchTable = Arc<RwLock<HashMap<String, GrpcDispatchEntry>>>;

type ServerKey = (String, u16);

struct ServerHandle {
    dispatch: GrpcDispatchTable,
    #[allow(dead_code)]
    _task: tokio::task::JoinHandle<()>,
    #[allow(dead_code)]
    config: GrpcServerConfig,
}

pub(crate) struct GrpcServerRegistry {
    inner: Mutex<HashMap<ServerKey, Arc<OnceCell<ServerHandle>>>>,
}

impl GrpcServerRegistry {
    pub(crate) fn global() -> &'static Self {
        static INSTANCE: OnceLock<GrpcServerRegistry> = OnceLock::new();
        INSTANCE.get_or_init(|| GrpcServerRegistry {
            inner: Mutex::new(HashMap::new()),
        })
    }

    pub(crate) async fn get_or_spawn(
        &'static self,
        host: &str,
        port: u16,
        config: GrpcServerConfig,
        runtime: Arc<dyn RuntimeObservability>,
    ) -> Result<GrpcDispatchTable, CamelError> {
        let host_owned = host.to_string();

        let cell = {
            let mut guard = self.inner.lock().map_err(|_| {
                CamelError::EndpointCreationFailed("GrpcServerRegistry lock poisoned".into())
            })?;
            let key = (host.to_string(), port);
            guard
                .entry(key)
                .or_insert_with(|| Arc::new(OnceCell::new()))
                .clone()
        };

        let handle = cell
            .get_or_try_init(|| async {
                let addr = format!("{host_owned}:{port}");
                let listener = tokio::net::TcpListener::bind(&addr).await.map_err(|e| {
                    CamelError::EndpointCreationFailed(format!(
                        "failed to bind gRPC server on {addr}: {e}"
                    ))
                })?;
                let dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
                let rt = Arc::clone(&runtime);
                let task = tokio::spawn(run_grpc_server(
                    listener,
                    Arc::clone(&dispatch),
                    config.clone(),
                    rt,
                ));
                Ok::<ServerHandle, CamelError>(ServerHandle {
                    dispatch,
                    _task: task,
                    config,
                })
            })
            .await?;

        if handle._task.is_finished() {
            return Err(CamelError::EndpointCreationFailed(
                "gRPC server task has terminated unexpectedly".into(),
            ));
        }

        Ok(Arc::clone(&handle.dispatch))
    }

    pub(crate) async fn get_or_spawn_with_listener(
        &'static self,
        listener: tokio::net::TcpListener,
        host: &str,
        port: u16,
        config: GrpcServerConfig,
        runtime: Arc<dyn RuntimeObservability>,
    ) -> Result<GrpcDispatchTable, CamelError> {
        let cell = {
            let mut guard = self.inner.lock().map_err(|_| {
                CamelError::EndpointCreationFailed("GrpcServerRegistry lock poisoned".into())
            })?;
            let key = (host.to_string(), port);
            guard
                .entry(key)
                .or_insert_with(|| Arc::new(OnceCell::new()))
                .clone()
        };

        let handle = cell
            .get_or_try_init(|| async {
                let dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
                let rt = Arc::clone(&runtime);
                let task = tokio::spawn(run_grpc_server(
                    listener,
                    Arc::clone(&dispatch),
                    config.clone(),
                    rt,
                ));
                Ok::<ServerHandle, CamelError>(ServerHandle {
                    dispatch,
                    _task: task,
                    config,
                })
            })
            .await?;

        if handle._task.is_finished() {
            return Err(CamelError::EndpointCreationFailed(
                "gRPC server task has terminated unexpectedly".into(),
            ));
        }

        Ok(Arc::clone(&handle.dispatch))
    }

    pub(crate) async fn unregister(&self, host: &str, port: u16, path: &str) {
        let key = (host.to_string(), port);
        let dispatch = {
            let guard = self.inner.lock().ok();
            guard
                .as_ref()
                .and_then(|g| g.get(&key))
                .and_then(|cell| cell.get())
                .map(|handle| Arc::clone(&handle.dispatch))
        };
        if let Some(dispatch) = dispatch {
            let mut table = dispatch.write().await;
            table.remove(path);
        }
    }
}

async fn run_grpc_server(
    listener: tokio::net::TcpListener,
    dispatch: GrpcDispatchTable,
    config: GrpcServerConfig,
    runtime: Arc<dyn RuntimeObservability>,
) {
    // Q-B1: route_id derived from listener local address. The accept loop runs
    // below route dispatch — multiple GrpcEndpoints register on one shared
    // listener, so no single per-route route_id is correct. The local address
    // is stable, attributable, and meaningful to operators.
    let route_id = listener
        .local_addr()
        .map(|addr| format!("grpc-server:{addr}"))
        .unwrap_or_else(|_| "grpc-server:unknown".to_string());

    loop {
        let (stream, _) = match listener.accept().await {
            Ok(s) => s,
            Err(e) => {
                runtime
                    .metrics()
                    .increment_errors(&route_id, "e:grpc:accept");
                // log-policy: outside-contract
                error!(error = %e, "gRPC server accept error");
                continue;
            }
        };

        let io = TokioIo::new(stream);
        let dispatch = dispatch.clone();
        let config = config.clone();

        tokio::spawn(async move {
            let service = service_fn(move |req| {
                let dispatch = dispatch.clone();
                handle_grpc_request(req, dispatch)
            });

            let mut builder = http2::Builder::new(hyper_util::rt::TokioExecutor::new());

            // Apply max_receive_message_len as max_frame_size on the http2 builder.
            // NOTE: HTTP/2 frames are capped at 16 MB per spec; this limits individual
            // frame sizes. For true message-level limits, a Tower layer would be needed.
            if let Some(max_len) = config.max_receive_message_len {
                // Clamp to HTTP/2 spec maximum (16 MB - 1 byte)
                let frame_size = max_len.clamp(16_384, 16_777_215) as u32;
                builder.max_frame_size(frame_size);
            }

            if let Err(e) = builder.serve_connection(io, service).await {
                debug!(error = %e, "gRPC connection error");
            }
        });
    }
}

// ── Response stream type ───────────────────────────────────────────────────

type ResponseStream = Pin<Box<dyn futures::Stream<Item = Result<Vec<u8>, Status>> + Send>>;

// ── Manual stream implementation (no async-stream dep) ─────────────────────

struct GrpcItemStream {
    rx: mpsc::Receiver<GrpcStreamItem>,
}

impl futures::Stream for GrpcItemStream {
    type Item = Result<Vec<u8>, Status>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match self.rx.poll_recv(cx) {
            Poll::Ready(Some(GrpcStreamItem::Message(bytes))) => Poll::Ready(Some(Ok(bytes))),
            Poll::Ready(Some(GrpcStreamItem::Error(status))) => Poll::Ready(Some(Err(status))),
            Poll::Ready(Some(GrpcStreamItem::Done)) => Poll::Ready(None),
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Pending => Poll::Pending,
        }
    }
}

// ── Authentication helper ──────────────────────────────────────────────────

async fn extract_principal(
    authenticator: &dyn camel_auth::TokenAuthenticator,
    metadata: &tonic::metadata::MetadataMap,
) -> Result<camel_api::security_policy::Principal, tonic::Status> {
    let token = metadata
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .and_then(|v| {
            v.strip_prefix("Bearer ")
                .or_else(|| v.strip_prefix("bearer "))
        })
        .map(str::trim)
        .filter(|t| !t.is_empty());

    let token = match token {
        Some(t) => t.to_string(),
        None => {
            return Err(tonic::Status::unauthenticated(
                "missing or malformed authorization header",
            ));
        }
    };

    match authenticator.authenticate_bearer(&token).await {
        Ok(p) => Ok(p),
        Err(camel_api::CamelError::Unauthenticated(msg)) => {
            Err(tonic::Status::unauthenticated(msg))
        }
        Err(camel_api::CamelError::ProcessorError(msg))
            if msg.contains("auth provider unavailable") =>
        {
            Err(tonic::Status::unavailable(msg))
        }
        Err(e) => {
            // log-policy: system-broken
            tracing::error!(error = %e, "gRPC authentication error");
            Err(tonic::Status::internal(e.to_string()))
        }
    }
}

// ── Mode-aware dispatch ────────────────────────────────────────────────────

async fn handle_grpc_request(
    req: hyper::Request<hyper::body::Incoming>,
    dispatch: GrpcDispatchTable,
) -> Result<hyper::Response<TonicBody>, std::convert::Infallible> {
    let path = req.uri().path().to_string();

    let entry = {
        let table = dispatch.read().await;
        table
            .get(&path)
            .map(|(tx, mode, auth)| (tx.clone(), *mode, auth.clone()))
    };

    let Some((sender, mode, authenticator_opt)) = entry else {
        let handler = UnimplementedHandler;
        let mut grpc = tonic::server::Grpc::new(RawBytesCodec);
        let response = grpc.unary(handler, req).await;
        return Ok(response);
    };

    let mut grpc = tonic::server::Grpc::new(RawBytesCodec);

    match mode {
        GrpcMode::Unary => {
            let handler = UnaryHandler {
                sender,
                authenticator_opt,
            };
            let response = grpc.unary(handler, req).await;
            Ok(response)
        }
        GrpcMode::ServerStreaming => {
            let handler = ServerStreamingHandler {
                sender,
                authenticator_opt,
            };
            let response = grpc.server_streaming(handler, req).await;
            Ok(response)
        }
        GrpcMode::ClientStreaming => {
            let handler = ClientStreamingHandler {
                sender,
                authenticator_opt,
            };
            let response = grpc.client_streaming(handler, req).await;
            Ok(response)
        }
        GrpcMode::Bidi => {
            let handler = BidiHandler {
                sender,
                authenticator_opt,
            };
            let response = grpc.streaming(handler, req).await;
            Ok(response)
        }
    }
}

// ── Unimplemented handler (fallback) ───────────────────────────────────────

struct UnimplementedHandler;

impl Service<Request<Vec<u8>>> for UnimplementedHandler {
    type Response = Response<Vec<u8>>;
    type Error = Status;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, _req: Request<Vec<u8>>) -> Self::Future {
        Box::pin(async { Err(Status::unimplemented("no handler for path")) })
    }
}

// ── Unary handler ──────────────────────────────────────────────────────────

struct UnaryHandler {
    sender: mpsc::Sender<GrpcRequestEnvelope>,
    authenticator_opt: Option<Arc<dyn camel_auth::TokenAuthenticator>>,
}

impl tonic::server::UnaryService<Vec<u8>> for UnaryHandler {
    type Response = Vec<u8>;
    type Future = Pin<Box<dyn Future<Output = Result<Response<Self::Response>, Status>> + Send>>;

    fn call(&mut self, req: Request<Vec<u8>>) -> Self::Future {
        let authenticator_opt = self.authenticator_opt.clone();
        let (reply_tx, reply_rx) = tokio::sync::oneshot::channel();
        let sender = self.sender.clone();

        Box::pin(async move {
            let principal = if let Some(ref authenticator) = authenticator_opt {
                Some(extract_principal(authenticator.as_ref(), req.metadata()).await?)
            } else {
                None
            };

            let envelope = GrpcRequestEnvelope::Unary {
                metadata: req.metadata().clone(),
                body: req.into_inner(),
                reply_tx,
                principal,
            };
            sender
                .send(envelope)
                .await
                .map_err(|_| Status::unavailable("consumer stopped"))?;
            match reply_rx
                .await
                .map_err(|_| Status::internal("reply channel dropped"))?
            {
                GrpcReply::Ok(bytes) => Ok(Response::new(bytes)),
                GrpcReply::Err(status) => Err(status),
            }
        })
    }
}

// ── Server-streaming handler ───────────────────────────────────────────────

struct ServerStreamingHandler {
    sender: mpsc::Sender<GrpcRequestEnvelope>,
    authenticator_opt: Option<Arc<dyn camel_auth::TokenAuthenticator>>,
}

impl tonic::server::ServerStreamingService<Vec<u8>> for ServerStreamingHandler {
    type Response = Vec<u8>;
    type ResponseStream = ResponseStream;
    type Future =
        Pin<Box<dyn Future<Output = Result<Response<Self::ResponseStream>, Status>> + Send>>;

    fn call(&mut self, req: Request<Vec<u8>>) -> Self::Future {
        let authenticator_opt = self.authenticator_opt.clone();
        let (reply_tx, reply_rx) = mpsc::channel::<GrpcStreamItem>(64);
        let sender = self.sender.clone();

        Box::pin(async move {
            let principal = if let Some(ref authenticator) = authenticator_opt {
                Some(extract_principal(authenticator.as_ref(), req.metadata()).await?)
            } else {
                None
            };

            let envelope = GrpcRequestEnvelope::ServerStreaming {
                metadata: req.metadata().clone(),
                body: req.into_inner(),
                reply_tx,
                principal,
            };
            sender
                .send(envelope)
                .await
                .map_err(|_| Status::unavailable("consumer stopped"))?;
            Ok(Response::new(
                Box::pin(GrpcItemStream { rx: reply_rx }) as ResponseStream
            ))
        })
    }
}

// ── Client-streaming handler ───────────────────────────────────────────────

struct ClientStreamingHandler {
    sender: mpsc::Sender<GrpcRequestEnvelope>,
    authenticator_opt: Option<Arc<dyn camel_auth::TokenAuthenticator>>,
}

impl tonic::server::ClientStreamingService<Vec<u8>> for ClientStreamingHandler {
    type Response = Vec<u8>;
    type Future = Pin<Box<dyn Future<Output = Result<Response<Self::Response>, Status>> + Send>>;

    fn call(&mut self, req: Request<Streaming<Vec<u8>>>) -> Self::Future {
        let authenticator_opt = self.authenticator_opt.clone();
        let (body_tx, body_rx) = mpsc::channel::<Vec<u8>>(64);
        let (reply_tx, reply_rx) = tokio::sync::oneshot::channel::<GrpcReply>();
        let sender = self.sender.clone();

        Box::pin(async move {
            let principal = if let Some(ref authenticator) = authenticator_opt {
                Some(extract_principal(authenticator.as_ref(), req.metadata()).await?)
            } else {
                None
            };

            let envelope = GrpcRequestEnvelope::ClientStreaming {
                metadata: req.metadata().clone(),
                body_rx,
                reply_tx,
                principal,
            };

            let forward_handle = tokio::spawn(async move {
                let mut stream = req.into_inner();
                while let Some(result) = stream.next().await {
                    match result {
                        Ok(bytes) => {
                            if body_tx.send(bytes).await.is_err() {
                                break;
                            }
                        }
                        Err(status) => {
                            tracing::warn!(error = %status, "client streaming decode error");
                            return Some(status);
                        }
                    }
                }
                None
            });

            sender
                .send(envelope)
                .await
                .map_err(|_| Status::unavailable("consumer stopped"))?;

            let reply = reply_rx
                .await
                .map_err(|_| Status::internal("reply channel dropped"))?;

            // If the inbound stream had a decode error, propagate it instead of the consumer's reply.
            if let Ok(Some(status)) = forward_handle.await {
                return Err(status);
            }

            match reply {
                GrpcReply::Ok(bytes) => Ok(Response::new(bytes)),
                GrpcReply::Err(status) => Err(status),
            }
        })
    }
}

// ── Bidi handler ───────────────────────────────────────────────────────────

struct BidiHandler {
    sender: mpsc::Sender<GrpcRequestEnvelope>,
    authenticator_opt: Option<Arc<dyn camel_auth::TokenAuthenticator>>,
}

impl tonic::server::StreamingService<Vec<u8>> for BidiHandler {
    type Response = Vec<u8>;
    type ResponseStream = ResponseStream;
    type Future =
        Pin<Box<dyn Future<Output = Result<Response<Self::ResponseStream>, Status>> + Send>>;

    fn call(&mut self, req: Request<Streaming<Vec<u8>>>) -> Self::Future {
        let authenticator_opt = self.authenticator_opt.clone();
        let (body_tx, body_rx) = mpsc::channel::<Vec<u8>>(64);
        let (reply_tx, reply_rx) = mpsc::channel::<GrpcStreamItem>(64);
        let reply_tx_forward = reply_tx.clone();
        let sender = self.sender.clone();

        Box::pin(async move {
            let principal = if let Some(ref authenticator) = authenticator_opt {
                Some(extract_principal(authenticator.as_ref(), req.metadata()).await?)
            } else {
                None
            };

            let envelope = GrpcRequestEnvelope::Bidi {
                metadata: req.metadata().clone(),
                body_rx,
                reply_tx,
                principal,
            };

            tokio::spawn(async move {
                let mut stream = req.into_inner();
                while let Some(result) = stream.next().await {
                    match result {
                        Ok(bytes) => {
                            if body_tx.send(bytes).await.is_err() {
                                break;
                            }
                        }
                        Err(status) => {
                            tracing::warn!(error = %status, "bidi streaming decode error");
                            let _ = reply_tx_forward.send(GrpcStreamItem::Error(status)).await;
                            break;
                        }
                    }
                }
            });

            sender
                .send(envelope)
                .await
                .map_err(|_| Status::unavailable("consumer stopped"))?;

            Ok(Response::new(
                Box::pin(GrpcItemStream { rx: reply_rx }) as ResponseStream
            ))
        })
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::sync::Mutex;
    use std::task::Poll;
    use std::time::Duration;

    use camel_api::MetricsCollector;
    use camel_component_api::HealthCheckRegistry;
    use futures::{Stream, StreamExt};
    use tokio::sync::mpsc;
    use tonic::Status;
    use tonic::server::{ServerStreamingService, UnaryService};
    use tower::Service;

    use super::*;
    use crate::consumer::{GrpcReply, GrpcStreamItem};

    // -----------------------------------------------------------------------
    // Recording metrics collector for testing increment_errors calls
    // -----------------------------------------------------------------------

    struct RecordingMetrics {
        errors: Arc<Mutex<Vec<(String, String)>>>,
    }

    impl MetricsCollector for RecordingMetrics {
        fn record_exchange_duration(&self, _: &str, _: Duration) {}
        fn increment_errors(&self, route_id: &str, error_type: &str) {
            self.errors
                .lock()
                .unwrap()
                .push((route_id.to_string(), error_type.to_string()));
        }
        fn increment_exchanges(&self, _: &str) {}
        fn set_queue_depth(&self, _: &str, _: usize) {}
        fn record_circuit_breaker_change(&self, _: &str, _: &str, _: &str) {}
    }

    struct RecordingRuntime {
        metrics_collector: Arc<RecordingMetrics>,
    }

    impl RecordingRuntime {
        fn new(errors: Arc<Mutex<Vec<(String, String)>>>) -> Self {
            Self {
                metrics_collector: Arc::new(RecordingMetrics { errors }),
            }
        }
    }

    impl RuntimeObservability for RecordingRuntime {
        fn metrics(&self) -> Arc<dyn MetricsCollector> {
            self.metrics_collector.clone() as Arc<dyn MetricsCollector>
        }
        fn health(&self) -> Arc<dyn HealthCheckRegistry> {
            panic!("RecordingRuntime::health not used in this test")
        }
    }

    #[test]
    fn test_global_registry_returns_singleton() {
        let first = GrpcServerRegistry::global();
        let second = GrpcServerRegistry::global();
        assert!(std::ptr::eq(first, second));
    }

    #[tokio::test]
    async fn test_grpc_item_stream_yields_message() {
        let (tx, rx) = mpsc::channel::<GrpcStreamItem>(4);
        let mut stream = GrpcItemStream { rx };
        tx.send(GrpcStreamItem::Message(vec![1, 2, 3]))
            .await
            .unwrap();
        drop(tx);
        let item = stream.next().await.unwrap().unwrap();
        assert_eq!(item, vec![1, 2, 3]);
        assert!(stream.next().await.is_none());
    }

    #[tokio::test]
    async fn test_grpc_item_stream_yields_error() {
        let (tx, rx) = mpsc::channel::<GrpcStreamItem>(4);
        let mut stream = GrpcItemStream { rx };
        let status = Status::internal("test error");
        tx.send(GrpcStreamItem::Error(status.clone()))
            .await
            .unwrap();
        drop(tx);
        let item = stream.next().await.unwrap();
        assert!(item.is_err());
        assert_eq!(item.unwrap_err().code(), status.code());
    }

    #[tokio::test]
    async fn test_grpc_item_stream_yields_done_as_none() {
        let (tx, rx) = mpsc::channel::<GrpcStreamItem>(4);
        let mut stream = GrpcItemStream { rx };
        tx.send(GrpcStreamItem::Done).await.unwrap();
        drop(tx);
        assert!(stream.next().await.is_none());
    }

    #[tokio::test]
    async fn test_grpc_item_stream_closed_channel() {
        let (tx, rx) = mpsc::channel::<GrpcStreamItem>(4);
        let mut stream = GrpcItemStream { rx };
        drop(tx);
        assert!(stream.next().await.is_none());
    }

    #[tokio::test]
    async fn test_grpc_item_stream_multiple_messages() {
        let (tx, rx) = mpsc::channel::<GrpcStreamItem>(4);
        let stream = GrpcItemStream { rx };
        tx.send(GrpcStreamItem::Message(vec![1])).await.unwrap();
        tx.send(GrpcStreamItem::Message(vec![2])).await.unwrap();
        tx.send(GrpcStreamItem::Message(vec![3])).await.unwrap();
        drop(tx);
        let results: Vec<_> = stream.collect().await;
        assert_eq!(results.len(), 3);
        assert_eq!(results[0].as_ref().unwrap(), &vec![1]);
        assert_eq!(results[1].as_ref().unwrap(), &vec![2]);
        assert_eq!(results[2].as_ref().unwrap(), &vec![3]);
    }

    #[tokio::test]
    async fn test_grpc_item_stream_poll_pending() {
        let (_tx, rx) = mpsc::channel::<GrpcStreamItem>(4);
        let stream = GrpcItemStream { rx };
        let waker = futures::task::noop_waker();
        let mut cx = Context::from_waker(&waker);
        let mut stream_pinned = std::pin::Pin::new(Box::new(stream));
        assert!(matches!(
            Stream::poll_next(stream_pinned.as_mut(), &mut cx),
            Poll::Pending
        ));
    }

    #[test]
    fn test_unimplemented_handler_poll_ready() {
        let mut handler = UnimplementedHandler;
        let waker = futures::task::noop_waker();
        let mut cx = Context::from_waker(&waker);
        assert!(matches!(handler.poll_ready(&mut cx), Poll::Ready(Ok(()))));
    }

    #[tokio::test]
    async fn test_unimplemented_handler_returns_unimplemented_status() {
        let mut handler = UnimplementedHandler;
        let req = Request::new(vec![1, 2, 3]);
        let result = Service::call(&mut handler, req).await;
        assert!(result.is_err());
        let status = result.unwrap_err();
        assert_eq!(status.code(), tonic::Code::Unimplemented);
        assert_eq!(status.message(), "no handler for path");
    }

    #[tokio::test]
    async fn test_unregister_removes_path_from_dispatch() {
        let dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
        let (tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
        {
            let mut table = dispatch.write().await;
            table.insert(
                "/test.Service/Method".to_string(),
                (tx, GrpcMode::Unary, None),
            );
        }
        assert!(dispatch.read().await.contains_key("/test.Service/Method"));
        {
            let mut table = dispatch.write().await;
            table.remove("/test.Service/Method");
        }
        assert!(!dispatch.read().await.contains_key("/test.Service/Method"));
    }

    #[tokio::test]
    async fn test_unregister_nonexistent_path_is_noop() {
        let registry = GrpcServerRegistry::global();
        let dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
        registry
            .unregister("localhost", 50051, "/nonexistent.Path/Method")
            .await;
        assert!(dispatch.read().await.is_empty());
    }

    #[test]
    fn test_server_key_equality() {
        let key1: ServerKey = ("localhost".to_string(), 50051);
        let key2: ServerKey = ("localhost".to_string(), 50051);
        let key3: ServerKey = ("localhost".to_string(), 50052);
        let key4: ServerKey = ("remotehost".to_string(), 50051);
        assert_eq!(key1, key2);
        assert_ne!(key1, key3);
        assert_ne!(key1, key4);
    }

    #[tokio::test]
    async fn test_dispatch_table_insert_and_retrieve() {
        let dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
        let (tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
        let path = "/pkg.Service/Method".to_string();
        {
            let mut table = dispatch.write().await;
            table.insert(path.clone(), (tx, GrpcMode::ServerStreaming, None));
        }
        let table = dispatch.read().await;
        let (_, mode, _) = table.get(&path).unwrap();
        assert_eq!(*mode, GrpcMode::ServerStreaming);
    }

    #[tokio::test]
    async fn test_dispatch_table_remove_returns_entry() {
        let dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
        let (tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
        let path = "/pkg.Service/Method".to_string();
        {
            let mut table = dispatch.write().await;
            table.insert(path.clone(), (tx, GrpcMode::Bidi, None));
        }
        {
            let mut table = dispatch.write().await;
            let removed = table.remove(&path);
            assert!(removed.is_some());
            let (_, mode, _) = removed.unwrap();
            assert_eq!(mode, GrpcMode::Bidi);
        }
        assert!(dispatch.read().await.is_empty());
    }

    #[tokio::test]
    async fn test_dispatch_table_all_grpc_modes() {
        let dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
        let modes = [
            GrpcMode::Unary,
            GrpcMode::ServerStreaming,
            GrpcMode::ClientStreaming,
            GrpcMode::Bidi,
        ];
        {
            let mut table = dispatch.write().await;
            for (i, mode) in modes.iter().enumerate() {
                let (tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
                table.insert(format!("/svc/M{i}"), (tx, *mode, None));
            }
        }
        let table = dispatch.read().await;
        assert_eq!(table.len(), 4);
        for (i, expected_mode) in modes.iter().enumerate() {
            let (_, mode, _) = table.get(&format!("/svc/M{i}")).unwrap();
            assert_eq!(*mode, *expected_mode);
        }
    }

    #[test]
    fn test_grpc_reply_variants() {
        let ok_reply = GrpcReply::Ok(vec![4, 5, 6]);
        match ok_reply {
            GrpcReply::Ok(bytes) => assert_eq!(bytes, vec![4, 5, 6]),
            GrpcReply::Err(_) => panic!("expected Ok"),
        }
        let err_reply = GrpcReply::Err(Status::not_found("missing"));
        match err_reply {
            GrpcReply::Ok(_) => panic!("expected Err"),
            GrpcReply::Err(s) => assert_eq!(s.code(), tonic::Code::NotFound),
        }
    }

    #[tokio::test]
    async fn test_grpc_request_envelope_unary() {
        let (reply_tx, reply_rx) = tokio::sync::oneshot::channel();
        let mut metadata = tonic::metadata::MetadataMap::new();
        metadata.insert("x-test", "value".parse().unwrap());
        let body = vec![10, 20, 30];
        let envelope = GrpcRequestEnvelope::Unary {
            metadata: metadata.clone(),
            body: body.clone(),
            reply_tx,
            principal: None,
        };
        match envelope {
            GrpcRequestEnvelope::Unary {
                metadata: m,
                body: b,
                reply_tx: tx,
                ..
            } => {
                assert!(m.get("x-test").is_some());
                assert_eq!(b, body);
                let _ = tx.send(GrpcReply::Ok(vec![99]));
            }
            _ => panic!("expected Unary"),
        }
        let reply = reply_rx.await.unwrap();
        assert!(matches!(reply, GrpcReply::Ok(v) if v == vec![99]));
    }

    #[tokio::test]
    async fn test_grpc_request_envelope_server_streaming() {
        let (reply_tx, mut reply_rx) = mpsc::channel::<GrpcStreamItem>(4);
        let envelope = GrpcRequestEnvelope::ServerStreaming {
            metadata: tonic::metadata::MetadataMap::new(),
            body: vec![1],
            reply_tx,
            principal: None,
        };
        match envelope {
            GrpcRequestEnvelope::ServerStreaming { reply_tx: tx, .. } => {
                tx.send(GrpcStreamItem::Message(vec![42])).await.unwrap();
                tx.send(GrpcStreamItem::Done).await.unwrap();
            }
            _ => panic!("expected ServerStreaming"),
        }
        match reply_rx.recv().await {
            Some(GrpcStreamItem::Message(b)) => assert_eq!(b, vec![42]),
            _ => panic!("expected Message(42)"),
        }
        assert!(matches!(reply_rx.recv().await, Some(GrpcStreamItem::Done)));
    }

    #[tokio::test]
    async fn test_grpc_request_envelope_client_streaming() {
        let (body_tx, body_rx) = mpsc::channel::<Vec<u8>>(4);
        let (reply_tx, reply_rx) = tokio::sync::oneshot::channel();
        let envelope = GrpcRequestEnvelope::ClientStreaming {
            metadata: tonic::metadata::MetadataMap::new(),
            body_rx,
            reply_tx,
            principal: None,
        };
        let handle = tokio::spawn(async move {
            match envelope {
                GrpcRequestEnvelope::ClientStreaming {
                    body_rx: mut rx,
                    reply_tx: tx,
                    ..
                } => {
                    assert_eq!(rx.recv().await, Some(vec![1]));
                    assert_eq!(rx.recv().await, Some(vec![2]));
                    let _ = tx.send(GrpcReply::Ok(vec![99]));
                }
                _ => panic!("expected ClientStreaming"),
            }
        });
        body_tx.send(vec![1]).await.unwrap();
        body_tx.send(vec![2]).await.unwrap();
        drop(body_tx);
        handle.await.unwrap();
        let reply = reply_rx.await.unwrap();
        assert!(matches!(reply, GrpcReply::Ok(v) if v == vec![99]));
    }

    #[tokio::test]
    async fn test_grpc_request_envelope_bidi() {
        let (body_tx, body_rx) = mpsc::channel::<Vec<u8>>(4);
        let (reply_tx, mut reply_rx) = mpsc::channel::<GrpcStreamItem>(4);
        let envelope = GrpcRequestEnvelope::Bidi {
            metadata: tonic::metadata::MetadataMap::new(),
            body_rx,
            reply_tx,
            principal: None,
        };
        let handle = tokio::spawn(async move {
            match envelope {
                GrpcRequestEnvelope::Bidi {
                    body_rx: mut rx,
                    reply_tx: tx,
                    ..
                } => {
                    assert_eq!(rx.recv().await, Some(vec![10]));
                    tx.send(GrpcStreamItem::Message(vec![20])).await.unwrap();
                }
                _ => panic!("expected Bidi"),
            }
        });
        body_tx.send(vec![10]).await.unwrap();
        match reply_rx.recv().await {
            Some(GrpcStreamItem::Message(b)) => assert_eq!(b, vec![20]),
            _ => panic!("expected Message(20)"),
        }
        handle.await.unwrap();
    }

    #[tokio::test]
    async fn test_get_or_spawn_with_listener_success() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        let errors = Arc::new(Mutex::new(Vec::<(String, String)>::new()));
        let rt: Arc<dyn RuntimeObservability> = Arc::new(RecordingRuntime::new(errors));

        let dispatch = GrpcServerRegistry::global()
            .get_or_spawn_with_listener(
                listener,
                "127.0.0.1",
                port,
                GrpcServerConfig::default(),
                rt,
            )
            .await;
        assert!(dispatch.is_ok());
    }

    #[tokio::test]
    async fn test_unregister_from_global_registry() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        let errors = Arc::new(Mutex::new(Vec::<(String, String)>::new()));
        let rt: Arc<dyn RuntimeObservability> = Arc::new(RecordingRuntime::new(errors));

        let dispatch = GrpcServerRegistry::global()
            .get_or_spawn_with_listener(
                listener,
                "127.0.0.1",
                port,
                GrpcServerConfig::default(),
                rt,
            )
            .await
            .unwrap();

        let (tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
        let path = "/test.Unregister/Method".to_string();
        {
            let mut table = dispatch.write().await;
            table.insert(path.clone(), (tx, GrpcMode::Unary, None));
        }
        assert!(dispatch.read().await.contains_key(&path));

        GrpcServerRegistry::global()
            .unregister("127.0.0.1", port, &path)
            .await;

        assert!(!dispatch.read().await.contains_key(&path));
    }

    #[test]
    fn test_unary_handler_is_constructable() {
        let (_tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
        let _handler = UnaryHandler {
            sender: _tx,
            authenticator_opt: None,
        };
    }

    #[test]
    fn test_server_streaming_handler_is_constructable() {
        let (_tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
        let _handler = ServerStreamingHandler {
            sender: _tx,
            authenticator_opt: None,
        };
    }

    #[test]
    fn test_client_streaming_handler_is_constructable() {
        let (_tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
        let _handler = ClientStreamingHandler {
            sender: _tx,
            authenticator_opt: None,
        };
    }

    #[test]
    fn test_bidi_handler_is_constructable() {
        let (_tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
        let _handler = BidiHandler {
            sender: _tx,
            authenticator_opt: None,
        };
    }

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

        let mut handler = UnaryHandler {
            sender: tx,
            authenticator_opt: None,
        };
        let req = Request::new(vec![1, 2, 3]);
        let fut = handler.call(req);
        let handle = tokio::spawn(fut);

        let result = handle.await.unwrap();
        let err = result.unwrap_err();
        assert_eq!(err.code(), tonic::Code::Unavailable);
        assert!(err.message().contains("consumer stopped"));
    }

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

        let mut handler = ServerStreamingHandler {
            sender: tx,
            authenticator_opt: None,
        };
        let req = Request::new(vec![1, 2, 3]);
        let fut = handler.call(req);
        let handle = tokio::spawn(fut);

        match handle.await.unwrap() {
            Err(err) => {
                assert_eq!(err.code(), tonic::Code::Unavailable);
            }
            Ok(_) => panic!("expected error when consumer stopped"),
        }
    }

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

        let handler = ClientStreamingHandler {
            sender: tx,
            authenticator_opt: None,
        };
        assert!(handler.sender.is_closed());
    }

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

        let handler = BidiHandler {
            sender: tx,
            authenticator_opt: None,
        };
        assert!(handler.sender.is_closed());
    }

    #[tokio::test]
    async fn test_unary_handler_reply_channel_dropped() {
        let (tx, mut rx) = mpsc::channel::<GrpcRequestEnvelope>(4);

        let mut handler = UnaryHandler {
            sender: tx,
            authenticator_opt: None,
        };
        let req = Request::new(vec![1, 2, 3]);
        let fut = handler.call(req);

        let handle = tokio::spawn(fut);

        let envelope = rx.recv().await.unwrap();
        match envelope {
            GrpcRequestEnvelope::Unary { reply_tx, .. } => {
                drop(reply_tx);
            }
            _ => panic!("expected Unary"),
        }

        let result = handle.await.unwrap();
        let err = result.unwrap_err();
        assert_eq!(err.code(), tonic::Code::Internal);
        assert!(err.message().contains("reply channel dropped"));
    }

    #[tokio::test]
    async fn test_unary_handler_returns_ok_response() {
        let (tx, mut rx) = mpsc::channel::<GrpcRequestEnvelope>(4);

        let mut handler = UnaryHandler {
            sender: tx,
            authenticator_opt: None,
        };
        let req = Request::new(vec![10, 20, 30]);
        let fut = handler.call(req);
        let handle = tokio::spawn(fut);

        let envelope = rx.recv().await.unwrap();
        match envelope {
            GrpcRequestEnvelope::Unary { reply_tx, body, .. } => {
                assert_eq!(body, vec![10, 20, 30]);
                let _ = reply_tx.send(GrpcReply::Ok(vec![40, 50]));
            }
            _ => panic!("expected Unary"),
        }

        let result = handle.await.unwrap().unwrap();
        assert_eq!(result.into_inner(), vec![40, 50]);
    }

    #[tokio::test]
    async fn test_unary_handler_returns_error_response() {
        let (tx, mut rx) = mpsc::channel::<GrpcRequestEnvelope>(4);

        let mut handler = UnaryHandler {
            sender: tx,
            authenticator_opt: None,
        };
        let req = Request::new(vec![1]);
        let fut = handler.call(req);
        let handle = tokio::spawn(fut);

        let envelope = rx.recv().await.unwrap();
        match envelope {
            GrpcRequestEnvelope::Unary { reply_tx, .. } => {
                let _ = reply_tx.send(GrpcReply::Err(Status::not_found("not found")));
            }
            _ => panic!("expected Unary"),
        }

        let result = handle.await.unwrap();
        let err = result.unwrap_err();
        assert_eq!(err.code(), tonic::Code::NotFound);
    }

    #[tokio::test]
    async fn test_server_streaming_handler_success() {
        let (tx, mut rx) = mpsc::channel::<GrpcRequestEnvelope>(4);

        let mut handler = ServerStreamingHandler {
            sender: tx,
            authenticator_opt: None,
        };
        let req = Request::new(vec![1, 2]);
        let fut = handler.call(req);
        let handle = tokio::spawn(fut);

        let envelope = rx.recv().await.unwrap();
        match envelope {
            GrpcRequestEnvelope::ServerStreaming { reply_tx, body, .. } => {
                assert_eq!(body, vec![1, 2]);
                reply_tx
                    .send(GrpcStreamItem::Message(vec![100]))
                    .await
                    .unwrap();
                reply_tx.send(GrpcStreamItem::Done).await.unwrap();
            }
            _ => panic!("expected ServerStreaming"),
        }

        let result = handle.await.unwrap().unwrap();
        let mut stream = result.into_inner();
        assert_eq!(stream.next().await.unwrap().unwrap(), vec![100]);
        assert!(stream.next().await.is_none());
    }

    #[tokio::test]
    async fn test_server_streaming_handler_error_in_stream() {
        let (tx, mut rx) = mpsc::channel::<GrpcRequestEnvelope>(4);

        let mut handler = ServerStreamingHandler {
            sender: tx,
            authenticator_opt: None,
        };
        let req = Request::new(vec![1]);
        let fut = handler.call(req);
        let handle = tokio::spawn(fut);

        let envelope = rx.recv().await.unwrap();
        match envelope {
            GrpcRequestEnvelope::ServerStreaming { reply_tx, .. } => {
                reply_tx
                    .send(GrpcStreamItem::Error(Status::internal("stream error")))
                    .await
                    .unwrap();
            }
            _ => panic!("expected ServerStreaming"),
        }

        let result = handle.await.unwrap().unwrap();
        let mut stream = result.into_inner();
        let item = stream.next().await.unwrap();
        let err = item.unwrap_err();
        assert_eq!(err.code(), tonic::Code::Internal);
    }

    #[tokio::test]
    async fn test_bidi_handler_forwards_items() {
        let (tx, mut rx) = mpsc::channel::<GrpcRequestEnvelope>(4);
        let (reply_tx, _reply_rx) = mpsc::channel::<GrpcStreamItem>(4);

        let handler = BidiHandler {
            sender: tx,
            authenticator_opt: None,
        };
        let (body_tx, body_rx) = mpsc::channel::<Vec<u8>>(4);
        let envelope_for_test = GrpcRequestEnvelope::Bidi {
            metadata: tonic::metadata::MetadataMap::new(),
            body_rx,
            reply_tx,
            principal: None,
        };

        let send_result = handler.sender.send(envelope_for_test).await;
        assert!(send_result.is_ok());

        let received = rx.recv().await;
        assert!(received.is_some());

        body_tx.send(vec![10]).await.unwrap();
        body_tx.send(vec![20]).await.unwrap();
        drop(body_tx);
    }

    #[tokio::test]
    async fn test_grpc_stream_item_variants() {
        let msg = GrpcStreamItem::Message(vec![1, 2]);
        match msg {
            GrpcStreamItem::Message(b) => assert_eq!(b, vec![1, 2]),
            _ => panic!(),
        }

        let err = GrpcStreamItem::Error(Status::internal("err"));
        match err {
            GrpcStreamItem::Error(s) => assert_eq!(s.code(), tonic::Code::Internal),
            _ => panic!(),
        }

        let done = GrpcStreamItem::Done;
        match done {
            GrpcStreamItem::Done => {}
            _ => panic!(),
        }
    }

    #[derive(Debug)]
    struct MockAuthenticator {
        should_fail_unauthenticated: bool,
        should_fail_unavailable: bool,
    }

    #[async_trait::async_trait]
    impl camel_auth::TokenAuthenticator for MockAuthenticator {
        async fn authenticate_bearer(
            &self,
            _token: &str,
        ) -> Result<camel_api::security_policy::Principal, camel_api::CamelError> {
            if self.should_fail_unavailable {
                return Err(camel_api::CamelError::ProcessorError(
                    "auth provider unavailable".into(),
                ));
            }
            if self.should_fail_unauthenticated {
                return Err(camel_api::CamelError::Unauthenticated(
                    "invalid token".into(),
                ));
            }
            Ok(camel_api::security_policy::Principal {
                subject: "test-user".into(),
                issuer: "test-issuer".into(),
                audience: vec![],
                scopes: vec![],
                roles: vec![],
                claims: serde_json::json!({}),
            })
        }
    }

    #[tokio::test]
    async fn test_grpc_auth_valid_token() {
        let (tx, mut rx) = mpsc::channel::<GrpcRequestEnvelope>(1);
        let authenticator: Option<Arc<dyn camel_auth::TokenAuthenticator>> =
            Some(Arc::new(MockAuthenticator {
                should_fail_unauthenticated: false,
                should_fail_unavailable: false,
            }));

        let mut handler = UnaryHandler {
            sender: tx,
            authenticator_opt: authenticator,
        };

        let mut request = Request::new(vec![]);
        request
            .metadata_mut()
            .insert("authorization", "Bearer test-token".parse().unwrap());

        let handle = tokio::spawn(async move {
            let envelope = rx.recv().await.unwrap();
            match envelope {
                GrpcRequestEnvelope::Unary {
                    principal,
                    reply_tx,
                    ..
                } => {
                    assert!(principal.is_some());
                    let p = principal.unwrap();
                    assert_eq!(p.subject, "test-user");
                    assert_eq!(p.issuer, "test-issuer");
                    let _ = reply_tx.send(GrpcReply::Ok(vec![]));
                }
                _ => panic!("expected Unary"),
            }
        });

        let result = handler.call(request).await;
        assert!(result.is_ok());
        handle.await.unwrap();
    }

    #[tokio::test]
    async fn test_grpc_auth_missing_token() {
        let (tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(1);
        let authenticator: Option<Arc<dyn camel_auth::TokenAuthenticator>> =
            Some(Arc::new(MockAuthenticator {
                should_fail_unauthenticated: false,
                should_fail_unavailable: false,
            }));

        let mut handler = UnaryHandler {
            sender: tx,
            authenticator_opt: authenticator,
        };

        let request = Request::new(vec![]);

        let result = handler.call(request).await;
        assert!(result.is_err());
        let status = result.unwrap_err();
        assert_eq!(status.code(), tonic::Code::Unauthenticated);
    }

    #[tokio::test]
    async fn test_grpc_auth_invalid_token() {
        let (tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(1);
        let authenticator: Option<Arc<dyn camel_auth::TokenAuthenticator>> =
            Some(Arc::new(MockAuthenticator {
                should_fail_unauthenticated: true,
                should_fail_unavailable: false,
            }));

        let mut handler = UnaryHandler {
            sender: tx,
            authenticator_opt: authenticator,
        };

        let mut request = Request::new(vec![]);
        request
            .metadata_mut()
            .insert("authorization", "Bearer bad-token".parse().unwrap());

        let result = handler.call(request).await;
        assert!(result.is_err());
        let status = result.unwrap_err();
        assert_eq!(status.code(), tonic::Code::Unauthenticated);
    }

    #[tokio::test]
    async fn test_grpc_auth_provider_unavailable() {
        let (tx, _rx) = mpsc::channel::<GrpcRequestEnvelope>(1);
        let authenticator: Option<Arc<dyn camel_auth::TokenAuthenticator>> =
            Some(Arc::new(MockAuthenticator {
                should_fail_unauthenticated: false,
                should_fail_unavailable: true,
            }));

        let mut handler = UnaryHandler {
            sender: tx,
            authenticator_opt: authenticator,
        };

        let mut request = Request::new(vec![]);
        request
            .metadata_mut()
            .insert("authorization", "Bearer valid-token".parse().unwrap());

        let result = handler.call(request).await;
        assert!(result.is_err());
        let status = result.unwrap_err();
        assert_eq!(status.code(), tonic::Code::Unavailable);
    }

    #[tokio::test]
    async fn test_grpc_no_auth_configured() {
        let (tx, mut rx) = mpsc::channel::<GrpcRequestEnvelope>(1);
        let authenticator: Option<Arc<dyn camel_auth::TokenAuthenticator>> = None;

        let mut handler = UnaryHandler {
            sender: tx,
            authenticator_opt: authenticator,
        };

        let request = Request::new(vec![]);

        let handle = tokio::spawn(async move {
            let envelope = rx.recv().await.unwrap();
            match envelope {
                GrpcRequestEnvelope::Unary {
                    principal,
                    reply_tx,
                    ..
                } => {
                    assert!(principal.is_none());
                    let _ = reply_tx.send(GrpcReply::Ok(vec![]));
                }
                _ => panic!("expected Unary"),
            }
        });

        let result = handler.call(request).await;
        assert!(result.is_ok());
        handle.await.unwrap();
    }

    #[tokio::test]
    async fn test_server_handle_struct() {
        let dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
        let task = tokio::spawn(async {});
        let handle = ServerHandle {
            dispatch,
            _task: task,
            config: GrpcServerConfig::default(),
        };
        let _ = handle;
    }

    // ── ADR-0012 (e) site regression test ──────────────────────────────────

    #[tokio::test]
    async fn test_run_grpc_server_route_id_derivation() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let route_id = listener
            .local_addr()
            .map(|addr| format!("grpc-server:{addr}"))
            .unwrap_or_else(|_| "grpc-server:unknown".to_string());
        assert!(!route_id.is_empty(), "route_id must not be empty");
        assert!(
            route_id.starts_with("grpc-server:"),
            "route_id should start with 'grpc-server:': got {route_id}"
        );
    }

    #[tokio::test]
    async fn test_increment_errors_recording_works() {
        // This test validates the metrics recording machinery for the accept
        // error branch without driving the real accept loop into an error.
        //
        // Driving the real loop requires platform-specific fd manipulation
        // (dup+shutdown) that causes the accept to return EINVAL on every
        // iteration — the loop spins indefinitely recording spurious errors,
        // which cannot be precisely asserted. The error condition is not
        // single-shot; it persists after shutdown.
        //
        // The accept error BRANCH (server.rs:189-196) is exercised indirectly:
        //   - test_run_grpc_server_happy_path confirms the accept loop runs
        //     and records zero errors on success.
        //   - This test confirms the recording subsystem correctly captures
        //     the call signature (route_id + label) that the error branch
        //     would emit.
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let expected_route_id = listener
            .local_addr()
            .map(|addr| format!("grpc-server:{addr}"))
            .unwrap();
        let _dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
        let errors = Arc::new(Mutex::new(Vec::<(String, String)>::new()));
        let rt: Arc<dyn RuntimeObservability> = Arc::new(RecordingRuntime::new(errors.clone()));

        // Simulate the accept-error branch: verify the metric call signature
        rt.metrics()
            .increment_errors(&expected_route_id, "e:grpc:accept");

        let recorded = errors.lock().unwrap();
        assert_eq!(recorded.len(), 1, "expected one error record");
        assert_eq!(recorded[0].0, expected_route_id, "route_id mismatch");
        assert_eq!(recorded[0].1, "e:grpc:accept", "error label mismatch");
    }

    #[tokio::test]
    async fn test_run_grpc_server_happy_path() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let _dispatch: GrpcDispatchTable = Arc::new(RwLock::new(HashMap::new()));
        let errors = Arc::new(Mutex::new(Vec::<(String, String)>::new()));
        let rt: Arc<dyn RuntimeObservability> = Arc::new(RecordingRuntime::new(errors.clone()));

        let handle = tokio::spawn(run_grpc_server(
            listener,
            _dispatch,
            GrpcServerConfig::default(),
            rt,
        ));

        // Connect to verify the accept loop handles clients
        let conn =
            tokio::time::timeout(Duration::from_secs(2), tokio::net::TcpStream::connect(addr))
                .await;
        assert!(conn.is_ok(), "server should accept connections");

        // Verify no accept errors recorded on happy path
        {
            let recorded = errors.lock().unwrap();
            assert!(
                recorded.is_empty(),
                "no accept errors expected on happy path"
            );
        }

        handle.abort();
        let _ = handle.await;
    }
}