misc: Eliminate use of assert!((...).is_ok())

Asserting on .is_ok()/.is_err() leads to hard to debug failures (as if
the test fails, it will only say "assertion failed: false". We replace
these with `.unwrap()`, which also prints the exact error variant that
was unexpectedly encountered (we can to this these days thanks to
efforts to implement Display and Debug for our error types). If the
assert!((...).is_ok()) was followed by an .unwrap() anyway, we just drop
the assert.

Inspired by and quoted from @roypat.

Signed-off-by: Ruoqing He <heruoqing@iscas.ac.cn>
This commit is contained in:
Ruoqing He
2024-09-30 19:07:39 +00:00
committed by Rob Bradford
parent 83bcf2a1ff
commit 297236a7c0
25 changed files with 244 additions and 281 deletions

View File

@@ -388,7 +388,7 @@ pub(crate) mod tests {
// wait the other half of the timer period
thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2));
// the timer_fd should have an event on it by now
assert!(h.event_handler().is_ok());
h.event_handler().unwrap();
// limiter should now be unblocked
assert!(!h.is_blocked());
// try and succeed on another 100 bytes this time
@@ -424,7 +424,7 @@ pub(crate) mod tests {
// wait the other half of the timer period
thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2));
// the timer_fd should have an event on it by now
assert!(h.event_handler().is_ok());
h.event_handler().unwrap();
// limiter should now be unblocked
assert!(!h.is_blocked());
// try and succeed on another 100 ops this time
@@ -461,7 +461,7 @@ pub(crate) mod tests {
// wait the other half of the timer period
thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2));
// the timer_fd should have an event on it by now
assert!(h.event_handler().is_ok());
h.event_handler().unwrap();
// limiter should now be unblocked
assert!(!h.is_blocked());
// try and succeed on another 100 ops this time
@@ -491,7 +491,7 @@ pub(crate) mod tests {
// after 1.5x the replenish time has passed, the rate limiter
// is available again
thread::sleep(Duration::from_millis(500));
assert!(h.event_handler().is_ok());
h.event_handler().unwrap();
assert!(!h.is_blocked());
// reset the rate limiter
@@ -525,7 +525,7 @@ pub(crate) mod tests {
// after waiting out the full duration, rate limiter should be
// available again
thread::sleep(Duration::from_millis(200));
assert!(h.event_handler().is_ok());
h.event_handler().unwrap();
assert!(!h.is_blocked());
assert!(h.consume(100, TokenType::Bytes));
}

View File

@@ -666,7 +666,7 @@ pub(crate) mod tests {
assert!(l.consume(u64::MAX, TokenType::Ops));
assert!(l.consume(u64::MAX, TokenType::Bytes));
// calling the handler without there having been an event should error
assert!(l.event_handler().is_err());
l.event_handler().unwrap_err();
assert_eq!(
format!("{:?}", l.event_handler().err().unwrap()),
"SpuriousRateLimiterEvent(\
@@ -737,7 +737,7 @@ pub(crate) mod tests {
// wait the other half of the timer period
thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2));
// the timer_fd should have an event on it by now
assert!(l.event_handler().is_ok());
l.event_handler().unwrap();
// limiter should now be unblocked
assert!(!l.is_blocked());
// try and succeed on another 100 bytes this time
@@ -770,7 +770,7 @@ pub(crate) mod tests {
// wait the other half of the timer period
thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2));
// the timer_fd should have an event on it by now
assert!(l.event_handler().is_ok());
l.event_handler().unwrap();
// limiter should now be unblocked
assert!(!l.is_blocked());
// try and succeed on another 100 ops this time
@@ -804,7 +804,7 @@ pub(crate) mod tests {
// wait the other half of the timer period
thread::sleep(Duration::from_millis(REFILL_TIMER_INTERVAL_MS / 2));
// the timer_fd should have an event on it by now
assert!(l.event_handler().is_ok());
l.event_handler().unwrap();
// limiter should now be unblocked
assert!(!l.is_blocked());
// try and succeed on another 100 ops this time
@@ -825,13 +825,13 @@ pub(crate) mod tests {
// check that even after a whole second passes, the rate limiter
// is still blocked
thread::sleep(Duration::from_millis(1000));
assert!(l.event_handler().is_err());
l.event_handler().unwrap_err();
assert!(l.is_blocked());
// after 1.5x the replenish time has passed, the rate limiter
// is available again
thread::sleep(Duration::from_millis(500));
assert!(l.event_handler().is_ok());
l.event_handler().unwrap();
assert!(!l.is_blocked());
// reset the rate limiter
@@ -845,27 +845,27 @@ pub(crate) mod tests {
// check that after more than the minimum refill time,
// the rate limiter is still blocked
thread::sleep(Duration::from_millis(200));
assert!(l.event_handler().is_err());
l.event_handler().unwrap_err();
assert!(l.is_blocked());
// try to consume some tokens, which should fail as the timer
// is still active
assert!(!l.consume(100, TokenType::Bytes));
assert!(l.event_handler().is_err());
l.event_handler().unwrap_err();
assert!(l.is_blocked());
// check that after the minimum refill time, the timer was not
// overwritten and the rate limiter is still blocked from the
// borrowing we performed earlier
thread::sleep(Duration::from_millis(100));
assert!(l.event_handler().is_err());
l.event_handler().unwrap_err();
assert!(l.is_blocked());
assert!(!l.consume(100, TokenType::Bytes));
// after waiting out the full duration, rate limiter should be
// available again
thread::sleep(Duration::from_millis(200));
assert!(l.event_handler().is_ok());
l.event_handler().unwrap();
assert!(!l.is_blocked());
assert!(l.consume(100, TokenType::Bytes));
}