1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]
// https://github.com/rust-lang/futures-rs/blob/1803948ff091b4eabf7f3bf39e16bbbdefca5cc8/futures/tests/io_buf_writer.rs
use futures::task::{Context, Poll};
use std::io::{self, Cursor};
use std::pin::Pin;
use tokio::io::{AsyncSeek, AsyncSeekExt, AsyncWrite, AsyncWriteExt, BufWriter, SeekFrom};
use futures::future;
use tokio_test::assert_ok;
use std::cmp;
use std::io::IoSlice;
mod support {
pub(crate) mod io_vec;
}
use support::io_vec::IoBufs;
struct MaybePending {
inner: Vec<u8>,
ready: bool,
}
impl MaybePending {
fn new(inner: Vec<u8>) -> Self {
Self {
inner,
ready: false,
}
}
}
impl AsyncWrite for MaybePending {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
if self.ready {
self.ready = false;
Pin::new(&mut self.inner).poll_write(cx, buf)
} else {
self.ready = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.inner).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.inner).poll_shutdown(cx)
}
}
async fn write_vectored<W>(writer: &mut W, bufs: &[IoSlice<'_>]) -> io::Result<usize>
where
W: AsyncWrite + Unpin,
{
let mut writer = Pin::new(writer);
future::poll_fn(|cx| writer.as_mut().poll_write_vectored(cx, bufs)).await
}
#[tokio::test]
async fn buf_writer() {
let mut writer = BufWriter::with_capacity(2, Vec::new());
writer.write(&[0, 1]).await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(*writer.get_ref(), [0, 1]);
writer.write(&[2]).await.unwrap();
assert_eq!(writer.buffer(), [2]);
assert_eq!(*writer.get_ref(), [0, 1]);
writer.write(&[3]).await.unwrap();
assert_eq!(writer.buffer(), [2, 3]);
assert_eq!(*writer.get_ref(), [0, 1]);
writer.flush().await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
writer.write(&[4]).await.unwrap();
writer.write(&[5]).await.unwrap();
assert_eq!(writer.buffer(), [4, 5]);
assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
writer.write(&[6]).await.unwrap();
assert_eq!(writer.buffer(), [6]);
assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]);
writer.write(&[7, 8]).await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]);
writer.write(&[9, 10, 11]).await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
writer.flush().await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
}
#[tokio::test]
async fn buf_writer_inner_flushes() {
let mut w = BufWriter::with_capacity(3, Vec::new());
w.write(&[0, 1]).await.unwrap();
assert_eq!(*w.get_ref(), []);
w.flush().await.unwrap();
let w = w.into_inner();
assert_eq!(w, [0, 1]);
}
#[tokio::test]
async fn buf_writer_seek() {
let mut w = BufWriter::with_capacity(3, Cursor::new(Vec::new()));
w.write_all(&[0, 1, 2, 3, 4, 5]).await.unwrap();
w.write_all(&[6, 7]).await.unwrap();
assert_eq!(w.seek(SeekFrom::Current(0)).await.unwrap(), 8);
assert_eq!(&w.get_ref().get_ref()[..], &[0, 1, 2, 3, 4, 5, 6, 7][..]);
assert_eq!(w.seek(SeekFrom::Start(2)).await.unwrap(), 2);
w.write_all(&[8, 9]).await.unwrap();
w.flush().await.unwrap();
assert_eq!(&w.into_inner().into_inner()[..], &[0, 1, 8, 9, 4, 5, 6, 7]);
}
#[tokio::test]
async fn maybe_pending_buf_writer() {
let mut writer = BufWriter::with_capacity(2, MaybePending::new(Vec::new()));
writer.write(&[0, 1]).await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(&writer.get_ref().inner, &[0, 1]);
writer.write(&[2]).await.unwrap();
assert_eq!(writer.buffer(), [2]);
assert_eq!(&writer.get_ref().inner, &[0, 1]);
writer.write(&[3]).await.unwrap();
assert_eq!(writer.buffer(), [2, 3]);
assert_eq!(&writer.get_ref().inner, &[0, 1]);
writer.flush().await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(&writer.get_ref().inner, &[0, 1, 2, 3]);
writer.write(&[4]).await.unwrap();
writer.write(&[5]).await.unwrap();
assert_eq!(writer.buffer(), [4, 5]);
assert_eq!(&writer.get_ref().inner, &[0, 1, 2, 3]);
writer.write(&[6]).await.unwrap();
assert_eq!(writer.buffer(), [6]);
assert_eq!(writer.get_ref().inner, &[0, 1, 2, 3, 4, 5]);
writer.write(&[7, 8]).await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(writer.get_ref().inner, &[0, 1, 2, 3, 4, 5, 6, 7, 8]);
writer.write(&[9, 10, 11]).await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(
writer.get_ref().inner,
&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
);
writer.flush().await.unwrap();
assert_eq!(writer.buffer(), []);
assert_eq!(
&writer.get_ref().inner,
&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
);
}
#[tokio::test]
async fn maybe_pending_buf_writer_inner_flushes() {
let mut w = BufWriter::with_capacity(3, MaybePending::new(Vec::new()));
w.write(&[0, 1]).await.unwrap();
assert_eq!(&w.get_ref().inner, &[]);
w.flush().await.unwrap();
let w = w.into_inner().inner;
assert_eq!(w, [0, 1]);
}
#[tokio::test]
async fn maybe_pending_buf_writer_seek() {
struct MaybePendingSeek {
inner: Cursor<Vec<u8>>,
ready_write: bool,
ready_seek: bool,
seek_res: Option<io::Result<()>>,
}
impl MaybePendingSeek {
fn new(inner: Vec<u8>) -> Self {
Self {
inner: Cursor::new(inner),
ready_write: false,
ready_seek: false,
seek_res: None,
}
}
}
impl AsyncWrite for MaybePendingSeek {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
if self.ready_write {
self.ready_write = false;
Pin::new(&mut self.inner).poll_write(cx, buf)
} else {
self.ready_write = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.inner).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.inner).poll_shutdown(cx)
}
}
impl AsyncSeek for MaybePendingSeek {
fn start_seek(mut self: Pin<&mut Self>, pos: SeekFrom) -> io::Result<()> {
self.seek_res = Some(Pin::new(&mut self.inner).start_seek(pos));
Ok(())
}
fn poll_complete(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
if self.ready_seek {
self.ready_seek = false;
self.seek_res.take().unwrap_or(Ok(()))?;
Pin::new(&mut self.inner).poll_complete(cx)
} else {
self.ready_seek = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
let mut w = BufWriter::with_capacity(3, MaybePendingSeek::new(Vec::new()));
w.write_all(&[0, 1, 2, 3, 4, 5]).await.unwrap();
w.write_all(&[6, 7]).await.unwrap();
assert_eq!(w.seek(SeekFrom::Current(0)).await.unwrap(), 8);
assert_eq!(
&w.get_ref().inner.get_ref()[..],
&[0, 1, 2, 3, 4, 5, 6, 7][..]
);
assert_eq!(w.seek(SeekFrom::Start(2)).await.unwrap(), 2);
w.write_all(&[8, 9]).await.unwrap();
w.flush().await.unwrap();
assert_eq!(
&w.into_inner().inner.into_inner()[..],
&[0, 1, 8, 9, 4, 5, 6, 7]
);
}
struct MockWriter {
data: Vec<u8>,
write_len: usize,
vectored: bool,
}
impl MockWriter {
fn new(write_len: usize) -> Self {
MockWriter {
data: Vec::new(),
write_len,
vectored: false,
}
}
fn vectored(write_len: usize) -> Self {
MockWriter {
data: Vec::new(),
write_len,
vectored: true,
}
}
fn write_up_to(&mut self, buf: &[u8], limit: usize) -> usize {
let len = cmp::min(buf.len(), limit);
self.data.extend_from_slice(&buf[..len]);
len
}
}
impl AsyncWrite for MockWriter {
fn poll_write(
self: Pin<&mut Self>,
_: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
let this = self.get_mut();
let n = this.write_up_to(buf, this.write_len);
Ok(n).into()
}
fn poll_write_vectored(
self: Pin<&mut Self>,
_: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<Result<usize, io::Error>> {
let this = self.get_mut();
let mut total_written = 0;
for buf in bufs {
let n = this.write_up_to(buf, this.write_len - total_written);
total_written += n;
if total_written == this.write_len {
break;
}
}
Ok(total_written).into()
}
fn is_write_vectored(&self) -> bool {
self.vectored
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Ok(()).into()
}
fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Ok(()).into()
}
}
#[tokio::test]
async fn write_vectored_empty_on_non_vectored() {
let mut w = BufWriter::new(MockWriter::new(4));
let n = assert_ok!(write_vectored(&mut w, &[]).await);
assert_eq!(n, 0);
let io_vec = [IoSlice::new(&[]); 3];
let n = assert_ok!(write_vectored(&mut w, &io_vec).await);
assert_eq!(n, 0);
assert_ok!(w.flush().await);
assert!(w.get_ref().data.is_empty());
}
#[tokio::test]
async fn write_vectored_empty_on_vectored() {
let mut w = BufWriter::new(MockWriter::vectored(4));
let n = assert_ok!(write_vectored(&mut w, &[]).await);
assert_eq!(n, 0);
let io_vec = [IoSlice::new(&[]); 3];
let n = assert_ok!(write_vectored(&mut w, &io_vec).await);
assert_eq!(n, 0);
assert_ok!(w.flush().await);
assert!(w.get_ref().data.is_empty());
}
#[tokio::test]
async fn write_vectored_basic_on_non_vectored() {
let msg = b"foo bar baz";
let bufs = [
IoSlice::new(&msg[0..4]),
IoSlice::new(&msg[4..8]),
IoSlice::new(&msg[8..]),
];
let mut w = BufWriter::new(MockWriter::new(4));
let n = assert_ok!(write_vectored(&mut w, &bufs).await);
assert_eq!(n, msg.len());
assert!(w.buffer() == &msg[..]);
assert_ok!(w.flush().await);
assert_eq!(w.get_ref().data, msg);
}
#[tokio::test]
async fn write_vectored_basic_on_vectored() {
let msg = b"foo bar baz";
let bufs = [
IoSlice::new(&msg[0..4]),
IoSlice::new(&msg[4..8]),
IoSlice::new(&msg[8..]),
];
let mut w = BufWriter::new(MockWriter::vectored(4));
let n = assert_ok!(write_vectored(&mut w, &bufs).await);
assert_eq!(n, msg.len());
assert!(w.buffer() == &msg[..]);
assert_ok!(w.flush().await);
assert_eq!(w.get_ref().data, msg);
}
#[tokio::test]
async fn write_vectored_large_total_on_non_vectored() {
let msg = b"foo bar baz";
let mut bufs = [
IoSlice::new(&msg[0..4]),
IoSlice::new(&msg[4..8]),
IoSlice::new(&msg[8..]),
];
let io_vec = IoBufs::new(&mut bufs);
let mut w = BufWriter::with_capacity(8, MockWriter::new(4));
let n = assert_ok!(write_vectored(&mut w, &io_vec).await);
assert_eq!(n, 8);
assert!(w.buffer() == &msg[..8]);
let io_vec = io_vec.advance(n);
let n = assert_ok!(write_vectored(&mut w, &io_vec).await);
assert_eq!(n, 3);
assert!(w.get_ref().data.as_slice() == &msg[..8]);
assert!(w.buffer() == &msg[8..]);
}
#[tokio::test]
async fn write_vectored_large_total_on_vectored() {
let msg = b"foo bar baz";
let mut bufs = [
IoSlice::new(&msg[0..4]),
IoSlice::new(&msg[4..8]),
IoSlice::new(&msg[8..]),
];
let io_vec = IoBufs::new(&mut bufs);
let mut w = BufWriter::with_capacity(8, MockWriter::vectored(10));
let n = assert_ok!(write_vectored(&mut w, &io_vec).await);
assert_eq!(n, 10);
assert!(w.buffer().is_empty());
let io_vec = io_vec.advance(n);
let n = assert_ok!(write_vectored(&mut w, &io_vec).await);
assert_eq!(n, 1);
assert!(w.get_ref().data.as_slice() == &msg[..10]);
assert!(w.buffer() == &msg[10..]);
}
struct VectoredWriteHarness {
writer: BufWriter<MockWriter>,
buf_capacity: usize,
}
impl VectoredWriteHarness {
fn new(buf_capacity: usize) -> Self {
VectoredWriteHarness {
writer: BufWriter::with_capacity(buf_capacity, MockWriter::new(4)),
buf_capacity,
}
}
fn with_vectored_backend(buf_capacity: usize) -> Self {
VectoredWriteHarness {
writer: BufWriter::with_capacity(buf_capacity, MockWriter::vectored(4)),
buf_capacity,
}
}
async fn write_all<'a, 'b>(&mut self, mut io_vec: IoBufs<'a, 'b>) -> usize {
let mut total_written = 0;
while !io_vec.is_empty() {
let n = assert_ok!(write_vectored(&mut self.writer, &io_vec).await);
assert!(n != 0);
assert!(self.writer.buffer().len() <= self.buf_capacity);
total_written += n;
io_vec = io_vec.advance(n);
}
total_written
}
async fn flush(&mut self) -> &[u8] {
assert_ok!(self.writer.flush().await);
&self.writer.get_ref().data
}
}
#[tokio::test]
async fn write_vectored_odd_on_non_vectored() {
let msg = b"foo bar baz";
let mut bufs = [
IoSlice::new(&msg[0..4]),
IoSlice::new(&[]),
IoSlice::new(&msg[4..9]),
IoSlice::new(&msg[9..]),
];
let mut h = VectoredWriteHarness::new(8);
let bytes_written = h.write_all(IoBufs::new(&mut bufs)).await;
assert_eq!(bytes_written, msg.len());
assert_eq!(h.flush().await, msg);
}
#[tokio::test]
async fn write_vectored_odd_on_vectored() {
let msg = b"foo bar baz";
let mut bufs = [
IoSlice::new(&msg[0..4]),
IoSlice::new(&[]),
IoSlice::new(&msg[4..9]),
IoSlice::new(&msg[9..]),
];
let mut h = VectoredWriteHarness::with_vectored_backend(8);
let bytes_written = h.write_all(IoBufs::new(&mut bufs)).await;
assert_eq!(bytes_written, msg.len());
assert_eq!(h.flush().await, msg);
}
#[tokio::test]
async fn write_vectored_large_slice_on_non_vectored() {
let msg = b"foo bar baz";
let mut bufs = [
IoSlice::new(&[]),
IoSlice::new(&msg[..9]),
IoSlice::new(&msg[9..]),
];
let mut h = VectoredWriteHarness::new(8);
let bytes_written = h.write_all(IoBufs::new(&mut bufs)).await;
assert_eq!(bytes_written, msg.len());
assert_eq!(h.flush().await, msg);
}
#[tokio::test]
async fn write_vectored_large_slice_on_vectored() {
let msg = b"foo bar baz";
let mut bufs = [
IoSlice::new(&[]),
IoSlice::new(&msg[..9]),
IoSlice::new(&msg[9..]),
];
let mut h = VectoredWriteHarness::with_vectored_backend(8);
let bytes_written = h.write_all(IoBufs::new(&mut bufs)).await;
assert_eq!(bytes_written, msg.len());
assert_eq!(h.flush().await, msg);
}
|