Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pingora-load-balancing/src/selection/weighted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,16 @@ mod test {
let mut b2 = Backend::new("1.0.0.1:80").unwrap();
b2.weight = 8; // 8x than the rest
let b3 = Backend::new("1.0.0.255:80").unwrap();
// sorted with: [b2, b3, b1]
// weighted: [0, 0, 0, 0, 0, 0, 0, 0, 1, 2]
let backends = BTreeSet::from_iter([b1.clone(), b2.clone(), b3.clone()]);
let hash: Arc<Weighted<RoundRobin>> = Arc::new(Weighted::build(&backends));

// same hash iter over
let mut iter = hash.iter(b"test");
// first, should be weighted
// weighted: [0, 0, 0, 0, 0, 0, 0, 0, 1, 2]
// ^
assert_eq!(iter.next(), Some(&b2));
// fallbacks, should be round robin
assert_eq!(iter.next(), Some(&b3));
Expand All @@ -168,6 +172,9 @@ mod test {
assert_eq!(iter.next(), Some(&b3));

// round robin, ignoring the hash key
// index advanced 5 steps
// weighted: [0, 0, 0, 0, 0, 0, 0, 0, 1, 2]
// ^
let mut iter = hash.iter(b"test1");
assert_eq!(iter.next(), Some(&b2));
let mut iter = hash.iter(b"test1");
Expand All @@ -179,6 +186,7 @@ mod test {
let mut iter = hash.iter(b"test1");
assert_eq!(iter.next(), Some(&b1));
let mut iter = hash.iter(b"test1");
// rounded
assert_eq!(iter.next(), Some(&b2));
let mut iter = hash.iter(b"test1");
assert_eq!(iter.next(), Some(&b2));
Expand Down
Loading