camel-master 0.16.0

Master crate for Rust Camel, re-exporting all public APIs of the workspace
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
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use async_trait::async_trait;
use camel_api::{
    CamelError, Exchange, LeadershipEvent, LeadershipHandle, LeadershipService, Message,
    MetricsCollector, NoOpMetrics, NoopPlatformService, NoopReadinessGate, PlatformError,
    PlatformIdentity, PlatformService, ReadinessGate,
};
use camel_component_api::{
    BoxProcessor, Component, ComponentContext, Consumer, ConsumerContext, Endpoint,
    ProducerContext, RuntimeObservability,
};
use camel_language_api::Language;
use camel_master::MasterComponent;
use tokio::sync::{oneshot, watch};
use tokio::time::timeout;
use tokio_util::sync::CancellationToken;

struct TestComponentContext {
    delegate: Arc<dyn Component>,
    platform_service: Arc<dyn PlatformService>,
}

impl ComponentContext for TestComponentContext {
    fn resolve_component(&self, scheme: &str) -> Option<Arc<dyn Component>> {
        if self.delegate.scheme() == scheme {
            Some(Arc::clone(&self.delegate))
        } else {
            None
        }
    }

    fn resolve_language(&self, _name: &str) -> Option<Arc<dyn Language>> {
        None
    }

    fn metrics(&self) -> Arc<dyn MetricsCollector> {
        Arc::new(NoOpMetrics)
    }

    fn platform_service(&self) -> Arc<dyn PlatformService> {
        Arc::clone(&self.platform_service)
    }

    fn register_route_health_check(
        &self,
        _route_id: &str,
        _check: Arc<dyn camel_api::AsyncHealthCheck>,
    ) {
    }

    fn unregister_route_health_check(&self, _route_id: &str) {}
}

struct TestDelegateComponent;

impl Component for TestDelegateComponent {
    fn scheme(&self) -> &str {
        "test"
    }

    fn create_endpoint(
        &self,
        uri: &str,
        _ctx: &dyn ComponentContext,
    ) -> Result<Box<dyn Endpoint>, CamelError> {
        Ok(Box::new(TestDelegateEndpoint {
            uri: uri.to_string(),
        }))
    }
}

struct TestDelegateEndpoint {
    uri: String,
}

impl Endpoint for TestDelegateEndpoint {
    fn uri(&self) -> &str {
        &self.uri
    }

    fn create_consumer(
        &self,
        _rt: std::sync::Arc<dyn camel_component_api::RuntimeObservability>,
    ) -> Result<Box<dyn Consumer>, CamelError> {
        Ok(Box::new(TestDelegateConsumer))
    }

    fn create_producer(
        &self,
        _rt: std::sync::Arc<dyn camel_component_api::RuntimeObservability>,
        _ctx: &ProducerContext,
    ) -> Result<BoxProcessor, CamelError> {
        Err(CamelError::EndpointCreationFailed("not used".to_string()))
    }
}

struct TestDelegateConsumer;

#[async_trait]
impl Consumer for TestDelegateConsumer {
    async fn start(&mut self, context: ConsumerContext) -> Result<(), CamelError> {
        context
            .send(Exchange::new(Message::new("delegate-ok")))
            .await?;
        context.cancelled().await;
        Ok(())
    }

    async fn stop(&mut self) -> Result<(), CamelError> {
        Ok(())
    }
}

struct FakeLeadershipService {
    seen_lock_name: Arc<Mutex<Option<String>>>,
    tx: Arc<Mutex<Option<watch::Sender<Option<LeadershipEvent>>>>>,
}

struct DropSensitiveLeadershipService {
    canceled: Arc<AtomicBool>,
}

impl DropSensitiveLeadershipService {
    fn new() -> Self {
        Self {
            canceled: Arc::new(AtomicBool::new(false)),
        }
    }

    fn was_canceled(&self) -> bool {
        self.canceled.load(Ordering::Acquire)
    }
}

