Let the sharing tests choose their wall window instead of drawing one
Browse files at this commite6f927bfaba845c83ef9cb7843845b3cc0a297a6Parent: fcaf9ca- test
- let the sharing tests choose their wall window instead of drawing one
2 changed files +35 -4
- src/rate_limit.rs +23 -1
- tests/rate_limit.rs +12 -3
src/rate_limit.rsmodified+23 -1
| @@ -404,6 +404,28 @@ where | |||
| 404 | /// limiter rather than keeping a dropped one alive (test suites build | 404 | /// limiter rather than keeping a dropped one alive (test suites build |
| 405 | /// dozens). | 405 | /// dozens). |
| 406 | pub fn share(&self, tier: &'static str, db: Database, db_up: DbHealth) { | 406 | pub fn share(&self, tier: &'static str, db: Database, db_up: DbHealth) { |
| 407 | + self.share_windowed(tier, db, db_up, None); | ||
| 408 | + } | ||
| 409 | + | ||
| 410 | + /// [`RateLimiter::share`] with the wall window pinned, for tests only. | ||
| 411 | + /// | ||
| 412 | + /// A round reads the real clock, so any assertion about *the* shared row | ||
| 413 | + /// is otherwise a bet that the run does not cross a minute mid-round — | ||
| 414 | + /// two limiters of one tier would then report into two rows and neither | ||
| 415 | + /// would see the other's spend. An accidental relationship to the wall | ||
| 416 | + /// clock is precisely what kept the epoch-roll double-charge out of | ||
| 417 | + /// `tests/rate_limit.rs`, so the sharing tests choose their window instead. | ||
| 418 | + pub fn share_pinned(&self, tier: &'static str, db: Database, db_up: DbHealth, epoch: i64) { | ||
| 419 | + self.share_windowed(tier, db, db_up, Some(epoch)); | ||
| 420 | + } | ||
| 421 | + | ||
| 422 | + fn share_windowed( | ||
| 423 | + &self, | ||
| 424 | + tier: &'static str, | ||
| 425 | + db: Database, | ||
| 426 | + db_up: DbHealth, | ||
| 427 | + pinned: Option<i64>, | ||
| 428 | + ) { | ||
| 407 | if self.max == 0 { | 429 | if self.max == 0 { |
| 408 | // The tier is off: `check` records nothing, so there is nothing to | 430 | // The tier is off: `check` records nothing, so there is nothing to |
| 409 | // share and no reason to hold a task or a table row. | 431 | // share and no reason to hold a task or a table row. |
| @@ -424,7 +446,7 @@ where | |||
| 424 | if !db_up.is_up() { | 446 | if !db_up.is_up() { |
| 425 | continue; | 447 | continue; |
| 426 | } | 448 | } |
| 427 | - let epoch = current_epoch(window); | 449 | + let epoch = pinned.unwrap_or_else(|| current_epoch(window)); |
| 428 | sync_once(tier, &buckets, window, epoch, &db).await; | 450 | sync_once(tier, &buckets, window, epoch, &db).await; |
| 429 | // Piggybacked cleanup, once per window: rows for a window that | 451 | // Piggybacked cleanup, once per window: rows for a window that |
| 430 | // has passed can never be read again. | 452 | // has passed can never be read again. |
tests/rate_limit.rsmodified+12 -3
| @@ -422,15 +422,24 @@ async fn sync_round() { | |||
| 422 | tokio::time::pause(); | 422 | tokio::time::pause(); |
| 423 | } | 423 | } |
| 424 | 424 | ||
| 425 | -/// Two limiters of one tier, sharing `db`. | 425 | +/// One fixed wall window for every round below. The sync task reads the real |
| 426 | +/// clock, so without this the tests here assert about "the" shared row while a | ||
| 427 | +/// minute boundary landing between two limiters' rounds would give them a row | ||
| 428 | +/// each — a run that passes because of when it ran, which is the same | ||
| 429 | +/// accidental clock dependency that hid the epoch-roll double-charge from this | ||
| 430 | +/// suite. Rolling *between* windows is covered by the unit tests, which drive | ||
| 431 | +/// `sync_once`'s epoch directly. | ||
| 432 | +const PINNED_WINDOW: i64 = 60_000; | ||
| 433 | + | ||
| 434 | +/// Two limiters of one tier, sharing `db` and one wall window. | ||
| 426 | fn two_replicas(max: u32, db: &Database) -> (UserRateLimiter, UserRateLimiter, DbHealth) { | 435 | fn two_replicas(max: u32, db: &Database) -> (UserRateLimiter, UserRateLimiter, DbHealth) { |
| 427 | let health = DbHealth::default(); | 436 | let health = DbHealth::default(); |
| 428 | let (a, b) = ( | 437 | let (a, b) = ( |
| 429 | UserRateLimiter::per_user_minute(max), | 438 | UserRateLimiter::per_user_minute(max), |
| 430 | UserRateLimiter::per_user_minute(max), | 439 | UserRateLimiter::per_user_minute(max), |
| 431 | ); | 440 | ); |
| 432 | - a.share("test", db.clone(), health.clone()); | 441 | + a.share_pinned("test", db.clone(), health.clone(), PINNED_WINDOW); |
| 433 | - b.share("test", db.clone(), health.clone()); | 442 | + b.share_pinned("test", db.clone(), health.clone(), PINNED_WINDOW); |
| 434 | (a, b, health) | 443 | (a, b, health) |
| 435 | } | 444 | } |
| 436 | 445 | ||