Skip to content

Commit

Permalink
sync: fix watch borrow_and_update (#3913)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn committed Jul 2, 2021
1 parent 08ed41f commit c8ecfc8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tokio/src/sync/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl<T> Receiver<T> {
/// [`changed`]: Receiver::changed
pub fn borrow_and_update(&mut self) -> Ref<'_, T> {
let inner = self.shared.value.read().unwrap();
self.version = self.shared.version.load(SeqCst);
self.version = self.shared.version.load(SeqCst) & !CLOSED;
Ref { inner }
}

Expand Down
17 changes: 17 additions & 0 deletions tokio/tests/sync_watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,20 @@ fn poll_close() {

assert!(tx.send("two").is_err());
}

#[test]
fn borrow_and_update() {
let (tx, mut rx) = watch::channel("one");

tx.send("two").unwrap();
assert_ready!(spawn(rx.changed()).poll()).unwrap();
assert_pending!(spawn(rx.changed()).poll());

tx.send("three").unwrap();
assert_eq!(*rx.borrow_and_update(), "three");
assert_pending!(spawn(rx.changed()).poll());

drop(tx);
assert_eq!(*rx.borrow_and_update(), "three");
assert_ready!(spawn(rx.changed()).poll()).unwrap_err();
}

0 comments on commit c8ecfc8

Please sign in to comment.