mq-bridge 0.3.8

An asynchronous message bridging library connecting Kafka, MQTT, AMQP, NATS, MongoDB, HTTP, and more.
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
#![cfg(feature = "websocket")]

use futures::{SinkExt, StreamExt};
use mq_bridge::endpoints::websocket::WebSocketConsumer;
use mq_bridge::endpoints::websocket::WebSocketPublisher;
use mq_bridge::models::{Endpoint, EndpointType, WebSocketConfig, WebSocketExecutionMode};
use mq_bridge::traits::{MessageConsumer, MessageDisposition, MessagePublisher};
use mq_bridge::{CanonicalMessage, Handled, HandlerError, Route};
use tokio_websockets::{ClientBuilder, Message};

async fn echo(msg: CanonicalMessage) -> Result<Handled, HandlerError> {
    Ok(Handled::Publish(msg))
}

async fn ack(_msg: CanonicalMessage) -> Result<Handled, HandlerError> {
    Ok(Handled::Ack)
}

fn get_free_port() -> u16 {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    listener.local_addr().unwrap().port()
}

async fn connect(
    url: impl AsRef<str>,
) -> tokio_websockets::WebSocketStream<tokio_websockets::MaybeTlsStream<tokio::net::TcpStream>> {
    let uri = url.as_ref().parse().expect("websocket URL should parse");
    let (stream, _) = ClientBuilder::from_uri(uri)
        .connect()
        .await
        .expect("client should connect");
    stream
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_endpoint_roundtrip() {
    let mut consumer =
        WebSocketConsumer::new(&WebSocketConfig::new("127.0.0.1:0").with_path("/events"))
            .await
            .expect("consumer should be created");

    let publisher = WebSocketPublisher::new(&WebSocketConfig::new(consumer.url().to_string()));

    publisher
        .send(
            CanonicalMessage::from_vec("hello integration")
                .with_metadata_kv("ws_message_type", "text"),
        )
        .await
        .expect("publisher should send");

    let mut batch = consumer
        .receive_batch(1)
        .await
        .expect("consumer should receive");
    assert_eq!(batch.messages.len(), 1);
    let message = batch.messages.pop().expect("one message");
    assert_eq!(message.get_payload_str(), "hello integration");
    assert_eq!(
        message.metadata.get("ws_message_type").map(String::as_str),
        Some("text")
    );
    assert_eq!(
        message.metadata.get("ws_path").map(String::as_str),
        Some("/events")
    );
    (batch.commit)(vec![MessageDisposition::Ack])
        .await
        .expect("commit should succeed");
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_endpoint_handles_binary_payloads() {
    let mut consumer =
        WebSocketConsumer::new(&WebSocketConfig::new("127.0.0.1:0").with_path("/binary"))
            .await
            .expect("consumer should be created");

    let publisher = WebSocketPublisher::new(&WebSocketConfig::new(consumer.url().to_string()));

    publisher
        .send(
            CanonicalMessage::new(vec![0, 1, 2, 3, 255], None)
                .with_metadata_kv("ws_message_type", "binary"),
        )
        .await
        .expect("publisher should send");

    let mut batch = consumer
        .receive_batch(1)
        .await
        .expect("consumer should receive");
    assert_eq!(batch.messages.len(), 1);
    let message = batch.messages.pop().expect("one message");
    assert_eq!(message.payload.as_ref(), &[0, 1, 2, 3, 255]);
    assert_eq!(
        message.metadata.get("ws_message_type").map(String::as_str),
        Some("binary")
    );
    (batch.commit)(vec![MessageDisposition::Ack])
        .await
        .expect("commit should succeed");
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_publisher_reuses_connection_for_single_sends() {
    let mut consumer = WebSocketConsumer::new(&WebSocketConfig {
        routed_queue_capacity: Some(16),
        ..WebSocketConfig::new("127.0.0.1:0").with_path("/single-reuse")
    })
    .await
    .expect("consumer should be created");
    let publisher = WebSocketPublisher::new(&WebSocketConfig::new(consumer.url().to_string()));

    publisher
        .send(CanonicalMessage::from_vec("first").with_metadata_kv("ws_message_type", "text"))
        .await
        .expect("first message should send");
    publisher
        .send(CanonicalMessage::from_vec("second").with_metadata_kv("ws_message_type", "text"))
        .await
        .expect("second message should send");

    let mut peers = Vec::new();
    while peers.len() < 2 {
        let batch = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            consumer.receive_batch(2 - peers.len()),
        )
        .await
        .expect("consumer should not time out")
        .expect("consumer should receive");
        let batch_len = batch.messages.len();
        for message in batch.messages {
            peers.push(
                message
                    .metadata
                    .get("ws_peer_addr")
                    .expect("message should include peer addr")
                    .clone(),
            );
        }
        (batch.commit)(vec![MessageDisposition::Ack; batch_len])
            .await
            .expect("commit should succeed");
    }

    let first_peer = &peers[0];
    let second_peer = &peers[1];
    assert_eq!(first_peer, second_peer);
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_publisher_batch_ack_means_all_messages_are_received() {
    let mut consumer = WebSocketConsumer::new(&WebSocketConfig {
        routed_queue_capacity: Some(2048),
        ..WebSocketConfig::new("127.0.0.1:0").with_path("/batch")
    })
    .await
    .expect("consumer should be created");
    let publisher = WebSocketPublisher::new(&WebSocketConfig::new(consumer.url().to_string()));

    let expected = 1000;
    let messages = (0..expected)
        .map(|index| {
            CanonicalMessage::from_vec(format!("message-{index}"))
                .with_metadata_kv("ws_message_type", "text")
        })
        .collect();
    publisher
        .send_batch(messages)
        .await
        .expect("publisher should ack only after flushing the batch");

    let mut received = 0;
    while received < expected {
        let batch = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            consumer.receive_batch(expected - received),
        )
        .await
        .expect("consumer should not time out")
        .expect("consumer should receive");
        let batch_len = batch.messages.len();
        received += batch_len;
        let commit = batch.commit;
        commit(vec![MessageDisposition::Ack; batch_len])
            .await
            .expect("commit should succeed");
    }

    assert_eq!(received, expected);
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_consumer_sends_commit_reply_to_client() {
    let mut consumer =
        WebSocketConsumer::new(&WebSocketConfig::new("127.0.0.1:0").with_path("/reply"))
            .await
            .expect("consumer should be created");

    let mut stream = connect(consumer.url()).await;

    stream
        .send(Message::text("request"))
        .await
        .expect("client should send request");

    let mut batch = consumer
        .receive_batch(1)
        .await
        .expect("consumer should receive request");
    assert_eq!(batch.messages.len(), 1);
    let request = batch.messages.pop().expect("one request");
    assert_eq!(request.get_payload_str(), "request");

    (batch.commit)(vec![MessageDisposition::Reply(
        CanonicalMessage::from_vec("response").with_metadata_kv("ws_message_type", "text"),
    )])
    .await
    .expect("reply commit should succeed");

    let reply = stream
        .next()
        .await
        .expect("client should receive response")
        .expect("response frame should be valid");
    assert_eq!(reply.as_text(), Some("response"));
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_routed_consumer_replies_to_ping_exactly_once() {
    let consumer = WebSocketConsumer::new(&WebSocketConfig::new("127.0.0.1:0").with_path("/ping"))
        .await
        .expect("consumer should be created");

    let mut stream = connect(consumer.url()).await;
    stream
        .send(Message::ping("hello"))
        .await
        .expect("client should send ping");

    let pong = stream
        .next()
        .await
        .expect("client should receive pong")
        .expect("pong frame should be valid");
    assert!(pong.is_pong());
    assert_eq!(&pong.as_payload()[..], b"hello");

    // The routed transport must not emit a second (duplicate) pong frame.
    let extra = tokio::time::timeout(std::time::Duration::from_millis(150), stream.next()).await;
    assert!(extra.is_err(), "ping should produce exactly one pong reply");

    drop(consumer);
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_direct_response_route_replies_with_handler() {
    let port = get_free_port();
    let input = Endpoint::new(EndpointType::WebSocket(
        WebSocketConfig::new(format!("127.0.0.1:{port}")).with_path("/inline"),
    ));
    let output = Endpoint::new_response();
    let route = Route::new(input, output).with_handler(echo);
    let handle = route
        .run(&format!("websocket-inline-response-fast-path-{port}"))
        .await
        .expect("route should start");

    let mut stream = connect(format!("ws://127.0.0.1:{port}/inline")).await;
    stream
        .send(Message::text("request"))
        .await
        .expect("client should send request");

    let reply = stream
        .next()
        .await
        .expect("client should receive response")
        .expect("response frame should be valid");
    assert_eq!(reply.as_text(), Some("request"));

    handle.stop().await;
    handle.join().await.expect("route task should finish");
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_direct_response_route_echoes_without_handler() {
    let port = get_free_port();
    let input = Endpoint::new(EndpointType::WebSocket(
        WebSocketConfig::new(format!("127.0.0.1:{port}")).with_path("/echo"),
    ));
    let output = Endpoint::new_response();
    let route = Route::new(input, output);
    let handle = route
        .run(&format!("websocket-direct-echo-{port}"))
        .await
        .expect("route should start");

    let mut stream = connect(format!("ws://127.0.0.1:{port}/echo")).await;
    stream
        .send(Message::binary(vec![0, 1, 2, 3]))
        .await
        .expect("client should send request");

    let reply = stream
        .next()
        .await
        .expect("client should receive response")
        .expect("response frame should be valid");
    assert!(reply.is_binary());
    assert_eq!(&reply.as_payload()[..], &[0, 1, 2, 3]);

    handle.stop().await;
    handle.join().await.expect("route task should finish");
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_direct_response_route_ack_sends_no_reply() {
    let port = get_free_port();
    let input = Endpoint::new(EndpointType::WebSocket(
        WebSocketConfig::new(format!("127.0.0.1:{port}")).with_path("/ack"),
    ));
    let output = Endpoint::new_response();
    let route = Route::new(input, output).with_handler(ack);
    let handle = route
        .run(&format!("websocket-direct-ack-{port}"))
        .await
        .expect("route should start");

    let mut stream = connect(format!("ws://127.0.0.1:{port}/ack")).await;
    stream
        .send(Message::text("request"))
        .await
        .expect("client should send request");
    let result = tokio::time::timeout(std::time::Duration::from_millis(100), stream.next()).await;
    assert!(result.is_err(), "ack should not produce a websocket reply");

    handle.stop().await;
    handle.join().await.expect("route task should finish");
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_direct_response_route_replies_to_ping() {
    let port = get_free_port();
    let input = Endpoint::new(EndpointType::WebSocket(
        WebSocketConfig::new(format!("127.0.0.1:{port}"))
            .with_path("/ping")
            .with_execution_mode(WebSocketExecutionMode::DirectOnly),
    ));
    let output = Endpoint::new_response();
    let route = Route::new(input, output);
    let handle = route
        .run(&format!("websocket-direct-ping-{port}"))
        .await
        .expect("route should start");

    let mut stream = connect(format!("ws://127.0.0.1:{port}/ping")).await;
    stream
        .send(Message::ping("hello"))
        .await
        .expect("client should send ping");
    let reply = stream
        .next()
        .await
        .expect("client should receive pong")
        .expect("pong frame should be valid");
    assert!(reply.is_pong());
    assert_eq!(&reply.as_payload()[..], b"hello");

    handle.stop().await;
    handle.join().await.expect("route task should finish");
}

#[tokio::test(flavor = "multi_thread")]
async fn websocket_direct_response_route_rejects_wrong_path_with_404() {
    let port = get_free_port();
    let input = Endpoint::new(EndpointType::WebSocket(
        WebSocketConfig::new(format!("127.0.0.1:{port}"))
            .with_path("/expected")
            .with_execution_mode(WebSocketExecutionMode::DirectOnly),
    ));
    let output = Endpoint::new_response();
    let route = Route::new(input, output);
    let handle = route
        .run(&format!("websocket-direct-path-{port}"))
        .await
        .expect("route should start");

    // The upgrade handshake must be rejected before completing, so the client
    // never reaches an open WebSocket connection.
    let uri = format!("ws://127.0.0.1:{port}/wrong")
        .parse()
        .expect("websocket URL should parse");
    let result = ClientBuilder::from_uri(uri).connect().await;
    assert!(
        result.is_err(),
        "wrong path should be rejected with 404 before the upgrade completes"
    );

    handle.stop().await;
    handle.join().await.expect("route task should finish");
}