@@ -110,7 +110,7 @@ mod tests {
110
110
#[ test]
111
111
fn take_token_with_no_bucket_creates_new_one ( ) -> CargoResult < ( ) > {
112
112
let conn = pg_connection ( ) ;
113
- let now = Utc :: now ( ) . naive_utc ( ) ;
113
+ let now = now ( ) ;
114
114
115
115
let rate = PublishRateLimit {
116
116
rate : Duration :: from_secs ( 1 ) ,
@@ -141,7 +141,7 @@ mod tests {
141
141
#[ test]
142
142
fn take_token_with_existing_bucket_modifies_existing_bucket ( ) -> CargoResult < ( ) > {
143
143
let conn = pg_connection ( ) ;
144
- let now = Utc :: now ( ) . naive_utc ( ) ;
144
+ let now = now ( ) ;
145
145
146
146
let rate = PublishRateLimit {
147
147
rate : Duration :: from_secs ( 1 ) ,
@@ -161,7 +161,7 @@ mod tests {
161
161
#[ test]
162
162
fn take_token_after_delay_refills ( ) -> CargoResult < ( ) > {
163
163
let conn = pg_connection ( ) ;
164
- let now = Utc :: now ( ) . naive_utc ( ) ;
164
+ let now = now ( ) ;
165
165
166
166
let rate = PublishRateLimit {
167
167
rate : Duration :: from_secs ( 1 ) ,
@@ -206,7 +206,7 @@ mod tests {
206
206
#[ test]
207
207
fn last_refill_always_advanced_by_multiple_of_rate ( ) -> CargoResult < ( ) > {
208
208
let conn = pg_connection ( ) ;
209
- let now = Utc :: now ( ) . naive_utc ( ) ;
209
+ let now = now ( ) ;
210
210
211
211
let rate = PublishRateLimit {
212
212
rate : Duration :: from_millis ( 100 ) ,
@@ -227,7 +227,7 @@ mod tests {
227
227
#[ test]
228
228
fn zero_tokens_returned_when_user_has_no_tokens_left ( ) -> CargoResult < ( ) > {
229
229
let conn = pg_connection ( ) ;
230
- let now = Utc :: now ( ) . naive_utc ( ) ;
230
+ let now = now ( ) ;
231
231
232
232
let rate = PublishRateLimit {
233
233
rate : Duration :: from_secs ( 1 ) ,
@@ -250,7 +250,7 @@ mod tests {
250
250
#[ test]
251
251
fn a_user_with_no_tokens_gets_a_token_after_exactly_rate ( ) -> CargoResult < ( ) > {
252
252
let conn = pg_connection ( ) ;
253
- let now = Utc :: now ( ) . naive_utc ( ) ;
253
+ let now = now ( ) ;
254
254
255
255
let rate = PublishRateLimit {
256
256
rate : Duration :: from_secs ( 1 ) ,
@@ -272,7 +272,7 @@ mod tests {
272
272
#[ test]
273
273
fn tokens_never_refill_past_burst ( ) -> CargoResult < ( ) > {
274
274
let conn = pg_connection ( ) ;
275
- let now = Utc :: now ( ) . naive_utc ( ) ;
275
+ let now = now ( ) ;
276
276
277
277
let rate = PublishRateLimit {
278
278
rate : Duration :: from_secs ( 1 ) ,
@@ -294,7 +294,7 @@ mod tests {
294
294
#[ test]
295
295
fn override_is_used_instead_of_global_burst_if_present ( ) -> CargoResult < ( ) > {
296
296
let conn = pg_connection ( ) ;
297
- let now = Utc :: now ( ) . naive_utc ( ) ;
297
+ let now = now ( ) ;
298
298
299
299
let rate = PublishRateLimit {
300
300
rate : Duration :: from_secs ( 1 ) ,
@@ -343,4 +343,14 @@ mod tests {
343
343
. get_result ( conn)
344
344
. map_err ( Into :: into)
345
345
}
346
+
347
+ /// Strips ns precision from `Utc::now`. PostgreSQL only has microsecond
348
+ /// precision, but some platforms (notably Linux) provide nanosecond
349
+ /// precision, meaning that round tripping through the database would
350
+ /// change the value.
351
+ fn now ( ) -> NaiveDateTime {
352
+ let now = Utc :: now ( ) . naive_utc ( ) ;
353
+ let nanos = now. timestamp_subsec_nanos ( ) ;
354
+ now - chrono:: Duration :: nanoseconds ( nanos as i64 )
355
+ }
346
356
}
0 commit comments