retrier 0.2.0

A wasm-compatible retry library for futures
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
//! `Retrier` is a wasm-compatible utility for retrying standard library [`Futures`](https://doc.rust-lang.org/std/future/trait.Future.html) with a `Result` output type
//!
//! A goal of any operation should be a successful outcome. This crate gives
//! operations a better chance at achieving that.
//!
//! # Examples
//!
//! ## Hello world
//!
//! For simple cases, you can use the module level [`retry`](fn.retry.html) fn,
//! which will retry a task every second for 5 seconds with an exponential
//! backoff.
//!
//! ```no_run
//! retrier::retry(|| reqwest::get("https://api.company.com"));
//! ```
//!
//! ## Conditional retries
//!
//! By default, `retrier` will retry any failed `Future` if its `Result` output
//! type is an `Err`. You may not want to retry _every_ kind of error. In those
//! cases you may wish to use the [`retry_if`](fn.retry_if.html) fn, which
//! accepts an additional argument to conditionally determine if the error
//! should be retried.
//!
//! ```no_run
//! retrier::retry_if(
//! 	|| reqwest::get("https://api.company.com"),
//! 	reqwest::Error::is_status,
//! );
//! ```
//!
//! ## Retry policies
//!
//! Every application has different needs. The default retry behavior in
//! `retrier` likely will not suit all of them. You can define your own retry
//! behavior with a [`RetryPolicy`](struct.RetryPolicy.html). A `RetryPolicy`
//! can be configured with a fixed or exponential backoff, jitter, and other
//! common retry options. This objects may be reused across operations. For more
//! information see the [`RetryPolicy`](struct.RetryPolicy.html) docs.
//!
//! ```ignore
//! use retrier::RetryPolicy;
//! use std::time::Duration;
//!
//! let policy = RetryPolicy::fixed(Duration::from_millis(100))
//!     .with_max_retries(10)
//!     .with_jitter(false);
//!
//! policy.retry(|| reqwest::get("https://api.company.com"));
//! ```
//!
//! # Logging
//!
//! For visibility on when operations fail and are retried, a `log::trace`
//! message is emitted, logging the `Debug` display of the error and the delay
//! before the next attempt.
//!
//! # wasm features
//!
//! `retrier` supports [WebAssembly](https://webassembly.org/) targets i.e. `wasm32-unknown-unknown` which should make this
//! crate a good fit for most environments
//!
//! Two cargo features exist to support various wasm runtimes: `wasm-bindgen`
//! and `stdweb`. To enable them add the following to your `Cargo.toml` file.
//!
//! ```toml
//! [dependencies]
//! retrier = { version = "x", features = ["js"] }
//! ```
use std::cmp::min;
use std::future::Future;
use std::time::Duration;

use futures_timer::Delay;
#[cfg(feature = "rand")]
use rand::Rng;
#[cfg(feature = "rand")]
use rand::distr::OpenClosed01;
#[cfg(feature = "rand")]
use rand::rng;

/// Retries a fallible `Future` with a default `RetryPolicy`
///
/// ```
/// retrier::retry(|| async { Ok::<u32, ()>(42) });
/// ```
pub async fn retry<T>(task: T) -> Result<T::Item, T::Error>
where
	T: Task,
{
	retry_if(task, Always).await
}

/// Retries a fallible `Future` under a certain provided condition with a
/// default `RetryPolicy`
///
/// ```
/// retrier::retry_if(|| async { Err::<u32, u32>(7) }, |err: &u32| *err != 42);
/// ```
pub async fn retry_if<T, C>(task: T, condition: C) -> Result<T::Item, T::Error>
where
	T: Task,
	C: Condition<T::Error>,
{
	RetryPolicy::default().retry_if(task, condition).await
}

