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
// 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.

//! Handlers for acknowledging or rejecting messages.
//!
//! To acknowledge (ack) a message, you call [`Handler::ack()`].
//!
//! To reject (nack) a message, you call [`Handler::nack()`]. The
//! message will be redelivered.
//!
//! # Example
//!
//! ```
//! use google_cloud_pubsub::model::Message;
//! # use google_cloud_pubsub::subscriber::handler::Handler;
//! fn on_message(m: Message, h: Handler) {
//!   match process(m) {
//!     Ok(_) => h.ack(),
//!     Err(e) => {
//!         println!("failed to process message: {e:?}");
//!         h.nack();
//!     }
//!   }
//! }
//!
//! fn process(m: Message) -> anyhow::Result<()> {
//!   // some business logic here...
//!   # panic!()
//! }
//! ```

use crate::error::AckError;
use crate::subscriber::lease_state::NACK_SHUTDOWN_ERROR;
use tokio::sync::mpsc::UnboundedSender;
use tokio::sync::oneshot::Receiver;

/// The action an application does with a message.
#[derive(Debug, PartialEq)]
pub(super) enum Action {
    Ack(String),
    Nack(String),
    ExactlyOnceAck(String),
    ExactlyOnceNack(String),
}

/// A handler for acknowledging or rejecting messages.
///
/// # Example
///
/// ```
/// use google_cloud_pubsub::model::Message;
/// # use google_cloud_pubsub::subscriber::handler::Handler;
/// fn on_message(m: Message, h: Handler) {
///   match process(m) {
///     Ok(_) => h.ack(),
///     Err(e) => {
///         println!("failed to process message: {e:?}");
///         h.nack();
///     }
///   }
/// }
///
/// fn process(m: Message) -> anyhow::Result<()> {
///   // some business logic here...
///   # panic!()
/// }
/// ```
///
/// To acknowledge (ack) a message, you call [`Handler::ack()`].
///
/// To reject (nack) a message, you call [`Handler::nack()`]. The
/// service will redeliver the message.
///
/// ## Exactly-once delivery
///
/// If your subscription has [exactly-once delivery] enabled, you should
/// destructure this enum into its [`Handler::ExactlyOnce`] branch.
///
/// Only when `ExactlyOnce::confirmed_ack()` returns `Ok` can you be certain
/// that the message will not be redelivered.
///
/// [exactly-once delivery]: https://docs.cloud.google.com/pubsub/docs/exactly-once-delivery
///
/// ```
/// use google_cloud_pubsub::model::Message;
/// # use google_cloud_pubsub::subscriber::handler::Handler;
/// async fn on_message(m: Message, h: Handler) {
///   let Handler::ExactlyOnce(h) = h else {
///     panic!("Oops, my subscription does not have exactly-once delivery enabled.")
///   };
///   match h.confirmed_ack().await {
///     Ok(()) => println!("Confirmed ack for message={m:?}. The message will not be redelivered."),
///     Err(e) => println!("Failed to confirm ack for message={m:?} with error={e:?}"),
///   }
/// }
/// ```
#[derive(Debug)]
#[non_exhaustive]
pub enum Handler {
    AtLeastOnce(AtLeastOnce),
    ExactlyOnce(ExactlyOnce),
}

impl Handler {
    /// Acknowledge the message associated with this handler.
    ///
    /// # Example
    ///
    /// ```
    /// use google_cloud_pubsub::model::Message;
    /// # use google_cloud_pubsub::subscriber::handler::Handler;
    /// fn on_message(m: Message, h: Handler) {
    ///   println!("Received message: {m:?}");
    ///   h.ack();
    /// }
    /// ```
    ///
    /// Note that the acknowledgement is best effort. The message may still be
    /// redelivered to this client, or another client, even if exactly-once
    /// delivery is enabled on the subscription.
    pub fn ack(self) {
        match self {
            Handler::AtLeastOnce(h) => h.ack(),
            Handler::ExactlyOnce(h) => h.ack(),
        }
    }

    /// Rejects the message associated with this handler.
    ///
    /// # Example
    ///
    /// ```
    /// use google_cloud_pubsub::model::Message;
    /// # use google_cloud_pubsub::subscriber::handler::Handler;
    /// fn on_message(m: Message, h: Handler) {
    ///   println!("Received message: {m:?}");
    ///   h.nack();
    /// }
    /// ```
    ///
    /// The message will be removed from this `Subscriber`'s lease management.
    /// The service will redeliver this message, possibly to another client.
    pub fn nack(self) {
        match self {
            Handler::AtLeastOnce(h) => h.nack(),
            Handler::ExactlyOnce(h) => h.nack(),
        }
    }

