From 1d0be87e06f0ef758dc8ea2807519b5c50a583c2 Mon Sep 17 00:00:00 2001 From: Wisha Wa Date: Mon, 9 Jan 2023 20:48:46 +0000 Subject: [PATCH] add a test for the new Arc-sharing WakerArray --- src/utils/wakers/array/waker_array.rs | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/utils/wakers/array/waker_array.rs b/src/utils/wakers/array/waker_array.rs index 345c46b..7797672 100644 --- a/src/utils/wakers/array/waker_array.rs +++ b/src/utils/wakers/array/waker_array.rs @@ -75,3 +75,43 @@ impl WakeDataContainer for WakerArrayInner { } } } + +#[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); + } +}