diff options
author | Carl Lerche <me@carllerche.com> | 2019-11-12 15:23:40 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-12 15:23:40 -0800 |
commit | 27e5b41067d01c0c9fac230c5addb58034201a63 (patch) | |
tree | f9bd8333dfe1853dfe1d8710b4dc966bd8555d54 /tokio/src/runtime/tests | |
parent | e3df2eafd32e6f813d08617f0e2cd7abbc05c2b1 (diff) |
reorganize modules (#1766)
This patch started as an effort to make `time::Timer` private. However, in an
effort to get the build compiling again, more and more changes were made. This
probably should have been broken up, but here we are. I will attempt to
summarize the changes here.
* Feature flags are reorganized to make clearer. `net-driver` becomes
`io-driver`. `rt-current-thread` becomes `rt-core`.
* The `Runtime` can be created without any executor. This replaces `enter`. It
also allows creating I/O / time drivers that are standalone.
* `tokio::timer` is renamed to `tokio::time`. This brings it in line with `std`.
* `tokio::timer::Timer` is renamed to `Driver` and made private.
* The `clock` module is removed. Instead, an `Instant` type is provided. This
type defaults to calling `std::time::Instant`. A `test-util` feature flag can
be used to enable hooking into time.
* The `blocking` module is moved to the top level and is cleaned up.
* The `task` module is moved to the top level.
* The thread-pool's in-place blocking implementation is cleaned up.
* `runtime::Spawner` is renamed to `runtime::Handle` and can be used to "enter"
a runtime context.
Diffstat (limited to 'tokio/src/runtime/tests')
-rw-r--r-- | tokio/src/runtime/tests/backoff.rs | 32 | ||||
-rw-r--r-- | tokio/src/runtime/tests/loom_schedule.rs | 51 | ||||
-rw-r--r-- | tokio/src/runtime/tests/mock_schedule.rs | 131 | ||||
-rw-r--r-- | tokio/src/runtime/tests/mod.rs | 33 | ||||
-rw-r--r-- | tokio/src/runtime/tests/track_drop.rs | 57 |
5 files changed, 0 insertions, 304 deletions
diff --git a/tokio/src/runtime/tests/backoff.rs b/tokio/src/runtime/tests/backoff.rs deleted file mode 100644 index 358ab2da..00000000 --- a/tokio/src/runtime/tests/backoff.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::future::Future; -use std::pin::Pin; -use std::task::{Context, Poll}; - -pub(crate) struct Backoff(usize, bool); - -pub(crate) fn backoff(n: usize) -> impl Future<Output = ()> { - Backoff(n, false) -} - -/// Back off, but clone the waker each time -pub(crate) fn backoff_clone(n: usize) -> impl Future<Output = ()> { - Backoff(n, true) -} - -impl Future for Backoff { - type Output = (); - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { - if self.0 == 0 { - return Poll::Ready(()); - } - - self.0 -= 1; - if self.1 { - cx.waker().clone().wake(); - } else { - cx.waker().wake_by_ref(); - } - Poll::Pending - } -} diff --git a/tokio/src/runtime/tests/loom_schedule.rs b/tokio/src/runtime/tests/loom_schedule.rs deleted file mode 100644 index 1dad6062..00000000 --- a/tokio/src/runtime/tests/loom_schedule.rs +++ /dev/null @@ -1,51 +0,0 @@ -use crate::runtime::task::{Schedule, Task}; - -use loom::sync::Notify; -use std::collections::VecDeque; -use std::sync::Mutex; - -pub(crate) struct LoomSchedule { - notify: Notify, - pending: Mutex<VecDeque<Option<Task<Self>>>>, -} - -impl LoomSchedule { - pub(crate) fn new() -> LoomSchedule { - LoomSchedule { - notify: Notify::new(), - pending: Mutex::new(VecDeque::new()), - } - } - - pub(crate) fn push_task(&self, task: Task<Self>) { - self.schedule(task); - } - - pub(crate) fn recv(&self) -> Option<Task<Self>> { - loop { - if let Some(task) = self.pending.lock().unwrap().pop_front() { - return task; - } - - self.notify.wait(); - } - } -} - -impl Schedule for LoomSchedule { - fn bind(&self, _task: &Task<Self>) {} - - fn release(&self, task: Task<Self>) { - self.release_local(&task); - } - - fn release_local(&self, _task: &Task<Self>) { - self.pending.lock().unwrap().push_back(None); - self.notify.notify(); - } - - fn schedule(&self, task: Task<Self>) { - self.pending.lock().unwrap().push_back(Some(task)); - self.notify.notify(); - } -} diff --git a/tokio/src/runtime/tests/mock_schedule.rs b/tokio/src/runtime/tests/mock_schedule.rs deleted file mode 100644 index ab15c54e..00000000 --- a/tokio/src/runtime/tests/mock_schedule.rs +++ /dev/null @@ -1,131 +0,0 @@ -#![allow(warnings)] - -use crate::runtime::task::{Header, Schedule, Task}; - -use std::collections::VecDeque; -use std::sync::Mutex; -use std::thread; - -pub(crate) struct Mock { - inner: Mutex<Inner>, -} - -pub(crate) struct Noop; -pub(crate) static NOOP_SCHEDULE: Noop = Noop; - -struct Inner { - calls: VecDeque<Call>, - pending_run: VecDeque<Task<Mock>>, - pending_drop: VecDeque<Task<Mock>>, -} - -unsafe impl Send for Inner {} -unsafe impl Sync for Inner {} - -#[derive(Debug, Eq, PartialEq)] -enum Call { - Bind(*const Header), - Release, - ReleaseLocal, - Schedule, -} - -pub(crate) fn mock() -> Mock { - Mock { - inner: Mutex::new(Inner { - calls: VecDeque::new(), - pending_run: VecDeque::new(), - pending_drop: VecDeque::new(), - }), - } -} - -impl Mock { - pub(crate) fn bind(self, task: &Task<Mock>) -> Self { - self.push(Call::Bind(task.header() as *const _)); - self - } - - pub(crate) fn release(self) -> Self { - self.push(Call::Release); - self - } - - pub(crate) fn release_local(self) -> Self { - self.push(Call::ReleaseLocal); - self - } - - pub(crate) fn schedule(self) -> Self { - self.push(Call::Schedule); - self - } - - pub(crate) fn next_pending_run(&self) -> Option<Task<Self>> { - self.inner.lock().unwrap().pending_run.pop_front() - } - - pub(crate) fn next_pending_drop(&self) -> Option<Task<Self>> { - self.inner.lock().unwrap().pending_drop.pop_front() - } - - fn push(&self, call: Call) { - self.inner.lock().unwrap().calls.push_back(call); - } - - fn next(&self, name: &str) -> Call { - self.inner - .lock() - .unwrap() - .calls - .pop_front() - .expect(&format!("received `{}`, but none expected", name)) - } -} - -impl Schedule for Mock { - fn bind(&self, task: &Task<Self>) { - match self.next("bind") { - Call::Bind(ptr) => { - assert!(ptr.eq(&(task.header() as *const _))); - } - call => panic!("expected `Bind`, was {:?}", call), - } - } - - fn release(&self, task: Task<Self>) { - match self.next("release") { - Call::Release => { - self.inner.lock().unwrap().pending_drop.push_back(task); - } - call => panic!("expected `Release`, was {:?}", call), - } - } - - fn release_local(&self, _task: &Task<Self>) { - assert_eq!(Call::ReleaseLocal, self.next("release_local")); - } - - fn schedule(&self, task: Task<Self>) { - self.inner.lock().unwrap().pending_run.push_back(task); - assert_eq!(Call::Schedule, self.next("schedule")); - } -} - -impl Drop for Mock { - fn drop(&mut self) { - if !thread::panicking() { - assert!(self.inner.lock().unwrap().calls.is_empty()); - } - } -} - -impl Schedule for Noop { - fn bind(&self, _task: &Task<Self>) {} - - fn release(&self, _task: Task<Self>) {} - - fn release_local(&self, _task: &Task<Self>) {} - - fn schedule(&self, _task: Task<Self>) {} -} diff --git a/tokio/src/runtime/tests/mod.rs b/tokio/src/runtime/tests/mod.rs index b287bcf2..99ed8cd8 100644 --- a/tokio/src/runtime/tests/mod.rs +++ b/tokio/src/runtime/tests/mod.rs @@ -1,40 +1,7 @@ //! Testing utilities -#[cfg(not(loom))] -pub(crate) mod backoff; - #[cfg(loom)] pub(crate) mod loom_oneshot; -#[cfg(loom)] -pub(crate) mod loom_schedule; - #[cfg(not(loom))] pub(crate) mod mock_park; - -pub(crate) mod mock_schedule; - -#[cfg(not(loom))] -pub(crate) mod track_drop; - -/// Panic if expression results in `None`. -#[macro_export] -macro_rules! assert_some { - ($e:expr) => {{ - match $e { - Some(v) => v, - _ => panic!("expected some, was none"), - } - }}; -} - -/// Panic if expression results in `Some`. -#[macro_export] -macro_rules! assert_none { - ($e:expr) => {{ - match $e { - Some(v) => panic!("expected none, was {:?}", v), - _ => {} - } - }}; -} diff --git a/tokio/src/runtime/tests/track_drop.rs b/tokio/src/runtime/tests/track_drop.rs deleted file mode 100644 index c3ded845..00000000 --- a/tokio/src/runtime/tests/track_drop.rs +++ /dev/null @@ -1,57 +0,0 @@ -use std::future::Future; -use std::pin::Pin; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::Ordering::SeqCst; -use std::sync::Arc; -use std::task::{Context, Poll}; - -#[derive(Debug)] -pub(crate) struct TrackDrop<T>(T, Arc<AtomicBool>); - -#[derive(Debug)] -pub(crate) struct DidDrop(Arc<AtomicBool>, Arc<AtomicBool>); - -pub(crate) fn track_drop<T: Future>( - future: T, -) -> (impl Future<Output = TrackDrop<T::Output>>, DidDrop) { - let did_drop_future = Arc::new(AtomicBool::new(false)); - let did_drop_output = Arc::new(AtomicBool::new(false)); - let did_drop = DidDrop(did_drop_future.clone(), did_drop_output.clone()); - - let future = async move { TrackDrop(future.await, did_drop_output) }; - - let future = TrackDrop(future, did_drop_future); - - (future, did_drop) -} - -impl<T> TrackDrop<T> { - pub(crate) fn get_ref(&self) -> &T { - &self.0 - } -} - -impl<T: Future> Future for TrackDrop<T> { - type Output = T::Output; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { - let me = unsafe { Pin::map_unchecked_mut(self, |x| &mut x.0) }; - me.poll(cx) - } -} - -impl<T> Drop for TrackDrop<T> { - fn drop(&mut self) { - self.1.store(true, SeqCst); - } -} - -impl DidDrop { - pub(crate) fn did_drop_future(&self) -> bool { - self.0.load(SeqCst) - } - - pub(crate) fn did_drop_output(&self) -> bool { - self.1.load(SeqCst) - } -} |