#[async_trait]
impl LeadershipService for DropSensitiveLeadershipService {
    async fn start(&self, _lock_name: &str) -> Result<LeadershipHandle, PlatformError> {
        let (tx, rx) = watch::channel(Some(LeadershipEvent::StartedLeading));
        let cancel = CancellationToken::new();
        let cancel_wait = cancel.clone();
        let canceled = Arc::clone(&self.canceled);
        let (term_tx, term_rx) = oneshot::channel();

        tokio::spawn(async move {
            cancel_wait.cancelled().await;
            canceled.store(true, Ordering::Release);
            let _ = tx.send(Some(LeadershipEvent::StoppedLeading));
            let _ = term_tx.send(());
        });

        Ok(LeadershipHandle::new(
            rx,
            Arc::new(AtomicBool::new(true)),
            cancel,
            term_rx,
        ))
    }
}

impl FakeLeadershipService {
    fn new() -> Self {
        Self {
            seen_lock_name: Arc::new(Mutex::new(None)),
            tx: Arc::new(Mutex::new(None)),
        }
    }

    fn seen_lock_name(&self) -> Option<String> {
        self.seen_lock_name
            .lock()
            .expect("fake leadership mutex poisoned")
            .clone()
    }
}

#[async_trait]
impl LeadershipService for FakeLeadershipService {
    async fn start(&self, lock_name: &str) -> Result<LeadershipHandle, PlatformError> {
        *self
            .seen_lock_name
            .lock()
            .expect("fake leadership mutex poisoned") = Some(lock_name.to_string());

        let (tx, rx) = watch::channel(Some(LeadershipEvent::StartedLeading));
        *self.tx.lock().expect("fake leadership tx mutex poisoned") = Some(tx.clone());

        let cancel = CancellationToken::new();
        let cancel_wait = cancel.clone();
        let tx_for_task = tx;
        let tx_store = Arc::clone(&self.tx);
        let (term_tx, term_rx) = oneshot::channel();
        tokio::spawn(async move {
            cancel_wait.cancelled().await;
            let _ = tx_for_task.send(Some(LeadershipEvent::StoppedLeading));
            if let Ok(mut guard) = tx_store.lock() {
                let _ = guard.take();
            }
            let _ = term_tx.send(());
        });

        Ok(LeadershipHandle::new(
            rx,
            Arc::new(AtomicBool::new(true)),
            cancel,
            term_rx,
        ))
    }
}

struct FlakyDelegateComponent {
    failures_remaining: Arc<std::sync::atomic::AtomicUsize>,
}

impl Component for FlakyDelegateComponent {
    fn scheme(&self) -> &str {
        "test"
    }

    fn create_endpoint(
        &self,
        uri: &str,
        _ctx: &dyn ComponentContext,
    ) -> Result<Box<dyn Endpoint>, CamelError> {
        let prev = self
            .failures_remaining
            .fetch_update(
                std::sync::atomic::Ordering::SeqCst,
                std::sync::atomic::Ordering::SeqCst,
                |v| v.checked_sub(1),
            )
            .unwrap_or(0);

        if prev > 0 {
            return Err(CamelError::Io(
                "transient delegate endpoint error".to_string(),
            ));
        }

        Ok(Box::new(TestDelegateEndpoint {
            uri: uri.to_string(),
        }))
    }
}

struct FakePlatformService {
    identity: PlatformIdentity,
    readiness_gate: Arc<dyn ReadinessGate>,
    leadership: Arc<dyn LeadershipService>,
}

impl FakePlatformService {
    fn new(leadership: Arc<dyn LeadershipService>) -> Self {
        Self {
            identity: PlatformIdentity::local("test-node"),
            readiness_gate: Arc::new(NoopReadinessGate),
            leadership,
        }
    }
}

impl PlatformService for FakePlatformService {
    fn identity(&self) -> PlatformIdentity {
        self.identity.clone()
    }

    fn readiness_gate(&self) -> Arc<dyn ReadinessGate> {
        Arc::clone(&self.readiness_gate)
    }

    fn leadership(&self) -> Arc<dyn LeadershipService> {
        Arc::clone(&self.leadership)
    }
}

