ruststream 0.7.0-rc.11

Async messaging framework for Rust: broker-agnostic traits, router, codecs, and a conformance harness for broker authors.
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
//! The request-reply suite.

use std::fmt;

use futures::Stream;
use tokio::sync::oneshot;

use super::{
    AFTER_SHUTDOWN_REQUEST_TIMEOUT, DEFAULT_TIMEOUT, MISS_TIMEOUT, ack_or_unsupported, promptly,
    within,
};
use crate::conformance::harness::{expect_next, on_foreign_runtime};
use crate::conformance::helpers::unique_subject;
use crate::conformance::lifecycle::shutdown_within;
use crate::{
    Broker, Connected, HeaderMap, IncomingMessage, OutgoingMessage, Publisher, RequestReply,
    Subscriber, SubscriptionSource,
};

/// Verifies the [`RequestReply`] contract.
///
/// A request reaches a responder with a usable `reply-to` header, the correlated reply resolves
/// the request, and a request nobody answers fails once its timeout elapses. Two requests in
/// flight at once each resolve with their own reply, a reply that arrives after its request timed
/// out resolves no later request, and a request through the requester after the broker shut down
/// returns an error at once.
///
/// The first request comes from a current-thread runtime on a thread of its own, which stops once
/// the reply arrives, and a second request from the suite's runtime must still resolve: what the
/// requester starts on the first request (a reply dispatcher, a reply consumer) runs on the
/// runtime the broker connected on, not on the caller's. The requester moves to that thread, so it
/// is `'static`.
///
/// The factories mirror [`harness::lifecycle`](crate::conformance::harness::lifecycle): `make_source` opens
/// the responder's subscription, `make_requester` produces the [`RequestReply`] publisher under
/// test, and `make_publisher` produces the plain publisher the responder replies through.
///
/// # Examples
///
/// ```no_run
/// # #[cfg(feature = "memory")]
/// # async fn run() {
/// use ruststream::conformance::capabilities;
/// use ruststream::memory::{MemoryBroker, MemorySource};
///
/// capabilities::request_reply(
///     MemoryBroker::new,
///     |name| MemorySource::new(name),
///     |broker| broker.requester(),
///     |broker| broker.publisher(),
/// )
/// .await;
/// # }
/// ```
///
/// # Panics
///
/// Panics with a descriptive message if any step violates the contract.
pub async fn request_reply<B, MkBroker, Src, MkSrc, Req, MkReq, Pub, MkPub>(
    make_broker: MkBroker,
    make_source: MkSrc,
    make_requester: MkReq,
    make_publisher: MkPub,
) where
    B: Broker,
    MkBroker: Fn() -> B,
    Src: SubscriptionSource<Connected<B>> + Send,
    Src::Subscriber: Send,
    MkSrc: Fn(&str) -> Src,
    Req: RequestReply + 'static,
    MkReq: Fn(&Connected<B>) -> Req,
    Pub: Publisher,
    MkPub: Fn(&Connected<B>) -> Pub,
{
    let subject = unique_subject("conformance.request_reply");
    // Derived from the same run, so the "nobody answers" leg cannot collide with a responder
    // another run of this suite left listening.
    let unanswered_subject = format!("{subject}.void");

    let connected = within(make_broker().connect(), "request_reply: connect")
        .await
        .expect("broker must connect");

    let mut responder = within(
        make_source(&subject).subscribe(&connected),
        "request_reply: subscribe",
    )
    .await
    .expect("responder subscription must open after connect");
    let publisher = make_publisher(&connected);
    let requester = make_requester(&connected);
    let mut inbox = std::pin::pin!(responder.stream());

    let requester = requests_across_runtimes(requester, &publisher, &mut inbox, &subject).await;

    late_reply_resolves_no_later_request(&requester, &publisher, &mut inbox, &subject).await;
    concurrent_requests_get_their_own_replies(&requester, &publisher, &mut inbox, &subject).await;

    let unanswered = within(
        requester.request(
            OutgoingMessage::new(&unanswered_subject, b"ping".as_slice()),
            MISS_TIMEOUT,
        ),
        "request_reply: a request nobody answers",
    )
    .await;
    assert!(
        unanswered.is_err(),
        "a request nobody answers must fail once its timeout elapses",
    );

    shutdown_within(connected, "request_reply").await;

    let after_shutdown = promptly(
        requester.request(
            OutgoingMessage::new(&subject, b"ping".as_slice()),
            AFTER_SHUTDOWN_REQUEST_TIMEOUT,
        ),
        "request_reply: a request after shutdown",
    )
    .await;
    assert!(
        after_shutdown.is_err(),
        "request_reply: a request through a requester that outlived the shutdown resolved; it \
         must return an error, never succeed against the dead connection",
    );
}