/// Reruns and collects the results of a successful `Future` under a certain
/// provided condition with a default `RetryPolicy`
///
/// ```
/// retrier::collect(
/// 	|i: u32| async move { Ok::<u32, ()>(i + 1) },
/// 	|r: &u32| if *r != 32 { Some(*r) } else { None },
/// 	1 as u32,
/// );
/// ```
pub async fn collect<T, C, S>(
	task: T,
	condition: C,
	start_value: S,
) -> Result<Vec<T::Item>, T::Error>
where
	T: TaskWithParameter<S>,
	C: SuccessCondition<T::Item, S>,
{
	RetryPolicy::default()
		.collect(task, condition, start_value)
		.await
}

/// Reruns and collects the results of a `Future`, if successful, with a default
/// `RetryPolicy` under a certain provided success condition. Also retries the
/// `Future`, if not successful under the same policy configuration and the
/// provided error condition.
///
/// ```
/// retrier::collect_and_retry(
/// 	|input: u32| async move { Ok::<u32, u32>(input + 1) },
/// 	|result: &u32| if *result < 2 { Some(*result) } else { None },
/// 	|err: &u32| *err > 1,
/// 	0,
/// );
/// ```
pub async fn collect_and_retry<T, C, D, S>(
	task: T,
	success_condition: C,
	error_condition: D,
	start_value: S,
) -> Result<Vec<T::Item>, T::Error>
where
	T: TaskWithParameter<S>,
	C: SuccessCondition<T::Item, S>,
	D: Condition<T::Error>,
	S: Clone,
{
	RetryPolicy::default()
		.collect_and_retry(task, success_condition, error_condition, start_value)
		.await
}

#[derive(Clone, Copy)]
enum Backoff {
	Fixed,
	Exponential { exponent: f64 },
}

impl Default for Backoff {
	fn default() -> Self {
		Backoff::Exponential { exponent: 2.0 }
	}
}

impl Backoff {
	fn iter(self, policy: &RetryPolicy) -> BackoffIter {
		BackoffIter {
			backoff: self,
			current: 1.0,
			#[cfg(feature = "rand")]
			jitter: policy.jitter,
			delay: policy.delay,
			max_delay: policy.max_delay,
			max_retries: policy.max_retries,
		}
	}
}

struct BackoffIter {
	backoff: Backoff,
	current: f64,
	#[cfg(feature = "rand")]
	jitter: bool,
	delay: Duration,
	max_delay: Option<Duration>,
	max_retries: usize,
}

impl Iterator for BackoffIter {
	type Item = Duration;

	fn next(&mut self) -> Option<Self::Item> {
		if self.max_retries > 0 {
			let factor = match self.backoff {
				Backoff::Fixed => self.current,
				Backoff::Exponential { exponent } => {
					let factor = self.current;
					let next_factor = self.current * exponent;
					self.current = next_factor;
					factor
				}
			};

			let mut delay = self.delay.mul_f64(factor);
			#[cfg(feature = "rand")]
			{
				if self.jitter {
					delay = jitter(delay);
				}
			}
			if let Some(max_delay) = self.max_delay {
				delay = min(delay, max_delay);
			}
			self.max_retries -= 1;

			return Some(delay);
		}
		None
	}
}

/// A template for configuring retry behavior
///
/// A default is provided, configured
/// to retry a task 5 times with exponential backoff
/// starting with a 1 second delay
#[derive(Clone)]
pub struct RetryPolicy {
	backoff: Backoff,
	#[cfg(feature = "rand")]
	jitter: bool,
	delay: Duration,
	max_delay: Option<Duration>,
	max_retries: usize,
}

impl Default for RetryPolicy {
	fn default() -> Self {
		Self {
			backoff: Backoff::default(),
			delay: Duration::from_secs(1),
			#[cfg(feature = "rand")]
			jitter: false,
			max_delay: None,
			max_retries: 5,
		}
	}
}

#[cfg(feature = "rand")]
fn jitter(duration: Duration) -> Duration {
	let jitter: f64 = rng().sample(OpenClosed01);
	let secs = (duration.as_secs() as f64) * jitter;
	let nanos = f64::from(duration.subsec_nanos()) * jitter;
	let millis = (secs * 1_000_f64) + (nanos / 1_000_000_f64);
	Duration::from_millis(millis as u64)
}

