camel-component-stream 0.50.0

Stream (stdio) component 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
use super::*;
use std::sync::Mutex;

use camel_component_api::{Body, ExchangeEnvelope};
use tokio_util::sync::CancellationToken;

type ErrorLog = Arc<Mutex<Vec<(String, String)>>>;

// -------------------------------------------------------------------
// Recording metrics/runtime double (pattern: camel-timer
// tests::RecordingRuntime, itself from camel-direct RecordingMetrics)
// -------------------------------------------------------------------

struct RecordingMetrics {
    errors: ErrorLog,
}

impl camel_api::MetricsCollector for RecordingMetrics {
    fn record_exchange_duration(&self, _: &str, _: std::time::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: ErrorLog) -> Self {
        Self {
            metrics_collector: Arc::new(RecordingMetrics { errors }),
        }
    }
}

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

// -------------------------------------------------------------------
// Reader test doubles
// -------------------------------------------------------------------

/// StreamReader double over a shared byte buffer: reads consume from the
/// front, mimicking a pipe. Empty buffer = EOF.
struct SharedReader(Arc<Mutex<Vec<u8>>>);

impl SharedReader {
    fn new(input: &[u8]) -> Self {
        Self(Arc::new(Mutex::new(input.to_vec())))
    }

    fn drain(&self, max: usize) -> Vec<u8> {
        let mut guard = self.0.lock().unwrap();
        let n = max.min(guard.len());
        guard.drain(..n).collect()
    }
}

#[async_trait::async_trait]
impl StreamReader for SharedReader {
    async fn read_until_newline(&mut self, buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        let mut guard = self.0.lock().unwrap();
        if guard.is_empty() {
            return Ok(0);
        }
        let upto = guard
            .iter()
            .position(|&b| b == b'\n')
            .map_or(guard.len(), |i| i + 1);
        let chunk: Vec<u8> = guard.drain(..upto).collect();
        buf.extend_from_slice(&chunk);
        Ok(chunk.len())
    }

    async fn read_exact_chunk(&mut self, n: usize, buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        let chunk = self.drain(n);
        let len = chunk.len();
        buf.extend_from_slice(&chunk);
        Ok(len)
    }

    async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        let chunk = self.drain(usize::MAX);
        let len = chunk.len();
        buf.extend_from_slice(&chunk);
        Ok(len)
    }
}

/// Reader whose second `read_until_newline` holds a gate until the test
/// opens it. Proves the consumer sends envelope 1 BEFORE starting the
/// next read: a read-ahead consumer blocks in the gate and trips the
/// test's timeout; completing the gated read before the test recorded
/// consumption panics.
struct GatedReader {
    data: SharedReader,
    gate: Arc<tokio::sync::Notify>,
    consumed: Arc<AtomicBool>,
    calls: usize,
}

#[async_trait::async_trait]
impl StreamReader for GatedReader {
    async fn read_until_newline(&mut self, buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        let call = self.calls;
        self.calls += 1;
        if call == 1 {
            self.gate.notified().await;
            assert!(
                self.consumed.load(Ordering::SeqCst),
                "second read completed before the first envelope was consumed"
            );
        }
        self.data.read_until_newline(buf).await
    }

    async fn read_exact_chunk(&mut self, n: usize, buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        self.data.read_exact_chunk(n, buf).await
    }

    async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        self.data.read_to_end(buf).await
    }
}

/// Reader whose reads never complete (models a stdin with no input and
/// no EOF) — used for the cancellation test.
struct BlockingReader;

#[async_trait::async_trait]
impl StreamReader for BlockingReader {
    async fn read_until_newline(&mut self, _buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        std::future::pending::<()>().await;
        Ok(0)
    }

    async fn read_exact_chunk(
        &mut self,
        _n: usize,
        _buf: &mut Vec<u8>,
    ) -> Result<usize, CamelError> {
        std::future::pending::<()>().await;
        Ok(0)
    }

    async fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        std::future::pending::<()>().await;
        Ok(0)
    }
}

/// Reader whose reads always fail with an IO error — models a genuine
/// source failure (EIO and friends), which is NOT EOF.
struct ErroringReader;

#[async_trait::async_trait]
impl StreamReader for ErroringReader {
    async fn read_until_newline(&mut self, _buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        Err(CamelError::Io("simulated EIO".to_string()))
    }

