laser-sdk 0.8.2

LaserData SDK over Apache Iggy: typed streaming, operational and lakehouse query, materialization destinations, key-value state, copy-on-write forks, and an optional agent runtime with the Agent Data Exchange Protocol (AGDX).
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
use crate::harness;
use laser_sdk::iggy::prelude::{
    Consumer, Identifier, MessageClient, PollingStrategy, StreamClient, TopicClient,
};
use laser_sdk::prelude::{CommitPolicy, ConsumerMessage, ConsumerStart, ProducerMessage, Routing};
use laser_sdk::stream::{HeaderKey, HeaderValue};
use std::str::FromStr;
use std::time::Duration;

const RECEIVE_TIMEOUT: Duration = Duration::from_secs(15);

#[tokio::test]
async fn given_native_offset_history_when_rewound_and_deleted_then_should_follow_the_replay_option()
{
    let laser = harness::laser().await;
    let topic = laser.topic("native-offset-history");
    topic
        .producer()
        .partitions(1)
        .build()
        .await
        .expect("the native producer initializes")
        .send_batch([
            ProducerMessage::new(b"zero".as_slice()),
            ProducerMessage::new(b"one".as_slice()),
            ProducerMessage::new(b"two".as_slice()),
        ])
        .await
        .expect("the native offset records publish");
    for allow_replay in [false, true] {
        let builder = topic
            .consumer(format!("native-offset-history-{allow_replay}"), 0)
            .start_at(ConsumerStart::First)
            .commit_policy(CommitPolicy::Disabled);
        let builder = if allow_replay {
            builder.allow_replay()
        } else {
            builder
        };
        let mut consumer = builder.build().await.expect("the native consumer builds");
        let mut records = Vec::new();
        for _ in 0..3 {
            records.push(
                consumer
                    .next_within(RECEIVE_TIMEOUT)
                    .await
                    .expect("the native record arrives"),
            );
        }
        consumer.commit(&records[2]).await.expect("commit two");
        consumer.commit(&records[1]).await.expect("commit one");
        let expected = if allow_replay { 1 } else { 2 };
        assert_eq!(consumer.last_stored_offset(0), Some(expected));
        consumer.store_offset(2, Some(0)).await.expect("store two");
        consumer.store_offset(2, Some(0)).await.expect("repeat two");
        consumer.store_offset(1, Some(0)).await.expect("store one");
        assert_eq!(
            consumer
                .stored_offset(0)
                .await
                .expect("read the stored offset")
                .expect("an offset exists")
                .stored_offset,
            expected
        );
        consumer
            .delete_offset(Some(0))
            .await
            .expect("delete the offset");
        assert!(
            consumer
                .stored_offset(0)
                .await
                .expect("read after deletion")
                .is_none()
        );
        assert_eq!(consumer.last_stored_offset(0), Some(expected));
        consumer
            .store_offset(1, Some(0))
            .await
            .expect("store after deletion");
        assert_eq!(
            consumer
                .stored_offset(0)
                .await
                .expect("read after storing")
                .map(|offset| offset.stored_offset),
            allow_replay.then_some(1)
        );
        consumer
            .store_offset(0, Some(0))
            .await
            .expect("zero always stores");
        assert_eq!(consumer.last_stored_offset(0), Some(0));
        assert_eq!(
            consumer
                .stored_offset(0)
                .await
                .expect("read zero")
                .expect("zero exists")
                .stored_offset,
            0
        );
        consumer
            .shutdown()
            .await
            .expect("the native consumer closes");
    }
}

