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
406
407
408
409
410
411
412
413
//! The seeking suites.

use futures::StreamExt;
use tokio::time::{Instant, timeout_at};

use super::{
    DEFAULT_TIMEOUT, RESUBSCRIBE_TIMEOUT, SeekPosition, ack_or_unsupported, expect_within,
    promptly, publish_all, within,
};
use crate::conformance::harness::{expect_next, expect_no_more, on_foreign_runtime};
use crate::conformance::helpers::unique_subject;
use crate::conformance::lifecycle::shutdown_within;
use crate::{
    Broker, Connected, IncomingMessage, Positioned, Publisher, Seekable, Seeker, Subscriber,
    SubscriptionSource,
};

/// How many messages the first subscription reads before it starts seeking.
const SEEK_COUNT: u8 = 5;

/// Verifies the [`Seekable`] contract.
///
/// Positions captured from delivered messages ([`Positioned::position`]) follow the pinned
/// semantics: a seek back to a captured position redelivers exactly that message and the suffix
/// after it in order, a seek forward past queued deliveries skips them, and the subscription
/// keeps delivering new publishes after repositioning. The seeker is minted before the stream
/// borrows the subscriber, which is the shape the capability exists for.
///
/// A second subscription to the same subject then opens once the first is gone, and seeks right
/// away, before its first delivery, to a position the first one captured: that is what a
/// service's start position does at startup, and the first delivery must be the message at that
/// position, not the committed position or the tip. A seek issued from a current-thread runtime
/// that stops right after (a handler on a dedicated thread) repositions all the same, and a seek
/// through the seeker after the broker shut down returns an error.
///
/// # Examples
///
/// ```no_run
/// # #[cfg(feature = "memory")]
/// # async fn run() {
/// use ruststream::conformance::capabilities;
/// use ruststream::memory::{MemoryBroker, MemorySource, Retention};
/// use ruststream::nonzero;
///
/// capabilities::seeking(
///     || MemoryBroker::retaining(Retention::Messages(nonzero!(64))),
///     |name| MemorySource::new(name),
///     |broker| broker.publisher(),
/// )
/// .await;
/// # }
/// ```
///
/// # Panics
///
/// Panics with a descriptive message if any step violates the contract.
pub async fn seeking<B, MkBroker, Src, MkSrc, Pub, MkPub>(
    make_broker: MkBroker,
    make_source: MkSrc,
    make_publisher: MkPub,
) where
    B: Broker,
    MkBroker: Fn() -> B,
    Src: SubscriptionSource<Connected<B>> + Send,
    Src::Subscriber: Seekable + Send,
    <Src::Subscriber as Subscriber>::Message:
        Positioned<Position = <<Src::Subscriber as Seekable>::Seeker as Seeker>::Position>,
    MkSrc: Fn(&str) -> Src,
    Pub: Publisher,
    MkPub: Fn(&Connected<B>) -> Pub,
{
    let subject = unique_subject("conformance.seeking");

    let connected = within(make_broker().connect(), "seeking: connect")
        .await
        .expect("broker must connect");
    let publisher = make_publisher(&connected);
    let first = within(
        make_source(&subject).subscribe(&connected),
        "seeking: subscribe",
    )
    .await
    .expect("subscription must open after connect");
    let (resume_at, replay_at) = seek_within_one_subscription(first, &publisher, &subject).await;

    // The second subscription seeks before its first delivery, the way a start position is
    // applied at startup: a broker that only honours a seek once the subscription has settled
    // (a consumer group assignment, a first fetch) reads from the committed position instead.
    let mut resumed = within(
        make_source(&subject).subscribe(&connected),
        "seeking: the second subscribe",
    )
    .await
    .expect("a second subscription to the same subject must open");
    let seeker = resumed.seeker();
    let after_shutdown =
        seek_on_fresh_subscription(&mut resumed, &seeker, resume_at, replay_at).await;

    shutdown_within(connected, "seeking").await;

    let refused = promptly(
        seeker.seek(after_shutdown),
        "seeking: a seek after shutdown",
    )
    .await;
    assert!(
        refused.is_err(),
        "seeking: a seek through a seeker that outlived the shutdown reported success; it must \
         return an error, never a reposition against the dead connection",
    );
}

