Skip to content

Commit e18cacb

Browse files
authored
tests: Fix rate limit test flakiness by using database reset instead of sleep (#11484)
1 parent 4ab8b12 commit e18cacb

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/tests/krate/publish/rate_limit.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use chrono::{DateTime, Utc};
66
use diesel::ExpressionMethods;
77
use diesel_async::RunQueryDsl;
88
use insta::assert_snapshot;
9-
use std::thread;
109
use std::time::Duration;
1110

1211
#[tokio::test(flavor = "multi_thread")]
@@ -238,7 +237,7 @@ async fn publish_new_crate_rate_limit_doesnt_affect_existing_crates() {
238237

239238
#[tokio::test(flavor = "multi_thread")]
240239
async fn publish_existing_crate_rate_limited() {
241-
const RATE_LIMIT: Duration = Duration::from_millis(1000);
240+
const RATE_LIMIT: Duration = Duration::from_secs(60);
242241

243242
let (app, anon, _, token) = TestApp::full()
244243
.with_rate_limit(LimitedAction::PublishUpdate, RATE_LIMIT, 1)
@@ -293,8 +292,17 @@ async fn publish_existing_crate_rate_limited() {
293292
rss/updates.xml
294293
");
295294

296-
// Wait for the limit to be up
297-
thread::sleep(RATE_LIMIT);
295+
// Reset the rate limit by updating the database timestamp
296+
let mut conn = app.db_conn().await;
297+
let past_time = Utc::now().naive_utc() - RATE_LIMIT - Duration::from_secs(10);
298+
299+
diesel::update(publish_limit_buckets::table)
300+
.filter(publish_limit_buckets::user_id.eq(token.as_model().user_id))
301+
.filter(publish_limit_buckets::action.eq(LimitedAction::PublishUpdate))
302+
.set(publish_limit_buckets::last_refill.eq(past_time))
303+
.execute(&mut conn)
304+
.await
305+
.expect("Failed to reset rate limit");
298306

299307
let crate_to_publish = PublishBuilder::new("rate_limited1", "1.0.2");
300308
token.publish_crate(crate_to_publish).await.good();

0 commit comments

Comments
 (0)