impl RetryPolicy {
	fn backoffs(&self) -> impl Iterator<Item = Duration> + '_ {
		self.backoff.iter(self)
	}

	/// Configures policy with an exponential
	/// backoff delay.
	///
	/// By default, Futures will be retried 5 times.
	///
	/// These delays will increase in
	/// length over time. You may wish to cap just how long
	/// using the [`with_max_delay`](struct.Policy.html#method.with_max_delay)
	/// fn
	///
	/// By default an exponential backoff exponential of 2 will be used. This
	/// can be modified using the
	/// [`with_backoff_exponent`](struct.RetryPolicy.html#method.
	/// `with_backoff_exponent`) fn.
	pub fn exponential(delay: Duration) -> Self {
		Self {
			backoff: Backoff::Exponential { exponent: 2.0f64 },
			delay,
			..Self::default()
		}
	}

	/// Configures policy with a fixed
	/// backoff delay.
	///
	/// By default, Futures will be retried 5 times.
	///
	/// These delays will increase in
	/// length over time. You may wish to configure how many
	/// times a Future will be retried using the
	/// [`with_max_retries`](struct.RetryPolicy.html#method.with_max_retries) fn
	pub fn fixed(delay: Duration) -> Self {
		Self {
			backoff: Backoff::Fixed,
			delay,
			..Self::default()
		}
	}

	/// Set the exponential backoff exponent to be used
	///
	/// If not using an exponential backoff, this call will be ignored.
	pub fn with_backoff_exponent(mut self, exp: f64) -> Self {
		if let Backoff::Exponential { ref mut exponent } = self.backoff {
			*exponent = exp;
		}
		self
	}

	/// Configures randomness to the delay between retries.
	///
	/// This is useful for services that have many clients which might all retry
	/// at the same time to avoid the ["thundering herd" problem](https://en.wikipedia.org/wiki/Thundering_herd_problem)
	#[cfg(feature = "rand")]
	pub fn with_jitter(mut self, jitter: bool) -> Self {
		self.jitter = jitter;
		self
	}

	/// Limits the maximum length of delay between retries
	pub fn with_max_delay(mut self, max: Duration) -> Self {
		self.max_delay = Some(max);
		self
	}

	/// Limits the maximum number of attempts a Future will be tried
	pub fn with_max_retries(mut self, max: usize) -> Self {
		self.max_retries = max;
		self
	}

	/// Retries a fallible `Future` with this policy's configuration
	pub async fn retry<T>(&self, task: T) -> Result<T::Item, T::Error>
	where
		T: Task,
	{
		self.retry_if(task, Always).await
	}

	/// Reruns and collects the results of a successful `Future` with this
	/// policy's configuration under a certain provided condition
	pub async fn collect<T, C, S>(
		&self,
		task: T,
		condition: C,
		start_value: S,
	) -> Result<Vec<T::Item>, T::Error>
	where
		T: TaskWithParameter<S>,
		C: SuccessCondition<T::Item, S>,
	{
		let mut backoffs = self.backoffs();
		let mut condition = condition;
		let mut task = task;
		let mut results = vec![];
		let mut input = start_value;

		loop {
			match task.call(input).await {
				Ok(result) => {
					let maybe_new_input = condition.retry_with(&result);
					results.push(result);

					if let Some(new_input) = maybe_new_input {
						if let Some(delay) = backoffs.next() {
							#[cfg(feature = "log")]
							{
								log::trace!(
									"task succeeded and condition is met. will run again in {:?}",
									delay
								);
							}
							let () = Delay::new(delay).await;
							input = new_input;
							continue;
						}
					}

					return Ok(results);
				}
				Err(err) => return Err(err),
			}
		}
	}

	/// Reruns and collects the results of a `Future`, if successful, with this
	/// policy's configuration under a certain provided success condition. Also
	/// retries the `Future`, if not successful under the same policy
	/// configuration and the provided error condition.
	pub async fn collect_and_retry<T, C, D, S>(
		&self,
		task: T,
		success_condition: C,
		error_condition: D,
		start_value: S,
	) -> Result<Vec<T::Item>, T::Error>
	where
		T: TaskWithParameter<S>,
		C: SuccessCondition<T::Item, S>,
		D: Condition<T::Error>,
		S: Clone,
	{
		let mut success_backoffs = self.backoffs();
		let mut error_backoffs = self.backoffs();
		let mut success_condition = success_condition;
		let mut error_condition = error_condition;
		let mut task = task;
		let mut results = vec![];
		let mut input = start_value.clone();
		let mut last_result = start_value;

		loop {
			match task.call(input).await {
				Ok(result) => {
					let maybe_new_input = success_condition.retry_with(&result);
					results.push(result);

					if let Some(new_input) = maybe_new_input {
						if let Some(delay) = success_backoffs.next() {
							#[cfg(feature = "log")]
							{
								log::trace!(
									"task succeeded and condition is met. will run again in {:?}",
									delay
								);
							}
							let () = Delay::new(delay).await;
							input = new_input.clone();
							last_result = new_input;
							continue;
						}
					}

					return Ok(results);
				}
				Err(err) => {
					if error_condition.is_retryable(&err) {
						if let Some(delay) = error_backoffs.next() {
							#[cfg(feature = "log")]
							{
								log::trace!(
									"task failed with error {:?}. will try again in {:?}",
									err,
									delay
								);
							}
							let () = Delay::new(delay).await;
							input = last_result.clone();
							continue;
						}
					}
					return Err(err);
				}
			}
		}
	}

	/// Retries a fallible `Future` with this policy's configuration under
	/// certain provided conditions
	pub async fn retry_if<T, C>(&self, task: T, condition: C) -> Result<T::Item, T::Error>
	where
		T: Task,
		C: Condition<T::Error>,
	{
		let mut backoffs = self.backoffs();
		let mut task = task;
		let mut condition = condition;
		loop {
			match task.call().await {
				Ok(result) => return Ok(result),
				Err(err) => {
					if condition.is_retryable(&err) {
						if let Some(delay) = backoffs.next() {
							#[cfg(feature = "log")]
							{
								log::trace!(
									"task failed with error {:?}. will try again in {:?}",
									err,
									delay
								);
							}
							let () = Delay::new(delay).await;
							continue;
						}
					}
					return Err(err);
				}
			}
		}
	}
}

