camel-component-api 0.14.0

Component API trait and registry 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
use super::*;
use std::time::Duration;

#[test]
fn delay_for_grows_exponentially() {
    let p = NetworkRetryPolicy::default();
    let d0 = p.delay_for(0);
    let d1 = p.delay_for(1);
    let d2 = p.delay_for(2);
    // jitter makes exact values non-deterministic; verify ordering and bounds
    assert!(d0 >= Duration::from_millis(80)); // 100ms - 20% jitter floor
    assert!(d1 > d0);
    assert!(d2 > d1);
}

#[test]
fn delay_for_is_capped_at_max_delay() {
    let p = NetworkRetryPolicy::default();
    let d_high = p.delay_for(100);
    assert!(d_high <= p.max_delay);
}

#[test]
fn should_retry_false_when_disabled() {
    let p = NetworkRetryPolicy::disabled();
    assert!(!p.should_retry(0));
}

#[test]
fn should_retry_false_when_max_attempts_reached() {
    let p = NetworkRetryPolicy {
        max_attempts: 3,
        ..NetworkRetryPolicy::default()
    };
    assert!(p.should_retry(2));
    assert!(!p.should_retry(3));
}

#[test]
fn disabled_returns_disabled_policy() {
    let p = NetworkRetryPolicy::disabled();
    assert!(!p.enabled);
}

#[tokio::test]
async fn retry_async_succeeds_on_first_attempt() {
    let policy = NetworkRetryPolicy::default();
    let count = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
    let count_clone = count.clone();
    let result = retry_async::<u32, _, _, _, CamelError>(
        &policy,
        None,
        || {
            let c = count_clone.clone();
            async move {
                c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Ok::<_, CamelError>(42u32)
            }
        },
        |_| false,
    )
    .await;
    assert_eq!(result.unwrap(), 42);
    assert_eq!(count.load(std::sync::atomic::Ordering::SeqCst), 1);
}

#[tokio::test]
async fn retry_async_retries_on_transient_error() {
    let policy = NetworkRetryPolicy {
        max_attempts: 3,
        ..NetworkRetryPolicy::default()
    };
    let attempts = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
    let attempts_clone = attempts.clone();
    let result = retry_async::<u32, _, _, _, CamelError>(
        &policy,
        None,
        || {
            let c = attempts_clone.clone();
            async move {
                let n = c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                if n < 2 {
                    Err(CamelError::ProcessorError("transient".to_string()))
                } else {
                    Ok(99u32)
                }
            }
        },
        |_| true, // all errors transient
    )
    .await;
    assert_eq!(result.unwrap(), 99);
    assert_eq!(attempts.load(std::sync::atomic::Ordering::SeqCst), 3);
}

#[tokio::test]
async fn retry_async_does_not_retry_permanent_error() {
    let policy = NetworkRetryPolicy {
        max_attempts: 5,
        ..NetworkRetryPolicy::default()
    };
    let attempts = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
    let attempts_clone = attempts.clone();
    let result = retry_async::<u32, _, _, _, CamelError>(
        &policy,
        None,
        || {
            let c = attempts_clone.clone();
            async move {
                c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Err(CamelError::ProcessorError("permanent".to_string()))
            }
        },
        |_| false, // no errors transient
    )
    .await;
    assert!(result.is_err());
    assert_eq!(attempts.load(std::sync::atomic::Ordering::SeqCst), 1);
}

#[tokio::test]
async fn retry_async_exhausts_max_attempts() {
    let policy = NetworkRetryPolicy {
        max_attempts: 3,
        initial_delay: Duration::from_millis(1),
        max_delay: Duration::from_millis(5),
        ..NetworkRetryPolicy::default()
    };
    let attempts = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
    let attempts_clone = attempts.clone();
    let result = retry_async::<u32, _, _, _, CamelError>(
        &policy,
        None,
        || {
            let c = attempts_clone.clone();
            async move {
                c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Err(CamelError::ProcessorError("always fails".to_string()))
            }
        },
        |_| true,
    )
    .await;
    assert!(result.is_err());
    assert_eq!(attempts.load(std::sync::atomic::Ordering::SeqCst), 3);
}

