Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/cell/datacell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ where
/// Retrieves the data from the cell
pub fn get(&self) -> Result<P> {
let mut value = self.inner.value.lock();
if value.closed {
return Err(Error::ChannelClosed);
}
loop {
if value.closed {
return Err(Error::ChannelClosed);
}
if let Some(current) = value.current.take() {
return Ok(current);
}
Expand All @@ -109,10 +109,10 @@ where
/// Retrieves the data from the cell with the given timeout
pub fn get_timeout(&self, timeout: Duration) -> Result<P> {
let mut value = self.inner.value.lock();
if value.closed {
return Err(Error::ChannelClosed);
}
loop {
if value.closed {
return Err(Error::ChannelClosed);
}
if let Some(current) = value.current.take() {
return Ok(current);
}
Expand Down Expand Up @@ -180,6 +180,19 @@ mod test {
handle.join().unwrap();
}

#[test]
fn test_datacell_close_empty() {
let cell: DataCell<u8> = DataCell::new();
let cell2 = cell.clone();
let handle = thread::spawn(
move || cell2.get(), // blocks until the cell is closed
);
thread::sleep(Duration::from_millis(100));
cell.close();
assert!(matches!(handle.join().unwrap(), Err(Error::ChannelClosed)));
assert!(matches!(cell.get().unwrap_err(), Error::ChannelClosed));
}

#[test]
fn test_datacell_try_get() {
let cell: DataCell<_> = DataCell::new();
Expand Down
Loading