Skip to content

Commit

Permalink
Add two unit test cases
Browse files Browse the repository at this point in the history
Add two unit test cases for SubscriberId and remote endpoint.

Signed-off-by: Liu Jiang <[email protected]>
  • Loading branch information
jiangliu committed Jun 6, 2021
1 parent 4d39b46 commit f8ecd76
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,22 @@ impl<T: MutEventSubscriber + ?Sized> MutEventSubscriber for Box<T> {
self.deref_mut().init(ops);
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_subscriber_id_derives() {
let a = SubscriberId(1);
let b = SubscriberId(1);
let c = SubscriberId(2);

assert_eq!(a, b);
assert_ne!(a, c);
assert_ne!(c, b);

let d = c.clone();
assert_eq!(c, d);
}
}
27 changes: 27 additions & 0 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,31 @@ mod tests {
Error::InvalidId
);
}

#[test]
#[cfg(feature = "remote_endpoint")]
fn test_endpoint() {
use std::thread;

let mut event_manager = EventManager::<DummySubscriber>::new().unwrap();
let dummy = DummySubscriber::new();
let endpoint = event_manager.remote_endpoint();
let kicker = event_manager.remote_endpoint();

let thread_handle = thread::spawn(move || {
event_manager.run().unwrap();
event_manager.run().unwrap();
});

dummy.event_fd_1.write(1).unwrap();

let token = endpoint
.call_blocking(|sub_ops| -> Result<SubscriberId> { Ok(sub_ops.add_subscriber(dummy)) })
.unwrap();
assert_eq!(token, SubscriberId(1));

kicker.kick().unwrap();

thread_handle.join().unwrap();
}
}

0 comments on commit f8ecd76

Please sign in to comment.