/// One request from a runtime that stops once it is answered, then one from the suite's runtime:
/// both resolve with the responder's reply. Hands the requester back for the rest of the suite.
async fn requests_across_runtimes<Req, Pub, S, M, E>(
    requester: Req,
    publisher: &Pub,
    inbox: &mut S,
    subject: &str,
) -> Req
where
    Req: RequestReply + 'static,
    Pub: Publisher,
    S: Stream<Item = Result<M, E>> + Unpin,
    M: IncomingMessage,
    E: fmt::Debug,
{
    let respond = async {
        // One request from another runtime, then one from this one.
        for label in [
            "request_reply responder, the request from another runtime",
            "request_reply responder, the request after that runtime stopped",
        ] {
            let msg = expect_next(&mut *inbox, label).await;
            assert_eq!(
                msg.payload(),
                b"ping",
                "responder must receive the request payload"
            );
            answer(publisher, &msg, b"pong".as_slice())
                .await
                .expect("reply publish failed");
            ack_or_unsupported(msg, label).await;
        }
    };
    let requests = async {
        // The first request comes from a runtime that stops once it is answered, the way a
        // handler on a dedicated thread requests: whatever the requester starts on it (a reply
        // dispatcher, a correlation table's reader) must run on the broker's runtime, or the
        // second request finds it gone.
        let first = subject.to_owned();
        let requester = on_foreign_runtime(async move || {
            let reply = within(
                requester.request(
                    OutgoingMessage::new(&first, b"ping".as_slice()),
                    DEFAULT_TIMEOUT,
                ),
                "request_reply: the request from another runtime",
            )
            .await
            .expect("request must resolve once the responder replies");
            assert_eq!(
                reply.payload(),
                b"pong",
                "the correlated reply must carry the responder payload"
            );
            settle_reply(reply).await;
            requester
        })
        .await;
        let reply = within(
            requester.request(
                OutgoingMessage::new(subject, b"ping".as_slice()),
                DEFAULT_TIMEOUT,
            ),
            "request_reply: the request after that runtime stopped",
        )
        .await
        .expect(
            "a request must resolve after an earlier one was made from a runtime that has \
                 stopped; the broker must run its internal tasks on the runtime it connected on",
        );
        assert_eq!(
            reply.payload(),
            b"pong",
            "the correlated reply must carry the responder payload"
        );
        settle_reply(reply).await;
        requester
    };

    let (requester, ()) = futures::join!(requests, respond);
    requester
}

/// A request times out while the responder holds its reply back; the reply then goes out, and the
/// next request must resolve with its own reply, not with the late one.
async fn late_reply_resolves_no_later_request<Req, Pub, S, M, E>(
    requester: &Req,
    publisher: &Pub,
    inbox: &mut S,
    subject: &str,
) where
    Req: RequestReply,
    Pub: Publisher,
    S: Stream<Item = Result<M, E>> + Unpin,
    M: IncomingMessage,
    E: fmt::Debug,
{
    let (timed_out, held_back) = oneshot::channel::<()>();
    let (late_sent, late_out) = oneshot::channel::<()>();

    let requests = async move {
        let late = within(
            requester.request(
                OutgoingMessage::new(subject, b"late".as_slice()),
                MISS_TIMEOUT,
            ),
            "request_reply: the request that times out",
        )
        .await;
        assert!(
            late.is_err(),
            "request_reply: the responder held its reply back past the request's timeout, so the \
             request must fail once {MISS_TIMEOUT:?} elapse",
        );
        let _ = timed_out.send(());
        late_out
            .await
            .expect("request_reply: the responder sends the late reply before the next request");
        let fresh = within(
            requester.request(
                OutgoingMessage::new(subject, b"fresh".as_slice()),
                DEFAULT_TIMEOUT,
            ),
            "request_reply: the request after the timeout",
        )
        .await
        .expect("request_reply: a request after a timed-out one must resolve once answered");
        assert_eq!(
            fresh.payload(),
            b"re:fresh",
            "request_reply: a reply that arrived after its request timed out resolved the next \
             request; a late reply must be discarded, never handed to another request",
        );
        settle_reply(fresh).await;
    };
    let respond = async move {
        let late = expect_next(
            &mut *inbox,
            "request_reply responder, the request that times out",
        )
        .await;
        held_back
            .await
            .expect("request_reply: the requester reports the timeout");
        // Nobody waits on that reply address any more, so a transport may refuse the publish;
        // either way the reply must reach no other request.
        let _ = answer(publisher, &late, b"re:late".as_slice()).await;
        ack_or_unsupported(late, "request_reply responder, the request that timed out").await;
        let _ = late_sent.send(());

        let fresh = expect_next(
            &mut *inbox,
            "request_reply responder, the request after the timeout",
        )
        .await;
        answer(publisher, &fresh, b"re:fresh".as_slice())
            .await
            .expect("reply publish failed");
        ack_or_unsupported(
            fresh,
            "request_reply responder, the request after the timeout",
        )
        .await;
    };
    futures::join!(requests, respond);
}