    async fn read_exact_chunk(
        &mut self,
        _n: usize,
        _buf: &mut Vec<u8>,
    ) -> Result<usize, CamelError> {
        Err(CamelError::Io("simulated EIO".to_string()))
    }

    async fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        Err(CamelError::Io("simulated EIO".to_string()))
    }
}

/// Cap on bytes returned per `read_exact_chunk` call for
/// [`DripReader`], smaller than the frame size used in its test.
const DRIP_MAX_CHUNK: usize = 2;

/// Reader double that delivers at most [`DRIP_MAX_CHUNK`] bytes per
/// `read_exact_chunk` call, modeling a trickling stdin. Proves fixed
/// framing accumulates short reads into a full frame instead of
/// emitting a premature partial frame.
struct DripReader {
    data: SharedReader,
}

#[async_trait::async_trait]
impl StreamReader for DripReader {
    async fn read_until_newline(&mut self, buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        self.data.read_until_newline(buf).await
    }

    async fn read_exact_chunk(&mut self, n: usize, buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        self.data.read_exact_chunk(n.min(DRIP_MAX_CHUNK), buf).await
    }

    async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        self.data.read_to_end(buf).await
    }
}

/// Models `cat /dev/zero | camel run <raw-route>`: an endless source
/// that never EOFs, so raw or line accumulation would grow without
/// bound. To keep the test cheap it never materializes the 100 MiB
/// bound: it reports the violation exactly where the production
/// `StdinReader` detects it — the pre-growth `ensure_frame_bound`
/// check — by returning `StreamLimitExceeded` on the first read. Fixed
/// framing is not exercised here: `size` is config-validated within
/// the bound, so a fixed frame can never trip it.
struct UnboundedSourceReader;

#[async_trait::async_trait]
impl StreamReader for UnboundedSourceReader {
    async fn read_until_newline(&mut self, _buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        // Endless zeros hold no `\n`; accumulation would cross the bound.
        Err(CamelError::StreamLimitExceeded(MAX_MATERIALIZE_BYTES))
    }

    async fn read_exact_chunk(
        &mut self,
        _n: usize,
        _buf: &mut Vec<u8>,
    ) -> Result<usize, CamelError> {
        panic!("fixed framing cannot exceed the config-validated bound; not used in this test")
    }

    async fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> Result<usize, CamelError> {
        Err(CamelError::StreamLimitExceeded(MAX_MATERIALIZE_BYTES))
    }
}

// -------------------------------------------------------------------
// Fixtures
// -------------------------------------------------------------------

fn errors_log() -> ErrorLog {
    Arc::new(Mutex::new(Vec::new()))
}

/// Build a consumer on the injected reader plus a live context wired to
/// an mpsc channel; the test asserts on the received envelopes.
fn test_setup(
    config: StreamConfig,
    reader: Box<dyn StreamReader>,
) -> (
    StreamConsumer,
    ConsumerContext,
    tokio::sync::mpsc::Receiver<ExchangeEnvelope>,
    CancellationToken,
    ErrorLog,
) {
    let errors = errors_log();
    let (tx, rx) = tokio::sync::mpsc::channel::<ExchangeEnvelope>(16);
    let token = CancellationToken::new();
    let consumer = StreamConsumer::with_reader(
        config,
        Arc::new(RecordingRuntime::new(Arc::clone(&errors))),
        reader,
    );
    let context = ConsumerContext::new(tx, token.clone(), "stream-test-route".to_string());
    (consumer, context, rx, token, errors)
}

async fn recv_text(rx: &mut tokio::sync::mpsc::Receiver<ExchangeEnvelope>) -> String {
    let envelope = rx.recv().await.expect("expected an envelope");
    match &envelope.exchange.input.body {
        Body::Text(text) => text.clone(),
        other => panic!("expected Text body, got {other:?}"),
    }
}

async fn recv_bytes(rx: &mut tokio::sync::mpsc::Receiver<ExchangeEnvelope>) -> Vec<u8> {
    let envelope = rx.recv().await.expect("expected an envelope");
    match &envelope.exchange.input.body {
        Body::Bytes(bytes) => bytes.to_vec(),
        other => panic!("expected Bytes body, got {other:?}"),
    }
}

// -------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------

