Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions pingora-cache/src/eviction/lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,21 @@ mod test {
assert!(lru.peek(&key2));
}

#[test]
fn test_peek_does_not_promote() {
let lru = Manager::<1>::with_capacity(4, 10);
let until = SystemTime::now(); // unused value as a placeholder

let key1 = CacheKey::new("a", "1").to_compact();
lru.admit(key1.clone(), 1, until);
let key2 = CacheKey::new("b", "1").to_compact();
lru.admit(key2, 1, until);

// key1 is the least recently used and a peek must leave it there
assert!(lru.peek(&key1));
assert_eq!(lru.peek_lru(0).unwrap(), CacheEntryKey::key_only(key1));
}

#[test]
fn test_serde() {
let lru = Manager::<1>::with_capacity(4, 10);
Expand Down
12 changes: 12 additions & 0 deletions pingora-ketama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,18 @@ mod tests {
assert_eq!(start_idx, iter.idx);
}

#[test]
fn node_idx_wraps_past_last_point() {
let c = Continuum::new(&[
Bucket::new(get_sockaddr("127.0.0.1:6443"), 1),
Bucket::new(get_sockaddr("127.0.0.2:6443"), 1),
]);

// A hash above the last point on the ring belongs to the first point
assert!(c.ring.len() > 1);
assert_eq!(c.ring.node_idx(u32::MAX), 0);
}

#[test]
fn test_empty() {
let c = Continuum::new(&[]);
Expand Down
15 changes: 15 additions & 0 deletions pingora-load-balancing/src/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,21 @@ mod test {
use async_trait::async_trait;
use http::Extensions;

#[test]
fn test_health_counter_restarts_after_flip() {
let health = Health::default();

assert!(!health.observe_health(false, 2));
assert!(health.observe_health(false, 2));
assert!(!health.ready());

// Coming back takes the full threshold again
assert!(!health.observe_health(true, 2));
assert!(!health.ready());
assert!(health.observe_health(true, 2));
assert!(health.ready());
}

#[tokio::test]
async fn test_tcp_check() {
let tcp_check = TcpHealthCheck::default();
Expand Down
14 changes: 14 additions & 0 deletions pingora-load-balancing/src/selection/consistent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,18 @@ mod test {
let mut iter = hash.iter(b"test9");
assert_eq!(iter.next(), Some(&b2));
}

#[test]
fn test_ketama_honors_backend_weight() {
let b1 = Backend::new("1.1.1.1:80").unwrap();
let b2 = Backend::new_with_weight("1.0.0.1:80", 3).unwrap();
let backends = BTreeSet::from_iter([b1, b2.clone()]);
let hash = Arc::new(KetamaHashing::build(&backends));

// weight 3 of 4 in total, so about 3000 of these 4000 keys
let on_b2 = (0..4000)
.filter(|i| hash.iter(format!("key{i}").as_bytes()).next() == Some(&b2))
.count();
assert!(on_b2 > 2500, "weight 3 backend got {on_b2} of 4000 keys");
}
}
14 changes: 14 additions & 0 deletions pingora-pool/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,20 @@ mod tests {
assert!(node.is_empty(), "node should be empty after draining all");
}

#[test]
fn test_pool_node_remove_keeps_other_hot_queue_items() {
let node: PoolNode<String> = PoolNode::new();
node.insert(1, "v1".to_string());
node.insert(2, "v2".to_string());
node.insert(3, "v3".to_string());

// 1 and 2 sit ahead of 3 in the hot queue
assert_eq!(node.remove(3).as_deref(), Some("v3"));
assert_eq!(node.remove(1).as_deref(), Some("v1"));
assert_eq!(node.remove(2).as_deref(), Some("v2"));
assert!(node.is_empty());
}

#[tokio::test]
async fn test_empty_node_removed_after_pop_closed() {
// Reproducer from GitHub issue #748: a single connection is added and
Expand Down
Loading