Skip to content

Commit

Permalink
integration-test: deflake test_loaded_at
Browse files Browse the repository at this point in the history
The test could fail due to the lack of clock monotonicity. This PR
deflakes the test by adding retries.

See
https://github.com/aya-rs/aya/actions/runs/6340369670/job/17221591723.
  • Loading branch information
ajwerner committed Sep 28, 2023
1 parent 373fb7b commit b685d90
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions test/integration-test/src/tests/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,39 @@ fn unload_xdp() {
fn test_loaded_at() {
let mut bpf = Bpf::load(crate::TEST).unwrap();
let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
let t1 = SystemTime::now();
prog.load().unwrap();
let t2 = SystemTime::now();
assert_loaded("pass");

let loaded_at = prog.info().unwrap().loaded_at();

let range = t1..t2;
// The SystemTime timestamps are not monotonic, which can cause this test
// to flake. We don't expect the clock timestamp to continuously jump around,
// so we add some retries. If the test is ever correct, we know that loaded_at
// was reasonable.
let mut failures = Vec::new();
for _ in 0..5 {
let t1 = SystemTime::now();
prog.load().unwrap();
let t2 = SystemTime::now();
assert_loaded("pass");
let loaded_at = prog.info().unwrap().loaded_at();
let range = t1..t2;
prog.unload().unwrap();
if range.contains(&loaded_at) {
failures.clear();
break;
}
failures.push(LoadedAtRange(loaded_at, range));
}
assert!(
range.contains(&loaded_at),
"{range:?}.contains({loaded_at:?})"
failures.is_empty(),
"loaded_at was not in range: {failures:?}",
);

prog.unload().unwrap();

assert_unloaded("pass");

struct LoadedAtRange(SystemTime, std::ops::Range<SystemTime>);
impl std::fmt::Debug for LoadedAtRange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self(loaded_at, range) = self;
write!(f, "{range:?}.contains({loaded_at:?})")
}
}
}

#[test]
Expand Down

0 comments on commit b685d90

Please sign in to comment.