Skip to content

Commit e8938d9

Browse files
bors[bot]Nemo157
andauthored
Merge #92
92: Test and fix flushing r=Nemo157 a=Nemo157 Tests and finishes fixing issues from #91 Co-authored-by: Wim Looman <[email protected]>
2 parents 0852f42 + 84f83a5 commit e8938d9

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

src/futures/write/generic/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<W: AsyncWrite, D: Decode> AsyncWrite for Decoder<W, D> {
151151

152152
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
153153
ready!(self.as_mut().do_poll_flush(cx))?;
154-
ready!(self.project().writer.as_mut().poll_close(cx))?;
154+
ready!(self.project().writer.as_mut().poll_flush(cx))?;
155155
Poll::Ready(Ok(()))
156156
}
157157

tests/utils/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![allow(dead_code, unused_macros)] // Different tests use a different subset of functions
22

3+
mod track_closed;
4+
35
use bytes::Bytes;
46
use futures::{
57
io::AsyncBufRead,
@@ -61,6 +63,7 @@ impl From<Vec<Vec<u8>>> for InputStream {
6163
}
6264

6365
pub mod prelude {
66+
use super::track_closed::TrackClosedExt as _;
6467
pub use async_compression::Level;
6568
pub use bytes::Bytes;
6669
pub use futures::{
@@ -109,13 +112,17 @@ pub mod prelude {
109112
{
110113
let mut test_writer = (&mut output)
111114
.limited_write(limit)
112-
.interleave_pending_write();
113-
let mut writer = create_writer(&mut test_writer);
114-
for chunk in input {
115-
block_on(writer.write_all(chunk)).unwrap();
116-
block_on(writer.flush()).unwrap();
115+
.interleave_pending_write()
116+
.track_closed();
117+
{
118+
let mut writer = create_writer(&mut test_writer);
119+
for chunk in input {
120+
block_on(writer.write_all(chunk)).unwrap();
121+
block_on(writer.flush()).unwrap();
122+
}
123+
block_on(writer.close()).unwrap();
117124
}
118-
block_on(writer.close()).unwrap();
125+
assert!(test_writer.is_closed());
119126
}
120127
output
121128
}

tests/utils/track_closed.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use core::{
2+
pin::Pin,
3+
task::{Context, Poll},
4+
};
5+
use futures::io::AsyncWrite;
6+
use std::io::{IoSlice, Result};
7+
8+
pub trait TrackClosedExt: AsyncWrite {
9+
fn track_closed(self) -> TrackClosed<Self>
10+
where
11+
Self: Sized + Unpin,
12+
{
13+
TrackClosed {
14+
inner: self,
15+
closed: false,
16+
}
17+
}
18+
}
19+
20+
impl<W: AsyncWrite> TrackClosedExt for W {}
21+
22+
pub struct TrackClosed<W: AsyncWrite + Unpin> {
23+
inner: W,
24+
closed: bool,
25+
}
26+
27+
impl<W: AsyncWrite + Unpin> TrackClosed<W> {
28+
pub fn is_closed(&self) -> bool {
29+
self.closed
30+
}
31+
}
32+
33+
impl<W: AsyncWrite + Unpin> AsyncWrite for TrackClosed<W> {
34+
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<Result<usize>> {
35+
assert!(!self.closed);
36+
Pin::new(&mut self.inner).poll_write(cx, buf)
37+
}
38+
39+
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>> {
40+
assert!(!self.closed);
41+
Pin::new(&mut self.inner).poll_flush(cx)
42+
}
43+
44+
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>> {
45+
assert!(!self.closed);
46+
match Pin::new(&mut self.inner).poll_close(cx) {
47+
Poll::Ready(Ok(())) => {
48+
self.closed = true;
49+
Poll::Ready(Ok(()))
50+
}
51+
other => other,
52+
}
53+
}
54+
55+
fn poll_write_vectored(
56+
mut self: Pin<&mut Self>,
57+
cx: &mut Context,
58+
bufs: &[IoSlice],
59+
) -> Poll<Result<usize>> {
60+
assert!(!self.closed);
61+
Pin::new(&mut self.inner).poll_write_vectored(cx, bufs)
62+
}
63+
}

0 commit comments

Comments
 (0)