#[tokio::test]
async fn given_production_profile_when_streaming_then_should_preserve_delivery_and_offsets() {
    let laser = harness::connected_laser().await;
    let topic = laser.topic("production-streaming");
    let producer = topic
        .producer()
        .batch_length(1000)
        .linger(Duration::from_millis(5))
        .routing(Routing::Balanced)
        .partitions(1)
        .build()
        .await
        .expect("the Laser producer should initialize");

    let type_key = HeaderKey::from_str("type").expect("the type header should be valid");
    let message = ProducerMessage::new(b"production-event".as_slice())
        .header(type_key.clone(), HeaderValue::from(7_u16));
    let first_send = producer
        .send_keyed(message, b"account-42".to_vec())
        .await
        .expect("the keyed message should publish");
    let first_confirmation = first_send
        .confirmations
        .first()
        .expect("the server should confirm the keyed message");
    assert_eq!(first_confirmation.partition_id, 0);
    let sent = producer
        .send_batch_with_routing(
            [ProducerMessage::new(b"production-batch".as_slice())],
            Some(Routing::Partition(0)),
        )
        .await
        .expect("the Laser batch should publish");
    let batch_confirmation = sent
        .confirmations
        .first()
        .expect("the server should confirm the batch");
    assert_eq!(batch_confirmation.partition_id, 0);
    assert!(batch_confirmation.base_offset > first_confirmation.base_offset);

    let mut consumer = topic
        .consumer_group("production-auto-workers")
        .consumer()
        .batch_length(1000)
        .poll_interval(Duration::from_millis(5))
        .commit_policy(CommitPolicy::IntervalOrEach(Duration::from_secs(1)))
        .start_at(ConsumerStart::Next)
        .build()
        .await
        .expect("the Laser consumer should initialize");
    let received: ConsumerMessage = consumer
        .next_within(RECEIVE_TIMEOUT)
        .await
        .expect("the Laser consumer should receive the production event");
    assert_eq!(received.payload.as_ref(), b"production-event");
    assert_eq!(
        received
            .headers
            .get(&type_key)
            .expect("the type header should be present")
            .as_uint16()
            .expect("the type header should remain uint16"),
        7
    );
    let batch = consumer
        .next_within(RECEIVE_TIMEOUT)
        .await
        .expect("the Laser consumer should receive the batch message");
    assert_eq!(batch.payload.as_ref(), b"production-batch");
    assert!(matches!(
        consumer.next_within(Duration::from_millis(50)).await,
        Err(laser_sdk::LaserError::Timeout(_))
    ));
    harness::eventually(|| async {
        (consumer.last_stored_offset(batch.partition_id) == Some(batch.position.offset))
            .then_some(())
    })
    .await;
    consumer
        .shutdown()
        .await
        .expect("the auto-commit consumer should shut down");
    producer
        .send(b"auto-resumed".as_slice())
        .await
        .expect("the resume marker should publish");
    let mut resumed = topic
        .consumer_group("production-auto-workers")
        .consumer()
        .poll_interval(Duration::from_millis(5))
        .commit_policy(CommitPolicy::Disabled)
        .start_at(ConsumerStart::Next)
        .build()
        .await
        .expect("the auto-commit group should rejoin");
    let received = resumed
        .next_within(RECEIVE_TIMEOUT)
        .await
        .expect("the resumed group should receive the auto-resumed marker");
    assert_eq!(received.payload.as_ref(), b"auto-resumed");
    resumed
        .shutdown()
        .await
        .expect("the resumed group should shut down");

    let mut uncommitted = topic
        .consumer_group("production-uncommitted-workers")
        .consumer()
        .poll_interval(Duration::from_millis(5))
        .commit_policy(CommitPolicy::Disabled)
        .start_at(ConsumerStart::Next)
        .build()
        .await
        .expect("the uncommitted group should initialize");
    let first = uncommitted
        .next_within(RECEIVE_TIMEOUT)
        .await
        .expect("the uncommitted group should receive a message");
    uncommitted
        .shutdown()
        .await
        .expect("the uncommitted group should shut down without committing");
    let mut uncommitted = topic
        .consumer_group("production-uncommitted-workers")
        .consumer()
        .poll_interval(Duration::from_millis(5))
        .commit_policy(CommitPolicy::Disabled)
        .start_at(ConsumerStart::Next)
        .build()
        .await
        .expect("the uncommitted group should rejoin");
    let replayed = uncommitted
        .next_within(RECEIVE_TIMEOUT)
        .await
        .expect("the uncommitted group should redeliver the record");
    assert_eq!(replayed.position, first.position);
    uncommitted
        .commit(&replayed)
        .await
        .expect("the redelivered record should commit");
    uncommitted
        .shutdown()
        .await
        .expect("the committed group should shut down");

    let mut manual = topic
        .consumer_group("production-manual-workers")
        .consumer()
        .batch_length(1000)
        .poll_interval(Duration::from_millis(5))
        .commit_policy(CommitPolicy::Disabled)
        .start_at(ConsumerStart::Next)
        .build()
        .await
        .expect("the manual Laser consumer should initialize");
    for _ in 0..3 {
        let received = manual
            .next_within(RECEIVE_TIMEOUT)
            .await
            .expect("the manual Laser consumer should receive a message");
        manual
            .commit(&received)
            .await
            .expect("the handled offset should store");
        assert_eq!(
            manual.last_stored_offset(received.partition_id),
            Some(received.position.offset)
        );
    }
    manual
        .shutdown()
        .await
        .expect("the manual consumer should shut down");
    producer
        .send(b"manual-resumed".as_slice())
        .await
        .expect("the manual resume marker should publish");
    let mut manual = topic
        .consumer_group("production-manual-workers")
        .consumer()
        .poll_interval(Duration::from_millis(5))
        .commit_policy(CommitPolicy::Disabled)
        .start_at(ConsumerStart::Next)
        .build()
        .await
        .expect("the manual group should rejoin");
    let received = manual
        .next_within(RECEIVE_TIMEOUT)
        .await
        .expect("the manual group should receive the manual-resumed marker");
    assert_eq!(received.payload.as_ref(), b"manual-resumed");
    manual
        .shutdown()
        .await
        .expect("the resumed manual group should shut down");

    let mut standalone = topic
        .consumer("production-audit", 0)
        .start_at(ConsumerStart::First)
        .commit_policy(CommitPolicy::Disabled)
        .allow_replay()
        .build()
        .await
        .expect("the standalone Laser consumer should initialize");
    let replayed = standalone
        .next_within(RECEIVE_TIMEOUT)
        .await
        .expect("the standalone consumer should receive the replayed event");
    assert_eq!(replayed.payload.as_ref(), b"production-event");
    standalone
        .shutdown()
        .await
        .expect("the standalone consumer should shut down");
}