    #[cfg(test)]
    pub(crate) fn ack_id(&self) -> &str {
        match self {
            Handler::AtLeastOnce(h) => h.ack_id(),
            Handler::ExactlyOnce(h) => h.ack_id(),
        }
    }
}

#[derive(Debug)]
struct AtLeastOnceImpl {
    ack_id: String,
    ack_tx: UnboundedSender<Action>,
}

impl AtLeastOnceImpl {
    fn ack(self) {
        let _ = self.ack_tx.send(Action::Ack(self.ack_id));
    }

    fn nack(self) {
        let _ = self.ack_tx.send(Action::Nack(self.ack_id));
    }
}

/// A handler for at-least-once delivery.
#[derive(Debug)]
pub struct AtLeastOnce {
    inner: Option<AtLeastOnceImpl>,
}

impl AtLeastOnce {
    pub(super) fn new(ack_id: String, ack_tx: UnboundedSender<Action>) -> Self {
        Self {
            inner: Some(AtLeastOnceImpl { ack_id, ack_tx }),
        }
    }

    /// Acknowledge the message associated with this handler.
    ///
    /// Note that the acknowledgement is best effort. The message may still be
    /// redelivered to this client, or another client.
    pub fn ack(mut self) {
        if let Some(inner) = self.inner.take() {
            inner.ack();
        }
    }

    /// Rejects the message associated with this handler.
    ///
    /// # Example
    ///
    /// ```
    /// use google_cloud_pubsub::model::Message;
    /// # use google_cloud_pubsub::subscriber::handler::AtLeastOnce;
    /// fn on_message(m: Message, h: AtLeastOnce) {
    ///   println!("Received message: {m:?}");
    ///   h.nack();
    /// }
    /// ```
    ///
    /// The message will be removed from this `Subscriber`'s lease management.
    /// The service will redeliver this message, possibly to another client.
    pub fn nack(mut self) {
        if let Some(inner) = self.inner.take() {
            inner.nack();
        }
    }

    #[cfg(test)]
    pub(crate) fn ack_id(&self) -> &str {
        self.inner
            .as_ref()
            .map(|i| i.ack_id.as_str())
            .unwrap_or_default()
    }
}

impl Drop for AtLeastOnce {
    /// Rejects the message associated with this handler.
    ///
    /// The message will be removed from this `Subscriber`'s lease management.
    /// The service will redeliver this message, possibly to another client.
    fn drop(&mut self) {
        if let Some(inner) = self.inner.take() {
            inner.nack();
        }
    }
}

/// A handler for exactly-once delivery.
#[derive(Debug)]
pub struct ExactlyOnce {
    inner: Option<ExactlyOnceImpl>,
}

impl ExactlyOnce {
    pub(super) fn new(
        ack_id: String,
        ack_tx: UnboundedSender<Action>,
        result_rx: Receiver<AckResult>,
    ) -> Self {
        Self {
            inner: Some(ExactlyOnceImpl {
                ack_id,
                ack_tx,
                result_rx,
            }),
        }
    }

    /// Acknowledge the message associated with this handler.
    ///
    /// Note that the acknowledgement is best effort. The message may still be
    /// redelivered to this client, or another client.
    pub(crate) fn ack(mut self) {
        if let Some(inner) = self.inner.take() {
            inner.ack();
        }
    }

    pub(crate) fn nack(mut self) {
        if let Some(inner) = self.inner.take() {
            inner.nack();
        }
    }

    /// Strongly acknowledge the message associated with this handler.
    ///
    /// ```
    /// use google_cloud_pubsub::model::Message;
    /// # use google_cloud_pubsub::subscriber::handler::ExactlyOnce;
    /// async fn on_message(m: Message, h: ExactlyOnce) {
    ///   match h.confirmed_ack().await {
    ///     Ok(()) => println!("Confirmed ack for message={m:?}. The message will not be redelivered."),
    ///     Err(e) => println!("Failed to confirm ack for message={m:?} with error={e:?}"),
    ///   }
    /// }
    /// ```
    ///
    /// If the result is an `Ok`, the message is guaranteed not to be delivered
    /// again.
    ///
    /// If the result is an `Err`, the message may be redelivered, but this is
    /// not guaranteed. If no redelivery occurs a sufficient interval after an
    /// error, the acknowledgement likely succeeded.
    pub async fn confirmed_ack(mut self) -> std::result::Result<(), AckError> {
        let inner = self.inner.take().expect("handler impl is always some");
        inner.confirmed_ack().await
    }