#[test]
fn deserializes_from_toml_with_ms_fields() {
    let toml_str = r#"
            max_attempts = 5
            initial_delay_ms = 200
            max_delay_ms = 60000
            multiplier = 1.5
            jitter_factor = 0.1
        "#;
    let p: NetworkRetryPolicy = toml::from_str(toml_str).unwrap();
    assert_eq!(p.max_attempts, 5);
    assert_eq!(p.initial_delay, Duration::from_millis(200));
    assert_eq!(p.max_delay, Duration::from_millis(60_000));
    assert!((p.multiplier - 1.5).abs() < f64::EPSILON);
    assert!((p.jitter_factor - 0.1).abs() < f64::EPSILON);
}

#[tokio::test]
async fn retry_async_does_not_retry_when_disabled() {
    let policy = NetworkRetryPolicy::disabled();
    let attempts = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
    let attempts_clone = attempts.clone();
    let result = retry_async::<u32, _, _, _, CamelError>(
        &policy,
        None,
        || {
            let c = attempts_clone.clone();
            async move {
                c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Err(CamelError::ProcessorError("always fails".to_string()))
            }
        },
        |_| true,
    )
    .await;
    assert!(result.is_err());
    assert_eq!(attempts.load(std::sync::atomic::Ordering::SeqCst), 1);
}

#[test]
fn should_retry_with_max_attempts_zero_is_unlimited() {
    let p = NetworkRetryPolicy {
        max_attempts: 0,
        ..NetworkRetryPolicy::default()
    };
    assert!(p.should_retry(0));
    assert!(p.should_retry(100));
    assert!(p.should_retry(10_000));
}

#[test]
fn delay_for_with_zero_jitter_is_exact_exponential() {
    let p = NetworkRetryPolicy {
        initial_delay: Duration::from_millis(10),
        multiplier: 2.0,
        max_delay: Duration::from_millis(100),
        jitter_factor: 0.0,
        ..NetworkRetryPolicy::default()
    };
    assert_eq!(p.delay_for(0), Duration::from_millis(10));
    assert_eq!(p.delay_for(1), Duration::from_millis(20));
    assert_eq!(p.delay_for(2), Duration::from_millis(40));
    assert_eq!(p.delay_for(3), Duration::from_millis(80));
    assert_eq!(p.delay_for(4), Duration::from_millis(100));
}

// ── Generic retry_async with custom error type ──────────────────────

#[derive(Debug, PartialEq)]
struct TestError(bool); // bool = is_retryable

impl std::fmt::Display for TestError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "TestError({})", self.0)
    }
}

