crabbyq 1.0.0

A declarative async Rust framework for message-driven microservices.
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
mod common;

use common::{TestBroker, TestBrokerMessage};
use crabbyq::prelude::*;
use crabbyq::response::HandlerOutcome;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use tower::{Layer, Service};

#[derive(Clone)]
struct AppState {
    records: Arc<Mutex<Vec<String>>>,
}

#[derive(Clone)]
struct SharedRecords(Arc<Mutex<Vec<String>>>);

impl FromRef<AppState> for SharedRecords {
    fn from_ref(input: &AppState) -> Self {
        Self(input.records.clone())
    }
}

#[derive(Deserialize, Serialize)]
struct Payload {
    id: u32,
}

#[derive(Deserialize, Serialize)]
struct SumRequest {
    left: i32,
    right: i32,
}

#[derive(Deserialize, Serialize)]
struct SumResponse {
    result: i32,
}

#[derive(Debug)]
struct TestError(&'static str);

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

impl std::error::Error for TestError {}

impl IntoResponse for TestError {
    fn into_response(self) -> Result<HandlerResponse, CrabbyError> {
        Ok(None)
    }
}

#[derive(Deserialize)]
struct ErrorEnvelope {
    subject: String,
    reply_to: Option<String>,
    headers: Option<HeaderMap>,
    payload: Vec<u8>,
    error: String,
}

async fn noop_handler(_event: Event) -> CrabbyResult<()> {
    Ok(())
}

#[derive(Clone)]
struct RecordingLayer {
    seen: Arc<Mutex<Vec<String>>>,
}

impl<S> Layer<S> for RecordingLayer {
    type Service = RecordingService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        RecordingService {
            inner,
            seen: self.seen.clone(),
        }
    }
}

#[derive(Clone)]
struct RecordingService<S> {
    inner: S,
    seen: Arc<Mutex<Vec<String>>>,
}

impl<S> Service<Event> for RecordingService<S>
where
    S: Service<Event, Response = HandlerOutcome, Error = CrabbyError> + Send,
    S::Future: Send + 'static,
{
    type Response = HandlerOutcome;
    type Error = CrabbyError;
    type Future = S::Future;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, request: Event) -> Self::Future {
        self.seen
            .lock()
            .unwrap()
            .push(request.subject().to_string());
        self.inner.call(request)
    }
}

#[test]
#[should_panic(expected = "duplicate route key 'dup' is already registered")]
fn duplicate_route_keys_panic() {
    let _ = Router::new()
        .route("dup", noop_handler)
        .route("dup", noop_handler);
}

#[tokio::test]
async fn state_subject_and_json_extractors_work_together() {
    let records = Arc::new(Mutex::new(Vec::new()));
    let state = AppState {
        records: records.clone(),
    };

    let message = TestBrokerMessage::new(
        "orders.created",
        serde_json::to_vec(&Payload { id: 7 }).unwrap(),
    );
    let broker = TestBroker::new(vec![message]);

    let app = Router::new()
        .set_state(state)
        .route(
            "orders.created",
            |State(shared): State<SharedRecords>,
             Subject(subject): Subject,
             Json(payload): Json<Payload>| async move {
                shared
                    .0
                    .lock()
                    .unwrap()
                    .push(format!("{subject}:{}", payload.id));
                Ok::<(), CrabbyError>(())
            },
        )
        .into_service(broker);

    app.serve().await.unwrap();

    assert_eq!(records.lock().unwrap().as_slice(), ["orders.created:7"]);
}

#[tokio::test]
async fn routes_registers_one_handler_for_multiple_subjects() {
    let records = Arc::new(Mutex::new(Vec::new()));
    let broker = TestBroker::new(vec![
        TestBrokerMessage::new("alpha", Vec::new()),
        TestBrokerMessage::new("beta", Vec::new()),
    ]);

    let app = Router::new()
        .set_state(records.clone())
        .routes(
            ["alpha", "beta"],
            |event: Event, records: Arc<Mutex<Vec<String>>>| async move {
                records.lock().unwrap().push(event.subject().to_string());
                Ok::<(), CrabbyError>(())
            },
        )
        .into_service(broker);

    app.serve().await.unwrap();

    assert_eq!(records.lock().unwrap().as_slice(), ["alpha", "beta"]);
}

#[tokio::test]
async fn publisher_extractor_emits_follow_up_message() {
    let broker = TestBroker::new(vec![TestBrokerMessage::new("source", Vec::new())]);
    let published_broker = broker.clone();

    let app = Router::new()
        .route(
            "source",
            |Publish(publisher): Publish, Subject(subject): Subject| async move {
                publisher
                    .publish(
                        "follow-up",
                        Json(Payload {
                            id: subject.len() as u32,
                        }),
                    )
                    .await?;
                Ok::<(), CrabbyError>(())
            },
        )
        .into_service(broker);

    app.serve().await.unwrap();

    let published = published_broker.published_messages();
    assert_eq!(published.len(), 1);
    assert_eq!(published[0].subject, "follow-up");
    assert_eq!(
        published[0].headers.as_ref().unwrap().get("content-type"),
        Some(&"application/json".to_string())
    );

    let payload: Payload = serde_json::from_slice(&published[0].payload).unwrap();
    assert_eq!(payload.id, "source".len() as u32);
}