/// Seeks back and forward within one subscription, and returns the positions of the last two
/// deliveries for the next subscription to seek to. The subscription is consumed, so it is gone
/// when this returns: on a broker whose subscriptions share a consumer group, the next one then
/// owns the log.
async fn seek_within_one_subscription<Sub, Pub>(
    mut subscriber: Sub,
    publisher: &Pub,
    subject: &str,
) -> (SeekPosition<Sub>, SeekPosition<Sub>)
where
    Sub: Seekable,
    Sub::Message: Positioned<Position = SeekPosition<Sub>>,
    Pub: Publisher,
{
    // Minted before `stream` borrows the subscriber; usable while the stream runs.
    let seeker = subscriber.seeker();

    let sent: Vec<[u8; 1]> = (0..SEEK_COUNT).map(|i| [i]).collect();
    let sent: Vec<&[u8]> = sent.iter().map(<[u8; 1]>::as_slice).collect();
    publish_all(publisher, subject, &sent).await;

    let mut stream = std::pin::pin!(subscriber.stream());
    let mut positions = Vec::new();
    for i in 0..SEEK_COUNT {
        let msg = expect_next(&mut stream, "seeking: initial delivery").await;
        assert_eq!(
            msg.payload(),
            &[i],
            "initial deliveries must arrive in publish order",
        );
        positions.push(msg.position());
        ack_or_unsupported(msg, "seeking: initial delivery").await;
    }
    expect_no_more(&mut stream, "seeking: after the initial drain").await;

    // Extraction order matters: swap_remove(4) pops the last element, which leaves the value
    // at index 1 in place for the second extraction.
    let forward_to = positions.swap_remove(4);
    let back_to = positions.swap_remove(1);

    within(seeker.seek(back_to), "seeking: a seek back")
        .await
        .expect("seek back failed");
    let redelivered = expect_next(&mut stream, "seeking: after the seek back").await;
    assert_eq!(
        redelivered.payload(),
        &[1],
        "a seek back must redeliver the message at the captured position",
    );
    ack_or_unsupported(redelivered, "seeking: after the seek back").await;
    // One more delivery pins the suffix: replaying only the sought message is not enough.
    let suffix = expect_next(&mut stream, "seeking: suffix after the seek back").await;
    assert_eq!(
        suffix.payload(),
        &[2],
        "a seek back must redeliver the ordered suffix after the captured position",
    );
    ack_or_unsupported(suffix, "seeking: suffix after the seek back").await;

    // Delivery 3 is pending now; jumping to the captured position of 4 must skip it.
    within(seeker.seek(forward_to), "seeking: a seek forward")
        .await
        .expect("seek forward failed");
    let skipped_to = expect_next(&mut stream, "seeking: after the seek forward").await;
    assert_eq!(
        skipped_to.payload(),
        &[4],
        "a seek forward must skip the queued deliveries before the target",
    );
    let resume_at = skipped_to.position();
    ack_or_unsupported(skipped_to, "seeking: after the seek forward").await;
    expect_no_more(&mut stream, "seeking: after the forward target").await;

    publish_all(publisher, subject, &[&[SEEK_COUNT]]).await;
    let live = expect_next(&mut stream, "seeking: after a new publish").await;
    assert_eq!(
        live.payload(),
        &[SEEK_COUNT],
        "the subscription must keep delivering new publishes after repositioning",
    );
    let replay_at = live.position();
    ack_or_unsupported(live, "seeking: after a new publish").await;
    (resume_at, replay_at)
}