#[tokio::test]
async fn given_a_bootstrapped_stream_when_deleted_then_should_remove_it_once() {
    let laser = harness::laser().await;
    let stream = laser
        .default_stream()
        .expect("the test laser names its stream")
        .to_owned();
    assert!(
        laser
            .stream(&stream)
            .delete()
            .await
            .expect("the stream should delete")
    );
    assert!(
        !laser
            .stream(&stream)
            .delete()
            .await
            .expect("a missing stream should report absence")
    );
    let identifier = Identifier::named(&stream).expect("the stream name is a valid identifier");
    assert!(
        laser
            .client()
            .get_stream(&identifier)
            .await
            .expect("the stream lookup should succeed")
            .is_none()
    );
}

#[tokio::test]
async fn given_a_closed_laser_when_used_then_should_refuse_further_requests() {
    let laser = harness::laser().await;
    let clone = laser.clone();
    laser.close().await.expect("the connection should close");
    laser
        .close()
        .await
        .expect("a second close should be a no-op");
    assert!(clone.client().get_streams().await.is_err());
}

#[tokio::test]
async fn given_a_cached_stream_when_deleted_and_recreated_then_should_discard_its_previous_state() {
    let laser = harness::laser().await;
    let stream = laser.default_stream().expect("the test stream is selected");
    let identifier = Identifier::named(stream).expect("the stream name is valid");
    let agent = "retired-agent".parse().expect("the agent id is valid");
    for externally_deleted in [false, true] {
        laser
            .bootstrap(
                1,
                laser_sdk::agent::TopicRetention::expire_after(std::time::Duration::from_secs(
                    86_400,
                )),
            )
            .await
            .expect("bootstrap the stream");
        laser
            .quarantine(
                "operator".parse().expect("the operator id is valid"),
                &agent,
            )
            .await
            .expect("publish a registry fact");
        let mut registry = laser.agent_registry().expect("open the registry");
        registry.refresh(0).await.expect("read the registry");
        assert!(registry.is_quarantined(&agent));
        if externally_deleted {
            laser
                .client()
                .delete_stream(&identifier)
                .await
                .expect("delete outside Laser");
        }
        assert_eq!(
            laser
                .stream(stream)
                .delete()
                .await
                .expect("invalidate the stream"),
            !externally_deleted
        );
        laser
            .bootstrap(
                1,
                laser_sdk::agent::TopicRetention::expire_after(std::time::Duration::from_secs(
                    86_400,
                )),
            )
            .await
            .expect("recreate the stream");
        let mut registry = laser.agent_registry().expect("open a fresh registry");
        assert!(!registry.is_quarantined(&agent));
        laser
            .quarantine(
                "operator".parse().expect("the operator id is valid"),
                &agent,
            )
            .await
            .expect("publish with a fresh producer");
        registry
            .refresh(0)
            .await
            .expect("read from the new stream's start");
        assert!(registry.is_quarantined(&agent));
        laser
            .stream(stream)
            .delete()
            .await
            .expect("remove the recreated stream");
    }
    laser.close().await.expect("close the connection");
}

