From 88cdf9ef69bc50638437c7093aab69fd29740c7d Mon Sep 17 00:00:00 2001 From: Cesar Rodas Date: Wed, 9 Oct 2024 20:05:55 -0300 Subject: [PATCH] Add unit tests --- crates/cdk/src/subscription/index.rs | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/crates/cdk/src/subscription/index.rs b/crates/cdk/src/subscription/index.rs index 8a005b605..a8aa218d9 100644 --- a/crates/cdk/src/subscription/index.rs +++ b/crates/cdk/src/subscription/index.rs @@ -106,3 +106,60 @@ impl Default for Unique { Self(COUNTER.fetch_add(1, Ordering::Relaxed)) } } + +#[cfg(test)] +mod tests { + use tokio::sync::mpsc; + + use super::*; + use crate::subscription::ActiveSubscription; + + #[test] + fn test_index_from_tuple() { + let sub_id = SubId::from("test_sub_id"); + let prefix = "test_prefix"; + let index: Index<&str> = Index::from((prefix, sub_id.clone())); + assert_eq!(index.prefix, "test_prefix"); + assert_eq!(index.id, sub_id); + } + + #[test] + fn test_index_cmp_prefix() { + let sub_id = SubId::from("test_sub_id"); + let index1: Index<&str> = Index::from(("a", sub_id.clone())); + let index2: Index<&str> = Index::from(("b", sub_id.clone())); + assert_eq!(index1.cmp_prefix(&index2), std::cmp::Ordering::Less); + } + + #[test] + fn test_sub_id_from_str() { + let sub_id = SubId::from("test_sub_id"); + assert_eq!(sub_id.0, "test_sub_id"); + } + + #[test] + fn test_sub_id_deref() { + let sub_id = SubId::from("test_sub_id"); + assert_eq!(&*sub_id, "test_sub_id"); + } + + #[test] + fn test_active_subscription_drop() { + let (tx, rx) = mpsc::channel::<(SubId, ())>(10); + let sub_id = SubId::from("test_sub_id"); + let indexes: Vec> = vec![Index::from(("test".to_string(), sub_id.clone()))]; + let (drop_tx, mut drop_rx) = mpsc::channel(10); + + { + let _active_subscription = ActiveSubscription { + sub_id: sub_id.clone(), + indexes, + receiver: rx, + drop: drop_tx, + }; + // When it goes out of scope, it should notify + } + assert_eq!(drop_rx.try_recv().unwrap().0, sub_id); // it should have notified + assert!(tx.try_send(("foo".into(), ())).is_err()); // subscriber is dropped + } +}