diff options
| author | Ibraheem Ahmed <ibraheem@ibraheem.ca> | 2023-01-10 21:54:53 -0500 |
|---|---|---|
| committer | Ibraheem Ahmed <ibraheem@ibraheem.ca> | 2023-01-10 21:54:53 -0500 |
| commit | f8276c94ac06b272e88fb1bb9c5f6615fc5876ef (patch) | |
| tree | 888bf877a29e526d0476d5bc20064940ac543a1b | |
| parent | 2538c0c17095834c994cbfdc4909338a31f83fb7 (diff) | |
| download | rust-f8276c94ac06b272e88fb1bb9c5f6615fc5876ef.tar.gz rust-f8276c94ac06b272e88fb1bb9c5f6615fc5876ef.zip | |
add `SyncSender::send_timeout` test
| -rw-r--r-- | library/std/src/sync/mpmc/mod.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sync/mpsc/mod.rs | 9 | ||||
| -rw-r--r-- | library/std/src/sync/mpsc/sync_tests.rs | 8 |
3 files changed, 18 insertions, 1 deletions
diff --git a/library/std/src/sync/mpmc/mod.rs b/library/std/src/sync/mpmc/mod.rs index cef99c58843..7a602cecd3b 100644 --- a/library/std/src/sync/mpmc/mod.rs +++ b/library/std/src/sync/mpmc/mod.rs @@ -43,7 +43,7 @@ mod zero; use crate::fmt; use crate::panic::{RefUnwindSafe, UnwindSafe}; use crate::time::{Duration, Instant}; -use error::*; +pub use error::*; /// Creates a channel of unbounded capacity. /// diff --git a/library/std/src/sync/mpsc/mod.rs b/library/std/src/sync/mpsc/mod.rs index adb488d4378..6e3c28f10bb 100644 --- a/library/std/src/sync/mpsc/mod.rs +++ b/library/std/src/sync/mpsc/mod.rs @@ -738,6 +738,15 @@ impl<T> SyncSender<T> { pub fn try_send(&self, t: T) -> Result<(), TrySendError<T>> { self.inner.try_send(t) } + + // Attempts to send for a value on this receiver, returning an error if the + // corresponding channel has hung up, or if it waits more than `timeout`. + // + // This method is currently private and only used for tests. + #[allow(unused)] + fn send_timeout(&self, t: T, timeout: Duration) -> Result<(), mpmc::SendTimeoutError<T>> { + self.inner.send_timeout(t, timeout) + } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/sync/mpsc/sync_tests.rs b/library/std/src/sync/mpsc/sync_tests.rs index 63c79436974..9d2f92ffc9b 100644 --- a/library/std/src/sync/mpsc/sync_tests.rs +++ b/library/std/src/sync/mpsc/sync_tests.rs @@ -1,5 +1,6 @@ use super::*; use crate::env; +use crate::sync::mpmc::SendTimeoutError; use crate::thread; use crate::time::Duration; @@ -42,6 +43,13 @@ fn recv_timeout() { } #[test] +fn send_timeout() { + let (tx, _rx) = sync_channel::<i32>(1); + assert_eq!(tx.send_timeout(1, Duration::from_millis(1)), Ok(())); + assert_eq!(tx.send_timeout(1, Duration::from_millis(1)), Err(SendTimeoutError::Timeout(1))); +} + +#[test] fn smoke_threads() { let (tx, rx) = sync_channel::<i32>(0); let _t = thread::spawn(move || { |
