Skip to content

Commit f18d6ed

Browse files
committed
make io_async_fd tests always drain written amount
1 parent 35c1465 commit f18d6ed

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

tokio/tests/io_async_fd.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,17 @@ fn socketpair() -> (FileDescriptor, FileDescriptor) {
135135
fds
136136
}
137137

138-
fn drain(mut fd: &FileDescriptor) {
138+
fn drain(mut fd: &FileDescriptor, mut amt: usize) {
139139
let mut buf = [0u8; 512];
140-
#[allow(clippy::unused_io_amount)]
141-
loop {
140+
while amt > 0 {
142141
match fd.read(&mut buf[..]) {
143-
Err(e) if e.kind() == ErrorKind::WouldBlock => break,
142+
Err(e) if e.kind() == ErrorKind::WouldBlock => {}
144143
Ok(0) => panic!("unexpected EOF"),
145144
Err(e) => panic!("unexpected error: {:?}", e),
146-
Ok(_) => continue,
145+
Ok(x) => {
146+
amt -= x;
147+
continue;
148+
}
147149
}
148150
}
149151
}
@@ -219,10 +221,10 @@ async fn reset_writable() {
219221
let mut guard = afd_a.writable().await.unwrap();
220222

221223
// Write until we get a WouldBlock. This also clears the ready state.
222-
while guard
223-
.try_io(|_| afd_a.get_ref().write(&[0; 512][..]))
224-
.is_ok()
225-
{}
224+
let mut bytes = 0;
225+
while let Ok(Ok(amt)) = guard.try_io(|_| afd_a.get_ref().write(&[0; 512][..])) {
226+
bytes += amt;
227+
}
226228

227229
// Writable state should be cleared now.
228230
let writable = afd_a.writable();
@@ -234,7 +236,7 @@ async fn reset_writable() {
234236
}
235237

236238
// Read from the other side; we should become writable now.
237-
drain(&b);
239+
drain(&b, bytes);
238240

239241
let _ = writable.await.unwrap();
240242
}
@@ -386,7 +388,10 @@ async fn poll_fns() {
386388
let afd_b = Arc::new(AsyncFd::new(b).unwrap());
387389

388390
// Fill up the write side of A
389-
while afd_a.get_ref().write(&[0; 512]).is_ok() {}
391+
let mut bytes = 0;
392+
while let Ok(amt) = afd_a.get_ref().write(&[0; 512]) {
393+
bytes += amt;
394+
}
390395

391396
let waker = TestWaker::new();
392397

@@ -446,7 +451,7 @@ async fn poll_fns() {
446451
}
447452

448453
// Make it writable now
449-
drain(afd_b.get_ref());
454+
drain(afd_b.get_ref(), bytes);
450455

451456
// now we should be writable (ie - the waker for poll_write should still be registered after we wake the read side)
452457
let _ = write_fut.await;

0 commit comments

Comments
 (0)