/// A type to determine if a failed Future should be retried
///
/// A implementation is provided for `Fn(&Err) -> bool` allowing you
/// to use a simple closure or fn handles
pub trait Condition<E> {
	/// Return true if a Future error is worth retrying
	fn is_retryable(&mut self, error: &E) -> bool;
}

struct Always;

impl<E> Condition<E> for Always {
	#[inline]
	fn is_retryable(&mut self, _: &E) -> bool {
		true
	}
}

impl<F, E> Condition<E> for F
where
	F: FnMut(&E) -> bool,
{
	fn is_retryable(&mut self, error: &E) -> bool {
		self(error)
	}
}

/// A type to determine if a successful Future should be retried
///
/// A implementation is provided for `Fn(&Result) -> Option<S>`, where S
/// represents the next input value, allowing you to use a simple closure
/// or fn handles
pub trait SuccessCondition<R, S> {
	/// Return true if a Future result is worth retrying
	fn retry_with(&mut self, result: &R) -> Option<S>;
}

impl<F, R, S> SuccessCondition<R, S> for F
where
	F: Fn(&R) -> Option<S>,
{
	fn retry_with(&mut self, result: &R) -> Option<S> {
		self(result)
	}
}

/// A unit of work to be retried, that accepts a parameter
///
/// A implementation is provided for `FnMut() -> Future`
pub trait TaskWithParameter<P> {
	/// The `Ok` variant of a `Futures` associated Output type
	type Item;
	/// The `Err` variant of `Futures` associated Output type
	type Error: std::fmt::Debug;
	/// The resulting `Future` type
	type Fut: Future<Output = Result<Self::Item, Self::Error>>;
	/// Call the operation which invokes results in a `Future`
	fn call(&mut self, parameter: P) -> Self::Fut;
}