#[tokio::test]
async fn given_a_fresh_consumer_when_reading_next_then_should_start_at_zero_and_resume_after_a_real_commit()
 {
    let laser = harness::laser().await;
    let topic = laser.topic("default-next");
    let producer = topic
        .producer()
        .partitions(1)
        .build()
        .await
        .expect("producer");
    producer
        .send_batch_with_routing(
            [
                ProducerMessage::new(b"zero".as_slice()),
                ProducerMessage::new(b"one".as_slice()),
            ],
            Some(Routing::Partition(0)),
        )
        .await
        .expect("publish");
    let build = || {
        topic
            .consumer("fresh", 0)
            .batch_length(1)
            .commit_policy(CommitPolicy::Disabled)
            .build()
    };
    let mut first = build().await.expect("first consumer");
    let record = first
        .next_within(RECEIVE_TIMEOUT)
        .await
        .expect("first record");
    assert_eq!(record.position.offset, 0);
    first.shutdown().await.expect("uncommitted shutdown");
    let mut retried = build().await.expect("retry consumer");
    let record = retried
        .next_within(RECEIVE_TIMEOUT)
        .await
        .expect("uncommitted record repeats");
    assert_eq!(record.position.offset, 0);
    retried.commit(&record).await.expect("commit zero");
    retried.shutdown().await.expect("shutdown");
    let mut resumed = build().await.expect("resume consumer");
    assert_eq!(
        resumed
            .next_within(RECEIVE_TIMEOUT)
            .await
            .expect("next after committed zero")
            .position
            .offset,
        1
    );
    resumed.shutdown().await.expect("shutdown");
}