/// A fresh subscription seeks right after it opens, to a position an earlier one captured, then
/// once more from another runtime; returns a position it captured, for the seek after shutdown.
async fn seek_on_fresh_subscription<Sub>(
    resumed: &mut Sub,
    seeker: &Sub::Seeker,
    resume_at: SeekPosition<Sub>,
    replay_at: SeekPosition<Sub>,
) -> SeekPosition<Sub>
where
    Sub: Seekable,
    Sub::Message: Positioned<Position = SeekPosition<Sub>>,
{
    within(
        seeker.seek(resume_at),
        "seeking: a seek right after subscribing",
    )
    .await
    .unwrap_or_else(|err| {
        panic!(
            "seeking: a seek right after subscribing, before the first delivery, was refused: \
             {err}"
        )
    });
    let mut stream = std::pin::pin!(resumed.stream());
    let resumed_first = expect_within(
        &mut stream,
        RESUBSCRIBE_TIMEOUT,
        "seeking: a fresh subscription that sought before its first delivery",
        "a seek made right after subscribing must decide where the subscription starts",
    )
    .await;
    assert_eq!(
        resumed_first.payload(),
        &[SEEK_COUNT - 1],
        "seeking: a fresh subscription that sought right after subscribing to a position captured \
         on an earlier subscription must first deliver the message at that position, not read \
         from the committed position or the tip",
    );
    let after_shutdown = resumed_first.position();
    ack_or_unsupported(resumed_first, "seeking: a fresh subscription").await;
    let resumed_next =
        expect_next(&mut stream, "seeking: the suffix on a fresh subscription").await;
    assert_eq!(
        resumed_next.payload(),
        &[SEEK_COUNT],
        "seeking: after a seek on a fresh subscription the ordered suffix must follow",
    );
    ack_or_unsupported(resumed_next, "seeking: the suffix on a fresh subscription").await;
    expect_no_more(
        &mut stream,
        "seeking: after the suffix on a fresh subscription",
    )
    .await;

    // Issued from a runtime that stops right after, the way a handler on a dedicated thread
    // repositions its own subscription. The answer comes back to be judged here, so a refusal
    // fails the suite with its own message rather than as a panic on the other thread.
    let foreign = seeker.clone();
    on_foreign_runtime(async move || {
        within(
            foreign.seek(replay_at),
            "seeking: a seek from another runtime",
        )
        .await
    })
    .await
    .unwrap_or_else(|err| panic!("seeking: a seek from another runtime was refused: {err}"));
    let replayed = expect_within(
        &mut stream,
        DEFAULT_TIMEOUT,
        "seeking: after a seek from a runtime that has since stopped",
        "a seek made from a runtime that has since stopped must still reposition the \
         subscription; the broker must run its internal tasks on the runtime it connected on",
    )
    .await;
    assert_eq!(
        replayed.payload(),
        &[SEEK_COUNT],
        "seeking: a seek from another runtime must redeliver the message at the captured position",
    );
    ack_or_unsupported(replayed, "seeking: after a seek from another runtime").await;
    expect_no_more(&mut stream, "seeking: after a seek from another runtime").await;
    after_shutdown
}

