summaryrefslogtreecommitdiff
path: root/learn_22/tests/event.rs
diff options
context:
space:
mode:
Diffstat (limited to 'learn_22/tests/event.rs')
-rw-r--r--learn_22/tests/event.rs245
1 files changed, 245 insertions, 0 deletions
diff --git a/learn_22/tests/event.rs b/learn_22/tests/event.rs
index e34016e..f9cf63c 100644
--- a/learn_22/tests/event.rs
+++ b/learn_22/tests/event.rs
@@ -1,3 +1,4 @@
+<<<<<<< HEAD
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use learn_22::event::{Event, EventBus};
@@ -166,3 +167,247 @@ fn test_accept() {
// order of registered callbacks is honored
assert_eq!(*ordering.borrow(), vec![0,0,0,0,0,1,1,1,2]);
}
+||||||| dfab0ca
+=======
+use std::cell::{Cell, RefCell};
+use std::rc::Rc;
+use learn_22::event::{Event, EventBus, SubscriptionId};
+
+#[test]
+fn test_new() {
+ let eb = EventBus::new();
+ assert_eq!(eb.subscriber_count(), 0);
+}
+
+#[test]
+fn test_subscribe() {
+ let mut eb = EventBus::new();
+ let mut count: u32 = 0;
+
+ let subscription_id0 = eb.subscribe(move |event| {
+ count += 1;
+ });
+ assert_eq!(subscription_id0, SubscriptionId::from(0));
+ assert_eq!(eb.subscriber_count(), 1);
+
+ let subscription_id1 = eb.subscribe(move |event| {
+ count += 1;
+ });
+ assert_eq!(subscription_id1, SubscriptionId::from(1));
+ assert_eq!(eb.subscriber_count(), 2);
+}
+
+#[test]
+fn test_unsubscribe() {
+ let mut eb = EventBus::new();
+ let mut count: u32 = 0;
+ let subscription_id0 = eb.subscribe(move |event| {
+ count += 1;
+ });
+ assert_eq!(subscription_id0, SubscriptionId::from(0));
+ assert_eq!(eb.subscriber_count(), 1);
+
+ let subscription_id1 = eb.subscribe(move |event| {
+ count += 1;
+ });
+ assert_eq!(subscription_id1, SubscriptionId::from(1));
+ assert_eq!(eb.subscriber_count(), 2);
+ assert!(!eb.unsubscribe(SubscriptionId::from(3))); // not found
+ assert_eq!(eb.subscriber_count(), 2);
+ assert!(eb.unsubscribe(SubscriptionId::from(0)));
+ assert_eq!(eb.subscriber_count(), 1);
+ assert!(eb.unsubscribe(SubscriptionId::from(1)));
+ assert_eq!(eb.subscriber_count(), 0);
+}
+
+#[test]
+fn test_publish() {
+ let mut eb = EventBus::new();
+ let count = Rc::new(Cell::new(0u32));
+
+ let c0 = Rc::clone(&count);
+ let subscription_id0 = eb.subscribe(move |event| {
+ c0.set(c0.get() + 1);
+ });
+ assert_eq!(subscription_id0, SubscriptionId::from(0));
+ assert_eq!(eb.subscriber_count(), 1);
+
+ let c1 = Rc::clone(&count);
+ let subscription_id1 = eb.subscribe(move |event| {
+ c1.set(c1.get() + 2);
+ });
+ assert_eq!(subscription_id1, SubscriptionId::from(1));
+ assert_eq!(eb.subscriber_count(), 2);
+
+ let ev0 = Event::UserCreated{id:0, name:"0".to_string()};
+ eb.publish(ev0);
+ assert_eq!(count.get(), 3);
+}
+
+#[test]
+fn test_accept() {
+ // new bus
+ let mut eb = EventBus::new();
+ assert_eq!(eb.subscriber_count(), 0);
+
+ let count = Rc::new(Cell::new(0u32));
+ let ordering = Rc::new(RefCell::new(Vec::<u32>::new()));
+ let seen_events = Rc::new(RefCell::new(Vec::<Event>::new()));
+
+ // subscribe one callback
+ let c0 = Rc::clone(&count);
+ let o0 = Rc::clone(&ordering);
+ let s0 = Rc::clone(&seen_events);
+ let mut subscription_id0 = eb.subscribe(move |event| {
+ c0.set(c0.get() + 1);
+ o0.borrow_mut().push(0);
+ s0.borrow_mut().push(event.clone());
+ });
+ assert_eq!(subscription_id0, SubscriptionId::from(0));
+ assert_eq!(eb.subscriber_count(), 1);
+
+ // publish one event
+ eb.publish(Event::UserCreated{id:0, name:"0".to_string()});
+ // before: count = 0, so count = 1 is proof of callback 0 execution
+ assert_eq!(count.get(), 1);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0]);
+ assert_eq!(
+ seen_events
+ .borrow()
+ .iter()
+ .filter_map(|e| match e {
+ Event::UserCreated { id, .. } => Some(*id),
+ _ => None,
+ })
+ .collect::<Vec<_>>(),
+ vec![0]
+ );
+ // publish multiple events
+ eb.publish(Event::UserCreated{id:1, name:"1".to_string()});
+ eb.publish(Event::UserCreated{id:2, name:"2".to_string()});
+ eb.publish(Event::UserCreated{id:3, name:"3".to_string()});
+ // before: count = 1, so count = 4 is proof of callback 0 execution
+ assert_eq!(count.get(), 4);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0]);
+ assert_eq!(
+ seen_events
+ .borrow()
+ .iter()
+ .filter_map(|e| match e {
+ Event::UserCreated { id, .. } => Some(*id),
+ _ => None,
+ })
+ .collect::<Vec<_>>(),
+ vec![0,1,2,3]
+ );
+
+ // subscribe another callback
+ let c1 = Rc::clone(&count);
+ let o1 = Rc::clone(&ordering);
+ let s1 = Rc::clone(&seen_events);
+ let subscription_id1 = eb.subscribe(move |event| {
+ c1.set(c1.get() + 2);
+ o1.borrow_mut().push(1);
+ s1.borrow_mut().push(event.clone());
+ });
+ assert_eq!(subscription_id1, SubscriptionId::from(1));
+ assert_eq!(eb.subscriber_count(), 2);
+
+ eb.publish(Event::UserCreated{id:4, name:"4".to_string()});
+ // before: count = 4, so count = 7 is proof of callbacks 0,1 execution
+ assert_eq!(count.get(), 7);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0,0,1]);
+ assert_eq!(
+ seen_events
+ .borrow()
+ .iter()
+ .filter_map(|e| match e {
+ Event::UserCreated { id, .. } => Some(*id),
+ _ => None,
+ })
+ .collect::<Vec<_>>(),
+ vec![0,1,2,3,4,4]
+ );
+
+ // unsubscribe existing callback id
+ assert!(eb.unsubscribe(subscription_id0));
+ eb.publish(Event::UserCreated{id:5, name:"5".to_string()});
+ // before: count = 7, so count = 9 is proof of callback 1 execution
+ assert_eq!(count.get(), 9);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0,0,1,1]);
+ assert_eq!(
+ seen_events
+ .borrow()
+ .iter()
+ .filter_map(|e| match e {
+ Event::UserCreated { id, .. } => Some(*id),
+ _ => None,
+ })
+ .collect::<Vec<_>>(),
+ vec![0,1,2,3,4,4,5]
+ );
+
+ // unsubscribe unknown id
+ assert!(!eb.unsubscribe(SubscriptionId::from(3)));
+
+ // unsubscribe previously unsubscribed id
+ assert!(!eb.unsubscribe(subscription_id0));
+
+ // subscribe after unsubscribe
+ let c2 = Rc::clone(&count);
+ let o2 = Rc::clone(&ordering);
+ let s2 = Rc::clone(&seen_events);
+ subscription_id0 = eb.subscribe(move |event| {
+ c2.set(c2.get() + 1);
+ o2.borrow_mut().push(2);
+ s2.borrow_mut().push(event.clone());
+ });
+ assert_eq!(subscription_id0, SubscriptionId::from(2));
+ assert_eq!(eb.subscriber_count(), 2);
+
+ eb.publish(Event::UserCreated{id:6, name:"6".to_string()});
+ // before: count = 9, so count = 12 is proof of callbacks 1,2 execution
+ assert_eq!(count.get(), 12);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0,0,1,1,1,2]);
+ assert_eq!(
+ seen_events
+ .borrow()
+ .iter()
+ .filter_map(|e| match e {
+ Event::UserCreated { id, .. } => Some(*id),
+ _ => None,
+ })
+ .collect::<Vec<_>>(),
+ vec![0,1,2,3,4,4,5,6,6]
+ );
+
+ // unsubscribe all callbacks and demonstrate state unchanged
+ assert!(eb.unsubscribe(subscription_id0));
+ assert_eq!(eb.subscriber_count(), 1);
+ assert!(eb.unsubscribe(subscription_id1));
+ assert_eq!(eb.subscriber_count(), 0);
+
+ eb.publish(Event::UserCreated{id:7, name:"7".to_string()});
+ // before: count = 12, so count = 12 is proof of no callbacks
+ assert_eq!(count.get(), 12);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0,0,1,1,1,2]);
+ assert_eq!(
+ seen_events
+ .borrow()
+ .iter()
+ .filter_map(|e| match e {
+ Event::UserCreated { id, .. } => Some(*id),
+ _ => None,
+ })
+ .collect::<Vec<_>>(),
+ vec![0,1,2,3,4,4,5,6,6]
+ );
+
+}
+>>>>>>> learn_22