#[tokio::test]
async fn one_line_one_exchange() {
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in").unwrap(),
        Box::new(SharedReader::new(b"a\nb\nc\n")),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    assert_eq!(recv_text(&mut rx).await, "a");
    assert_eq!(recv_text(&mut rx).await, "b");
    assert_eq!(recv_text(&mut rx).await, "c");
    handle.await.unwrap().unwrap();
    assert!(matches!(
        rx.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Disconnected)
    ));
}

#[tokio::test]
async fn final_unterminated_line_emits() {
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in").unwrap(),
        Box::new(SharedReader::new(b"x\ny")),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    assert_eq!(recv_text(&mut rx).await, "x");
    assert_eq!(recv_text(&mut rx).await, "y");
    handle.await.unwrap().unwrap();
}

#[tokio::test]
async fn crlf_stripped() {
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in").unwrap(),
        Box::new(SharedReader::new(b"w\r\n")),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    assert_eq!(recv_text(&mut rx).await, "w");
    handle.await.unwrap().unwrap();
}

/// A trailing `\r` with no `\n` is DATA, not a terminator: only a `\r`
/// that precedes a stripped `\n` may be stripped. (Apache Camel's
/// stream: also splits on `\r`; this component diverges — documented on
/// the component page.)
#[tokio::test]
async fn cr_only_line_preserved() {
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in").unwrap(),
        Box::new(SharedReader::new(b"x\r")),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    assert_eq!(recv_text(&mut rx).await, "x\r");
    handle.await.unwrap().unwrap();
}

#[tokio::test]
async fn raw_frame_one_exchange() {
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in?frame=raw").unwrap(),
        Box::new(SharedReader::new(b"l1\nl2\nl3\n")),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    assert_eq!(recv_bytes(&mut rx).await, b"l1\nl2\nl3\n");
    handle.await.unwrap().unwrap();
}

#[tokio::test]
async fn raw_empty_zero_exchanges() {
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in?frame=raw").unwrap(),
        Box::new(SharedReader::new(b"")),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    handle.await.unwrap().unwrap();
    assert!(matches!(
        rx.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Disconnected)
    ));
}

#[tokio::test]
async fn fixed_frames_and_partial_chunk() {
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in?frame=fixed&size=4").unwrap(),
        Box::new(SharedReader::new(b"0123456789")),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    assert_eq!(recv_bytes(&mut rx).await, b"0123");
    assert_eq!(recv_bytes(&mut rx).await, b"4567");
    assert_eq!(recv_bytes(&mut rx).await, b"89");
    handle.await.unwrap().unwrap();
}

/// Fixed framing must accumulate across short reads: the drip reader
/// returns at most 2 bytes per read call, so the single 4-byte frame can
/// only materialize if `read_exact_chunk` loops. 6 input bytes at
/// `size=4` yield one full frame plus a 2-byte partial at EOF.
#[tokio::test]
async fn fixed_frames_short_reads_accumulate() {
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in?frame=fixed&size=4").unwrap(),
        Box::new(DripReader {
            data: SharedReader::new(b"012345"),
        }),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    assert_eq!(recv_bytes(&mut rx).await, b"0123");
    assert_eq!(recv_bytes(&mut rx).await, b"45");
    handle.await.unwrap().unwrap();
}

/// LOAD-BEARING: a read IO error is NOT EOF — it must fail `start`
/// loudly (CamelError::Io), send no envelope, and reset the started
/// flag so the consumer can be restarted after the failure.
#[tokio::test]
async fn read_io_error_fails_loudly() {
    let errors = errors_log();
    let mut consumer = StreamConsumer::with_reader(
        StreamConfig::from_uri("stream:in").unwrap(),
        Arc::new(RecordingRuntime::new(errors)),
        Box::new(ErroringReader),
    );

    let (tx1, mut rx1) = tokio::sync::mpsc::channel::<ExchangeEnvelope>(16);
    let ctx1 = ConsumerContext::new(
        tx1,
        CancellationToken::new(),
        "stream-test-route".to_string(),
    );
    let first = consumer.start(ctx1).await;
    assert!(
        matches!(&first, Err(CamelError::Io(msg)) if msg.contains("simulated EIO")),
        "IO error must fail loudly, got: {first:?}"
    );
    assert!(
        rx1.try_recv().is_err(),
        "no envelope may be sent when the read fails"
    );

    // The failure path must reset the started flag: a second start
    // retries the read (IO error again), not "already started".
    let (tx2, _rx2) = tokio::sync::mpsc::channel::<ExchangeEnvelope>(16);
    let ctx2 = ConsumerContext::new(
        tx2,
        CancellationToken::new(),
        "stream-test-route".to_string(),
    );
    let second = consumer.start(ctx2).await;
    assert!(
        matches!(second, Err(CamelError::Io(_))),
        "second start must retry (flag reset), got: {second:?}"
    );
}

