Skip to content

Commit

Permalink
stable formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfrank63 committed Dec 8, 2022
1 parent ff1e597 commit 05aaec4
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 79 deletions.
3 changes: 1 addition & 2 deletions governor/src/clock/quanta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ mod test {
let one_ns = Nanos::new(1);
// let _c1 =
// QuantaUpkeepClock::from_builder(quanta::Upkeep::new(Duration::from_secs(1))).unwrap();
let c = QuantaUpkeepClock::from_interval(Duration::from_secs(1))
.unwrap();
let c = QuantaUpkeepClock::from_interval(Duration::from_secs(1)).unwrap();
let now = c.now();
assert_ne!(now + one_ns, now);
assert_eq!(one_ns, Reference::duration_since(&(now + one_ns), now));
Expand Down
16 changes: 10 additions & 6 deletions governor/src/gcra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,8 @@ impl Gcra {
} else {
let next = cmp::max(tat, t0) + t;
Ok((
MW::allow(
key,
StateSnapshot::new(self.t, self.tau, t0, next)),
next,
MW::allow(key, StateSnapshot::new(self.t, self.tau, t0, next)),
next,
))
}
})
Expand Down Expand Up @@ -164,7 +162,10 @@ impl Gcra {
}
}) {
Some(outcome) => outcome,
None => Ok(MW::allow(key, StateSnapshot::new(self.t, self.tau, t0, tau))),
None => Ok(MW::allow(
key,
StateSnapshot::new(self.t, self.tau, t0, tau),
)),
}
}

Expand Down Expand Up @@ -262,7 +263,10 @@ impl Gcra {
}
}) {
Some(outcome) => outcome,
None => Ok(MW::allow(key, StateSnapshot::new(self.t, self.tau, t0, tau))),
None => Ok(MW::allow(
key,
StateSnapshot::new(self.t, self.tau, t0, tau),
)),
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions governor/src/state/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,13 @@ where
&self,
n: NonZeroU32,
) -> Result<MW::PositiveOutcome, NegativeMultiDecision<MW::NegativeOutcome>> {
self.gcra
.test_n_all_peek::<NotKeyed, C::Instant, S, MW>(
self.start,
&NotKeyed::NonKey,
n,
&self.state,
self.clock.now(),
)
self.gcra.test_n_all_peek::<NotKeyed, C::Instant, S, MW>(
self.start,
&NotKeyed::NonKey,
n,
&self.state,
self.clock.now(),
)
}
}

Expand Down
11 changes: 3 additions & 8 deletions governor/src/state/keyed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,14 @@ where
}

pub fn peek_key(&self, key: &K) -> Result<MW::PositiveOutcome, MW::NegativeOutcome> {
self.gcra.peek_test::<K, C::Instant, S, MW>(
self.start,
key,
&self.state,
self.clock.now(),
)
self.gcra
.peek_test::<K, C::Instant, S, MW>(self.start, key, &self.state, self.clock.now())
}

pub fn reset_key(&self, key: &K) {
self.state.reset(key);
}

/// Allow *only all* `n` cells through the rate limiter for the given key.
///
/// This method can succeed in only one way and fail in two ways:
Expand Down Expand Up @@ -152,7 +148,6 @@ where
self.clock.now(),
)
}

}

