Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Oct 17, 2023
1 parent caac9c4 commit f6fb059
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 145 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ jobs:
- x86_64-pc-windows-gnu
- i686-unknown-linux-gnu
- powerpc64-unknown-linux-gnu
- mips64-unknown-linux-gnuabi64
- riscv64gc-unknown-linux-gnu
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 2 additions & 2 deletions ci/sanitizer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export ASAN_OPTIONS="detect_odr_violation=0 detect_leaks=0"

# Run address sanitizer with cargo-hack
RUSTFLAGS="-Z sanitizer=address" \
cargo hack test --lib --each-feature
cargo hack test --lib --each-feature --include-features sync,async --exclude-no-default-features

# Run leak sanitizer with cargo-hack
RUSTFLAGS="-Z sanitizer=leak" \
cargo hack test --lib --each-feature
cargo hack test --lib --each-feature --include-features sync,async --exclude-no-default-features

# Run thread sanitizer with cargo-hack
RUSTFLAGS="-Z sanitizer=thread" \
Expand Down
290 changes: 148 additions & 142 deletions src/policy/test.rs
Original file line number Diff line number Diff line change
@@ -1,166 +1,172 @@
use crate::metrics::Metrics;
use crate::policy::{LFUPolicy, SampledLFU, TinyLFU};
use crate::policy::{SampledLFU, TinyLFU};
use std::collections::hash_map::RandomState;
use std::sync::Arc;

use std::time::Duration;

static WAIT: Duration = Duration::from_millis(100);

use std::thread::sleep;
#[cfg(feature = "sync")]
mod sync_test {
use super::*;
use crate::policy::LFUPolicy;
use std::sync::Arc;
use std::thread::sleep;

#[test]
fn test_policy() {
let _ = LFUPolicy::new(100, 10);
}
#[test]
fn test_policy() {
let _ = LFUPolicy::new(100, 10);
}

#[test]
fn test_policy_metrics() {
let mut p = LFUPolicy::new(100, 10).unwrap();
p.collect_metrics(Arc::new(Metrics::new_op()));
assert!(p.metrics.is_op());
assert!(p.inner.lock().costs.metrics.is_op());
}
#[test]
fn test_policy_metrics() {
let mut p = LFUPolicy::new(100, 10).unwrap();
p.collect_metrics(Arc::new(Metrics::new_op()));
assert!(p.metrics.is_op());
assert!(p.inner.lock().costs.metrics.is_op());
}

#[test]
fn test_policy_process_items() {
let p = LFUPolicy::new(100, 10).unwrap();
p.items_tx.send(vec![1, 2, 2]).unwrap();
sleep(WAIT);
let inner = p.inner.lock();
assert_eq!(inner.admit.estimate(2), 2);
assert_eq!(inner.admit.estimate(1), 1);
drop(inner);

p.stop_tx.send(()).unwrap();
sleep(WAIT);
assert!(p.push(vec![3, 3, 3]).is_err());
let inner = p.inner.lock();
assert_eq!(inner.admit.estimate(3), 0);
}
#[test]
fn test_policy_process_items() {
let p = LFUPolicy::new(100, 10).unwrap();
p.items_tx.send(vec![1, 2, 2]).unwrap();
sleep(WAIT);
let inner = p.inner.lock();
assert_eq!(inner.admit.estimate(2), 2);
assert_eq!(inner.admit.estimate(1), 1);
drop(inner);

#[test]
fn test_policy_push() {
let p = LFUPolicy::new(100, 10).unwrap();
assert!(p.push(vec![]).unwrap());

let mut keep_count = 0;
(0..10).for_each(|_| {
if p.push(vec![1, 2, 3, 4, 5]).unwrap() {
keep_count += 1;
}
});
p.stop_tx.send(()).unwrap();
sleep(WAIT);
assert!(p.push(vec![3, 3, 3]).is_err());
let inner = p.inner.lock();
assert_eq!(inner.admit.estimate(3), 0);
}

assert_ne!(0, keep_count);
}
#[test]
fn test_policy_push() {
let p = LFUPolicy::new(100, 10).unwrap();
assert!(p.push(vec![]).unwrap());

#[test]
fn test_policy_add() {
let p = LFUPolicy::new(1000, 100).unwrap();
let (victims, added) = p.add(1, 101);
assert!(victims.is_none());
assert!(!added);

let mut inner = p.inner.lock();
inner.costs.increment(1, 1);
inner.admit.increment(1);
inner.admit.increment(2);
inner.admit.increment(3);
drop(inner);

let (victims, added) = p.add(1, 1);
assert!(victims.is_none());
assert!(!added);

let (victims, added) = p.add(2, 20);
assert!(victims.is_none());
assert!(added);

let (victims, added) = p.add(3, 90);
assert!(victims.is_some());
assert!(added);

let (victims, added) = p.add(4, 20);
assert!(victims.is_some());
assert!(!added);
}
let mut keep_count = 0;
(0..10).for_each(|_| {
if p.push(vec![1, 2, 3, 4, 5]).unwrap() {
keep_count += 1;
}
});

#[test]
fn test_policy_has() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
assert!(p.contains(&1));
assert!(!p.contains(&2));
}
assert_ne!(0, keep_count);
}

