Skip to content

Commit

Permalink
add a test for the new Arc-sharing WakerArray
Browse files Browse the repository at this point in the history
  • Loading branch information
wishawa committed Jan 9, 2023
1 parent b396a85 commit 1d0be87
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/utils/wakers/array/waker_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,43 @@ impl<const N: usize> WakeDataContainer for WakerArrayInner<N> {
}
}
}

#[cfg(test)]
mod tests {
use crate::utils::wakers::dummy_waker;

use super::*;
#[test]
fn check_refcount() {
let mut wa = WakerArray::<5>::new();
assert_eq!(Arc::strong_count(&wa.inner), 6);
wa.wakers[4] = dummy_waker();
assert_eq!(Arc::strong_count(&wa.inner), 5);
let cloned = wa.wakers[3].clone();
assert_eq!(Arc::strong_count(&wa.inner), 6);
wa.wakers[3] = wa.wakers[4].clone();
assert_eq!(Arc::strong_count(&wa.inner), 5);
drop(cloned);
assert_eq!(Arc::strong_count(&wa.inner), 4);

wa.wakers[0].wake_by_ref();
wa.wakers[0].wake_by_ref();
wa.wakers[0].wake_by_ref();
assert_eq!(Arc::strong_count(&wa.inner), 4);

wa.wakers[0] = wa.wakers[1].clone();
assert_eq!(Arc::strong_count(&wa.inner), 4);

let taken = std::mem::replace(&mut wa.wakers[2], dummy_waker());
assert_eq!(Arc::strong_count(&wa.inner), 4);
taken.wake_by_ref();
taken.wake_by_ref();
taken.wake_by_ref();
assert_eq!(Arc::strong_count(&wa.inner), 4);
taken.wake();
assert_eq!(Arc::strong_count(&wa.inner), 3);

wa.wakers = array::from_fn(|_| dummy_waker());
assert_eq!(Arc::strong_count(&wa.inner), 1);
}
}

0 comments on commit 1d0be87

Please sign in to comment.