google-cloud-pubsub 0.33.2

Google Cloud Client Libraries for Rust - Pub/Sub
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod at_least_once;
mod exactly_once;

pub(super) use exactly_once::NACK_SHUTDOWN_ERROR;

use super::handler::AckResult;
use super::handler::Action;
use super::leaser::ConfirmedAcks;
use super::leaser::Leaser;
use super::shutdown_behavior::ShutdownBehavior;
use at_least_once::Leases;
use exactly_once::Leases as EoLeases;
use tokio::sync::oneshot::Sender;
// Use a `tokio::time::Instant` to facilitate time-based unit testing.
use tokio::task::JoinSet;
use tokio::time::{Duration, Instant, Interval, interval_at};
use tokio_util::task::TaskTracker;

// Request sizes are limited to 512kB. 500 bytes is a conservative upper limit
// on the size of an ack ID. We can safely fit 1000 ack IDs into a request.
//
// https://docs.cloud.google.com/pubsub/quotas
const MAX_IDS_PER_RPC: usize = 1000;

// How often we extend deadlines for messages under lease
const EXTEND_PERIOD: Duration = Duration::from_secs(3);

// Helper function to chunk ack ids into chunks of MAX_IDS_PER_RPC.
fn batch(ack_ids: Vec<String>) -> Vec<Vec<String>> {
    ack_ids
        .chunks(MAX_IDS_PER_RPC)
        .map(|c| c.to_vec())
        .collect()
}

pub(super) struct LeaseOptions {
    /// How often we flush acks/nacks
    pub(super) flush_period: Duration,
    /// How long we wait for the initial flush
    pub(super) flush_start: Duration,
    /// How often we extend deadlines for messages under lease
    pub(super) extend_period: Duration,
    /// How long we wait for the initial extensions
    pub(super) extend_start: Duration,
    /// How long messages can be kept under lease. A message's lease can be
    /// extended as long as `max_lease` has not elapsed.
    pub(super) max_lease: Duration,
    /// How long a message's lease can be extended by.
    pub(super) max_lease_extension: Duration,
    /// The shutdown behavior of the lease loop
    pub(super) shutdown_behavior: ShutdownBehavior,
}

impl Default for LeaseOptions {
    fn default() -> Self {
        LeaseOptions {
            flush_period: Duration::from_millis(100),
            flush_start: Duration::from_millis(100),
            extend_period: EXTEND_PERIOD,
            extend_start: Duration::from_millis(500),
            max_lease: Duration::from_secs(600),
            max_lease_extension: Duration::from_secs(60),
            shutdown_behavior: ShutdownBehavior::WaitForProcessing,
        }
    }
}

#[derive(Debug)]
pub(super) struct NewMessage {
    pub(super) ack_id: String,
    pub(super) lease_info: LeaseInfo,
}

#[derive(Debug)]
pub(super) enum LeaseInfo {
    AtLeastOnce(AtLeastOnceInfo),
    ExactlyOnce(ExactlyOnceInfo),
}

#[derive(Debug)]
pub(super) struct AtLeastOnceInfo {
    receive_time: Instant,
    last_extension: Option<Instant>,
}