#[tokio::test]
async fn given_default_polling_when_shutdown_after_offset_zero_then_should_resume_at_one() {
    let laser = harness::connected_laser().await;
    let topic = laser.topic("default-shutdown-zero");
    let producer = topic
        .producer()
        .partitions(1)
        .build()
        .await
        .expect("producer");
    producer
        .send_batch_with_routing(
            [
                ProducerMessage::new(b"zero".as_slice()),
                ProducerMessage::new(b"one".as_slice()),
                ProducerMessage::new(b"two".as_slice()),
                ProducerMessage::new(b"three".as_slice()),
            ],
            Some(Routing::Partition(0)),
        )
        .await
        .expect("publish");
    let mut first = topic
        .consumer("partial", 0)
        .batch_length(4)
        .build()
        .await
        .expect("consumer");
    assert_eq!(
        first
            .next_within(RECEIVE_TIMEOUT)
            .await
            .expect("first record")
            .position
            .offset,
        0
    );
    first
        .shutdown()
        .await
        .expect("shutdown with a partial batch");
    let mut resumed = topic
        .consumer("partial", 0)
        .batch_length(4)
        .build()
        .await
        .expect("resume");
    assert_eq!(
        resumed
            .next_within(RECEIVE_TIMEOUT)
            .await
            .expect("remaining record")
            .position
            .offset,
        1
    );
    resumed.shutdown().await.expect("shutdown");
}

#[tokio::test]
async fn given_each_commit_policy_when_shutdown_after_zero_then_should_preserve_its_resume_contract()
 {
    let server = crate::test_iggy::TestIggy::start_pinned().await;
    let laser = harness::connected_laser_on(&server).await;
    let interval = Duration::from_secs(60);
    let policies = [
        CommitPolicy::Disabled,
        CommitPolicy::Interval(interval),
        CommitPolicy::Polling,
        CommitPolicy::IntervalOrPolling(interval),
        CommitPolicy::All,
        CommitPolicy::IntervalOrAll(interval),
        CommitPolicy::Each,
        CommitPolicy::IntervalOrEach(interval),
        CommitPolicy::Every(10),
        CommitPolicy::IntervalOrEvery(interval, 10),
    ];
    for (index, policy) in policies.into_iter().enumerate() {
        for group in [false, true] {
            let topic = laser.topic(format!("policy-zero-{index}-{group}"));
            let producer = topic
                .producer()
                .partitions(1)
                .build()
                .await
                .expect("producer");
            producer
                .send_batch_with_routing(
                    [
                        ProducerMessage::new(b"zero".as_slice()),
                        ProducerMessage::new(b"one".as_slice()),
                    ],
                    Some(Routing::Partition(0)),
                )
                .await
                .expect("publish");
            let builder = if group {
                topic.consumer_group("worker").consumer()
            } else {
                topic.consumer("worker", 0)
            };
            let mut first = builder
                .batch_length(2)
                .commit_policy(policy)
                .build()
                .await
                .expect("consumer");
            assert_eq!(
                first
                    .next_within(RECEIVE_TIMEOUT)
                    .await
                    .expect("zero")
                    .position
                    .offset,
                0
            );
            first.shutdown().await.expect("shutdown");
            let builder = if group {
                topic.consumer_group("worker").consumer()
            } else {
                topic.consumer("worker", 0)
            };
            let mut resumed = builder
                .commit_policy(CommitPolicy::Disabled)
                .build()
                .await
                .expect("resume");
            let found = resumed
                .next_within(RECEIVE_TIMEOUT)
                .await
                .expect("resumed message")
                .position
                .offset;
            let expected = u64::from(policy != CommitPolicy::Disabled);
            assert_eq!(found, expected, "policy {policy:?}, group {group}");
            resumed.shutdown().await.expect("close");
        }
    }
}