#[tokio::test]
async fn retry_async_works_with_custom_error_type() {
    use std::sync::atomic::{AtomicU32, Ordering};

    let policy = NetworkRetryPolicy {
        max_attempts: 2,
        initial_delay: Duration::from_millis(1),
        max_delay: Duration::from_millis(5),
        ..NetworkRetryPolicy::default()
    };

    let calls = std::sync::Arc::new(AtomicU32::new(0));
    let calls_clone = calls.clone();
    let result: Result<(), TestError> = retry_async(
        &policy,
        None,
        || {
            let c = calls_clone.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                Err(TestError(true))
            }
        },
        |e: &TestError| e.0,
    )
    .await;
    assert!(result.is_err());
    assert_eq!(calls.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn retry_async_custom_error_permanent_stops_immediately() {
    use std::sync::atomic::{AtomicU32, Ordering};

    let policy = NetworkRetryPolicy {
        max_attempts: 5,
        initial_delay: Duration::from_millis(1),
        ..NetworkRetryPolicy::default()
    };

    let calls = std::sync::Arc::new(AtomicU32::new(0));
    let calls_clone = calls.clone();
    let result: Result<(), TestError> = retry_async(
        &policy,
        None,
        || {
            let c = calls_clone.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                Err(TestError(false)) // permanent
            }
        },
        |e: &TestError| e.0,
    )
    .await;
    assert!(result.is_err());
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

// ── is_retryable_camel_error ────────────────────────────────────────

#[test]
fn is_retryable_camel_error_io_is_retryable() {
    let err = CamelError::Io("connection refused".to_string());
    assert!(is_retryable_camel_error(&err));
}

#[test]
fn is_retryable_camel_error_transient_marker_is_retryable() {
    let err = CamelError::ProcessorError("grpc[TRANSIENT][UNAVAILABLE]: down".to_string());
    assert!(is_retryable_camel_error(&err));
}

#[test]
fn is_retryable_camel_error_config_is_not_retryable() {
    let err = CamelError::Config("bad config".to_string());
    assert!(!is_retryable_camel_error(&err));
}

#[test]
fn is_retryable_camel_error_processor_error_no_marker_is_not_retryable() {
    let err = CamelError::ProcessorError("something went wrong".to_string());
    assert!(!is_retryable_camel_error(&err));
}

#[test]
fn is_retryable_camel_error_processor_with_source_transient_marker_is_retryable() {
    let err = CamelError::ProcessorErrorWithSource(
        "grpc[TRANSIENT][UNAVAILABLE]: down".to_string(),
        std::sync::Arc::new(std::io::Error::other("underlying cause")),
    );
    assert!(is_retryable_camel_error(&err));
}

#[test]
fn is_retryable_camel_error_processor_with_source_no_marker_is_not_retryable() {
    let err = CamelError::ProcessorErrorWithSource(
        "some non-transient error".to_string(),
        std::sync::Arc::new(std::io::Error::other("underlying cause")),
    );
    assert!(!is_retryable_camel_error(&err));
}

// ── is_retryable_camel_error via retry_async ─────────────────────────

#[tokio::test]
async fn retry_async_with_is_retryable_camel_error_retries_on_io_error() {
    use std::sync::atomic::{AtomicU32, Ordering};

    let policy = NetworkRetryPolicy {
        max_attempts: 3,
        initial_delay: Duration::from_millis(1),
        max_delay: Duration::from_millis(5),
        ..NetworkRetryPolicy::default()
    };

    let calls = std::sync::Arc::new(AtomicU32::new(0));
    let calls_clone = calls.clone();
    let result = retry_async(
        &policy,
        None,
        || {
            let c = calls_clone.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                Err::<u32, _>(CamelError::Io("connection refused".to_string()))
            }
        },
        is_retryable_camel_error,
    )
    .await;
    assert!(result.is_err());
    assert_eq!(calls.load(Ordering::SeqCst), 3);
}

#[tokio::test]
async fn retry_async_with_is_retryable_camel_error_stops_on_permanent_camel_error() {
    use std::sync::atomic::{AtomicU32, Ordering};

    let policy = NetworkRetryPolicy {
        max_attempts: 5,
        initial_delay: Duration::from_millis(1),
        ..NetworkRetryPolicy::default()
    };

    let calls = std::sync::Arc::new(AtomicU32::new(0));
    let calls_clone = calls.clone();
    let result = retry_async(
        &policy,
        None,
        || {
            let c = calls_clone.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                Err::<u32, _>(CamelError::Config("permanent".to_string()))
            }
        },
        is_retryable_camel_error,
    )
    .await;
    assert!(result.is_err());
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

// ── retry_async_cancelable tests ─────────────────────────────────────