/// Keyed rate limiters that can be "cleaned up".
Expand Down
7 changes: 4 additions & 3 deletions governor/src/state/keyed/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ impl<K: Hash + Eq + Clone> StateStore for HashMapStateStore<K> {
return Some(v.measure_and_peek_one(f));
}
// not-so-fast path: make a new entry and measure it.
let entry = (*map)
.entry(key.clone());
let entry = (*map).entry(key.clone());
match entry {
std::collections::hash_map::Entry::Occupied(occupied) => Some(occupied.get().measure_and_peek_one(f)),
std::collections::hash_map::Entry::Occupied(occupied) => {
Some(occupied.get().measure_and_peek_one(f))
}
std::collections::hash_map::Entry::Vacant(_) => None,
}
}
Expand Down
69 changes: 34 additions & 35 deletions governor/tests/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,56 +42,55 @@ fn peek_does_not_change_the_decision() {
let lb = RateLimiter::direct_with_clock(Quota::per_second(nonzero!(2u32)), &clock);
let ms = Duration::from_millis(1);

// no key is always positive outcome
for _ in 0..10 {
clock.advance(ms);
assert_eq!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}

// no key is always positive outcome
for _ in 0..10 {
clock.advance(ms);
assert_eq!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}
// use up our burst capacity (2 in the first second):
assert_eq!(Ok(()), lb.check(), "Now: {:?}", clock.now());

// use up our burst capacity (2 in the first second):
assert_eq!(Ok(()), lb.check(), "Now: {:?}", clock.now());
for _ in 0..10 {
clock.advance(ms);
assert_eq!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}

for _ in 0..10 {
clock.advance(ms);
assert_eq!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}
clock.advance(ms);
assert_eq!(Ok(()), lb.check(), "Now: {:?}", clock.now());

for _ in 0..10 {
clock.advance(ms);
assert_eq!(Ok(()), lb.check(), "Now: {:?}", clock.now());
assert_ne!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}

for _ in 0..10 {
clock.advance(ms);
assert_ne!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}
clock.advance(ms);
assert_ne!(Ok(()), lb.check(), "Now: {:?}", clock.now());

for _ in 0..10 {
clock.advance(ms);
assert_ne!(Ok(()), lb.check(), "Now: {:?}", clock.now());
assert_ne!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}

for _ in 0..10 {
clock.advance(ms);
assert_ne!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}
// should be ok again in 1s:
clock.advance(ms * 1000);
assert_eq!(Ok(()), lb.check(), "Now: {:?}", clock.now());
clock.advance(ms);

// should be ok again in 1s:
clock.advance(ms * 1000);
assert_eq!(Ok(()), lb.check(), "Now: {:?}", clock.now());
for _ in 0..10 {
clock.advance(ms);
assert_eq!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}

for _ in 0..10 {
clock.advance(ms);
assert_eq!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}
assert_eq!(Ok(()), lb.check());

assert_eq!(Ok(()), lb.check());
clock.advance(ms);
assert_ne!(Ok(()), lb.check(), "{:?}", lb);

for _ in 0..10 {
clock.advance(ms);
assert_ne!(Ok(()), lb.check(), "{:?}", lb);

for _ in 0..10 {
clock.advance(ms);
assert_ne!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}
assert_ne!(Ok(()), lb.peek(), "Now: {:?}", clock.now());
}
}

#[test]
Expand Down
30 changes: 24 additions & 6 deletions governor/tests/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ fn pauses() {
}
let i = Instant::now();
block_on(lim.until_ready());
assert_ge!(i.elapsed(), Duration::from_millis(PAUSES_DURATION_MIN_MILLIS));
assert_ge!(
i.elapsed(),
Duration::from_millis(PAUSES_DURATION_MIN_MILLIS)
);
}

#[test]
Expand All @@ -34,7 +37,10 @@ fn pauses_n() {
}
let i = Instant::now();
block_on(lim.until_n_ready(nonzero!(5u32))).unwrap();
assert_ge!(i.elapsed(), Duration::from_millis(PAUSES_DURATION_MIN_MILLIS));
assert_ge!(
i.elapsed(),
Duration::from_millis(PAUSES_DURATION_MIN_MILLIS)
);
}

#[test]
Expand All @@ -49,7 +55,10 @@ fn pauses_keyed() {
}
let i = Instant::now();
block_on(lim.until_key_ready(&1u32));
assert_ge!(i.elapsed(), Duration::from_millis(PAUSES_DURATION_MIN_MILLIS));
assert_ge!(
i.elapsed(),
Duration::from_millis(PAUSES_DURATION_MIN_MILLIS)
);
}

#[test]
Expand All @@ -58,7 +67,10 @@ fn proceeds() {

let i = Instant::now();
block_on(lim.until_ready());
assert_le!(i.elapsed(), Duration::from_micros(PROCEEDS_DURATION_MAX_MICROS));
assert_le!(
i.elapsed(),
Duration::from_micros(PROCEEDS_DURATION_MAX_MICROS)
);
}

#[test]
Expand All @@ -67,7 +79,10 @@ fn proceeds_n() {

let i = Instant::now();
block_on(lim.until_n_ready(nonzero!(10u32))).unwrap();
assert_le!(i.elapsed(), Duration::from_micros(PROCEEDS_DURATION_MAX_MICROS));
assert_le!(
i.elapsed(),
Duration::from_micros(PROCEEDS_DURATION_MAX_MICROS)
);
}

#[test]
Expand All @@ -76,7 +91,10 @@ fn proceeds_keyed() {

let i = Instant::now();
block_on(lim.until_key_ready(&1u32));
assert_le!(i.elapsed(), Duration::from_micros(PROCEEDS_DURATION_MAX_MICROS));
assert_le!(
i.elapsed(),
Duration::from_micros(PROCEEDS_DURATION_MAX_MICROS)
);
}

