1 use super::*;
2 use crate::{
3     fs::mocks::*,
4     io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
5 };
6 use mockall::{predicate::eq, Sequence};
7 use tokio_test::{assert_pending, assert_ready_err, assert_ready_ok, task};
8 
9 const HELLO: &[u8] = b"hello world...";
10 const FOO: &[u8] = b"foo bar baz...";
11 
12 #[test]
open_read()13 fn 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]
read_twice_before_dispatch()39 fn 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]
read_with_smaller_buf()63 fn 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]
read_with_bigger_buf()99 fn 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]
read_err_then_read_success()154 fn 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]
open_write()196 fn 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]
flush_while_idle()221 fn 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]
read_with_buffer_larger_than_max()231 fn read_with_buffer_larger_than_max() {
232     // Chunks
233     let chunk_a = 16 * 1024;
234     let chunk_b = chunk_a * 2;
235     let chunk_c = chunk_a * 3;
236     let chunk_d = chunk_a * 4;
237 
238     assert_eq!(chunk_d / 1024, 64);
239 
240     let mut data = vec![];
241     for i in 0..(chunk_d - 1) {
242         data.push((i % 151) as u8);
243     }
244     let data = Arc::new(data);
245     let d0 = data.clone();
246     let d1 = data.clone();
247     let d2 = data.clone();
248     let d3 = data.clone();
249 
250     let mut seq = Sequence::new();
251     let mut file = MockFile::default();
252     file.expect_inner_read()
253         .once()
254         .in_sequence(&mut seq)
255         .returning(move |buf| {
256             buf[0..chunk_a].copy_from_slice(&d0[0..chunk_a]);
257             Ok(chunk_a)
258         });
259     file.expect_inner_read()
260         .once()
261         .in_sequence(&mut seq)
262         .returning(move |buf| {
263             buf[..chunk_a].copy_from_slice(&d1[chunk_a..chunk_b]);
264             Ok(chunk_b - chunk_a)
265         });
266     file.expect_inner_read()
267         .once()
268         .in_sequence(&mut seq)
269         .returning(move |buf| {
270             buf[..chunk_a].copy_from_slice(&d2[chunk_b..chunk_c]);
271             Ok(chunk_c - chunk_b)
272         });
273     file.expect_inner_read()
274         .once()
275         .in_sequence(&mut seq)
276         .returning(move |buf| {
277             buf[..chunk_a - 1].copy_from_slice(&d3[chunk_c..]);
278             Ok(chunk_a - 1)
279         });
280     let mut file = File::from_std(file);
281 
282     let mut actual = vec![0; chunk_d];
283     let mut pos = 0;
284 
285     while pos < data.len() {
286         let mut t = task::spawn(file.read(&mut actual[pos..]));
287 
288         assert_pending!(t.poll());
289         pool::run_one();
290         assert!(t.is_woken());
291 
292         let n = assert_ready_ok!(t.poll());
293         assert!(n <= chunk_a);
294 
295         pos += n;
296     }
297 
298     assert_eq!(&data[..], &actual[..data.len()]);
299 }
300 
301 #[test]
write_with_buffer_larger_than_max()302 fn write_with_buffer_larger_than_max() {
303     // Chunks
304     let chunk_a = 16 * 1024;
305     let chunk_b = chunk_a * 2;
306     let chunk_c = chunk_a * 3;
307     let chunk_d = chunk_a * 4;
308 
309     assert_eq!(chunk_d / 1024, 64);
310 
311     let mut data = vec![];
312     for i in 0..(chunk_d - 1) {
313         data.push((i % 151) as u8);
314     }
315     let data = Arc::new(data);
316     let d0 = data.clone();
317     let d1 = data.clone();
318     let d2 = data.clone();
319     let d3 = data.clone();
320 
321     let mut file = MockFile::default();
322     let mut seq = Sequence::new();
323     file.expect_inner_write()
324         .once()
325         .in_sequence(&mut seq)
326         .withf(move |buf| buf == &d0[0..chunk_a])
327         .returning(|buf| Ok(buf.len()));
328     file.expect_inner_write()
329         .once()
330         .in_sequence(&mut seq)
331         .withf(move |buf| buf == &d1[chunk_a..chunk_b])
332         .returning(|buf| Ok(buf.len()));
333     file.expect_inner_write()
334         .once()
335         .in_sequence(&mut seq)
336         .withf(move |buf| buf == &d2[chunk_b..chunk_c])
337         .returning(|buf| Ok(buf.len()));
338     file.expect_inner_write()
339         .once()
340         .in_sequence(&mut seq)
341         .withf(move |buf| buf == &d3[chunk_c..chunk_d - 1])
342         .returning(|buf| Ok(buf.len()));
343 
344     let mut file = File::from_std(file);
345 
346     let mut rem = &data[..];
347 
348     let mut first = true;
349 
350     while !rem.is_empty() {
351         let mut task = task::spawn(file.write(rem));
352 
353         if !first {
354             assert_pending!(task.poll());
355             pool::run_one();
356             assert!(task.is_woken());
357         }
358 
359         first = false;
360 
361         let n = assert_ready_ok!(task.poll());
362 
363         rem = &rem[n..];
364     }
365 
366     pool::run_one();
367 }
368 
369 #[test]
write_twice_before_dispatch()370 fn write_twice_before_dispatch() {
371     let mut file = MockFile::default();
372     let mut seq = Sequence::new();
373     file.expect_inner_write()
374         .once()
375         .in_sequence(&mut seq)
376         .with(eq(HELLO))
377         .returning(|buf| Ok(buf.len()));
378     file.expect_inner_write()
379         .once()
380         .in_sequence(&mut seq)
381         .with(eq(FOO))
382         .returning(|buf| Ok(buf.len()));
383 
384     let mut file = File::from_std(file);
385 
386     let mut t = task::spawn(file.write(HELLO));
387     assert_ready_ok!(t.poll());
388 
389     let mut t = task::spawn(file.write(FOO));
390     assert_pending!(t.poll());
391 
392     assert_eq!(pool::len(), 1);
393     pool::run_one();
394 
395     assert!(t.is_woken());
396 
397     assert_ready_ok!(t.poll());
398 
399     let mut t = task::spawn(file.flush());
400     assert_pending!(t.poll());
401 
402     assert_eq!(pool::len(), 1);
403     pool::run_one();
404 
405     assert!(t.is_woken());
406     assert_ready_ok!(t.poll());
407 }
408 
409 #[test]
incomplete_read_followed_by_write()410 fn incomplete_read_followed_by_write() {
411     let mut file = MockFile::default();
412     let mut seq = Sequence::new();
413     file.expect_inner_read()
414         .once()
415         .in_sequence(&mut seq)
416         .returning(|buf| {
417             buf[0..HELLO.len()].copy_from_slice(HELLO);
418             Ok(HELLO.len())
419         });
420     file.expect_inner_seek()
421         .once()
422         .with(eq(SeekFrom::Current(-(HELLO.len() as i64))))
423         .in_sequence(&mut seq)
424         .returning(|_| Ok(0));
425     file.expect_inner_write()
426         .once()
427         .with(eq(FOO))
428         .returning(|_| Ok(FOO.len()));
429 
430     let mut file = File::from_std(file);
431 
432     let mut buf = [0; 32];
433 
434     let mut t = task::spawn(file.read(&mut buf));
435     assert_pending!(t.poll());
436 
437     pool::run_one();
438 
439     let mut t = task::spawn(file.write(FOO));
440     assert_ready_ok!(t.poll());
441 
442     assert_eq!(pool::len(), 1);
443     pool::run_one();
444 
445     let mut t = task::spawn(file.flush());
446     assert_ready_ok!(t.poll());
447 }
448 
449 #[test]
incomplete_partial_read_followed_by_write()450 fn incomplete_partial_read_followed_by_write() {
451     let mut file = MockFile::default();
452     let mut seq = Sequence::new();
453     file.expect_inner_read()
454         .once()
455         .in_sequence(&mut seq)
456         .returning(|buf| {
457             buf[0..HELLO.len()].copy_from_slice(HELLO);
458             Ok(HELLO.len())
459         });
460     file.expect_inner_seek()
461         .once()
462         .in_sequence(&mut seq)
463         .with(eq(SeekFrom::Current(-10)))
464         .returning(|_| Ok(0));
465     file.expect_inner_write()
466         .once()
467         .in_sequence(&mut seq)
468         .with(eq(FOO))
469         .returning(|_| Ok(FOO.len()));
470 
471     let mut file = File::from_std(file);
472 
473     let mut buf = [0; 32];
474     let mut t = task::spawn(file.read(&mut buf));
475     assert_pending!(t.poll());
476 
477     pool::run_one();
478 
479     let mut buf = [0; 4];
480     let mut t = task::spawn(file.read(&mut buf));
481     assert_ready_ok!(t.poll());
482 
483     let mut t = task::spawn(file.write(FOO));
484     assert_ready_ok!(t.poll());
485 
486     assert_eq!(pool::len(), 1);
487     pool::run_one();
488 
489     let mut t = task::spawn(file.flush());
490     assert_ready_ok!(t.poll());
491 }
492 
493 #[test]
incomplete_read_followed_by_flush()494 fn incomplete_read_followed_by_flush() {
495     let mut file = MockFile::default();
496     let mut seq = Sequence::new();
497     file.expect_inner_read()
498         .once()
499         .in_sequence(&mut seq)
500         .returning(|buf| {
501             buf[0..HELLO.len()].copy_from_slice(HELLO);
502             Ok(HELLO.len())
503         });
504     file.expect_inner_seek()
505         .once()
506         .in_sequence(&mut seq)
507         .with(eq(SeekFrom::Current(-(HELLO.len() as i64))))
508         .returning(|_| Ok(0));
509     file.expect_inner_write()
510         .once()
511         .in_sequence(&mut seq)
512         .with(eq(FOO))
513         .returning(|_| Ok(FOO.len()));
514 
515     let mut file = File::from_std(file);
516 
517     let mut buf = [0; 32];
518 
519     let mut t = task::spawn(file.read(&mut buf));
520     assert_pending!(t.poll());
521 
522     pool::run_one();
523 
524     let mut t = task::spawn(file.flush());
525     assert_ready_ok!(t.poll());
526 
527     let mut t = task::spawn(file.write(FOO));
528     assert_ready_ok!(t.poll());
529 
530     pool::run_one();
531 }
532 
533 #[test]
incomplete_flush_followed_by_write()534 fn incomplete_flush_followed_by_write() {
535     let mut file = MockFile::default();
536     let mut seq = Sequence::new();
537     file.expect_inner_write()
538         .once()
539         .in_sequence(&mut seq)
540         .with(eq(HELLO))
541         .returning(|_| Ok(HELLO.len()));
542     file.expect_inner_write()
543         .once()
544         .in_sequence(&mut seq)
545         .with(eq(FOO))
546         .returning(|_| Ok(FOO.len()));
547 
548     let mut file = File::from_std(file);
549 
550     let mut t = task::spawn(file.write(HELLO));
551     let n = assert_ready_ok!(t.poll());
552     assert_eq!(n, HELLO.len());
553 
554     let mut t = task::spawn(file.flush());
555     assert_pending!(t.poll());
556 
557     // TODO: Move under write
558     pool::run_one();
559 
560     let mut t = task::spawn(file.write(FOO));
561     assert_ready_ok!(t.poll());
562 
563     pool::run_one();
564 
565     let mut t = task::spawn(file.flush());
566     assert_ready_ok!(t.poll());
567 }
568 
569 #[test]
read_err()570 fn read_err() {
571     let mut file = MockFile::default();
572     file.expect_inner_read()
573         .once()
574         .returning(|_| Err(io::ErrorKind::Other.into()));
575 
576     let mut file = File::from_std(file);
577 
578     let mut buf = [0; 1024];
579     let mut t = task::spawn(file.read(&mut buf));
580 
581     assert_pending!(t.poll());
582 
583     pool::run_one();
584     assert!(t.is_woken());
585 
586     assert_ready_err!(t.poll());
587 }
588 
589 #[test]
write_write_err()590 fn write_write_err() {
591     let mut file = MockFile::default();
592     file.expect_inner_write()
593         .once()
594         .returning(|_| Err(io::ErrorKind::Other.into()));
595 
596     let mut file = File::from_std(file);
597 
598     let mut t = task::spawn(file.write(HELLO));
599     assert_ready_ok!(t.poll());
600 
601     pool::run_one();
602 
603     let mut t = task::spawn(file.write(FOO));
604     assert_ready_err!(t.poll());
605 }
606 
607 #[test]
write_read_write_err()608 fn write_read_write_err() {
609     let mut file = MockFile::default();
610     let mut seq = Sequence::new();
611     file.expect_inner_write()
612         .once()
613         .in_sequence(&mut seq)
614         .returning(|_| Err(io::ErrorKind::Other.into()));
615     file.expect_inner_read()
616         .once()
617         .in_sequence(&mut seq)
618         .returning(|buf| {
619             buf[0..HELLO.len()].copy_from_slice(HELLO);
620             Ok(HELLO.len())
621         });
622 
623     let mut file = File::from_std(file);
624 
625     let mut t = task::spawn(file.write(HELLO));
626     assert_ready_ok!(t.poll());
627 
628     pool::run_one();
629 
630     let mut buf = [0; 1024];
631     let mut t = task::spawn(file.read(&mut buf));
632 
633     assert_pending!(t.poll());
634 
635     pool::run_one();
636 
637     let mut t = task::spawn(file.write(FOO));
638     assert_ready_err!(t.poll());
639 }
640 
641 #[test]
write_read_flush_err()642 fn write_read_flush_err() {
643     let mut file = MockFile::default();
644     let mut seq = Sequence::new();
645     file.expect_inner_write()
646         .once()
647         .in_sequence(&mut seq)
648         .returning(|_| Err(io::ErrorKind::Other.into()));
649     file.expect_inner_read()
650         .once()
651         .in_sequence(&mut seq)
652         .returning(|buf| {
653             buf[0..HELLO.len()].copy_from_slice(HELLO);
654             Ok(HELLO.len())
655         });
656 
657     let mut file = File::from_std(file);
658 
659     let mut t = task::spawn(file.write(HELLO));
660     assert_ready_ok!(t.poll());
661 
662     pool::run_one();
663 
664     let mut buf = [0; 1024];
665     let mut t = task::spawn(file.read(&mut buf));
666 
667     assert_pending!(t.poll());
668 
669     pool::run_one();
670 
671     let mut t = task::spawn(file.flush());
672     assert_ready_err!(t.poll());
673 }
674 
675 #[test]
write_seek_write_err()676 fn write_seek_write_err() {
677     let mut file = MockFile::default();
678     let mut seq = Sequence::new();
679     file.expect_inner_write()
680         .once()
681         .in_sequence(&mut seq)
682         .returning(|_| Err(io::ErrorKind::Other.into()));
683     file.expect_inner_seek()
684         .once()
685         .with(eq(SeekFrom::Start(0)))
686         .in_sequence(&mut seq)
687         .returning(|_| Ok(0));
688 
689     let mut file = File::from_std(file);
690 
691     let mut t = task::spawn(file.write(HELLO));
692     assert_ready_ok!(t.poll());
693 
694     pool::run_one();
695 
696     {
697         let mut t = task::spawn(file.seek(SeekFrom::Start(0)));
698         assert_pending!(t.poll());
699     }
700 
701     pool::run_one();
702 
703     let mut t = task::spawn(file.write(FOO));
704     assert_ready_err!(t.poll());
705 }
706 
707 #[test]
write_seek_flush_err()708 fn write_seek_flush_err() {
709     let mut file = MockFile::default();
710     let mut seq = Sequence::new();
711     file.expect_inner_write()
712         .once()
713         .in_sequence(&mut seq)
714         .returning(|_| Err(io::ErrorKind::Other.into()));
715     file.expect_inner_seek()
716         .once()
717         .with(eq(SeekFrom::Start(0)))
718         .in_sequence(&mut seq)
719         .returning(|_| Ok(0));
720 
721     let mut file = File::from_std(file);
722 
723     let mut t = task::spawn(file.write(HELLO));
724     assert_ready_ok!(t.poll());
725 
726     pool::run_one();
727 
728     {
729         let mut t = task::spawn(file.seek(SeekFrom::Start(0)));
730         assert_pending!(t.poll());
731     }
732 
733     pool::run_one();
734 
735     let mut t = task::spawn(file.flush());
736     assert_ready_err!(t.poll());
737 }
738 
739 #[test]
sync_all_ordered_after_write()740 fn sync_all_ordered_after_write() {
741     let mut file = MockFile::default();
742     let mut seq = Sequence::new();
743     file.expect_inner_write()
744         .once()
745         .in_sequence(&mut seq)
746         .with(eq(HELLO))
747         .returning(|_| Ok(HELLO.len()));
748     file.expect_sync_all().once().returning(|| Ok(()));
749 
750     let mut file = File::from_std(file);
751     let mut t = task::spawn(file.write(HELLO));
752     assert_ready_ok!(t.poll());
753 
754     let mut t = task::spawn(file.sync_all());
755     assert_pending!(t.poll());
756 
757     assert_eq!(1, pool::len());
758     pool::run_one();
759 
760     assert!(t.is_woken());
761     assert_pending!(t.poll());
762 
763     assert_eq!(1, pool::len());
764     pool::run_one();
765 
766     assert!(t.is_woken());
767     assert_ready_ok!(t.poll());
768 }
769 
770 #[test]
sync_all_err_ordered_after_write()771 fn sync_all_err_ordered_after_write() {
772     let mut file = MockFile::default();
773     let mut seq = Sequence::new();
774     file.expect_inner_write()
775         .once()
776         .in_sequence(&mut seq)
777         .with(eq(HELLO))
778         .returning(|_| Ok(HELLO.len()));
779     file.expect_sync_all()
780         .once()
781         .returning(|| Err(io::ErrorKind::Other.into()));
782 
783     let mut file = File::from_std(file);
784     let mut t = task::spawn(file.write(HELLO));
785     assert_ready_ok!(t.poll());
786 
787     let mut t = task::spawn(file.sync_all());
788     assert_pending!(t.poll());
789 
790     assert_eq!(1, pool::len());
791     pool::run_one();
792 
793     assert!(t.is_woken());
794     assert_pending!(t.poll());
795 
796     assert_eq!(1, pool::len());
797     pool::run_one();
798 
799     assert!(t.is_woken());
800     assert_ready_err!(t.poll());
801 }
802 
803 #[test]
sync_data_ordered_after_write()804 fn sync_data_ordered_after_write() {
805     let mut file = MockFile::default();
806     let mut seq = Sequence::new();
807     file.expect_inner_write()
808         .once()
809         .in_sequence(&mut seq)
810         .with(eq(HELLO))
811         .returning(|_| Ok(HELLO.len()));
812     file.expect_sync_data().once().returning(|| Ok(()));
813 
814     let mut file = File::from_std(file);
815     let mut t = task::spawn(file.write(HELLO));
816     assert_ready_ok!(t.poll());
817 
818     let mut t = task::spawn(file.sync_data());
819     assert_pending!(t.poll());
820 
821     assert_eq!(1, pool::len());
822     pool::run_one();
823 
824     assert!(t.is_woken());
825     assert_pending!(t.poll());
826 
827     assert_eq!(1, pool::len());
828     pool::run_one();
829 
830     assert!(t.is_woken());
831     assert_ready_ok!(t.poll());
832 }
833 
834 #[test]
sync_data_err_ordered_after_write()835 fn sync_data_err_ordered_after_write() {
836     let mut file = MockFile::default();
837     let mut seq = Sequence::new();
838     file.expect_inner_write()
839         .once()
840         .in_sequence(&mut seq)
841         .with(eq(HELLO))
842         .returning(|_| Ok(HELLO.len()));
843     file.expect_sync_data()
844         .once()
845         .returning(|| Err(io::ErrorKind::Other.into()));
846 
847     let mut file = File::from_std(file);
848     let mut t = task::spawn(file.write(HELLO));
849     assert_ready_ok!(t.poll());
850 
851     let mut t = task::spawn(file.sync_data());
852     assert_pending!(t.poll());
853 
854     assert_eq!(1, pool::len());
855     pool::run_one();
856 
857     assert!(t.is_woken());
858     assert_pending!(t.poll());
859 
860     assert_eq!(1, pool::len());
861     pool::run_one();
862 
863     assert!(t.is_woken());
864     assert_ready_err!(t.poll());
865 }
866 
867 #[test]
open_set_len_ok()868 fn open_set_len_ok() {
869     let mut file = MockFile::default();
870     file.expect_set_len().with(eq(123)).returning(|_| Ok(()));
871 
872     let file = File::from_std(file);
873     let mut t = task::spawn(file.set_len(123));
874 
875     assert_pending!(t.poll());
876 
877     pool::run_one();
878 
879     assert!(t.is_woken());
880     assert_ready_ok!(t.poll());
881 }
882 
883 #[test]
open_set_len_err()884 fn open_set_len_err() {
885     let mut file = MockFile::default();
886     file.expect_set_len()
887         .with(eq(123))
888         .returning(|_| Err(io::ErrorKind::Other.into()));
889 
890     let file = File::from_std(file);
891     let mut t = task::spawn(file.set_len(123));
892 
893     assert_pending!(t.poll());
894 
895     pool::run_one();
896 
897     assert!(t.is_woken());
898     assert_ready_err!(t.poll());
899 }
900 
901 #[test]
partial_read_set_len_ok()902 fn partial_read_set_len_ok() {
903     let mut file = MockFile::default();
904     let mut seq = Sequence::new();
905     file.expect_inner_read()
906         .once()
907         .in_sequence(&mut seq)
908         .returning(|buf| {
909             buf[0..HELLO.len()].copy_from_slice(HELLO);
910             Ok(HELLO.len())
911         });
912     file.expect_inner_seek()
913         .once()
914         .with(eq(SeekFrom::Current(-(HELLO.len() as i64))))
915         .in_sequence(&mut seq)
916         .returning(|_| Ok(0));
917     file.expect_set_len()
918         .once()
919         .in_sequence(&mut seq)
920         .with(eq(123))
921         .returning(|_| Ok(()));
922     file.expect_inner_read()
923         .once()
924         .in_sequence(&mut seq)
925         .returning(|buf| {
926             buf[0..FOO.len()].copy_from_slice(FOO);
927             Ok(FOO.len())
928         });
929 
930     let mut buf = [0; 32];
931     let mut file = File::from_std(file);
932 
933     {
934         let mut t = task::spawn(file.read(&mut buf));
935         assert_pending!(t.poll());
936     }
937 
938     pool::run_one();
939 
940     {
941         let mut t = task::spawn(file.set_len(123));
942 
943         assert_pending!(t.poll());
944         pool::run_one();
945         assert_ready_ok!(t.poll());
946     }
947 
948     let mut t = task::spawn(file.read(&mut buf));
949     assert_pending!(t.poll());
950     pool::run_one();
951     let n = assert_ready_ok!(t.poll());
952 
953     assert_eq!(n, FOO.len());
954     assert_eq!(&buf[..n], FOO);
955 }
956