/// LOAD-BEARING: a frame past the materialization bound is NOT EOF and
/// NOT a silent skip — it fails `start` loudly with the same
/// `StreamLimitExceeded` variant the producer's `into_bytes` bound
/// uses, sends no envelope, and resets the started flag. The
/// [`UnboundedSourceReader`] double reports the violation on the first
/// read without materializing 100 MiB (see its doc comment for the
/// honest-cheap wiring).
#[tokio::test]
async fn frame_bound_exceeded_fails_loudly() {
    let errors = errors_log();
    let mut consumer = StreamConsumer::with_reader(
        StreamConfig::from_uri("stream:in?frame=raw").unwrap(),
        Arc::new(RecordingRuntime::new(errors)),
        Box::new(UnboundedSourceReader),
    );

    let (tx1, mut rx1) = tokio::sync::mpsc::channel::<ExchangeEnvelope>(16);
    let ctx1 = ConsumerContext::new(
        tx1,
        CancellationToken::new(),
        "stream-test-route".to_string(),
    );
    let first = consumer.start(ctx1).await;
    assert!(
        matches!(
            &first,
            Err(CamelError::StreamLimitExceeded(MAX_MATERIALIZE_BYTES))
        ),
        "bound violation must fail loudly, got: {first:?}"
    );
    assert!(
        rx1.try_recv().is_err(),
        "no envelope may be sent when the bound is exceeded"
    );

    // The failure path must reset the started flag, same as the IO
    // error path: a second start retries (limit again), not "already
    // started".
    let (tx2, _rx2) = tokio::sync::mpsc::channel::<ExchangeEnvelope>(16);
    let ctx2 = ConsumerContext::new(
        tx2,
        CancellationToken::new(),
        "stream-test-route".to_string(),
    );
    let second = consumer.start(ctx2).await;
    assert!(
        matches!(second, Err(CamelError::StreamLimitExceeded(_))),
        "second start must retry (flag reset), got: {second:?}"
    );
}

// LOAD-BEARING (ruling R2): EOF with no input is a graceful `Ok(())`,
// never an error.
#[tokio::test]
async fn eof_no_input_completes_gracefully() {
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in").unwrap(),
        Box::new(SharedReader::new(b"")),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    handle.await.unwrap().unwrap();
    assert!(matches!(
        rx.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Disconnected)
    ));
}

#[tokio::test]
async fn cancellation_stops_cleanly() {
    let (mut consumer, ctx, mut rx, token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in").unwrap(),
        Box::new(BlockingReader),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    // Let the consumer enter the never-completing read, then cancel.
    tokio::time::sleep(std::time::Duration::from_millis(50)).await; // allow-test-sleep: settle window so the consumer parks in the read before cancel (clean-stop is the assertion, not the path)
    token.cancel();

    let result = tokio::time::timeout(std::time::Duration::from_secs(1), handle)
        .await
        .expect("consumer must stop after cancellation")
        .unwrap();
    assert!(matches!(result, Ok(())));
    assert!(matches!(
        rx.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Disconnected)
    ));
}