    /// Rejects the message associated with this handler and waits for
    /// confirmation.
    ///
    /// ```
    /// use google_cloud_pubsub::model::Message;
    /// # use google_cloud_pubsub::subscriber::handler::ExactlyOnce;
    /// async fn on_message(m: Message, h: ExactlyOnce) {
    ///   match h.confirmed_nack().await {
    ///     Ok(()) => println!("Confirmed nack for message={m:?}. The message will be redelivered."),
    ///     Err(e) => println!("Failed to confirm nack for message={m:?} with error={e:?}"),
    ///   }
    /// }
    /// ```
    ///
    /// If the result is an `Ok`, the message is guaranteed to be immediately
    /// considered for redelivery. If an error occurs, the message will still
    /// be redelivered, but it may be held for the remainder of its
    /// `max_lease_extension`.
    pub async fn confirmed_nack(mut self) -> std::result::Result<(), AckError> {
        let inner = self.inner.take().expect("handler impl is always some");
        inner.confirmed_nack().await
    }

    #[cfg(test)]
    pub(crate) fn ack_id(&self) -> &str {
        self.inner
            .as_ref()
            .map(|i| i.ack_id.as_str())
            .unwrap_or_default()
    }
}

impl Drop for ExactlyOnce {
    /// Rejects the message associated with this handler.
    ///
    /// The message will be removed from this `Subscriber`'s lease management.
    /// The service will redeliver this message, possibly to another client.
    fn drop(&mut self) {
        if let Some(inner) = self.inner.take() {
            inner.nack();
        }
    }
}

#[derive(Debug)]
struct ExactlyOnceImpl {
    pub(super) ack_id: String,
    pub(super) ack_tx: UnboundedSender<Action>,
    pub(super) result_rx: Receiver<AckResult>,
}

impl ExactlyOnceImpl {
    pub fn ack(self) {
        let _ = self.ack_tx.send(Action::ExactlyOnceAck(self.ack_id));
    }

    pub fn nack(self) {
        let _ = self.ack_tx.send(Action::ExactlyOnceNack(self.ack_id));
    }

    pub async fn confirmed_ack(self) -> AckResult {
        self.ack_tx
            .send(Action::ExactlyOnceAck(self.ack_id))
            .map_err(|_| AckError::ShutdownBeforeAck)?;
        self.result_rx
            .await
            .map_err(|e| AckError::Shutdown(e.into()))?
    }

    pub async fn confirmed_nack(self) -> AckResult {
        self.ack_tx
            .send(Action::ExactlyOnceNack(self.ack_id))
            .map_err(|_| AckError::Shutdown(NACK_SHUTDOWN_ERROR.into()))?;
        self.result_rx
            .await
            .map_err(|e| AckError::Shutdown(e.into()))?
    }
}

/// The result of a confirmed acknowledgement.
pub(super) type AckResult = std::result::Result<(), AckError>;

#[cfg(test)]
mod tests {
    use std::error::Error;

    use super::super::lease_state::tests::test_id;
    use super::*;
    use tokio::sync::mpsc::error::TryRecvError;
    use tokio::sync::mpsc::unbounded_channel;
    use tokio::sync::oneshot::channel;

