1use super::*;
2use crate::{
3 fs::mocks::*,
4 io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
5};
6use mockall::{predicate::eq, Sequence};
7use tokio_test::{assert_pending, assert_ready_err, assert_ready_ok, task};
8
9const HELLO: &[u8] = b"hello world...";
10const FOO: &[u8] = b"foo bar baz...";
11
12#[test]
13fn open_read() {
14 let mut file = MockFile::default();
15 file.expect_inner_read().once().returning(|buf| {
16 buf[0..HELLO.len()].copy_from_slice(HELLO);
17 Ok(HELLO.len())
18 });
19 let mut file = File::from_std(file);
20
21 let mut buf = [0; 1024];
22 let mut t = task::spawn(file.read(&mut buf));
23
24 assert_eq!(0, pool::len());
25 assert_pending!(t.poll());
26
27 assert_eq!(1, pool::len());
28
29 pool::run_one();
30
31 assert!(t.is_woken());
32
33 let n = assert_ready_ok!(t.poll());
34 assert_eq!(n, HELLO.len());
35 assert_eq!(&buf[..n], HELLO);
36}
37
38#[test]
39fn read_twice_before_dispatch() {
40 let mut file = MockFile::default();
41 file.expect_inner_read().once().returning(|buf| {
42 buf[0..HELLO.len()].copy_from_slice(HELLO);
43 Ok(HELLO.len())
44 });
45 let mut file = File::from_std(file);
46
47 let mut buf = [0; 1024];
48 let mut t = task::spawn(file.read(&mut buf));
49
50 assert_pending!(t.poll());
51 assert_pending!(t.poll());
52
53 assert_eq!(pool::len(), 1);
54 pool::run_one();
55
56 assert!(t.is_woken());
57
58 let n = assert_ready_ok!(t.poll());
59 assert_eq!(&buf[..n], HELLO);
60}
61
62#[test]
63fn read_with_smaller_buf() {
64 let mut file = MockFile::default();
65 file.expect_inner_read().once().returning(|buf| {
66 buf[0..HELLO.len()].copy_from_slice(HELLO);
67 Ok(HELLO.len())
68 });
69
70 let mut file = File::from_std(file);
71
72 {
73 let mut buf = [0; 32];
74 let mut t = task::spawn(file.read(&mut buf));
75 assert_pending!(t.poll());
76 }
77
78 pool::run_one();
79
80 {
81 let mut buf = [0; 4];
82 let mut t = task::spawn(file.read(&mut buf));
83 let n = assert_ready_ok!(t.poll());
84 assert_eq!(n, 4);
85 assert_eq!(&buf[..], &HELLO[..n]);
86 }
87
88 // Calling again immediately succeeds with the rest of the buffer
89 let mut buf = [0; 32];
90 let mut t = task::spawn(file.read(&mut buf));
91 let n = assert_ready_ok!(t.poll());
92 assert_eq!(n, 10);
93 assert_eq!(&buf[..n], &HELLO[4..]);
94
95 assert_eq!(0, pool::len());
96}
97
98#[test]
99fn read_with_bigger_buf() {
100 let mut seq = Sequence::new();
101 let mut file = MockFile::default();
102 file.expect_inner_read()
103 .once()
104 .in_sequence(&mut seq)
105 .returning(|buf| {
106 buf[0..4].copy_from_slice(&HELLO[..4]);
107 Ok(4)
108 });
109 file.expect_inner_read()
110 .once()
111 .in_sequence(&mut seq)
112 .returning(|buf| {
113 buf[0..HELLO.len() - 4].copy_from_slice(&HELLO[4..]);
114 Ok(HELLO.len() - 4)
115 });
116
117 let mut file = File::from_std(file);
118
119 {
120 let mut buf = [0; 4];
121 let mut t = task::spawn(file.read(&mut buf));
122 assert_pending!(t.poll());
123 }
124
125 pool::run_one();
126
127 {
128 let mut buf = [0; 32];
129 let mut t = task::spawn(file.read(&mut buf));
130 let n = assert_ready_ok!(t.poll());
131 assert_eq!(n, 4);
132 assert_eq!(&buf[..n], &HELLO[..n]);
133 }
134
135 // Calling again immediately succeeds with the rest of the buffer
136 let mut buf = [0; 32];
137 let mut t = task::spawn(file.read(&mut buf));
138
139 assert_pending!(t.poll());
140
141 assert_eq!(1, pool::len());
142 pool::run_one();
143
144 assert!(t.is_woken());
145
146 let n = assert_ready_ok!(t.poll());
147 assert_eq!(n, 10);
148 assert_eq!(&buf[..n], &HELLO[4..]);
149
150 assert_eq!(0, pool::len());
151}
152
153#[test]
154fn read_err_then_read_success() {
155 let mut file = MockFile::default();
156 let mut seq = Sequence::new();
157 file.expect_inner_read()
158 .once()
159 .in_sequence(&mut seq)
160 .returning(|_| Err(io::ErrorKind::Other.into()));
161 file.expect_inner_read()
162 .once()
163 .in_sequence(&mut seq)
164 .returning(|buf| {
165 buf[0..HELLO.len()].copy_from_slice(HELLO);
166 Ok(HELLO.len())
167 });
168
169 let mut file = File::from_std(file);
170
171 {
172 let mut buf = [0; 32];
173 let mut t = task::spawn(file.read(&mut buf));
174 assert_pending!(t.poll());
175
176 pool::run_one();
177
178 assert_ready_err!(t.poll());
179 }
180
181 {
182 let mut buf = [0; 32];
183 let mut t = task::spawn(file.read(&mut buf));
184 assert_pending!(t.poll());
185
186 pool::run_one();
187
188 let n = assert_ready_ok!(t.poll());
189
190 assert_eq!(n, HELLO.len());
191 assert_eq!(&buf[..n], HELLO);
192 }
193}
194
195#[test]
196fn open_write() {
197 let mut file = MockFile::default();
198 file.expect_inner_write()
199 .once()
200 .with(eq(HELLO))
201 .returning(|buf| Ok(buf.len()));
202
203 let mut file = File::from_std(file);
204
205 let mut t = task::spawn(file.write(HELLO));
206
207 assert_eq!(0, pool::len());
208 assert_ready_ok!(t.poll());
209
210 assert_eq!(1, pool::len());
211
212 pool::run_one();
213
214 assert!(!t.is_woken());
215
216 let mut t = task::spawn(file.flush());
217 assert_ready_ok!(t.poll());
218}
219
220#[test]
221fn flush_while_idle() {
222 let file = MockFile::default();
223
224 let mut file = File::from_std(file);
225
226 let mut t = task::spawn(file.flush());
227 assert_ready_ok!(t.poll());
228}
229
230#[test]
231#[cfg_attr(miri, ignore)] // takes a really long time with miri
232fn read_with_buffer_larger_than_max() {
233 // Chunks
234 let chunk_a = crate::io::blocking::MAX_BUF;
235 let chunk_b = chunk_a * 2;
236 let chunk_c = chunk_a * 3;
237 let chunk_d = chunk_a * 4;
238
239 assert_eq!(chunk_d / 1024 / 1024, 8);
240
241 let mut data = vec![];
242 for i in 0..(chunk_d - 1) {
243 data.push((i % 151) as u8);
244 }
245 let data = Arc::new(data);
246 let d0 = data.clone();
247 let d1 = data.clone();
248 let d2 = data.clone();
249 let d3 = data.clone();
250
251 let mut seq = Sequence::new();
252 let mut file = MockFile::default();
253 file.expect_inner_read()
254 .once()
255 .in_sequence(&mut seq)
256 .returning(move |buf| {
257 buf[0..chunk_a].copy_from_slice(&d0[0..chunk_a]);
258 Ok(chunk_a)
259 });
260 file.expect_inner_read()
261 .once()
262 .in_sequence(&mut seq)
263 .returning(move |buf| {
264 buf[..chunk_a].copy_from_slice(&d1[chunk_a..chunk_b]);
265 Ok(chunk_b - chunk_a)
266 });
267 file.expect_inner_read()
268 .once()
269 .in_sequence(&mut seq)
270 .returning(move |buf| {
271 buf[..chunk_a].copy_from_slice(&d2[chunk_b..chunk_c]);
272 Ok(chunk_c - chunk_b)
273 });
274 file.expect_inner_read()
275 .once()
276 .in_sequence(&mut seq)
277 .returning(move |buf| {
278 buf[..chunk_a - 1].copy_from_slice(&d3[chunk_c..]);
279 Ok(chunk_a - 1)
280 });
281 let mut file = File::from_std(file);
282
283 let mut actual = vec![0; chunk_d];
284 let mut pos = 0;
285
286 while pos < data.len() {
287 let mut t = task::spawn(file.read(&mut actual[pos..]));
288
289 assert_pending!(t.poll());
290 pool::run_one();
291 assert!(t.is_woken());
292
293 let n = assert_ready_ok!(t.poll());
294 assert!(n <= chunk_a);
295
296 pos += n;
297 }
298
299 assert_eq!(&data[..], &actual[..data.len()]);
300}
301
302#[test]
303#[cfg_attr(miri, ignore)] // takes a really long time with miri
304fn write_with_buffer_larger_than_max() {
305 // Chunks
306 let chunk_a = crate::io::blocking::MAX_BUF;
307 let chunk_b = chunk_a * 2;
308 let chunk_c = chunk_a * 3;
309 let chunk_d = chunk_a * 4;
310
311 assert_eq!(chunk_d / 1024 / 1024, 8);
312
313 let mut data = vec![];
314 for i in 0..(chunk_d - 1) {
315 data.push((i % 151) as u8);
316 }
317 let data = Arc::new(data);
318 let d0 = data.clone();
319 let d1 = data.clone();
320 let d2 = data.clone();
321 let d3 = data.clone();
322
323 let mut file = MockFile::default();
324 let mut seq = Sequence::new();
325 file.expect_inner_write()
326 .once()
327 .in_sequence(&mut seq)
328 .withf(move |buf| buf == &d0[0..chunk_a])
329 .returning(|buf| Ok(buf.len()));
330 file.expect_inner_write()
331 .once()
332 .in_sequence(&mut seq)
333 .withf(move |buf| buf == &d1[chunk_a..chunk_b])
334 .returning(|buf| Ok(buf.len()));
335 file.expect_inner_write()
336 .once()
337 .in_sequence(&mut seq)
338 .withf(move |buf| buf == &d2[chunk_b..chunk_c])
339 .returning(|buf| Ok(buf.len()));
340 file.expect_inner_write()
341 .once()
342 .in_sequence(&mut seq)
343 .withf(move |buf| buf == &d3[chunk_c..chunk_d - 1])
344 .returning(|buf| Ok(buf.len()));
345
346 let mut file = File::from_std(file);
347
348 let mut rem = &data[..];
349
350 let mut first = true;
351
352 while !rem.is_empty() {
353 let mut task = task::spawn(file.write(rem));
354
355 if !first {
356 assert_pending!(task.poll());
357 pool::run_one();
358 assert!(task.is_woken());
359 }
360
361 first = false;
362
363 let n = assert_ready_ok!(task.poll());
364
365 rem = &rem[n..];
366 }
367
368 pool::run_one();
369}
370
371#[test]
372fn write_twice_before_dispatch() {
373 let mut file = MockFile::default();
374 let mut seq = Sequence::new();
375 file.expect_inner_write()
376 .once()
377 .in_sequence(&mut seq)
378 .with(eq(HELLO))
379 .returning(|buf| Ok(buf.len()));
380 file.expect_inner_write()
381 .once()
382 .in_sequence(&mut seq)
383 .with(eq(FOO))
384 .returning(|buf| Ok(buf.len()));
385
386 let mut file = File::from_std(file);
387
388 let mut t = task::spawn(file.write(HELLO));
389 assert_ready_ok!(t.poll());
390
391 let mut t = task::spawn(file.write(FOO));
392 assert_pending!(t.poll());
393
394 assert_eq!(pool::len(), 1);
395 pool::run_one();
396
397 assert!(t.is_woken());
398
399 assert_ready_ok!(t.poll());
400
401 let mut t = task::spawn(file.flush());
402 assert_pending!(t.poll());
403
404 assert_eq!(pool::len(), 1);
405 pool::run_one();
406
407 assert!(t.is_woken());
408 assert_ready_ok!(t.poll());
409}
410
411#[test]
412fn incomplete_read_followed_by_write() {
413 let mut file = MockFile::default();
414 let mut seq = Sequence::new();
415 file.expect_inner_read()
416 .once()
417 .in_sequence(&mut seq)
418 .returning(|buf| {
419 buf[0..HELLO.len()].copy_from_slice(HELLO);
420 Ok(HELLO.len())
421 });
422 file.expect_inner_seek()
423 .once()
424 .with(eq(SeekFrom::Current(-(HELLO.len() as i64))))
425 .in_sequence(&mut seq)
426 .returning(|_| Ok(0));
427 file.expect_inner_write()
428 .once()
429 .with(eq(FOO))
430 .returning(|_| Ok(FOO.len()));
431
432 let mut file = File::from_std(file);
433
434 let mut buf = [0; 32];
435
436 let mut t = task::spawn(file.read(&mut buf));
437 assert_pending!(t.poll());
438
439 pool::run_one();
440
441 let mut t = task::spawn(file.write(FOO));
442 assert_ready_ok!(t.poll());
443
444 assert_eq!(pool::len(), 1);
445 pool::run_one();
446
447 let mut t = task::spawn(file.flush());
448 assert_ready_ok!(t.poll());
449}
450
451#[test]
452fn incomplete_partial_read_followed_by_write() {
453 let mut file = MockFile::default();
454 let mut seq = Sequence::new();
455 file.expect_inner_read()
456 .once()
457 .in_sequence(&mut seq)
458 .returning(|buf| {
459 buf[0..HELLO.len()].copy_from_slice(HELLO);
460 Ok(HELLO.len())
461 });
462 file.expect_inner_seek()
463 .once()
464 .in_sequence(&mut seq)
465 .with(eq(SeekFrom::Current(-10)))
466 .returning(|_| Ok(0));
467 file.expect_inner_write()
468 .once()
469 .in_sequence(&mut seq)
470 .with(eq(FOO))
471 .returning(|_| Ok(FOO.len()));
472
473 let mut file = File::from_std(file);
474
475 let mut buf = [0; 32];
476 let mut t = task::spawn(file.read(&mut buf));
477 assert_pending!(t.poll());
478
479 pool::run_one();
480
481 let mut buf = [0; 4];
482 let mut t = task::spawn(file.read(&mut buf));
483 assert_ready_ok!(t.poll());
484
485 let mut t = task::spawn(file.write(FOO));
486 assert_ready_ok!(t.poll());
487
488 assert_eq!(pool::len(), 1);
489 pool::run_one();
490
491 let mut t = task::spawn(file.flush());
492 assert_ready_ok!(t.poll());
493}
494
495#[test]
496fn incomplete_read_followed_by_flush() {
497 let mut file = MockFile::default();
498 let mut seq = Sequence::new();
499 file.expect_inner_read()
500 .once()
501 .in_sequence(&mut seq)
502 .returning(|buf| {
503 buf[0..HELLO.len()].copy_from_slice(HELLO);
504 Ok(HELLO.len())
505 });
506 file.expect_inner_seek()
507 .once()
508 .in_sequence(&mut seq)
509 .with(eq(SeekFrom::Current(-(HELLO.len() as i64))))
510 .returning(|_| Ok(0));
511 file.expect_inner_write()
512 .once()
513 .in_sequence(&mut seq)
514 .with(eq(FOO))
515 .returning(|_| Ok(FOO.len()));
516
517 let mut file = File::from_std(file);
518
519 let mut buf = [0; 32];
520
521 let mut t = task::spawn(file.read(&mut buf));
522 assert_pending!(t.poll());
523
524 pool::run_one();
525
526 let mut t = task::spawn(file.flush());
527 assert_ready_ok!(t.poll());
528
529 let mut t = task::spawn(file.write(FOO));
530 assert_ready_ok!(t.poll());
531
532 pool::run_one();
533}
534
535#[test]
536fn incomplete_flush_followed_by_write() {
537 let mut file = MockFile::default();
538 let mut seq = Sequence::new();
539 file.expect_inner_write()
540 .once()
541 .in_sequence(&mut seq)
542 .with(eq(HELLO))
543 .returning(|_| Ok(HELLO.len()));
544 file.expect_inner_write()
545 .once()
546 .in_sequence(&mut seq)
547 .with(eq(FOO))
548 .returning(|_| Ok(FOO.len()));
549
550 let mut file = File::from_std(file);
551
552 let mut t = task::spawn(file.write(HELLO));
553 let n = assert_ready_ok!(t.poll());
554 assert_eq!(n, HELLO.len());
555
556 let mut t = task::spawn(file.flush());
557 assert_pending!(t.poll());
558
559 // TODO: Move under write
560 pool::run_one();
561
562 let mut t = task::spawn(file.write(FOO));
563 assert_ready_ok!(t.poll());
564
565 pool::run_one();
566
567 let mut t = task::spawn(file.flush());
568 assert_ready_ok!(t.poll());
569}
570
571#[test]
572fn read_err() {
573 let mut file = MockFile::default();
574 file.expect_inner_read()
575 .once()
576 .returning(|_| Err(io::ErrorKind::Other.into()));
577
578 let mut file = File::from_std(file);
579
580 let mut buf = [0; 1024];
581 let mut t = task::spawn(file.read(&mut buf));
582
583 assert_pending!(t.poll());
584
585 pool::run_one();
586 assert!(t.is_woken());
587
588 assert_ready_err!(t.poll());
589}
590
591#[test]
592fn write_write_err() {
593 let mut file = MockFile::default();
594 file.expect_inner_write()
595 .once()
596 .returning(|_| Err(io::ErrorKind::Other.into()));
597
598 let mut file = File::from_std(file);
599
600 let mut t = task::spawn(file.write(HELLO));
601 assert_ready_ok!(t.poll());
602
603 pool::run_one();
604
605 let mut t = task::spawn(file.write(FOO));
606 assert_ready_err!(t.poll());
607}
608
609#[test]
610fn write_read_write_err() {
611 let mut file = MockFile::default();
612 let mut seq = Sequence::new();
613 file.expect_inner_write()
614 .once()
615 .in_sequence(&mut seq)
616 .returning(|_| Err(io::ErrorKind::Other.into()));
617 file.expect_inner_read()
618 .once()
619 .in_sequence(&mut seq)
620 .returning(|buf| {
621 buf[0..HELLO.len()].copy_from_slice(HELLO);
622 Ok(HELLO.len())
623 });
624
625 let mut file = File::from_std(file);
626
627 let mut t = task::spawn(file.write(HELLO));
628 assert_ready_ok!(t.poll());
629
630 pool::run_one();
631
632 let mut buf = [0; 1024];
633 let mut t = task::spawn(file.read(&mut buf));
634
635 assert_pending!(t.poll());
636
637 pool::run_one();
638
639 let mut t = task::spawn(file.write(FOO));
640 assert_ready_err!(t.poll());
641}
642
643#[test]
644fn write_read_flush_err() {
645 let mut file = MockFile::default();
646 let mut seq = Sequence::new();
647 file.expect_inner_write()
648 .once()
649 .in_sequence(&mut seq)
650 .returning(|_| Err(io::ErrorKind::Other.into()));
651 file.expect_inner_read()
652 .once()
653 .in_sequence(&mut seq)
654 .returning(|buf| {
655 buf[0..HELLO.len()].copy_from_slice(HELLO);
656 Ok(HELLO.len())
657 });
658
659 let mut file = File::from_std(file);
660
661 let mut t = task::spawn(file.write(HELLO));
662 assert_ready_ok!(t.poll());
663
664 pool::run_one();
665
666 let mut buf = [0; 1024];
667 let mut t = task::spawn(file.read(&mut buf));
668
669 assert_pending!(t.poll());
670
671 pool::run_one();
672
673 let mut t = task::spawn(file.flush());
674 assert_ready_err!(t.poll());
675}
676
677#[test]
678fn write_seek_write_err() {
679 let mut file = MockFile::default();
680 let mut seq = Sequence::new();
681 file.expect_inner_write()
682 .once()
683 .in_sequence(&mut seq)
684 .returning(|_| Err(io::ErrorKind::Other.into()));
685 file.expect_inner_seek()
686 .once()
687 .with(eq(SeekFrom::Start(0)))
688 .in_sequence(&mut seq)
689 .returning(|_| Ok(0));
690
691 let mut file = File::from_std(file);
692
693 let mut t = task::spawn(file.write(HELLO));
694 assert_ready_ok!(t.poll());
695
696 pool::run_one();
697
698 {
699 let mut t = task::spawn(file.seek(SeekFrom::Start(0)));
700 assert_pending!(t.poll());
701 }
702
703 pool::run_one();
704
705 let mut t = task::spawn(file.write(FOO));
706 assert_ready_err!(t.poll());
707}
708
709#[test]
710fn write_seek_flush_err() {
711 let mut file = MockFile::default();
712 let mut seq = Sequence::new();
713 file.expect_inner_write()
714 .once()
715 .in_sequence(&mut seq)
716 .returning(|_| Err(io::ErrorKind::Other.into()));
717 file.expect_inner_seek()
718 .once()
719 .with(eq(SeekFrom::Start(0)))
720 .in_sequence(&mut seq)
721 .returning(|_| Ok(0));
722
723 let mut file = File::from_std(file);
724
725 let mut t = task::spawn(file.write(HELLO));
726 assert_ready_ok!(t.poll());
727
728 pool::run_one();
729
730 {
731 let mut t = task::spawn(file.seek(SeekFrom::Start(0)));
732 assert_pending!(t.poll());
733 }
734
735 pool::run_one();
736
737 let mut t = task::spawn(file.flush());
738 assert_ready_err!(t.poll());
739}
740
741#[test]
742fn sync_all_ordered_after_write() {
743 let mut file = MockFile::default();
744 let mut seq = Sequence::new();
745 file.expect_inner_write()
746 .once()
747 .in_sequence(&mut seq)
748 .with(eq(HELLO))
749 .returning(|_| Ok(HELLO.len()));
750 file.expect_sync_all().once().returning(|| Ok(()));
751
752 let mut file = File::from_std(file);
753 let mut t = task::spawn(file.write(HELLO));
754 assert_ready_ok!(t.poll());
755
756 let mut t = task::spawn(file.sync_all());
757 assert_pending!(t.poll());
758
759 assert_eq!(1, pool::len());
760 pool::run_one();
761
762 assert!(t.is_woken());
763 assert_pending!(t.poll());
764
765 assert_eq!(1, pool::len());
766 pool::run_one();
767
768 assert!(t.is_woken());
769 assert_ready_ok!(t.poll());
770}
771
772#[test]
773fn sync_all_err_ordered_after_write() {
774 let mut file = MockFile::default();
775 let mut seq = Sequence::new();
776 file.expect_inner_write()
777 .once()
778 .in_sequence(&mut seq)
779 .with(eq(HELLO))
780 .returning(|_| Ok(HELLO.len()));
781 file.expect_sync_all()
782 .once()
783 .returning(|| Err(io::ErrorKind::Other.into()));
784
785 let mut file = File::from_std(file);
786 let mut t = task::spawn(file.write(HELLO));
787 assert_ready_ok!(t.poll());
788
789 let mut t = task::spawn(file.sync_all());
790 assert_pending!(t.poll());
791
792 assert_eq!(1, pool::len());
793 pool::run_one();
794
795 assert!(t.is_woken());
796 assert_pending!(t.poll());
797
798 assert_eq!(1, pool::len());
799 pool::run_one();
800
801 assert!(t.is_woken());
802 assert_ready_err!(t.poll());
803}
804
805#[test]
806fn sync_data_ordered_after_write() {
807 let mut file = MockFile::default();
808 let mut seq = Sequence::new();
809 file.expect_inner_write()
810 .once()
811 .in_sequence(&mut seq)
812 .with(eq(HELLO))
813 .returning(|_| Ok(HELLO.len()));
814 file.expect_sync_data().once().returning(|| Ok(()));
815
816 let mut file = File::from_std(file);
817 let mut t = task::spawn(file.write(HELLO));
818 assert_ready_ok!(t.poll());
819
820 let mut t = task::spawn(file.sync_data());
821 assert_pending!(t.poll());
822
823 assert_eq!(1, pool::len());
824 pool::run_one();
825
826 assert!(t.is_woken());
827 assert_pending!(t.poll());
828
829 assert_eq!(1, pool::len());
830 pool::run_one();
831
832 assert!(t.is_woken());
833 assert_ready_ok!(t.poll());
834}
835
836#[test]
837fn sync_data_err_ordered_after_write() {
838 let mut file = MockFile::default();
839 let mut seq = Sequence::new();
840 file.expect_inner_write()
841 .once()
842 .in_sequence(&mut seq)
843 .with(eq(HELLO))
844 .returning(|_| Ok(HELLO.len()));
845 file.expect_sync_data()
846 .once()
847 .returning(|| Err(io::ErrorKind::Other.into()));
848
849 let mut file = File::from_std(file);
850 let mut t = task::spawn(file.write(HELLO));
851 assert_ready_ok!(t.poll());
852
853 let mut t = task::spawn(file.sync_data());
854 assert_pending!(t.poll());
855
856 assert_eq!(1, pool::len());
857 pool::run_one();
858
859 assert!(t.is_woken());
860 assert_pending!(t.poll());
861
862 assert_eq!(1, pool::len());
863 pool::run_one();
864
865 assert!(t.is_woken());
866 assert_ready_err!(t.poll());
867}
868
869#[test]
870fn open_set_len_ok() {
871 let mut file = MockFile::default();
872 file.expect_set_len().with(eq(123)).returning(|_| Ok(()));
873
874 let file = File::from_std(file);
875 let mut t = task::spawn(file.set_len(123));
876
877 assert_pending!(t.poll());
878
879 pool::run_one();
880
881 assert!(t.is_woken());
882 assert_ready_ok!(t.poll());
883}
884
885#[test]
886fn open_set_len_err() {
887 let mut file = MockFile::default();
888 file.expect_set_len()
889 .with(eq(123))
890 .returning(|_| Err(io::ErrorKind::Other.into()));
891
892 let file = File::from_std(file);
893 let mut t = task::spawn(file.set_len(123));
894
895 assert_pending!(t.poll());
896
897 pool::run_one();
898
899 assert!(t.is_woken());
900 assert_ready_err!(t.poll());
901}
902
903#[test]
904fn partial_read_set_len_ok() {
905 let mut file = MockFile::default();
906 let mut seq = Sequence::new();
907 file.expect_inner_read()
908 .once()
909 .in_sequence(&mut seq)
910 .returning(|buf| {
911 buf[0..HELLO.len()].copy_from_slice(HELLO);
912 Ok(HELLO.len())
913 });
914 file.expect_inner_seek()
915 .once()
916 .with(eq(SeekFrom::Current(-(HELLO.len() as i64))))
917 .in_sequence(&mut seq)
918 .returning(|_| Ok(0));
919 file.expect_set_len()
920 .once()
921 .in_sequence(&mut seq)
922 .with(eq(123))
923 .returning(|_| Ok(()));
924 file.expect_inner_read()
925 .once()
926 .in_sequence(&mut seq)
927 .returning(|buf| {
928 buf[0..FOO.len()].copy_from_slice(FOO);
929 Ok(FOO.len())
930 });
931
932 let mut buf = [0; 32];
933 let mut file = File::from_std(file);
934
935 {
936 let mut t = task::spawn(file.read(&mut buf));
937 assert_pending!(t.poll());
938 }
939
940 pool::run_one();
941
942 {
943 let mut t = task::spawn(file.set_len(123));
944
945 assert_pending!(t.poll());
946 pool::run_one();
947 assert_ready_ok!(t.poll());
948 }
949
950 let mut t = task::spawn(file.read(&mut buf));
951 assert_pending!(t.poll());
952 pool::run_one();
953 let n = assert_ready_ok!(t.poll());
954
955 assert_eq!(n, FOO.len());
956 assert_eq!(&buf[..n], FOO);
957}
958
959#[test]
960fn busy_file_seek_error() {
961 let mut file = MockFile::default();
962 let mut seq = Sequence::new();
963 file.expect_inner_write()
964 .once()
965 .in_sequence(&mut seq)
966 .returning(|_| Err(io::ErrorKind::Other.into()));
967
968 let mut file = crate::io::BufReader::new(File::from_std(file));
969 {
970 let mut t = task::spawn(file.write(HELLO));
971 assert_ready_ok!(t.poll());
972 }
973
974 pool::run_one();
975
976 let mut t = task::spawn(file.seek(SeekFrom::Start(0)));
977 assert_ready_err!(t.poll());
978}
979