sync: don't panic in oneshot::try_recv (#3674)

This commit is contained in:
xd009642 2021-04-04 22:03:00 +04:00 committed by GitHub
parent b05b9a1788
commit f93bc9bad1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -672,7 +672,7 @@ impl<T> Receiver<T> {
return Err(TryRecvError::Empty);
}
} else {
panic!("called after complete");
Err(TryRecvError::Closed)
};
self.inner = None;

View File

@ -2,6 +2,7 @@
#![cfg(feature = "full")]
use tokio::sync::oneshot;
use tokio::sync::oneshot::error::TryRecvError;
use tokio_test::*;
use std::future::Future;
@ -190,6 +191,17 @@ fn close_after_recv() {
rx.close();
}
#[test]
fn try_recv_after_completion() {
let (tx, mut rx) = oneshot::channel::<i32>();
tx.send(17).unwrap();
assert_eq!(17, rx.try_recv().unwrap());
assert_eq!(Err(TryRecvError::Closed), rx.try_recv());
rx.close();
}
#[test]
fn drops_tasks() {
let (mut tx, mut rx) = oneshot::channel::<i32>();