Add more tests (#22)

This is done to increase the test coverage for this crate.

Signed-off-by: John Nunley <dev@notgull.net>
This commit is contained in:
John Nunley 2024-04-22 18:36:22 -07:00 committed by GitHub
parent 2aa2aece0f
commit 1a771e0c75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 122 additions and 4 deletions

View File

@ -12,3 +12,17 @@ fn smoke() {
u.unpark();
});
}
#[test]
fn yield_then_unpark() {
loom::model(|| {
let (p, u) = parking::pair();
loom::thread::spawn(move || {
loom::thread::yield_now();
u.unpark();
});
p.park();
});
}

View File

@ -1,15 +1,16 @@
use std::future::Future;
use std::task::{Context, Poll, Waker};
use std::thread::sleep;
use std::time::Duration;
use std::u32;
use std::time::{Duration, Instant};
use easy_parallel::Parallel;
use parking::Parker;
#[test]
fn park_timeout_unpark_before() {
let p = Parker::new();
let (p, u) = parking::pair();
for _ in 0..10 {
p.unparker().unpark();
u.unpark();
p.park_timeout(Duration::from_millis(u32::MAX as u64));
}
}
@ -39,3 +40,106 @@ fn park_timeout_unpark_called_other_thread() {
.run();
}
}
#[test]
fn park_deadline_unpark_called_other_thread() {
for _ in 0..10 {
let p = Parker::new();
let u = p.unparker();
Parallel::new()
.add(move || {
sleep(Duration::from_millis(50));
u.unpark();
})
.add(move || {
let deadline = Instant::now() + Duration::from_micros(u32::MAX as u64);
p.park_deadline(deadline);
})
.run();
}
}
#[test]
fn park_then_wake_from_other_thread() {
for _ in 0..10 {
let (p, u) = parking::pair();
Parallel::new()
.add(move || {
sleep(Duration::from_millis(50));
u.unpark();
})
.add(move || {
let start = Instant::now();
p.park();
assert!(Instant::now().duration_since(start) >= Duration::from_millis(50));
})
.run();
}
}
#[test]
fn unpark() {
let p = Parker::default();
assert!(p.unpark());
assert!(!p.unpark());
}
#[test]
fn same_parker() {
let (p1, u1) = parking::pair();
let (p2, u2) = parking::pair();
assert!(u1.will_unpark(&p1));
assert!(!u1.will_unpark(&p2));
assert!(u1.same_parker(&u1.clone()));
assert!(!u1.same_parker(&u2));
}
#[test]
fn waker() {
let (p, u) = parking::pair();
let waker: Waker = u.into();
waker.wake();
assert!(p.park_timeout(Duration::from_secs(2)));
}
#[test]
fn future() {
struct Yield(bool);
impl Future for Yield {
type Output = ();
fn poll(mut self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.0 {
Poll::Ready(())
} else {
self.0 = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
let (p, u) = parking::pair();
let waker = u.into();
let mut context = Context::from_waker(&waker);
let mut future = Box::pin(Yield(false));
assert!(!p.park_timeout(Duration::from_millis(0)));
assert_eq!(future.as_mut().poll(&mut context), Poll::Pending);
assert!(p.park_timeout(Duration::from_millis(0)));
assert_eq!(future.as_mut().poll(&mut context), Poll::Ready(()));
assert!(!p.park_timeout(Duration::from_millis(0)));
}
#[test]
fn debug_for_coverage() {
let (p, u) = parking::pair();
let _ = format!("{:?} {:?}", &p, &u);
}