#[test]
fn test_policy_del() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
p.remove(&1);
p.remove(&2);
assert!(!p.contains(&1));
assert!(!p.contains(&2));
}
#[test]
fn test_policy_add() {
let p = LFUPolicy::new(1000, 100).unwrap();
let (victims, added) = p.add(1, 101);
assert!(victims.is_none());
assert!(!added);

#[test]
fn test_policy_cap() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
assert_eq!(p.cap(), 9);
}
let mut inner = p.inner.lock();
inner.costs.increment(1, 1);
inner.admit.increment(1);
inner.admit.increment(2);
inner.admit.increment(3);
drop(inner);

#[test]
fn test_policy_update() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
p.update(&1, 2);
let inner = p.inner.lock();
assert_eq!(inner.costs.key_costs.get(&1).unwrap(), &2);
}
let (victims, added) = p.add(1, 1);
assert!(victims.is_none());
assert!(!added);

#[test]
fn test_policy_cost() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 2);
assert_eq!(p.cost(&1), 2);
assert_eq!(p.cost(&2), -1);
}
let (victims, added) = p.add(2, 20);
assert!(victims.is_none());
assert!(added);

#[test]
fn test_policy_clear() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
p.add(2, 2);
p.add(3, 3);
p.clear();

assert_eq!(p.cap(), 10);
assert!(!p.contains(&2));
assert!(!p.contains(&2));
assert!(!p.contains(&3));
}
let (victims, added) = p.add(3, 90);
assert!(victims.is_some());
assert!(added);

#[test]
fn test_policy_close() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
let _ = p.close();
sleep(WAIT);
assert!(p.items_tx.send(vec![1]).is_err())
}
let (victims, added) = p.add(4, 20);
assert!(victims.is_some());
assert!(!added);
}

#[test]
fn test_policy_push_after_close() {
let p = LFUPolicy::new(100, 10).unwrap();
let _ = p.close();
assert!(!p.push(vec![1, 2]).unwrap());
}
#[test]
fn test_policy_has() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
assert!(p.contains(&1));
assert!(!p.contains(&2));
}

#[test]
fn test_policy_add_after_close() {
let p = LFUPolicy::new(100, 10).unwrap();
let _ = p.close();
p.add(1, 1);
#[test]
fn test_policy_del() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
p.remove(&1);
p.remove(&2);
assert!(!p.contains(&1));
assert!(!p.contains(&2));
}

#[test]
fn test_policy_cap() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
assert_eq!(p.cap(), 9);
}

#[test]
fn test_policy_update() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
p.update(&1, 2);
let inner = p.inner.lock();
assert_eq!(inner.costs.key_costs.get(&1).unwrap(), &2);
}

#[test]
fn test_policy_cost() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 2);
assert_eq!(p.cost(&1), 2);
assert_eq!(p.cost(&2), -1);
}

#[test]
fn test_policy_clear() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
p.add(2, 2);
p.add(3, 3);
p.clear();

assert_eq!(p.cap(), 10);
assert!(!p.contains(&2));
assert!(!p.contains(&2));
assert!(!p.contains(&3));
}

#[test]
fn test_policy_close() {
let p = LFUPolicy::new(100, 10).unwrap();
p.add(1, 1);
let _ = p.close();
sleep(WAIT);
assert!(p.items_tx.send(vec![1]).is_err())
}

#[test]
fn test_policy_push_after_close() {
let p = LFUPolicy::new(100, 10).unwrap();
let _ = p.close();
assert!(!p.push(vec![1, 2]).unwrap());
}

#[test]
fn test_policy_add_after_close() {
let p = LFUPolicy::new(100, 10).unwrap();
let _ = p.close();
p.add(1, 1);
}
}

#[cfg(feature = "async")]
Expand Down

0 comments on commit f6fb059

Please sign in to comment.