// A purge restarts the partition at offset zero and clears every stored
// offset. A consumer rebuilt after the purge must read the replacement
// history from its first record under every commit policy.
#[tokio::test]
async fn given_a_purged_topic_when_the_consumer_is_rebuilt_then_should_start_at_the_new_offset_zero()
 {
    let laser = harness::connected_laser().await;
    let stream = laser
        .default_stream()
        .expect("the test laser names its stream")
        .to_owned();
    let interval = Duration::from_secs(60);
    let policies = [
        CommitPolicy::Disabled,
        CommitPolicy::Interval(interval),
        CommitPolicy::Polling,
        CommitPolicy::IntervalOrPolling(interval),
        CommitPolicy::All,
        CommitPolicy::IntervalOrAll(interval),
        CommitPolicy::Each,
        CommitPolicy::IntervalOrEach(interval),
        CommitPolicy::Every(10),
        CommitPolicy::IntervalOrEvery(interval, 10),
    ];
    for (index, policy) in policies.into_iter().enumerate() {
        for group in [false, true] {
            let topic_name = format!("policy-purge-{index}-{group}");
            let topic = laser.topic(&topic_name);
            let producer = topic
                .producer()
                .partitions(1)
                .build()
                .await
                .expect("producer");
            producer
                .send_batch_with_routing(
                    [
                        ProducerMessage::new(b"telemetry-0".as_slice()),
                        ProducerMessage::new(b"telemetry-1".as_slice()),
                        ProducerMessage::new(b"telemetry-2".as_slice()),
                    ],
                    Some(Routing::Partition(0)),
                )
                .await
                .expect("publish before purge");
            let builder = if group {
                topic.consumer_group("ground-station").consumer()
            } else {
                topic.consumer("ground-station", 0)
            };
            let mut before = builder
                .batch_length(3)
                .commit_policy(policy)
                .build()
                .await
                .expect("consumer before purge");
            for expected in 0..3 {
                let record = before
                    .next_within(RECEIVE_TIMEOUT)
                    .await
                    .expect("record before purge");
                assert_eq!(record.position.offset, expected);
                if policy == CommitPolicy::Disabled {
                    before.commit(&record).await.expect("manual commit");
                }
            }
            before.shutdown().await.expect("shutdown before purge");

            laser
                .client()
                .purge_topic(
                    &Identifier::named(&stream).expect("stream identifier"),
                    &Identifier::named(&topic_name).expect("topic identifier"),
                )
                .await
                .expect("purge");
            let stream_id = Identifier::named(&stream).expect("stream id");
            let topic_id = Identifier::named(&topic_name).expect("topic id");
            // Metadata acknowledges before the partition owner applies the purge.
            harness::eventually(|| async {
                let visible = laser
                    .client()
                    .poll_messages(
                        &stream_id,
                        &topic_id,
                        Some(0),
                        &Consumer::default(),
                        &PollingStrategy::first(),
                        3,
                        false,
                    )
                    .await
                    .expect("purge visibility poll");
                visible.messages.is_empty().then_some(())
            })
            .await;
            producer
                .send_batch_with_routing(
                    [
                        ProducerMessage::new(b"safe-mode-0".as_slice()),
                        ProducerMessage::new(b"safe-mode-1".as_slice()),
                    ],
                    Some(Routing::Partition(0)),
                )
                .await
                .expect("publish after purge");

            let builder = if group {
                topic.consumer_group("ground-station").consumer()
            } else {
                topic.consumer("ground-station", 0)
            };
            let mut after = builder
                .commit_policy(policy)
                .build()
                .await
                .expect("consumer after purge");
            let record = after
                .next_within(RECEIVE_TIMEOUT)
                .await
                .expect("first record after purge");
            assert_eq!(
                (record.position.offset, record.payload.as_ref()),
                (0, b"safe-mode-0".as_slice()),
                "policy {policy:?}, group {group}"
            );
            after.shutdown().await.expect("shutdown after purge");
            producer.shutdown().await.expect("producer shutdown");
        }
    }
}

#[tokio::test]
async fn given_producers_racing_on_a_new_topic_when_built_then_should_all_initialize() {
    let laser = harness::laser().await;
    let topic = laser.topic("racing-topic-creation");
    let builds = (0..8).map(|_| topic.producer().partitions(1).build());
    for result in futures::future::join_all(builds).await {
        result.expect("a producer that lost the creation race still initializes");
    }
}