diff options
| author | datdenkikniet <jcdra1@gmail.com> | 2025-03-17 19:28:38 +0100 |
|---|---|---|
| committer | Emil Fresk <emil.fresk@gmail.com> | 2025-06-18 19:19:37 +0000 |
| commit | 29cfd0d5f5f314dac1029c1140ef9dced7b94ace (patch) | |
| tree | 4f0708ea54f4e66fa32daf76bf4ce09d03f554ab /rtic-sync | |
| parent | 8fa0fbb9aeaffe3f23ff65d08effd4d311f0d5e0 (diff) | |
rtic-sync: add some tests
Diffstat (limited to 'rtic-sync')
| -rw-r--r-- | rtic-sync/src/channel.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/rtic-sync/src/channel.rs b/rtic-sync/src/channel.rs index f2f8aa1..3c0a27a 100644 --- a/rtic-sync/src/channel.rs +++ b/rtic-sync/src/channel.rs @@ -664,6 +664,9 @@ impl<T, const N: usize> Drop for Receiver<'_, T, N> { #[cfg(test)] #[cfg(not(loom))] mod tests { + use core::sync::atomic::AtomicBool; + use std::sync::Arc; + use cassette::Cassette; use super::*; @@ -801,6 +804,76 @@ mod tests { // Make sure that rx & tx are alive until here for good measure. drop((tx, rx)); } + + #[derive(Debug)] + struct SetToTrueOnDrop(Arc<AtomicBool>); + + impl Drop for SetToTrueOnDrop { + fn drop(&mut self) { + self.0.store(true, Ordering::SeqCst); + } + } + + #[test] + fn non_popped_item_is_dropped() { + let mut channel: Channel<SetToTrueOnDrop, 1> = Channel::new(); + + let (mut tx, rx) = channel.split(); + + let value = Arc::new(AtomicBool::new(false)); + tx.try_send(SetToTrueOnDrop(value.clone())).unwrap(); + + drop((tx, rx)); + drop(channel); + + assert!(value.load(Ordering::SeqCst)); + } + + #[test] + pub fn cleared_item_is_dropped() { + let mut channel: Channel<SetToTrueOnDrop, 1> = Channel::new(); + + let (mut tx, rx) = channel.split(); + + let value = Arc::new(AtomicBool::new(false)); + tx.try_send(SetToTrueOnDrop(value.clone())).unwrap(); + + drop((tx, rx)); + + assert!(!value.load(Ordering::SeqCst)); + + channel.clear(); + + assert!(value.load(Ordering::SeqCst)); + } + + #[test] + #[should_panic] + pub fn splitting_non_empty_channel_panics() { + let mut channel: Channel<(), 1> = Channel::new(); + + let (mut tx, rx) = channel.split(); + + tx.try_send(()).unwrap(); + + drop((tx, rx)); + + channel.split(); + } + + #[test] + pub fn splitting_empty_channel_works() { + let mut channel: Channel<(), 1> = Channel::new(); + + let (mut tx, rx) = channel.split(); + + tx.try_send(()).unwrap(); + + drop((tx, rx)); + + channel.clear(); + channel.split(); + } } #[cfg(not(loom))] |