impl<F, Fut, I, P, E> TaskWithParameter<P> for F
where
	F: FnMut(P) -> Fut,
	Fut: Future<Output = Result<I, E>>,
	E: std::fmt::Debug,
{
	type Error = E;
	type Fut = Fut;
	type Item = I;

	fn call(&mut self, p: P) -> Self::Fut {
		self(p)
	}
}

/// A unit of work to be retried
///
/// A implementation is provided for `FnMut() -> Future`
pub trait Task {
	/// The `Ok` variant of a `Futures` associated Output type
	type Item;
	/// The `Err` variant of `Futures` associated Output type
	type Error: std::fmt::Debug;
	/// The resulting `Future` type
	type Fut: Future<Output = Result<Self::Item, Self::Error>>;
	/// Call the operation which invokes results in a `Future`
	fn call(&mut self) -> Self::Fut;
}

impl<F, Fut, I, E> Task for F
where
	F: FnMut() -> Fut,
	Fut: Future<Output = Result<I, E>>,
	E: std::fmt::Debug,
{
	type Error = E;
	type Fut = Fut;
	type Item = I;

	fn call(&mut self) -> Self::Fut {
		self()
	}
}

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

	use approx::assert_relative_eq;

	use super::*;

	#[test]
	fn retry_policy_is_send() {
		fn test(_: impl Send) {}
		test(RetryPolicy::default());
	}

	#[test]
	#[cfg(feature = "rand")]
	fn jitter_adds_variance_to_durations() {
		assert!(jitter(Duration::from_secs(1)) != Duration::from_secs(1));
	}

	#[test]
	fn backoff_default() {
		if let Backoff::Exponential { exponent } = Backoff::default() {
			assert_relative_eq!(exponent, 2.0);
		} else {
			panic!("Default backoff expected to be exponential!");
		}
	}

	#[test]
	fn fixed_backoff() {
		let binding = RetryPolicy::fixed(Duration::from_secs(1));
		let mut iter = binding.backoffs();
		assert_eq!(iter.next(), Some(Duration::from_secs(1)));
		assert_eq!(iter.next(), Some(Duration::from_secs(1)));
		assert_eq!(iter.next(), Some(Duration::from_secs(1)));
		assert_eq!(iter.next(), Some(Duration::from_secs(1)));
	}

	#[test]
	fn exponential_backoff() {
		let binding = RetryPolicy::exponential(Duration::from_secs(1));
		let mut iter = binding.backoffs();
		assert_relative_eq!(iter.next().unwrap().as_secs_f64(), 1.0);
		assert_relative_eq!(iter.next().unwrap().as_secs_f64(), 2.0);
		assert_relative_eq!(iter.next().unwrap().as_secs_f64(), 4.0);
		assert_relative_eq!(iter.next().unwrap().as_secs_f64(), 8.0);
	}

	#[test]
	fn exponential_backoff_factor() {
		let binding = RetryPolicy::exponential(Duration::from_secs(1)).with_backoff_exponent(1.5);
		let mut iter = binding.backoffs();
		assert_relative_eq!(iter.next().unwrap().as_secs_f64(), 1.0);
		assert_relative_eq!(iter.next().unwrap().as_secs_f64(), 1.5);
		assert_relative_eq!(iter.next().unwrap().as_secs_f64(), 2.25);
		assert_relative_eq!(iter.next().unwrap().as_secs_f64(), 3.375);
	}

	#[test]
	fn always_is_always_retryable() {
		assert!(Always.is_retryable(&()));
	}

	#[test]
	fn closures_impl_condition() {
		fn test(_: impl Condition<()>) {}
		#[allow(clippy::trivially_copy_pass_by_ref)]
		fn foo(_err: &()) -> bool {
			true
		}
		test(foo);
		test(|_err: &()| true);
	}

	#[test]
	fn closures_impl_task() {
		fn test(_: impl Task) {}
		async fn foo() -> Result<u32, ()> {
			Ok(42)
		}
		test(foo);
		test(|| async { Ok::<u32, ()>(42) });
	}

	#[test]
	fn retried_futures_are_send_when_tasks_are_send() {
		fn test(_: impl Send) {}
		test(RetryPolicy::default().retry(|| async { Ok::<u32, ()>(42) }));
	}

	#[tokio::test]
	async fn collect_retries_when_condition_is_met() -> Result<(), Box<dyn Error>> {
		let result = RetryPolicy::fixed(Duration::from_millis(1))
			.collect(
				|input: u32| async move { Ok::<u32, ()>(input + 1) },
				|result: &u32| if *result < 2 { Some(*result) } else { None },
				0,
			)
			.await;
		assert_eq!(result, Ok(vec![1, 2]));
		Ok(())
	}

	#[tokio::test]
	async fn collect_does_not_retry_when_condition_is_not_met() -> Result<(), Box<dyn Error>> {
		let result = RetryPolicy::fixed(Duration::from_millis(1))
			.collect(
				|input: u32| async move { Ok::<u32, ()>(input + 1) },
				|result: &u32| if *result < 1 { Some(*result) } else { None },
				0,
			)
			.await;
		assert_eq!(result, Ok(vec![1]));
		Ok(())
	}

	#[tokio::test]
	async fn collect_and_retry_retries_when_success_condition_is_met() -> Result<(), Box<dyn Error>>
	{
		let result = RetryPolicy::fixed(Duration::from_millis(1))
			.collect_and_retry(
				|input: u32| async move { Ok::<u32, u32>(input + 1) },
				|result: &u32| if *result < 2 { Some(*result) } else { None },
				|err: &u32| *err > 1,
				0,
			)
			.await;
		assert_eq!(result, Ok(vec![1, 2]));
		Ok(())
	}

	#[tokio::test]
	async fn collect_and_retry_does_not_retry_when_success_condition_is_not_met()
	-> Result<(), Box<dyn Error>> {
		let result = RetryPolicy::fixed(Duration::from_millis(1))
			.collect_and_retry(
				|input: u32| async move { Ok::<u32, u32>(input + 1) },
				|result: &u32| if *result < 1 { Some(*result) } else { None },
				|err: &u32| *err > 1,
				0,
			)
			.await;
		assert_eq!(result, Ok(vec![1]));
		Ok(())
	}

	#[tokio::test]
	async fn collect_and_retry_retries_when_error_condition_is_met() -> Result<(), Box<dyn Error>> {
		let mut task_ran = 0;
		let _ = RetryPolicy::fixed(Duration::from_millis(1))
			.collect_and_retry(
				|_input: u32| {
					task_ran += 1;
					async move { Err::<u32, u32>(0) }
				},
				|result: &u32| if *result < 2 { Some(*result) } else { None },
				|err: &u32| *err == 0,
				0,
			)
			.await;
		// Default for retry policy is 5, so we end up with the task being
		// retries 5 times and being run 6 times.
		assert_eq!(task_ran, 6);
		Ok(())
	}

	#[tokio::test]
	async fn collect_and_retry_does_not_retry_when_error_condition_is_not_met()
	-> Result<(), Box<dyn Error>> {
		let result = RetryPolicy::fixed(Duration::from_millis(1))
			.collect_and_retry(
				|input: u32| async move { Err::<u32, u32>(input + 1) },
				|result: &u32| if *result < 1 { Some(*result) } else { None },
				|err: &u32| *err > 1,
				0,
			)
			.await;
		assert_eq!(result, Err(1));
		Ok(())
	}

	#[tokio::test]
	async fn ok_futures_yield_ok() -> Result<(), Box<dyn Error>> {
		let result = RetryPolicy::default()
			.retry(|| async { Ok::<u32, ()>(42) })
			.await;
		assert_eq!(result, Ok(42));
		Ok(())
	}

	#[tokio::test]
	async fn failed_futures_yield_err() -> Result<(), Box<dyn Error>> {
		let result = RetryPolicy::fixed(Duration::from_millis(1))
			.retry(|| async { Err::<u32, ()>(()) })
			.await;
		assert_eq!(result, Err(()));
		Ok(())
	}
}