Auto merge of #34731 - GGist:fix_sync_try_recv, r=alexcrichton

Check for data in Receiver::try_recv before reporting disconnect

Fixes #34711

r? @alexcrichton
This commit is contained in:
bors 2016-07-10 00:22:33 -07:00 committed by GitHub
commit 46e7f4b8c5
2 changed files with 10 additions and 1 deletions

View File

@ -2180,6 +2180,15 @@ mod sync_tests {
assert!(rx.recv().is_err());
}
#[test]
fn oneshot_single_thread_try_recv_closed_with_data() {
let (tx, rx) = sync_channel::<i32>(1);
tx.send(10).unwrap();
drop(tx);
assert_eq!(rx.try_recv(), Ok(10));
assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected));
}
#[test]
fn oneshot_single_thread_peek_data() {
let (tx, rx) = sync_channel::<i32>(1);

View File

@ -309,7 +309,7 @@ impl<T> Packet<T> {
let mut guard = self.lock.lock().unwrap();
// Easy cases first
if guard.disconnected { return Err(Disconnected) }
if guard.disconnected && guard.buf.size() == 0 { return Err(Disconnected) }
if guard.buf.size() == 0 { return Err(Empty) }
// Be sure to wake up neighbors