Skip to content

Commit

Permalink
fix tinylfu test flake
Browse files Browse the repository at this point in the history
The TinyLFU test was failing for changes unrelated to anything in the tinyufo
crate. This changes the test to stop that! What was occurring was expected
behavior, so the test is what's changing instead of any internal logic.

What was happening were hash collisions in the bloom filter used by the
estimator. Instead of asserting that we'd always start counting from 0, we now
allow for this in the test by `get()`ing first, then comparing relative
values. For the final comparisons, we check that values are greater-than or
equal-to their lower bound as determined by the number of `incr()`s we called on
their keys.
  • Loading branch information
gumpt authored and eaufavor committed Apr 22, 2024
1 parent 99cb79b commit 01c6965
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .bleep
Original file line number Diff line number Diff line change
@@ -1 +1 @@
802590d7d04730c3e93f9c2335175990eee1fb92
d87a50338cb6483a3b0cbfe73d3b0491a336b7a2
35 changes: 20 additions & 15 deletions tinyufo/src/estimation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,25 @@ mod tests {
assert_eq!(tiny.incr(1), 2);
assert_eq!(tiny.get(1), 2);

assert_eq!(tiny.get(2), 0);
assert_eq!(tiny.incr(2), 1);
assert_eq!(tiny.incr(2), 2);
assert_eq!(tiny.get(2), 2);

assert_eq!(tiny.incr(3), 1);
assert_eq!(tiny.incr(3), 2);
assert_eq!(tiny.incr(3), 3);
assert_eq!(tiny.incr(3), 4);

// 8 incr(), now reset

assert_eq!(tiny.incr(3), 3);
assert_eq!(tiny.incr(1), 2);
assert_eq!(tiny.incr(2), 2);
// Might have hash collisions for the others, need to
// get() before can assert on the incr() value.
let two = tiny.get(2);
assert_eq!(tiny.incr(2), two + 1);
assert_eq!(tiny.incr(2), two + 2);
assert_eq!(tiny.get(2), two + 2);

let three = tiny.get(3);
assert_eq!(tiny.incr(3), three + 1);
assert_eq!(tiny.incr(3), three + 2);
assert_eq!(tiny.incr(3), three + 3);
assert_eq!(tiny.incr(3), three + 4);

// 8 incr(), now resets on next incr
// can only assert they are greater than or equal
// to the incr() we do per key.

assert!(tiny.incr(3) >= 3); // had 4, reset to 2, added another.
assert!(tiny.incr(1) >= 2); // had 2, reset to 1, added another.
assert!(tiny.incr(2) >= 2); // had 2, reset to 1, added another.
}
}

0 comments on commit 01c6965

Please sign in to comment.