@@ -21,15 +21,14 @@ use crate::utils::{fmt_http_date, parse_http_date};
21
21
/// use std::time::{SystemTime, Duration};
22
22
/// use async_std::task;
23
23
///
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));
26
25
///
27
26
/// let mut headers = Response::new(429);
28
27
/// retry.apply(&mut headers);
29
28
///
30
29
/// // Sleep for the duration, then try the task again.
31
30
/// let retry = RetryAfter::from_headers(headers)?.unwrap();
32
- /// task::sleep(retry.duration_since(now)?);
31
+ /// task::sleep(retry.duration_since(SystemTime:: now() )?);
33
32
/// #
34
33
/// # Ok(()) }
35
34
/// ```
@@ -119,13 +118,41 @@ impl Into<SystemTime> for RetryAfter {
119
118
}
120
119
}
121
120
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
+
122
131
#[ cfg( test) ]
123
132
mod test {
124
133
use super :: * ;
125
134
use crate :: headers:: Headers ;
126
135
127
136
#[ test]
128
137
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 < ( ) > {
129
156
let now = SystemTime :: now ( ) ;
130
157
let retry = RetryAfter :: new_at ( now + Duration :: from_secs ( 10 ) ) ;
131
158
@@ -141,13 +168,3 @@ mod test {
141
168
Ok ( ( ) )
142
169
}
143
170
}
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