/// Verifies that a seek to a position the subscription's log does not hold is refused.
///
/// The suite publishes three messages to a fresh subject and drains them, then seeks to the
/// position `make_unknown` builds for that subject: one the log cannot hold after those three
/// publishes, such as a position the broker's retention has already evicted or one on a
/// partition or shard the subject does not have. The seek must return an error, and the
/// subscription must not move: its next delivery is the next publish, not a replay from wherever
/// the broker fell back to. A broker that ends the subscription with the refusal passes as well.
/// An error on the stream is not its end: the suite reads on, and the next delivery must still be
/// the next publish.
///
/// A broker that answers such a seek by waiting at that position (a position past the tip)
/// passes `make_unknown` a position of the other kinds.
///
/// # Examples
///
/// The in-memory broker keeps two messages here, so after three publishes the first one's
/// position is evicted.
///
/// ```no_run
/// # #[cfg(feature = "memory")]
/// # async fn run() {
/// use ruststream::conformance::capabilities;
/// use ruststream::memory::{MemoryBroker, MemoryPosition, MemorySource, Retention};
/// use ruststream::nonzero;
///
/// capabilities::seeking_unknown_position(
///     || MemoryBroker::retaining(Retention::Messages(nonzero!(2))),
///     |name| MemorySource::new(name),
///     |broker| broker.publisher(),
///     |_subject| MemoryPosition::sequence(0),
/// )
/// .await;
/// # }
/// ```
///
/// # Panics
///
/// Panics with a descriptive message if any step violates the contract.
pub async fn seeking_unknown_position<B, MkBroker, Src, MkSrc, Pub, MkPub, MkUnknown>(
    make_broker: MkBroker,
    make_source: MkSrc,
    make_publisher: MkPub,
    make_unknown: MkUnknown,
) where
    B: Broker,
    MkBroker: Fn() -> B,
    Src: SubscriptionSource<Connected<B>> + Send,
    Src::Subscriber: Seekable + Send,
    MkSrc: Fn(&str) -> Src,
    Pub: Publisher,
    MkPub: Fn(&Connected<B>) -> Pub,
    MkUnknown: Fn(&str) -> SeekPosition<Src::Subscriber>,
{
    const LABEL: &str = "seeking_unknown_position";

    let subject = unique_subject("conformance.seeking_unknown_position");

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

    let mut subscriber = within(
        make_source(&subject).subscribe(&connected),
        "seeking_unknown_position: subscribe",
    )
    .await
    .expect("subscription must open after connect");
    let seeker = subscriber.seeker();
    let publisher = make_publisher(&connected);

    let sent: [&[u8]; 3] = [&[0], &[1], &[2]];
    publish_all(&publisher, &subject, &sent).await;
    let mut stream = std::pin::pin!(subscriber.stream());
    for expected in sent {
        let msg = expect_next(&mut stream, LABEL).await;
        assert_eq!(
            msg.payload(),
            expected,
            "{LABEL}: deliveries must arrive in publish order",
        );
        ack_or_unsupported(msg, LABEL).await;
    }

    let refused = promptly(
        seeker.seek(make_unknown(&subject)),
        "seeking_unknown_position: a seek to a position the log does not hold",
    )
    .await;
    assert!(
        refused.is_err(),
        "{LABEL}: a seek to a position the log does not hold reported success; it must return an \
         error rather than read from somewhere else",
    );

    // The subscription must not move. A broker that ends the subscription with the refusal has
    // read nothing from elsewhere either, so the end of the stream is an answer too; a delivery of
    // anything but the next publish is not. An error is neither: the subscription may go on after
    // it, and where it goes on from is what the suite reads.
    publish_all(&publisher, &subject, &[&[3]]).await;
    // A stream that is ready with an error on every poll never lets `timeout_at` see its
    // deadline, so the deadline is read on every turn as well.
    let deadline = Instant::now() + DEFAULT_TIMEOUT;
    let after_refusal = loop {
        assert!(
            Instant::now() < deadline,
            "{LABEL}: after a refused seek the next publish did not arrive within \
             {DEFAULT_TIMEOUT:?}, and the subscription did not end either"
        );
        match timeout_at(deadline, stream.next()).await {
            Ok(Some(Err(_))) => {}
            Ok(delivery) => break delivery,
            Err(elapsed) => panic!(
                "{LABEL}: after a refused seek the next publish did not arrive and the \
                 subscription did not end either ({elapsed})"
            ),
        }
    };
    if let Some(Ok(next)) = after_refusal {
        assert_eq!(
            next.payload(),
            &[3],
            "{LABEL}: a refused seek must not move the subscription; the next delivery must be \
             the next publish, not a replay from wherever the broker fell back to",
        );
        ack_or_unsupported(next, LABEL).await;
        expect_no_more(&mut stream, LABEL).await;
    }

    shutdown_within(connected, LABEL).await;
}