#[tokio::test]
async fn retry_async_cancelable_succeeds_without_cancellation() {
    use std::sync::atomic::{AtomicU32, Ordering};

    let policy = NetworkRetryPolicy {
        max_attempts: 5,
        initial_delay: Duration::from_millis(1),
        max_delay: Duration::from_millis(5),
        ..NetworkRetryPolicy::default()
    };
    let cancel = tokio_util::sync::CancellationToken::new();

    let attempts = std::sync::Arc::new(AtomicU32::new(0));
    let attempts_clone = attempts.clone();
    let result: Result<u32, CamelError> = retry_async_cancelable(
        &policy,
        None,
        || {
            let c = attempts_clone.clone();
            async move {
                let n = c.fetch_add(1, Ordering::SeqCst);
                if n < 2 {
                    Err(CamelError::ProcessorError("transient".to_string()))
                } else {
                    Ok(99u32)
                }
            }
        },
        |_| true,
        &cancel,
    )
    .await;
    assert_eq!(result.unwrap(), 99);
    assert_eq!(attempts.load(Ordering::SeqCst), 3);
}

#[tokio::test]
async fn retry_async_cancelable_exhausts_attempts_when_cancel_not_signaled() {
    use std::sync::atomic::{AtomicU32, Ordering};

    let policy = NetworkRetryPolicy {
        max_attempts: 2,
        initial_delay: Duration::from_millis(1),
        max_delay: Duration::from_millis(5),
        ..NetworkRetryPolicy::default()
    };
    // Token created but never cancelled — should behave like regular retry_async
    let cancel = tokio_util::sync::CancellationToken::new();

    let calls = std::sync::Arc::new(AtomicU32::new(0));
    let calls_clone = calls.clone();
    let result: Result<u32, CamelError> = retry_async_cancelable(
        &policy,
        None,
        || {
            let c = calls_clone.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                Err(CamelError::ProcessorError("transient".to_string()))
            }
        },
        |_| true,
        &cancel,
    )
    .await;
    assert!(result.is_err());
    assert_eq!(calls.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn retry_async_cancelable_returns_last_error_when_cancelled_during_sleep() {
    use std::sync::atomic::{AtomicU32, Ordering};

    // Long delay so cancel is guaranteed to fire during the sleep,
    // not before op() runs or after it wakes.
    let policy = NetworkRetryPolicy {
        max_attempts: 5,
        initial_delay: Duration::from_secs(60),
        max_delay: Duration::from_secs(60),
        multiplier: 1.0,
        ..NetworkRetryPolicy::default()
    };
    let cancel = tokio_util::sync::CancellationToken::new();

    let calls = std::sync::Arc::new(AtomicU32::new(0));
    let calls_clone = calls.clone();
    let op_ran = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let op_ran_clone = op_ran.clone();
    let cancel_clone = cancel.clone();

    // Spawn cancel task: poll until op signals it has run (and is about to
    // return Err), THEN cancel. The retry will be in its sleep(60s) at
    // this point. Eliminates the race in the previous spawn+sleep(1ms) approach.
    let cancel_task = tokio::spawn(async move {
        while !op_ran_clone.load(std::sync::atomic::Ordering::SeqCst) {
            tokio::time::sleep(Duration::from_millis(1)).await;
        }
        cancel_clone.cancel();
    });

    let result: Result<u32, CamelError> = retry_async_cancelable(
        &policy,
        None,
        || {
            let c = calls_clone.clone();
            let flag = op_ran.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                flag.store(true, std::sync::atomic::Ordering::SeqCst);
                Err(CamelError::Io("connection refused".to_string()))
            }
        },
        |_| true,
        &cancel,
    )
    .await;

    cancel_task.await.ok();

    assert!(
        result.is_err(),
        "cancel should cause early return with error"
    );
    // Exactly one op call — cancel fired during first sleep, no second attempt
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn retry_async_cancelable_honors_cancel_signalled_during_op_at_next_sleep() {
    // Per docs: cancel is checked during inter-retry sleep, NOT during op itself.
    // If cancel fires while op is in-flight, op completes normally (proving
    // cancel didn't kill it), then the next sleep boundary observes the
    // cancel and aborts with the last operation error.
    use std::sync::atomic::{AtomicU32, Ordering};

    let policy = NetworkRetryPolicy {
        max_attempts: 5,
        initial_delay: Duration::from_secs(60),
        max_delay: Duration::from_secs(60),
        multiplier: 1.0,
        ..NetworkRetryPolicy::default()
    };
    let cancel = tokio_util::sync::CancellationToken::new();
    let cancel_clone = cancel.clone();

    let calls = std::sync::Arc::new(AtomicU32::new(0));
    let calls_clone = calls.clone();

    // Cancel fires during op's 20ms sleep (at ~5ms)
    let cancel_task = tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(5)).await;
        cancel_clone.cancel();
    });

    let result: Result<u32, CamelError> = retry_async_cancelable(
        &policy,
        None,
        || {
            let c = calls_clone.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                // Slow op: sleeps 20ms then returns retryable error.
                // Cancel fires at ~5ms during this sleep but op completes.
                tokio::time::sleep(Duration::from_millis(20)).await;
                Err(CamelError::Io("transient".to_string()))
            }
        },
        |_| true,
        &cancel,
    )
    .await;

    cancel_task.await.ok();

    // Op was called once and completed its 20ms sleep (cancel didn't kill it);
    // then the next sleep(60s) boundary observed cancel and aborted.
    assert!(
        result.is_err(),
        "cancel during op must abort at next sleep boundary"
    );
    assert_eq!(
        calls.load(Ordering::SeqCst),
        1,
        "op ran exactly once — cancel honored at sleep, not during op"
    );
}