#[test]
Expand Down
58 changes: 49 additions & 9 deletions governor/tests/keyed_dashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@ fn rejects_too_many_n() {

for key in KEYS {
// use up our burst capacity (2 in the first second):
assert_eq!(Ok(()), lb.check_key_n(key, nonzero!(2u32)), "Now: {:?}", clock.now());
assert_eq!(
Ok(()),
lb.check_key_n(key, nonzero!(2u32)),
"Now: {:?}",
clock.now()
);
clock.advance(ms);

assert_ne!(Ok(()), lb.check_key(key), "Now: {:?}", clock.now());

// should be ok again in 1s:
clock.advance(ms * 1000);
assert_eq!(Ok(()), lb.check_key_n(key, nonzero!(2u32)), "Now: {:?}", clock.now());
assert_eq!(
Ok(()),
lb.check_key_n(key, nonzero!(2u32)),
"Now: {:?}",
clock.now()
);
clock.advance(ms);

assert_ne!(Ok(()), lb.check_key(key), "{:?}", lb);
Expand Down Expand Up @@ -136,29 +146,59 @@ fn peek_does_not_change_the_decision_n() {
for key in KEYS {
// no key is always positive outcome
assert!(lb.peek_key_n(key, nonzero!(3u32)).is_err());
assert_eq!(Ok(()), lb.peek_key_n(key, nonzero!(2u32)), "Now: {:?}", clock.now());
assert_eq!(
Ok(()),
lb.peek_key_n(key, nonzero!(2u32)),
"Now: {:?}",
clock.now()
);

// use up our burst capacity (2 in the first second):
assert_eq!(Ok(()), lb.check_key(key), "Now: {:?}", clock.now());

assert_eq!(Ok(()), lb.peek_key_n(key, nonzero!(1u32)), "Now: {:?}", clock.now());
assert_eq!(
Ok(()),
lb.peek_key_n(key, nonzero!(1u32)),
"Now: {:?}",
clock.now()
);
assert!(lb.peek_key_n(key, nonzero!(2u32)).is_err());

clock.advance(ms);
assert_eq!(Ok(()), lb.check_key(key), "Now: {:?}", clock.now());

assert_ne!(Ok(()), lb.peek_key_n(key, nonzero!(2u32)), "Now: {:?}", clock.now());
assert_ne!(
Ok(()),
lb.peek_key_n(key, nonzero!(2u32)),
"Now: {:?}",
clock.now()
);

clock.advance(ms);
assert_ne!(Ok(()), lb.check_key(key), "Now: {:?}", clock.now());

assert_ne!(Ok(()), lb.peek_key_n(key, nonzero!(2u32)), "Now: {:?}", clock.now());
assert_ne!(
Ok(()),
lb.peek_key_n(key, nonzero!(2u32)),
"Now: {:?}",
clock.now()
);

// should be ok again in 1s:
clock.advance(ms * 1000);
assert_eq!(Ok(()), lb.peek_key_n(key, nonzero!(2u32)), "Now: {:?}", clock.now());

assert_eq!(Ok(()), lb.check_key_n(key, nonzero!(2u32)),"Now: {:?}", clock.now());
assert_eq!(
Ok(()),
lb.peek_key_n(key, nonzero!(2u32)),
"Now: {:?}",
clock.now()
);

assert_eq!(
Ok(()),
lb.check_key_n(key, nonzero!(2u32)),
"Now: {:?}",
clock.now()
);
assert!(lb.peek_key_n(key, nonzero!(2u32)).is_err());
}
}
Expand Down
2 changes: 1 addition & 1 deletion governor/tests/keyed_hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn reset_does_work() {
assert_eq!(Ok(()), lb.check_key(key), "Now: {:?}", clock.now());
clock.advance(ms);
assert_eq!(Ok(()), lb.check_key(key), "Now: {:?}", clock.now());

for _ in 0..10 {
clock.advance(ms);
assert_ne!(Ok(()), lb.peek_key(key), "Now: {:?}", clock.now());
Expand Down
3 changes: 2 additions & 1 deletion governor/tests/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ impl RateLimitingMiddleware<<FakeRelativeClock as clock::Clock>::Instant> for My
_key: &K,
_limiter: impl Into<StateSnapshot>,
_start_time: <FakeRelativeClock as clock::Clock>::Instant,
) -> Self::NegativeOutcome {}
) -> Self::NegativeOutcome {
}
}

#[test]
Expand Down

0 comments on commit 05aaec4

Please sign in to comment.