impl AtLeastOnceInfo {
    pub(super) fn new() -> Self {
        AtLeastOnceInfo {
            receive_time: Instant::now(),
            last_extension: None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) enum MessageStatus {
    Leased,
    /// We are currently trying to ack this message.
    ///
    /// We need to continue to extend these leases because the exactly-once
    /// confirmed ack retry loop can take arbitrarily long.
    ///
    /// The client will not expire leases in this state. The server will
    /// report if a lease has expired. We do not want to mask a success with
    /// a `LeaseExpired` error.
    Acking,
    /// We are currently trying to nack this message.
    ///
    /// We keep it in `under_lease` to hold onto `result_tx` until the nack is confirmed,
    /// but we do not want to extend its lease while we wait.
    Nacking,
}

#[derive(Debug)]
pub(super) struct ExactlyOnceInfo {
    receive_time: Instant,
    result_tx: Sender<AckResult>,
    status: MessageStatus,
}

impl ExactlyOnceInfo {
    pub(super) fn new(result_tx: Sender<AckResult>) -> Self {
        ExactlyOnceInfo {
            receive_time: Instant::now(),
            result_tx,
            status: MessageStatus::Leased,
        }
    }
}

#[derive(Debug)]
pub(super) struct LeaseState<L>
where
    L: Leaser + Clone + Send + 'static,
{
    // Ack IDs with at-least-once semantics under lease management.
    leases: Leases,
    // Ack IDs with exactly-once semantics under lease management.
    eo_leases: EoLeases,
    // The leaser, which performs lease operations - (acks, nacks, lease
    // extensions).
    leaser: L,

    // A timer for flushing acks/nacks
    flush_interval: Interval,
    // A timer for extending leases
    extend_interval: Interval,
    // How long messages can be kept under lease
    max_lease: Duration,
    // How long a message's lease can be extended by
    max_lease_extension: Duration,

    // In flight acks and nacks.
    pending_acks_nacks: TaskTracker,

    // In flight lease extension operations.
    //
    // These are held separate from pending acks/nacks because we do not need to
    // await them on shutdown.
    pending_extends: JoinSet<()>,
}

/// Actions taken by the `LeaseState` in the lease loop.
#[derive(Debug, PartialEq)]
pub(super) enum LeaseEvent {
    /// Flush acks/nacks
    Flush,
    /// Extend leases
    Extend,
}

impl<L> LeaseState<L>
where
    L: Leaser + Clone + Send + 'static,
{
    pub(super) fn new(leaser: L, options: LeaseOptions) -> Self {
        let flush_interval =
            interval_at(Instant::now() + options.flush_start, options.flush_period);
        let extend_interval =
            interval_at(Instant::now() + options.extend_start, options.extend_period);
        Self {
            leases: Leases::default(),
            eo_leases: EoLeases::default(),
            leaser,
            flush_interval,
            extend_interval,
            max_lease: options.max_lease,
            max_lease_extension: options.max_lease_extension,
            pending_acks_nacks: TaskTracker::new(),
            pending_extends: JoinSet::new(),
        }
    }

    /// A future that fires when it is time to either:
    /// - flush acks/nacks
    /// - extend leases
    ///
    /// We need to centralize event handling because the lease loop can only
    /// hold one mutable reference to `LeaseState` within its `select!`
    /// statement.
    pub(super) async fn next_event(&mut self) -> LeaseEvent {
        if self.leases.needs_flush() || self.eo_leases.needs_flush() {
            return LeaseEvent::Flush;
        }

        tokio::select! {
            _ = self.flush_interval.tick() => LeaseEvent::Flush,
            _ = self.extend_interval.tick() => LeaseEvent::Extend,
        }
    }

    /// Accept a new ack ID under lease management
    pub(super) fn add(&mut self, ack_id: String, info: LeaseInfo) {
        match info {
            LeaseInfo::AtLeastOnce(i) => {
                self.leases.add(ack_id, i);
            }
            LeaseInfo::ExactlyOnce(i) => {
                self.eo_leases.add(ack_id, i);
            }
        }
    }

    /// Process an action from the application.
    pub(super) fn process(&mut self, action: Action) {
        match action {
            Action::Ack(id) => self.leases.ack(id),
            Action::Nack(id) => self.leases.nack(id),
            Action::ExactlyOnceAck(id) => self.eo_leases.ack(id),
            Action::ExactlyOnceNack(id) => self.eo_leases.nack(id),
        }
    }

    /// Process ack results from the server.
    pub(super) fn confirm(&mut self, results: ConfirmedAcks) {
        for (ack_id, result) in results {
            self.eo_leases.confirm(ack_id, result);
        }
    }

    /// Flush pending acks/nacks
    pub(super) fn flush(&mut self) {
        let (to_ack, to_nack) = self.leases.drain();
        if !to_ack.is_empty() {
            let leaser = self.leaser.clone();
            self.pending_acks_nacks
                .spawn(async move { leaser.ack(to_ack).await });
        }
        if !to_nack.is_empty() {
            let leaser = self.leaser.clone();
            self.pending_acks_nacks
                .spawn(async move { leaser.nack(to_nack).await });
        }

        let (to_ack, to_nack) = self.eo_leases.drain();
        if !to_ack.is_empty() {
            let leaser = self.leaser.clone();
            self.pending_acks_nacks
                .spawn(async move { leaser.confirmed_ack(to_ack).await });
        }
        if !to_nack.is_empty() {
            let leaser = self.leaser.clone();
            self.pending_acks_nacks
                .spawn(async move { leaser.confirmed_nack(to_nack).await });
        }
    }

    /// Extends leases for messages under lease management
    ///
    /// Drops messages whose lease deadline cannot be extended any further.
    pub(super) fn extend(&mut self) {
        let batches = self.leases.retain(self.max_lease, self.max_lease_extension);
        for ack_ids in batches {
            let leaser = self.leaser.clone();
            self.pending_extends
                .spawn(async move { leaser.extend(ack_ids).await });
        }

        let batches = self.eo_leases.retain(self.max_lease);
        for ack_ids in batches {
            let leaser = self.leaser.clone();
            self.pending_extends
                .spawn(async move { leaser.extend(ack_ids).await });
        }

        // TODO(#5048) - we could process the results as a lease event.
        while self.pending_extends.try_join_next().is_some() {}
    }

    /// Shutdown the leaser
    ///
    /// This flushes all pending acks and nacks all other messages.
    pub(super) async fn shutdown(self) {
        // Note that if `WaitForProcessing` was selected by the application,
        // there are no messages under lease. They have all been processed.
        let (to_ack, to_nack) = self.leases.evict_and_drain();
        if !to_ack.is_empty() {
            let leaser = self.leaser.clone();
            self.pending_acks_nacks
                .spawn(async move { leaser.ack(to_ack).await });
        }
        for to_nack in to_nack {
            let leaser = self.leaser.clone();
            self.pending_acks_nacks
                .spawn(async move { leaser.nack(to_nack).await });
        }

        // TODO(#5109) - evicting exactly-once leases is ok, but not ideal.
        // Currently, evict returns NACK_SHUTDOWN_ERROR for all exactly once
        // leases. This includes the to_ack leases. Specifically,
        // the leases that have been acknowledged by the application but not yet
        // flushed. Therefore, we do not need to flush those leases.
        let (_, to_nack) = self.eo_leases.evict_and_drain();
        for to_nack in to_nack {
            let leaser = self.leaser.clone();
            self.pending_acks_nacks
                .spawn(async move { leaser.confirmed_nack(to_nack).await });
        }

        // Wait for pending acks/nacks to complete.
        self.pending_acks_nacks.close();
        self.pending_acks_nacks.wait().await;

        // Wait for pending lease extensions to complete. This is not useful in
        // practice, because we are nacking all the messages, but it simplifies
        // our tests.
        #[cfg(test)]
        self.pending_extends.join_all().await;
    }
}

#[cfg(test)]
pub(super) mod tests {
    use super::super::leaser::tests::MockLeaser;
    use super::Action::{Ack, ExactlyOnceAck, ExactlyOnceNack, Nack};
    use super::*;
    use std::collections::HashMap;
    use std::collections::HashSet;
    use std::sync::Arc;
    use test_case::test_case;
    use tokio::sync::mpsc::unbounded_channel;
    use tokio::sync::oneshot::channel;

    // Cover the constant, converting it to an integer for convenience.
    const MAX_IDS_PER_RPC: i32 = super::MAX_IDS_PER_RPC as i32;

    #[derive(Debug)]
    pub(super) struct TestLeases {
        pub(super) under_lease: Vec<String>,
        pub(super) to_ack: Vec<String>,
        pub(super) to_nack: Vec<String>,
    }

    #[derive(Debug)]
    pub(super) struct Batches {
        pub(super) counts: Vec<i32>,
        pub(super) ack_ids: Vec<String>,
    }

    impl Batches {
        pub(super) fn flatten(to_nack: Vec<Vec<String>>) -> Self {
            let counts = to_nack.iter().map(|v| v.len() as i32).collect();
            let ack_ids = to_nack.into_iter().flatten().collect();
            Self { counts, ack_ids }
        }
    }

    pub(in super::super) fn test_id(v: i32) -> String {
        format!("{v:05}")
    }

    pub(in super::super) fn test_ids(range: std::ops::Range<i32>) -> Vec<String> {
        range.map(test_id).collect()
    }

    pub(in super::super) fn sorted(v: &[String]) -> Vec<String> {
        let mut s = v.to_owned();
        s.sort();
        s
    }

    pub(in super::super) fn at_least_once_info() -> LeaseInfo {
        LeaseInfo::AtLeastOnce(AtLeastOnceInfo::new())
    }

    pub(in super::super) fn exactly_once_info() -> LeaseInfo {
        let (result_tx, _result_rx) = channel();
        LeaseInfo::ExactlyOnce(ExactlyOnceInfo::new(result_tx))
    }

    async fn extend_and_await<L>(state: &mut LeaseState<L>)
    where
        L: Leaser + Clone + Send + 'static,
    {
        state.extend();
        let pending_extends = std::mem::take(&mut state.pending_extends);
        let _ = pending_extends.join_all().await;
    }

    async fn flush_and_await<L>(state: &mut LeaseState<L>)
    where
        L: Leaser + Clone + Send + 'static,
    {
        state.flush();
        let pending_acks_nacks = std::mem::take(&mut state.pending_acks_nacks);
        pending_acks_nacks.close();
        pending_acks_nacks.wait().await;
    }

    #[tokio::test(start_paused = true)]
    async fn basic_add_ack_nack() {
        let mock = MockLeaser::new();
        let mut state = LeaseState::new(Arc::new(mock), LeaseOptions::default());
        assert_eq!(
            TestLeases {
                under_lease: Vec::new(),
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.leases
        );

        state.add(test_id(1), at_least_once_info());
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1)],
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.leases
        );

        state.add(test_id(2), at_least_once_info());
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1), test_id(2)],
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.leases
        );

        state.add(test_id(3), at_least_once_info());
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1), test_id(2), test_id(3)],
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.leases
        );

        state.process(Ack(test_id(1)));
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(2), test_id(3)],
                to_ack: vec![test_id(1)],
                to_nack: Vec::new(),
            },
            state.leases
        );

        state.process(Nack(test_id(2)));
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(3)],
                to_ack: vec![test_id(1)],
                to_nack: vec![test_id(2)],
            },
            state.leases
        );

        state.add(test_id(4), at_least_once_info());
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(3), test_id(4)],
                to_ack: vec![test_id(1)],
                to_nack: vec![test_id(2)],
            },
            state.leases
        );

        state.process(Ack(test_id(4)));
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(3)],
                to_ack: vec![test_id(1), test_id(4)],
                to_nack: vec![test_id(2)],
            },
            state.leases
        );

        state.process(Nack(test_id(3)));
        assert_eq!(
            TestLeases {
                under_lease: Vec::new(),
                to_ack: vec![test_id(1), test_id(4)],
                to_nack: vec![test_id(2), test_id(3)],
            },
            state.leases
        );
    }

    #[tokio::test(start_paused = true)]
    async fn basic_add_confirmed_ack_nack() {
        let mock = MockLeaser::new();
        let mut state = LeaseState::new(Arc::new(mock), LeaseOptions::default());
        assert_eq!(
            TestLeases {
                under_lease: Vec::new(),
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.eo_leases
        );

        state.add(test_id(1), exactly_once_info());
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1)],
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.eo_leases
        );

        state.add(test_id(2), exactly_once_info());
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1), test_id(2)],
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.eo_leases
        );

        state.add(test_id(3), exactly_once_info());
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1), test_id(2), test_id(3)],
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.eo_leases
        );

        state.process(ExactlyOnceAck(test_id(1)));
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1), test_id(2), test_id(3)],
                to_ack: vec![test_id(1)],
                to_nack: Vec::new(),
            },
            state.eo_leases
        );

        state.process(ExactlyOnceNack(test_id(2)));
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1), test_id(2), test_id(3)],
                to_ack: vec![test_id(1)],
                to_nack: vec![test_id(2)],
            },
            state.eo_leases
        );

        state.add(test_id(4), exactly_once_info());
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1), test_id(2), test_id(3), test_id(4)],
                to_ack: vec![test_id(1)],
                to_nack: vec![test_id(2)],
            },
            state.eo_leases
        );

        state.process(ExactlyOnceAck(test_id(4)));
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1), test_id(2), test_id(3), test_id(4)],
                to_ack: vec![test_id(1), test_id(4)],
                to_nack: vec![test_id(2)],
            },
            state.eo_leases
        );

        state.process(ExactlyOnceNack(test_id(3)));
        assert_eq!(
            TestLeases {
                under_lease: vec![test_id(1), test_id(2), test_id(3), test_id(4)],
                to_ack: vec![test_id(1), test_id(4)],
                to_nack: vec![test_id(2), test_id(3)],
            },
            state.eo_leases
        );
    }

    #[tokio::test]
    async fn leaser_noops() {
        let mock = MockLeaser::new();
        // Note that there are no calls expected into the leaser, as there are
        // no messages under lease management.
        let mut state = LeaseState::new(Arc::new(mock), LeaseOptions::default());
        extend_and_await(&mut state).await;
        state.flush();
        state.shutdown().await;
    }

    #[tokio::test(start_paused = true)]
    async fn flush() {
        let mut mock = MockLeaser::new();
        mock.expect_ack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(0..10))
            .returning(|_| ());
        mock.expect_nack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(10..20))
            .returning(|_| ());
        mock.expect_confirmed_ack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(110..120))
            .returning(|_| ());
        mock.expect_confirmed_nack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(100..110))
            .returning(|_| ());
        let mut state = LeaseState::new(Arc::new(mock), LeaseOptions::default());
        for i in 0..100 {
            state.add(test_id(i), at_least_once_info());
        }
        for i in 0..10 {
            state.process(Ack(test_id(i)));
        }
        for i in 10..20 {
            state.process(Nack(test_id(i)));
        }
        for i in 100..200 {
            state.add(test_id(i), exactly_once_info());
        }
        for i in 100..110 {
            state.process(ExactlyOnceNack(test_id(i)));
        }
        for i in 110..120 {
            state.process(ExactlyOnceAck(test_id(i)));
        }
        assert_eq!(
            TestLeases {
                under_lease: test_ids(20..100),
                to_ack: test_ids(0..10),
                to_nack: test_ids(10..20),
            },
            state.leases
        );
        assert_eq!(
            TestLeases {
                under_lease: test_ids(100..200),
                to_ack: test_ids(110..120),
                to_nack: test_ids(100..110),
            },
            state.eo_leases
        );

        flush_and_await(&mut state).await;
        assert_eq!(
            TestLeases {
                under_lease: test_ids(20..100),
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.leases
        );
        assert_eq!(
            TestLeases {
                under_lease: test_ids(100..200),
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.eo_leases
        );

        let mut ack_results = HashMap::new();
        for i in 110..115 {
            ack_results.insert(test_id(i), Ok(()));
        }
        state.confirm(ack_results);
        let mut expected_under_lease = test_ids(100..110);
        expected_under_lease.extend(test_ids(115..200));
        assert_eq!(
            TestLeases {
                under_lease: expected_under_lease,
                to_ack: Vec::new(),
                to_nack: Vec::new(),
            },
            state.eo_leases
        );
    }

    #[tokio::test(start_paused = true)]
    async fn pending_acks_nacks_size_management() {
        let mut mock = MockLeaser::new();
        mock.expect_ack()
            .times(1)
            .withf(|v| *v == vec![test_id(1)])
            .returning(|_| ());
        mock.expect_nack()
            .times(1)
            .withf(|v| *v == vec![test_id(2)])
            .returning(|_| ());

        let mut state = LeaseState::new(Arc::new(mock), LeaseOptions::default());

        state.add(test_id(1), at_least_once_info());
        state.process(Ack(test_id(1)));

        state.flush();
        // Yield execution so the ack attempt can execute.
        tokio::task::yield_now().await;
        assert!(
            state.pending_acks_nacks.is_empty(),
            "The ack task should have completed. We should not hold onto it."
        );

        state.add(test_id(2), at_least_once_info());
        state.process(Nack(test_id(2)));

        state.flush();
        // Yield execution so the nack attempt can execute.
        tokio::task::yield_now().await;
        assert!(
            state.pending_acks_nacks.is_empty(),
            "The nack task should have completed. We should not hold onto it."
        );
    }

    #[tokio::test(start_paused = true)]
    async fn extend_at_least_once() {
        let mut seq = mockall::Sequence::new();
        let mut mock = MockLeaser::new();
        mock.expect_extend()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(0..10))
            .returning(|_| ());
        mock.expect_extend()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(0..20))
            .returning(|_| ());
        mock.expect_extend()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(5..20))
            .returning(|_| ());
        mock.expect_extend()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(10..20))
            .returning(|_| ());

        let options = LeaseOptions {
            max_lease_extension: Duration::ZERO,
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        // Add 10 messages. These are now under lease management.
        for i in 0..10 {
            state.add(test_id(i), at_least_once_info());
        }
        extend_and_await(&mut state).await;

        // Add another 10 messages. These are now under lease management.
        for i in 10..20 {
            state.add(test_id(i), at_least_once_info());
        }
        extend_and_await(&mut state).await;

        // Ack the first 5 messages. We should not extend these leases.
        for i in 0..5 {
            state.process(Ack(test_id(i)));
        }
        extend_and_await(&mut state).await;

        // Nack the next 5 messages. We should not extend these leases.
        for i in 5..10 {
            state.process(Nack(test_id(i)));
        }
        extend_and_await(&mut state).await;
    }

    #[tokio::test(start_paused = true)]
    async fn extend_exactly_once() {
        let mut seq = mockall::Sequence::new();
        let mut mock = MockLeaser::new();
        mock.expect_extend()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(0..10))
            .returning(|_| ());
        mock.expect_extend()
            .times(2)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(0..20))
            .returning(|_| ());
        mock.expect_confirmed_ack()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(0..5))
            .returning(|_| ());
        mock.expect_extend()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(5..20))
            .returning(|_| ());
        mock.expect_extend()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(5..20))
            .returning(|_| ());

        let mut state = LeaseState::new(Arc::new(mock), LeaseOptions::default());

        // Add 10 messages. These are now under lease management.
        for i in 0..10 {
            state.add(test_id(i), exactly_once_info());
        }
        extend_and_await(&mut state).await;

        // Add another 10 messages. These are now under lease management.
        for i in 10..20 {
            state.add(test_id(i), exactly_once_info());
        }
        extend_and_await(&mut state).await;

        // Ack the first 5 messages. We should continue to extend these leases
        // as they are not yet confirmed.
        let mut ack_results = HashMap::new();
        for i in 0..5 {
            state.process(ExactlyOnceAck(test_id(i)));
            ack_results.insert(test_id(i), Ok(()));
        }
        extend_and_await(&mut state).await;

        // Flush the acks and confirm.
        flush_and_await(&mut state).await;
        state.confirm(ack_results);

        // We should not extend the confirmed acks.
        extend_and_await(&mut state).await;

        // Nack the next 5 messages. We should not extend these leases.
        for i in 0..5 {
            state.process(ExactlyOnceNack(test_id(i)));
        }
        extend_and_await(&mut state).await;
    }

    #[tokio::test(start_paused = true)]
    async fn pending_extends_size_management() {
        let mut mock = MockLeaser::new();
        mock.expect_extend()
            .times(2)
            .withf(|v| *v == vec![test_id(1)])
            .returning(|_| ());

        let options = LeaseOptions {
            max_lease_extension: Duration::ZERO,
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        state.add(test_id(1), at_least_once_info());
        state.extend();
        // Yield execution so the extend attempt can execute.
        tokio::task::yield_now().await;

        // TODO(#5048) - We currently clean up the completed pending extends in
        // `LeaseState::extend()`. If we decide to clean up the pending extends
        // elsewhere, this test will need an update.
        state.extend();
        let pending_extends = state.pending_extends.len();
        assert!(
            pending_extends < 2,
            "The first lease extension attempt should have completed. We should not hold onto it."
        );

        let _ = state.pending_extends.join_all().await;
    }

    #[tokio::test]
    async fn shutdown() {
        let mut mock = MockLeaser::new();
        // For exactly once, the current behavior is to Nack everything that has not yet
        // been confirmed.
        mock.expect_ack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(0..10))
            .returning(|_| ());
        mock.expect_nack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(10..30))
            .returning(|_| ());
        mock.expect_confirmed_nack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(30..60))
            .returning(|_| ());

        let mut state = LeaseState::new(Arc::new(mock), LeaseOptions::default());
        for i in 0..30 {
            state.add(test_id(i), at_least_once_info());
        }
        for i in 0..10 {
            state.process(Ack(test_id(i)));
        }
        for i in 10..20 {
            state.process(Nack(test_id(i)));
        }
        for i in 30..60 {
            state.add(test_id(i), exactly_once_info());
        }
        for i in 30..40 {
            state.process(ExactlyOnceAck(test_id(i)));
        }
        for i in 40..50 {
            state.process(ExactlyOnceNack(test_id(i)));
        }
        state.shutdown().await;
    }

    #[tokio::test(start_paused = true)]
    async fn lease_events_timing() {
        let start = Instant::now();

        // We expect flushes at time t=[1, 3, 5, 7, ...]
        const FLUSH_START: Duration = Duration::from_secs(1);
        const FLUSH_PERIOD: Duration = Duration::from_secs(2);

        // We expect extensions at time t=[2, 6, 10, 14, ...]
        const EXTEND_START: Duration = Duration::from_secs(2);
        const EXTEND_PERIOD: Duration = Duration::from_secs(4);

        let mock = MockLeaser::new();
        let options = LeaseOptions {
            flush_start: FLUSH_START,
            flush_period: FLUSH_PERIOD,
            extend_start: EXTEND_START,
            extend_period: EXTEND_PERIOD,
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), Duration::from_secs(1));

        assert_eq!(state.next_event().await, LeaseEvent::Extend);
        assert_eq!(start.elapsed(), Duration::from_secs(2));

        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), Duration::from_secs(3));

        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), Duration::from_secs(5));

        assert_eq!(state.next_event().await, LeaseEvent::Extend);
        assert_eq!(start.elapsed(), Duration::from_secs(6));

        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), Duration::from_secs(7));
    }

    #[tokio::test(start_paused = true)]
    async fn limit_size_of_ack_batch_at_least_once() {
        let start = Instant::now();

        const FLUSH_START: Duration = Duration::from_secs(1);

        let mut mock = MockLeaser::new();
        mock.expect_ack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(0..MAX_IDS_PER_RPC))
            .returning(|_| ());
        let options = LeaseOptions {
            flush_start: FLUSH_START,
            flush_period: Duration::from_secs(100),
            extend_start: Duration::from_secs(100),
            extend_period: Duration::from_secs(100),
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        for i in 0..MAX_IDS_PER_RPC {
            state.add(test_id(i), at_least_once_info());
            state.process(Ack(test_id(i)));
        }
        // With MAX_IDS_PER_RPC pending acks, the batch is full. We should flush it now.
        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), Duration::ZERO);
        flush_and_await(&mut state).await;

        // With the batch is not full. The next event should occur on the interval timer.
        for i in MAX_IDS_PER_RPC..(2 * MAX_IDS_PER_RPC - 1) {
            state.add(test_id(i), at_least_once_info());
            state.process(Ack(test_id(i)));
        }
        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), FLUSH_START);
    }

    #[tokio::test(start_paused = true)]
    async fn limit_size_of_ack_batch_exactly_once() {
        let start = Instant::now();

        const FLUSH_START: Duration = Duration::from_secs(1);

        let mut mock = MockLeaser::new();
        mock.expect_confirmed_ack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(0..MAX_IDS_PER_RPC))
            .returning(|_| ());
        let options = LeaseOptions {
            flush_start: FLUSH_START,
            flush_period: Duration::from_secs(100),
            extend_start: Duration::from_secs(100),
            extend_period: Duration::from_secs(100),
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        // With MAX_IDS_PER_RPC pending confirmed acks, the batch is full. We should flush it now.
        for i in 0..MAX_IDS_PER_RPC {
            state.add(test_id(i), exactly_once_info());
            state.process(ExactlyOnceAck(test_id(i)));
        }
        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), Duration::ZERO);
        flush_and_await(&mut state).await;

        // With the batch is not full. The next event should occur on the interval timer.
        for i in MAX_IDS_PER_RPC..(2 * MAX_IDS_PER_RPC - 1) {
            state.add(test_id(i), exactly_once_info());
            state.process(ExactlyOnceAck(test_id(i)));
        }
        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), FLUSH_START);
    }

    #[tokio::test(start_paused = true)]
    async fn limit_size_of_nack_batch_at_least_once() {
        let start = Instant::now();

        const FLUSH_START: Duration = Duration::from_secs(1);

        let mut mock = MockLeaser::new();
        mock.expect_nack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(0..MAX_IDS_PER_RPC))
            .returning(|_| ());
        let options = LeaseOptions {
            flush_start: FLUSH_START,
            flush_period: Duration::from_secs(100),
            extend_start: Duration::from_secs(100),
            extend_period: Duration::from_secs(100),
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        for i in 0..MAX_IDS_PER_RPC {
            state.add(test_id(i), at_least_once_info());
            state.process(Nack(test_id(i)));
        }
        // With MAX_IDS_PER_RPC pending nacks, the batch is full. We should flush it now.
        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), Duration::ZERO);
        flush_and_await(&mut state).await;

        // With the batch is not full. The next event should occur on the interval timer.
        for i in MAX_IDS_PER_RPC..(2 * MAX_IDS_PER_RPC - 1) {
            state.add(test_id(i), at_least_once_info());
            state.process(Nack(test_id(i)));
        }

        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), FLUSH_START);
    }

    #[tokio::test(start_paused = true)]
    async fn limit_size_of_nack_batch_exactly_once() {
        let start = Instant::now();

        const FLUSH_START: Duration = Duration::from_secs(1);

        let mut mock = MockLeaser::new();
        mock.expect_confirmed_nack()
            .times(1)
            .withf(|v| sorted(v) == test_ids(0..MAX_IDS_PER_RPC))
            .returning(|_| ());
        let options = LeaseOptions {
            flush_start: FLUSH_START,
            flush_period: Duration::from_secs(100),
            extend_start: Duration::from_secs(100),
            extend_period: Duration::from_secs(100),
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        for i in 0..MAX_IDS_PER_RPC {
            state.add(test_id(i), exactly_once_info());
            state.process(ExactlyOnceNack(test_id(i)));
        }
        // With MAX_IDS_PER_RPC pending nacks for exactly once leases, the batch is full. We should flush it now.
        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), Duration::ZERO);
        flush_and_await(&mut state).await;

        // With the batch is not full. The next event should occur on the interval timer.
        for i in MAX_IDS_PER_RPC..(2 * MAX_IDS_PER_RPC - 1) {
            state.add(test_id(i), exactly_once_info());
            state.process(ExactlyOnceNack(test_id(i)));
        }
        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), FLUSH_START);
    }

    #[tokio::test(start_paused = true)]
    async fn ack_and_nack_batches_are_independent_at_least_once() {
        let start = Instant::now();

        const FLUSH_START: Duration = Duration::from_secs(1);

        let mock = MockLeaser::new();
        let options = LeaseOptions {
            flush_start: FLUSH_START,
            flush_period: Duration::from_secs(100),
            extend_start: Duration::from_secs(100),
            extend_period: Duration::from_secs(100),
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        let over_half_full = MAX_IDS_PER_RPC / 2 + 100;
        for i in 0..over_half_full {
            state.add(test_id(i), at_least_once_info());
            state.process(Ack(test_id(i)));

            state.add(test_id(over_half_full + i), at_least_once_info());
            state.process(Nack(test_id(over_half_full + i)));
        }

        // While there are more than `MAX_IDS_PER_RPC` total messages under
        // lease management, neither the ack batch nor the nack batch are full.
        // The next event should occur on the interval timer.
        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), FLUSH_START);
    }

    #[tokio::test(start_paused = true)]
    async fn ack_and_nack_batches_are_independent_exactly_once() {
        let start = Instant::now();

        const FLUSH_START: Duration = Duration::from_secs(1);

        let mock = MockLeaser::new();
        let options = LeaseOptions {
            flush_start: FLUSH_START,
            flush_period: Duration::from_secs(100),
            extend_start: Duration::from_secs(100),
            extend_period: Duration::from_secs(100),
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        let over_half_full = MAX_IDS_PER_RPC / 2 + 100;
        for i in 0..over_half_full {
            state.add(test_id(i), exactly_once_info());
            state.process(ExactlyOnceAck(test_id(i)));

            state.add(test_id(over_half_full + i), exactly_once_info());
            state.process(ExactlyOnceNack(test_id(over_half_full + i)));
        }

        // While there are more than `MAX_IDS_PER_RPC` total messages under
        // lease management, neither the ack batch nor the nack batch are full.
        // The next event should occur on the interval timer.
        assert_eq!(state.next_event().await, LeaseEvent::Flush);
        assert_eq!(start.elapsed(), FLUSH_START);
    }

    #[test_case(super::at_least_once_info)]
    #[test_case(super::exactly_once_info)]
    #[tokio::test(start_paused = true)]
    async fn limit_size_of_extends(lease_info_factory: fn() -> LeaseInfo) -> anyhow::Result<()> {
        const NUM_BATCHES: i32 = 5;

        // We use this channel to surface ack_ids from the mock expectation.
        let (ack_id_tx, mut ack_id_rx) = unbounded_channel();

        let mut mock = MockLeaser::new();
        mock.expect_extend()
            .times(NUM_BATCHES as usize)
            .returning(move |ack_ids| {
                ack_id_tx
                    .send(ack_ids)
                    .expect("sending on channel always succeeds");
            });
        let mut state = LeaseState::new(Arc::new(mock), LeaseOptions::default());

        let mut want = HashSet::new();
        for i in 0..NUM_BATCHES * MAX_IDS_PER_RPC {
            state.add(test_id(i), lease_info_factory());

            // All ack IDs should be extended.
            want.insert(test_id(i));
        }
        extend_and_await(&mut state).await;

        let mut got = HashSet::new();
        for i in 0..NUM_BATCHES {
            let Some(ack_ids) = ack_id_rx.recv().await else {
                anyhow::bail!("expected batch {i}/{NUM_BATCHES}");
            };
            assert_eq!(ack_ids.len(), MAX_IDS_PER_RPC as usize);
            for ack_id in ack_ids {
                got.insert(ack_id);
            }
        }
        assert!(
            ack_id_rx.is_empty(),
            "There should be exactly {NUM_BATCHES} batches"
        );

        // Make sure all ack IDs were extended.
        assert_eq!(got, want);
        Ok(())
    }

    #[test_case(super::at_least_once_info)]
    #[test_case(super::exactly_once_info)]
    #[tokio::test(start_paused = true)]
    async fn message_expiration(lease_info_factory: fn() -> LeaseInfo) -> anyhow::Result<()> {
        const MAX_LEASE: Duration = Duration::from_secs(300);
        const DELTA: Duration = Duration::from_secs(1);

        let mut seq = mockall::Sequence::new();
        let mut mock = MockLeaser::new();
        mock.expect_extend()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(0..20))
            .returning(|_| ());
        mock.expect_extend()
            .times(1)
            .in_sequence(&mut seq)
            .withf(|v| sorted(v) == test_ids(10..20))
            .returning(|_| ());

        let options = LeaseOptions {
            max_lease: MAX_LEASE,
            ..Default::default()
        };
        let mut state = LeaseState::new(Arc::new(mock), options);

        // Add 10 messages under lease management
        for i in 0..10 {
            state.add(test_id(i), lease_info_factory());
        }

        // Add 10 more messages under lease management, a little later.
        tokio::time::advance(DELTA * 2).await;
        for i in 10..20 {
            state.add(test_id(i), lease_info_factory());
        }
        extend_and_await(&mut state).await;

        // Advance the time past the expiration of the original 10 messages.
        tokio::time::advance(MAX_LEASE - DELTA).await;
        extend_and_await(&mut state).await;

        // Advance the time past the expiration of the subsequent 10 messages.
        tokio::time::advance(DELTA * 2).await;
        extend_and_await(&mut state).await;

        Ok(())
    }
}