/// Two requests in flight at once, answered in the reverse of their arrival order: each must
/// resolve with the reply to itself, so a requester that hands out replies in arrival order, or
/// that shares one reply address between requests, is caught.
async fn concurrent_requests_get_their_own_replies<Req, Pub, S, M, E>(
    requester: &Req,
    publisher: &Pub,
    inbox: &mut S,
    subject: &str,
) where
    Req: RequestReply,
    Pub: Publisher,
    S: Stream<Item = Result<M, E>> + Unpin,
    M: IncomingMessage,
    E: fmt::Debug,
{
    let requests = async {
        let (alpha, beta) = within(
            async {
                futures::join!(
                    requester.request(
                        OutgoingMessage::new(subject, b"alpha".as_slice()),
                        DEFAULT_TIMEOUT,
                    ),
                    requester.request(
                        OutgoingMessage::new(subject, b"beta".as_slice()),
                        DEFAULT_TIMEOUT,
                    ),
                )
            },
            "request_reply: two concurrent requests",
        )
        .await;
        for (reply, expected) in [(alpha, b"re:alpha".as_slice()), (beta, b"re:beta")] {
            let reply = reply.expect(
                "request_reply: each of two concurrent requests must resolve once answered",
            );
            assert_eq!(
                reply.payload(),
                expected,
                "request_reply: two requests in flight at once must each resolve with the reply \
                 to itself",
            );
            settle_reply(reply).await;
        }
    };
    let respond = async {
        let first = expect_next(
            &mut *inbox,
            "request_reply responder, the first concurrent request",
        )
        .await;
        let second = expect_next(
            &mut *inbox,
            "request_reply responder, the second concurrent request",
        )
        .await;
        for request in [&second, &first] {
            let echoed = [b"re:".as_slice(), request.payload()].concat();
            answer(publisher, request, &echoed)
                .await
                .expect("reply publish failed");
        }
        ack_or_unsupported(
            first,
            "request_reply responder, the first concurrent request",
        )
        .await;
        ack_or_unsupported(
            second,
            "request_reply responder, the second concurrent request",
        )
        .await;
    };
    futures::join!(requests, respond);
}

/// Replies to `request` with `payload` at its `reply-to` address, echoing its correlation id when
/// the requester set one.
async fn answer<Pub, M>(publisher: &Pub, request: &M, payload: &[u8]) -> Result<(), Pub::Error>
where
    Pub: Publisher,
    M: IncomingMessage,
{
    let reply_to = request
        .headers()
        .reply_to()
        .expect("a request must carry a usable reply-to header")
        .to_owned();
    // Replies must at minimum go to the reply-to destination; the correlation id is echoed for
    // the requesters that match on it.
    let mut headers = HeaderMap::new();
    if let Some(correlation_id) = request.headers().correlation_id() {
        headers.insert("correlation-id", correlation_id.to_owned());
    }
    within(
        publisher.publish(
            OutgoingMessage::new(&reply_to, payload).with_headers(headers),
            None,
        ),
        "request_reply: the responder's reply",
    )
    .await
}

/// Settles a reply the way a caller that read it does. A reply is a delivery, and one left
/// unsettled may be handed back to whatever reads the reply subscription next. A transport with no
/// settlement for replies answers what it answers; the check asserts the reply, not the answer.
async fn settle_reply<M: IncomingMessage>(reply: M) {
    let _settled = within(reply.ack(), "request_reply: settling a reply").await;
}