Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crodas committed Oct 9, 2024
1 parent fd4191a commit 88cdf9e
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions crates/cdk/src/subscription/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Index<String>> = 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
}
}

0 comments on commit 88cdf9e

Please sign in to comment.