    #[test]
    fn handler_at_least_once_ack() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let h = Handler::AtLeastOnce(AtLeastOnce::new(test_id(1), ack_tx));
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        h.ack();
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::Ack(test_id(1)));

        Ok(())
    }

    #[test]
    fn handler_at_least_once_nack() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let h = Handler::AtLeastOnce(AtLeastOnce::new(test_id(1), ack_tx));
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        h.nack();
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::Nack(test_id(1)));

        Ok(())
    }

    #[test]
    fn handler_exactly_once_ack() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (_result_tx, result_rx) = channel();
        let h = Handler::ExactlyOnce(ExactlyOnce::new(test_id(1), ack_tx, result_rx));
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        h.ack();
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::ExactlyOnceAck(test_id(1)));

        Ok(())
    }

    #[test]
    fn handler_exactly_once_nack() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (_result_tx, result_rx) = channel();
        let h = Handler::ExactlyOnce(ExactlyOnce::new(test_id(1), ack_tx, result_rx));
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        h.nack();
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::ExactlyOnceNack(test_id(1)));

        Ok(())
    }

    #[test]
    fn at_least_once_ack() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let h = AtLeastOnce::new(test_id(1), ack_tx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        h.ack();
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::Ack(test_id(1)));

        Ok(())
    }

    #[test]
    fn at_least_once_nack() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let h = AtLeastOnce::new(test_id(1), ack_tx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        h.nack();
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::Nack(test_id(1)));

        Ok(())
    }

    #[test]
    fn exactly_once_ack() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (_result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        h.ack();
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::ExactlyOnceAck(test_id(1)));

        Ok(())
    }

    #[tokio::test]
    async fn exactly_once_success() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        let task = tokio::task::spawn(async move { h.confirmed_ack().await });

        let ack = ack_rx.recv().await.expect("ack should be sent");
        assert_eq!(ack, Action::ExactlyOnceAck(test_id(1)));

        result_tx
            .send(Ok(()))
            .expect("sending on a channel succeeds");
        task.await??;

        Ok(())
    }

    #[tokio::test]
    async fn exactly_once_nack_success() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        let task = tokio::task::spawn(async move { h.confirmed_nack().await });

        let nack = ack_rx.recv().await.expect("ack should be sent");
        assert_eq!(nack, Action::ExactlyOnceNack(test_id(1)));

        result_tx
            .send(Ok(()))
            .expect("sending on a channel succeeds");
        task.await??;

        Ok(())
    }

    #[tokio::test]
    async fn exactly_once_error() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        let task = tokio::task::spawn(async move { h.confirmed_ack().await });

        let ack = ack_rx.recv().await.expect("ack should be sent");
        assert_eq!(ack, Action::ExactlyOnceAck(test_id(1)));

        result_tx
            .send(Err(AckError::LeaseExpired))
            .expect("sending on a channel succeeds");
        let err = task.await?.expect_err("ack should fail");
        assert!(matches!(err, AckError::LeaseExpired), "{err:?}");

        Ok(())
    }

    #[tokio::test]
    async fn exactly_once_nack_error() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        let task = tokio::task::spawn(async move { h.confirmed_nack().await });

        let nack = ack_rx.recv().await.expect("ack should be sent");
        assert_eq!(nack, Action::ExactlyOnceNack(test_id(1)));

        result_tx
            .send(Err(AckError::LeaseExpired))
            .expect("sending on a channel succeeds");
        let err = task.await?.expect_err("ack should fail");
        assert!(matches!(err, AckError::LeaseExpired), "{err:?}");

        Ok(())
    }

    #[tokio::test]
    async fn exactly_once_action_channel_closed() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (_result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));
        drop(ack_rx);

        let err = h.confirmed_ack().await.expect_err("ack should fail");
        assert!(matches!(err, AckError::ShutdownBeforeAck), "{err:?}");

        Ok(())
    }

    #[tokio::test]
    async fn exactly_once_nack_action_channel_closed() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (_result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));
        drop(ack_rx);

        let err = h.confirmed_nack().await.expect_err("nack should fail");
        assert!(matches!(err, AckError::Shutdown(_)), "{err:?}");
        assert_eq!(
            err.source()
                .expect("shutdown errors have a source")
                .to_string(),
            NACK_SHUTDOWN_ERROR
        );

        Ok(())
    }

    #[tokio::test]
    async fn exactly_once_result_channel_closed() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        let task = tokio::task::spawn(async move { h.confirmed_ack().await });

        let ack = ack_rx.recv().await.expect("ack should be sent");
        assert_eq!(ack, Action::ExactlyOnceAck(test_id(1)));

        drop(result_tx);
        let err = task.await?.expect_err("ack should fail");
        assert!(matches!(err, AckError::Shutdown(_)), "{err:?}");

        Ok(())
    }

    #[test]
    fn exactly_once_nack() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (_result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        h.nack();
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::ExactlyOnceNack(test_id(1)));

        Ok(())
    }

    #[test]
    fn handler_at_least_once_nack_on_drop() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let h = Handler::AtLeastOnce(AtLeastOnce::new(test_id(1), ack_tx));
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        drop(h);
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::Nack(test_id(1)));

        Ok(())
    }

    #[test]
    fn handler_exactly_once_nack_on_drop() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (_result_tx, result_rx) = channel();
        let h = Handler::ExactlyOnce(ExactlyOnce::new(test_id(1), ack_tx, result_rx));
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        drop(h);
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::ExactlyOnceNack(test_id(1)));

        Ok(())
    }

    #[test]
    fn at_least_once_nack_on_drop() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let h = AtLeastOnce::new(test_id(1), ack_tx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        drop(h);
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::Nack(test_id(1)));

        Ok(())
    }

    #[test]
    fn exactly_once_nack_on_drop() -> anyhow::Result<()> {
        let (ack_tx, mut ack_rx) = unbounded_channel();
        let (_result_tx, result_rx) = channel();
        let h = ExactlyOnce::new(test_id(1), ack_tx, result_rx);
        assert_eq!(ack_rx.try_recv(), Err(TryRecvError::Empty));

        drop(h);
        let ack = ack_rx.try_recv()?;
        assert_eq!(ack, Action::ExactlyOnceNack(test_id(1)));

        Ok(())
    }
}