#[tokio::test]
async fn invalid_utf8_line_skipped_with_metric() {
    let (mut consumer, ctx, mut rx, _token, errors) = test_setup(
        StreamConfig::from_uri("stream:in").unwrap(),
        Box::new(SharedReader::new(b"ok\n\xff\xfe bad\nok2\n")),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    assert_eq!(recv_text(&mut rx).await, "ok");
    assert_eq!(recv_text(&mut rx).await, "ok2");
    handle.await.unwrap().unwrap();
    assert_eq!(
        *errors.lock().unwrap(),
        vec![(
            "stream-test-route".to_string(),
            "b-prime:stream:decode".to_string()
        )]
    );
}

#[tokio::test]
async fn closed_channel_ends_loop_with_metric() {
    let errors = errors_log();
    let (tx, rx) = tokio::sync::mpsc::channel::<ExchangeEnvelope>(16);
    drop(rx);

    let mut consumer = StreamConsumer::with_reader(
        StreamConfig::from_uri("stream:in").unwrap(),
        Arc::new(RecordingRuntime::new(Arc::clone(&errors))),
        Box::new(SharedReader::new(b"a\n")),
    );
    let ctx = ConsumerContext::new(
        tx,
        CancellationToken::new(),
        "stream-test-route".to_string(),
    );

    consumer.start(ctx).await.unwrap();
    assert_eq!(
        *errors.lock().unwrap(),
        vec![(
            "stream-test-route".to_string(),
            "b-prime:stream:fire-send".to_string()
        )]
    );
}

#[tokio::test]
async fn sequential_backpressure_no_read_ahead() {
    let gate = Arc::new(tokio::sync::Notify::new());
    let consumed = Arc::new(AtomicBool::new(false));
    let reader = GatedReader {
        data: SharedReader::new(b"a\nb\n"),
        gate: Arc::clone(&gate),
        consumed: Arc::clone(&consumed),
        calls: 0,
    };
    let (mut consumer, ctx, mut rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in").unwrap(),
        Box::new(reader),
    );
    let handle = tokio::spawn(async move { consumer.start(ctx).await });

    // The second read is gated: a read-ahead consumer would be stuck in
    // the gate before ever sending, and this recv would time out.
    let first = tokio::time::timeout(std::time::Duration::from_secs(2), recv_text(&mut rx))
        .await
        .expect("read-ahead detected: first envelope never arrived");
    assert_eq!(first, "a");

    consumed.store(true, Ordering::SeqCst);
    gate.notify_one();

    // Timeout guard on the second phase too: a post-gate deadlock must
    // fail this test with a clear message, not hang the whole suite.
    let (second, start_result) = tokio::time::timeout(std::time::Duration::from_secs(2), async {
        (recv_text(&mut rx).await, handle.await)
    })
    .await
    .expect("post-gate deadlock: second envelope or consumer completion never arrived");
    assert_eq!(second, "b");
    start_result.unwrap().unwrap();
}

#[tokio::test]
async fn double_start_rejected() {
    let (mut consumer, ctx, _rx, _token, _errors) = test_setup(
        StreamConfig::from_uri("stream:in").unwrap(),
        Box::new(SharedReader::new(b"")),
    );
    consumer.mark_started_for_test();

    let err = consumer.start(ctx).await.unwrap_err();
    assert!(err.to_string().contains("already started"), "got: {err}");
}

// -------------------------------------------------------------------
// Endpoint wiring (Task 2.2)
// -------------------------------------------------------------------

/// Build a `StreamEndpoint` from a URI (private struct, same crate).
fn endpoint(uri: &str) -> StreamEndpoint {
    StreamEndpoint {
        uri: uri.to_string(),
        config: StreamConfig::from_uri(uri).unwrap(),
    }
}

#[test]
fn endpoint_creates_consumer_for_in() {
    let endpoint = endpoint("stream:in");
    let consumer = endpoint.create_consumer(Arc::new(RecordingRuntime::new(errors_log())));
    assert!(consumer.is_ok(), "stream:in must create a consumer");
}

#[test]
fn endpoint_rejects_consumer_for_out_and_err() {
    for uri in ["stream:out", "stream:err"] {
        let endpoint = endpoint(uri);
        let result = endpoint.create_consumer(Arc::new(RecordingRuntime::new(errors_log())));
        match result {
            Err(CamelError::EndpointCreationFailed(msg)) => {
                assert!(
                    msg.contains("stream:in"),
                    "error must name stream:in, got: {msg}"
                );
            }
            _other => panic!("expected EndpointCreationFailed for {uri}"),
        }
    }
}

#[test]
fn endpoint_rejects_producer_for_in() {
    let endpoint = endpoint("stream:in");
    let ctx = ProducerContext::new();
    let result = endpoint.create_producer(Arc::new(RecordingRuntime::new(errors_log())), &ctx);
    match result {
        Err(CamelError::EndpointCreationFailed(msg)) => {
            assert!(
                msg.contains("stream:out") && msg.contains("stream:err"),
                "error must name stream:out/stream:err, got: {msg}"
            );
        }
        other => {
            panic!("expected EndpointCreationFailed for stream:in producer, got: {other:?}")
        }
    }
}