#[tokio::test]
async fn retry_async_cancelable_does_not_retry_permanent_error() {
    use std::sync::atomic::{AtomicU32, Ordering};

    let policy = NetworkRetryPolicy {
        max_attempts: 5,
        initial_delay: Duration::from_millis(1),
        ..NetworkRetryPolicy::default()
    };
    let cancel = tokio_util::sync::CancellationToken::new();

    let calls = std::sync::Arc::new(AtomicU32::new(0));
    let calls_clone = calls.clone();
    let result: Result<u32, CamelError> = retry_async_cancelable(
        &policy,
        None,
        || {
            let c = calls_clone.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                Err(CamelError::ProcessorError("permanent".to_string()))
            }
        },
        |_| false, // no errors are retryable
        &cancel,
    )
    .await;
    assert!(result.is_err());
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

// ── Labeled retry observability tests ───────────────────────────────

use std::fmt::Write as FmtWrite;
use std::sync::{Arc, Mutex};
use tracing::Subscriber;
use tracing_subscriber::layer::SubscriberExt;

/// A tracing `Layer` that captures formatted event field key=value pairs
/// into a shared buffer.
struct CollectingLayer {
    events: Arc<Mutex<Vec<String>>>,
}

impl<S: Subscriber> tracing_subscriber::Layer<S> for CollectingLayer {
    fn on_event(
        &self,
        event: &tracing::Event<'_>,
        _ctx: tracing_subscriber::layer::Context<'_, S>,
    ) {
        let mut buf = String::new();
        let mut visitor = CollectingVisitor { fields: &mut buf };
        event.record(&mut visitor);
        if let Ok(mut events) = self.events.lock() {
            events.push(buf);
        }
    }
}

struct CollectingVisitor<'a> {
    fields: &'a mut String,
}

impl CollectingVisitor<'_> {
    fn record_field(&mut self, name: &str, value: &str) {
        write!(self.fields, " {name}={value}").ok();
    }
}

impl tracing::field::Visit for CollectingVisitor<'_> {
    fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
        self.record_field(field.name(), value);
    }
    fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
        self.record_field(field.name(), &format!("{value:?}"));
    }
    fn record_u64(&mut self, field: &tracing::field::Field, value: u64) {
        self.record_field(field.name(), &value.to_string());
    }
}