#[tokio::test]
async fn works_with_noop_platform_service() {
    let delegate = Arc::new(TestDelegateComponent);
    let ctx = TestComponentContext {
        delegate,
        platform_service: Arc::new(NoopPlatformService::default()),
    };

    let component = MasterComponent::default();
    let endpoint = component
        .create_endpoint("master:leader-lock:test:delegate", &ctx)
        .unwrap();
    let rt: std::sync::Arc<dyn RuntimeObservability> =
        std::sync::Arc::new(camel_component_api::NoOpComponentContext);
    let mut consumer = endpoint.create_consumer(rt).unwrap();

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let cancel = CancellationToken::new();
    let consumer_ctx = ConsumerContext::new(tx, cancel.clone(), "master-test-route".to_string());

    consumer.start(consumer_ctx).await.unwrap();

    let first = timeout(Duration::from_millis(300), rx.recv())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(first.exchange.input.body.as_text(), Some("delegate-ok"));

    cancel.cancel();
    consumer.stop().await.unwrap();
}

#[tokio::test]
async fn lock_name_is_forwarded_to_leadership_service() {
    let delegate = Arc::new(TestDelegateComponent);
    let fake = Arc::new(FakeLeadershipService::new());
    let ctx = TestComponentContext {
        delegate,
        platform_service: Arc::new(FakePlatformService::new(fake.clone())),
    };

    let component = MasterComponent::default();
    let endpoint = component
        .create_endpoint("master:lease-name:test:delegate", &ctx)
        .unwrap();
    let rt: std::sync::Arc<dyn RuntimeObservability> =
        std::sync::Arc::new(camel_component_api::NoOpComponentContext);
    let mut consumer = endpoint.create_consumer(rt).unwrap();

    let (tx, _rx) = tokio::sync::mpsc::channel(8);
    let cancel = CancellationToken::new();
    let consumer_ctx = ConsumerContext::new(tx, cancel.clone(), "master-test-route".to_string());

    consumer.start(consumer_ctx).await.unwrap();

    assert_eq!(fake.seen_lock_name().as_deref(), Some("lease-name"));

    cancel.cancel();
    consumer.stop().await.unwrap();
}

#[tokio::test]
async fn retries_delegate_start_while_leadership_stays_started() {
    let delegate = Arc::new(FlakyDelegateComponent {
        failures_remaining: Arc::new(std::sync::atomic::AtomicUsize::new(1)),
    });
    let fake = Arc::new(FakeLeadershipService::new());
    let ctx = TestComponentContext {
        delegate,
        platform_service: Arc::new(FakePlatformService::new(fake)),
    };

    let component = MasterComponent::default();
    let endpoint = component
        .create_endpoint("master:retry-lock:test:delegate", &ctx)
        .unwrap();
    let rt: std::sync::Arc<dyn RuntimeObservability> =
        std::sync::Arc::new(camel_component_api::NoOpComponentContext);
    let mut consumer = endpoint.create_consumer(rt).unwrap();

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let cancel = CancellationToken::new();
    let consumer_ctx = ConsumerContext::new(tx, cancel.clone(), "master-test-route".to_string());

    consumer.start(consumer_ctx).await.unwrap();

    let first = timeout(Duration::from_secs(2), rx.recv())
        .await
        .expect("delegate should eventually recover while leadership is sustained")
        .expect("channel should contain delegate message");
    assert_eq!(first.exchange.input.body.as_text(), Some("delegate-ok"));

    cancel.cancel();
    consumer.stop().await.unwrap();
}

#[tokio::test]
async fn dropping_consumer_after_start_does_not_step_down_leadership_immediately() {
    let delegate = Arc::new(TestDelegateComponent);
    let leadership = Arc::new(DropSensitiveLeadershipService::new());
    let ctx = TestComponentContext {
        delegate,
        platform_service: Arc::new(FakePlatformService::new(leadership.clone())),
    };

    let component = MasterComponent::default();
    let endpoint = component
        .create_endpoint("master:drop-lock:test:delegate", &ctx)
        .unwrap();
    let rt: std::sync::Arc<dyn RuntimeObservability> =
        std::sync::Arc::new(camel_component_api::NoOpComponentContext);
    let mut consumer = endpoint.create_consumer(rt).unwrap();

    let (tx, _rx) = tokio::sync::mpsc::channel(8);
    let consumer_ctx = ConsumerContext::new(
        tx,
        CancellationToken::new(),
        "master-test-route".to_string(),
    );

    consumer.start(consumer_ctx).await.unwrap();
    drop(consumer);

    tokio::time::sleep(Duration::from_millis(50)).await;
    assert!(!leadership.was_canceled());
}