Skip to content

Commit dc1f718

Browse files
committed
improve tests
1 parent aa773bb commit dc1f718

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/other/retry_after.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ use crate::utils::{fmt_http_date, parse_http_date};
2121
/// use std::time::{SystemTime, Duration};
2222
/// use async_std::task;
2323
///
24-
/// let now = SystemTime::now();
25-
/// let retry = RetryAfter::new_at(now + Duration::from_secs(10));
24+
/// let retry = RetryAfter::new(Duration::from_secs(10));
2625
///
2726
/// let mut headers = Response::new(429);
2827
/// retry.apply(&mut headers);
2928
///
3029
/// // Sleep for the duration, then try the task again.
3130
/// let retry = RetryAfter::from_headers(headers)?.unwrap();
32-
/// task::sleep(retry.duration_since(now)?);
31+
/// task::sleep(retry.duration_since(SystemTime::now())?);
3332
/// #
3433
/// # Ok(()) }
3534
/// ```
@@ -119,13 +118,41 @@ impl Into<SystemTime> for RetryAfter {
119118
}
120119
}
121120

121+
/// What value are we decoding into?
122+
///
123+
/// This value is intionally never exposes; all end-users want is a `Duration`
124+
/// value that tells them how long to wait for before trying again.
125+
#[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
126+
enum RetryDirective {
127+
Duration(Duration),
128+
SystemTime(SystemTime),
129+
}
130+
122131
#[cfg(test)]
123132
mod test {
124133
use super::*;
125134
use crate::headers::Headers;
126135

127136
#[test]
128137
fn smoke() -> crate::Result<()> {
138+
let retry = RetryAfter::new(Duration::from_secs(10));
139+
140+
let mut headers = Headers::new();
141+
retry.apply(&mut headers);
142+
143+
// `SystemTime::now` uses sub-second precision which means there's some
144+
// offset that's not encoded.
145+
let now = SystemTime::now();
146+
let retry = RetryAfter::from_headers(headers)?.unwrap();
147+
assert_eq!(
148+
retry.duration_since(now)?.as_secs(),
149+
Duration::from_secs(10).as_secs()
150+
);
151+
Ok(())
152+
}
153+
154+
#[test]
155+
fn new_at() -> crate::Result<()> {
129156
let now = SystemTime::now();
130157
let retry = RetryAfter::new_at(now + Duration::from_secs(10));
131158

@@ -141,13 +168,3 @@ mod test {
141168
Ok(())
142169
}
143170
}
144-
145-
/// What value are we decoding into?
146-
///
147-
/// This value is intionally never exposes; all end-users want is a `Duration`
148-
/// value that tells them how long to wait for before trying again.
149-
#[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
150-
enum RetryDirective {
151-
Duration(Duration),
152-
SystemTime(SystemTime),
153-
}

0 commit comments

Comments
 (0)