#[tokio::test]
async fn labeled_policy_emits_component_in_log_message() {
    let events = Arc::new(Mutex::new(Vec::new()));
    let layer = CollectingLayer {
        events: events.clone(),
    };
    let subscriber = tracing_subscriber::registry().with(layer);
    let _guard = tracing::subscriber::set_default(subscriber);

    let policy = NetworkRetryPolicy {
        max_attempts: 2,
        initial_delay: Duration::from_millis(1),
        max_delay: Duration::from_millis(5),
        ..NetworkRetryPolicy::default()
    };

    let result = retry_async::<u32, _, _, _, CamelError>(
        &policy,
        Some("ws-producer"),
        || async { Err(CamelError::Io("connection refused".to_string())) },
        |_| true,
    )
    .await;

    assert!(result.is_err());
    let captured = events.lock().unwrap();
    assert!(
        !captured.is_empty(),
        "expected at least one log event, got none"
    );
    // The first (and only) retry should contain the component label
    let first = &captured[0];
    assert!(
        first.contains("component=ws-producer"),
        "expected 'component=ws-producer' in log event, got: {first}"
    );
    assert!(
        first.contains("ws-producer: transient error"),
        "expected 'ws-producer: transient error' in log event, got: {first}"
    );
}

#[tokio::test]
async fn labeled_policy_preserves_structured_fields() {
    let events = Arc::new(Mutex::new(Vec::new()));
    let layer = CollectingLayer {
        events: events.clone(),
    };
    let subscriber = tracing_subscriber::registry().with(layer);
    let _guard = tracing::subscriber::set_default(subscriber);

    let policy = NetworkRetryPolicy {
        max_attempts: 2,
        initial_delay: Duration::from_millis(1),
        max_delay: Duration::from_millis(5),
        ..NetworkRetryPolicy::default()
    };

    let result = retry_async::<u32, _, _, _, CamelError>(
        &policy,
        Some("sql-producer"),
        || async { Err(CamelError::Io("timeout".to_string())) },
        |_| true,
    )
    .await;

    assert!(result.is_err());
    let captured = events.lock().unwrap();
    assert!(!captured.is_empty());
    let first = &captured[0];
    // Structured fields must be present
    assert!(
        first.contains("attempt=0"),
        "expected 'attempt=0' field, got: {first}"
    );
    assert!(
        first.contains("component=sql-producer"),
        "expected 'component=sql-producer' field, got: {first}"
    );
    assert!(
        first.contains("error=IO error: timeout"),
        "expected 'error=IO error: timeout' field, got: {first}"
    );
    assert!(
        first.contains("delay_ms="),
        "expected 'delay_ms=' field, got: {first}"
    );
}

#[tokio::test]
async fn unlabeled_policy_omits_component_field() {
    let events = Arc::new(Mutex::new(Vec::new()));
    let layer = CollectingLayer {
        events: events.clone(),
    };
    let subscriber = tracing_subscriber::registry().with(layer);
    let _guard = tracing::subscriber::set_default(subscriber);

    let policy = NetworkRetryPolicy {
        max_attempts: 2,
        initial_delay: Duration::from_millis(1),
        max_delay: Duration::from_millis(5),
        ..NetworkRetryPolicy::default()
    };
    // Unlabeled path (label = None) — no component field emitted

    let result = retry_async::<u32, _, _, _, CamelError>(
        &policy,
        None,
        || async { Err(CamelError::Io("connection refused".to_string())) },
        |_| true,
    )
    .await;

    assert!(result.is_err());
    let captured = events.lock().unwrap();
    assert!(!captured.is_empty());
    let first = &captured[0];
    assert!(
        !first.contains("component="),
        "expected no 'component=' field in unlabeled path, got: {first}"
    );
    assert!(
        first.contains("transient error — retrying"),
        "expected bare 'transient error — retrying' message, got: {first}"
    );
}