Skip to content

Commit ed92388

Browse files
committed
Strip ns precision from rate limiting tests
Linux has ns precision when fetching the current timestamp, most other platforms only have us precision. PG *always* only stores us precision, so roundtripping `Utc::now()` through the database may change it on Linux. Since we're checking for exact equality in these tests, we need to normalize the value ahead of time.
1 parent 71f2c57 commit ed92388

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/publish_rate_limit.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ mod tests {
110110
#[test]
111111
fn take_token_with_no_bucket_creates_new_one() -> CargoResult<()> {
112112
let conn = pg_connection();
113-
let now = Utc::now().naive_utc();
113+
let now = now();
114114

115115
let rate = PublishRateLimit {
116116
rate: Duration::from_secs(1),
@@ -141,7 +141,7 @@ mod tests {
141141
#[test]
142142
fn take_token_with_existing_bucket_modifies_existing_bucket() -> CargoResult<()> {
143143
let conn = pg_connection();
144-
let now = Utc::now().naive_utc();
144+
let now = now();
145145

146146
let rate = PublishRateLimit {
147147
rate: Duration::from_secs(1),
@@ -161,7 +161,7 @@ mod tests {
161161
#[test]
162162
fn take_token_after_delay_refills() -> CargoResult<()> {
163163
let conn = pg_connection();
164-
let now = Utc::now().naive_utc();
164+
let now = now();
165165

166166
let rate = PublishRateLimit {
167167
rate: Duration::from_secs(1),
@@ -206,7 +206,7 @@ mod tests {
206206
#[test]
207207
fn last_refill_always_advanced_by_multiple_of_rate() -> CargoResult<()> {
208208
let conn = pg_connection();
209-
let now = Utc::now().naive_utc();
209+
let now = now();
210210

211211
let rate = PublishRateLimit {
212212
rate: Duration::from_millis(100),
@@ -227,7 +227,7 @@ mod tests {
227227
#[test]
228228
fn zero_tokens_returned_when_user_has_no_tokens_left() -> CargoResult<()> {
229229
let conn = pg_connection();
230-
let now = Utc::now().naive_utc();
230+
let now = now();
231231

232232
let rate = PublishRateLimit {
233233
rate: Duration::from_secs(1),
@@ -250,7 +250,7 @@ mod tests {
250250
#[test]
251251
fn a_user_with_no_tokens_gets_a_token_after_exactly_rate() -> CargoResult<()> {
252252
let conn = pg_connection();
253-
let now = Utc::now().naive_utc();
253+
let now = now();
254254

255255
let rate = PublishRateLimit {
256256
rate: Duration::from_secs(1),
@@ -272,7 +272,7 @@ mod tests {
272272
#[test]
273273
fn tokens_never_refill_past_burst() -> CargoResult<()> {
274274
let conn = pg_connection();
275-
let now = Utc::now().naive_utc();
275+
let now = now();
276276

277277
let rate = PublishRateLimit {
278278
rate: Duration::from_secs(1),
@@ -294,7 +294,7 @@ mod tests {
294294
#[test]
295295
fn override_is_used_instead_of_global_burst_if_present() -> CargoResult<()> {
296296
let conn = pg_connection();
297-
let now = Utc::now().naive_utc();
297+
let now = now();
298298

299299
let rate = PublishRateLimit {
300300
rate: Duration::from_secs(1),
@@ -343,4 +343,14 @@ mod tests {
343343
.get_result(conn)
344344
.map_err(Into::into)
345345
}
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+
}
346356
}

0 commit comments

Comments
 (0)