Skip to content

Commit f8276c9

Browse files
committed
add SyncSender::send_timeout test
1 parent 2538c0c commit f8276c9

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

library/std/src/sync/mpmc/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ mod zero;
4343
use crate::fmt;
4444
use crate::panic::{RefUnwindSafe, UnwindSafe};
4545
use crate::time::{Duration, Instant};
46-
use error::*;
46+
pub use error::*;
4747

4848
/// Creates a channel of unbounded capacity.
4949
///

library/std/src/sync/mpsc/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,15 @@ impl<T> SyncSender<T> {
738738
pub fn try_send(&self, t: T) -> Result<(), TrySendError<T>> {
739739
self.inner.try_send(t)
740740
}
741+
742+
// Attempts to send for a value on this receiver, returning an error if the
743+
// corresponding channel has hung up, or if it waits more than `timeout`.
744+
//
745+
// This method is currently private and only used for tests.
746+
#[allow(unused)]
747+
fn send_timeout(&self, t: T, timeout: Duration) -> Result<(), mpmc::SendTimeoutError<T>> {
748+
self.inner.send_timeout(t, timeout)
749+
}
741750
}
742751

743752
#[stable(feature = "rust1", since = "1.0.0")]

library/std/src/sync/mpsc/sync_tests.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use crate::env;
3+
use crate::sync::mpmc::SendTimeoutError;
34
use crate::thread;
45
use crate::time::Duration;
56

@@ -41,6 +42,13 @@ fn recv_timeout() {
4142
assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Ok(1));
4243
}
4344

45+
#[test]
46+
fn send_timeout() {
47+
let (tx, _rx) = sync_channel::<i32>(1);
48+
assert_eq!(tx.send_timeout(1, Duration::from_millis(1)), Ok(()));
49+
assert_eq!(tx.send_timeout(1, Duration::from_millis(1)), Err(SendTimeoutError::Timeout(1)));
50+
}
51+
4452
#[test]
4553
fn smoke_threads() {
4654
let (tx, rx) = sync_channel::<i32>(0);

0 commit comments

Comments
 (0)