#[tokio::test]
async fn rpc_reply_is_published_to_reply_subject() {
    let broker = TestBroker::new(vec![
        TestBrokerMessage::new(
            "sum",
            serde_json::to_vec(&SumRequest {
                left: 20,
                right: 22,
            })
            .unwrap(),
        )
        .with_reply_to("_reply.sum"),
    ]);
    let published_broker = broker.clone();

    let app = Router::new()
        .route("sum", |Json(request): Json<SumRequest>| async move {
            Ok::<Json<SumResponse>, CrabbyError>(Json(SumResponse {
                result: request.left + request.right,
            }))
        })
        .into_service(broker);

    app.serve().await.unwrap();

    let published = published_broker.published_messages();
    assert_eq!(published.len(), 1);
    assert_eq!(published[0].subject, "_reply.sum");
    assert_eq!(
        published[0].headers.as_ref().unwrap().get("content-type"),
        Some(&"application/json".to_string())
    );

    let reply: SumResponse = serde_json::from_slice(&published[0].payload).unwrap();
    assert_eq!(reply.result, 42);
}

#[tokio::test]
async fn service_level_error_topic_is_used_as_fallback() {
    let message_headers = HeaderMap::from([("trace-id".to_string(), "abc".to_string())]);

    let broker = TestBroker::new(vec![
        TestBrokerMessage::new("jobs.run", b"payload".to_vec())
            .with_headers(message_headers.clone()),
    ]);
    let published_broker = broker.clone();

    let app = Router::new()
        .route("jobs.run", |_event: Event| async move {
            Err::<(), _>(TestError("job failed"))
        })
        .into_service(broker)
        .on_error("errors.default");

    app.serve().await.unwrap();

    let published = published_broker.published_messages();
    assert_eq!(published.len(), 1);
    assert_eq!(published[0].subject, "errors.default");
    assert_eq!(
        published[0].headers.as_ref().unwrap().get("content-type"),
        Some(&"application/json".to_string())
    );

    let envelope: ErrorEnvelope = serde_json::from_slice(&published[0].payload).unwrap();
    assert_eq!(envelope.subject, "jobs.run");
    assert_eq!(envelope.reply_to, None);
    assert_eq!(envelope.headers.unwrap(), message_headers);
    assert_eq!(envelope.payload, b"payload".to_vec());
    assert_eq!(envelope.error, "job failed");
}

#[tokio::test]
async fn router_error_topic_overrides_service_fallback_and_merges_headers() {
    let broker = TestBroker::new(vec![TestBrokerMessage::new(
        "camera.sync",
        b"frame".to_vec(),
    )]);
    let published_broker = broker.clone();

    let route_headers = HeaderMap::from([
        ("x-router".to_string(), "camera".to_string()),
        ("x-shared".to_string(), "route".to_string()),
    ]);
    let service_headers = HeaderMap::from([
        ("x-service".to_string(), "default".to_string()),
        ("x-shared".to_string(), "service".to_string()),
    ]);

    let app = Router::new()
        .on_error("errors.camera")
        .error_headers(route_headers)
        .route("camera.sync", |_event: Event| async move {
            Err::<(), _>(TestError("camera failed"))
        })
        .into_service(broker)
        .dlq("errors.default")
        .error_headers(service_headers);

    app.serve().await.unwrap();

    let published = published_broker.published_messages();
    assert_eq!(published.len(), 1);
    assert_eq!(published[0].subject, "errors.camera");

    let headers = published[0].headers.as_ref().unwrap();
    assert_eq!(
        headers.get("content-type"),
        Some(&"application/json".to_string())
    );
    assert_eq!(headers.get("x-service"), Some(&"default".to_string()));
    assert_eq!(headers.get("x-router"), Some(&"camera".to_string()));
    assert_eq!(headers.get("x-shared"), Some(&"route".to_string()));
}

#[tokio::test]
async fn shutdown_hook_can_publish_final_message() {
    let broker = TestBroker::new(Vec::new());
    let published_broker = broker.clone();

    let app = Router::new()
        .into_service(broker)
        .with_graceful_shutdown(async {})
        .on_shutdown(|publisher| async move {
            publisher
                .publish("service.stopped", Json(Payload { id: 1 }))
                .await?;
            Ok(())
        });

    app.serve().await.unwrap();

    let published = published_broker.published_messages();
    assert_eq!(published.len(), 1);
    assert_eq!(published[0].subject, "service.stopped");
    assert_eq!(
        published[0].headers.as_ref().unwrap().get("content-type"),
        Some(&"application/json".to_string())
    );
}

#[tokio::test]
async fn publisher_request_returns_decodable_reply() {
    let broker = TestBroker::new(Vec::new()).with_request_replies(vec![
        TestBrokerMessage::new(
            "_reply.sum",
            serde_json::to_vec(&SumResponse { result: 42 }).unwrap(),
        )
        .with_headers(HeaderMap::from([(
            "content-type".to_string(),
            "application/json".to_string(),
        )])),
    ]);
    let recorded_broker = broker.clone();
    let publisher = Publisher::new(broker);

    let reply = publisher
        .request(
            "rpc.sum",
            Json(SumRequest {
                left: 19,
                right: 23,
            }),
        )
        .await
        .unwrap();

    assert_eq!(reply.subject(), "_reply.sum");
    assert_eq!(
        reply.headers().unwrap().get("content-type"),
        Some(&"application/json".to_string())
    );

    let body: SumResponse = reply.into_json().unwrap();
    assert_eq!(body.result, 42);

    let requests = recorded_broker.requested_messages();
    assert_eq!(requests.len(), 1);
    assert_eq!(requests[0].subject, "rpc.sum");
}

#[tokio::test]
async fn router_layer_wraps_registered_routes() {
    let seen = Arc::new(Mutex::new(Vec::new()));
    let broker = TestBroker::new(vec![TestBrokerMessage::new("layered", Vec::new())]);

    let app = Router::new()
        .route("layered", |_event: Event| async move {
            Ok::<(), CrabbyError>(())
        })
        .layer(RecordingLayer { seen: seen.clone() })
        .into_service(broker);

    app.serve().await.unwrap();

    assert_eq!(seen.lock().unwrap().as_slice(), ["layered"]);
}