@@ -24,6 +24,7 @@ async fn read_hello(file: &File) {
2424async fn basic_read ( ) {
2525 let mut tempfile = tempfile ( ) ;
2626 tempfile. write_all ( HELLO ) . unwrap ( ) ;
27+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
2728
2829 let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
2930 read_hello ( & file) . await ;
@@ -33,6 +34,7 @@ async fn basic_read() {
3334async fn basic_read_exact ( ) {
3435 let mut tempfile = tempfile ( ) ;
3536 tempfile. write_all ( HELLO ) . unwrap ( ) ;
37+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
3638
3739 let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
3840 let buf = Vec :: with_capacity ( HELLO . len ( ) ) ;
@@ -51,6 +53,7 @@ async fn basic_write() {
5153
5254 let file = File :: create ( tempfile. path ( ) ) . await . unwrap ( ) ;
5355 file. write_at ( HELLO , 0 ) . await . 0 . unwrap ( ) ;
56+ file. sync_all ( ) . await . unwrap ( ) ;
5457
5558 let file = std:: fs:: read ( tempfile. path ( ) ) . unwrap ( ) ;
5659 assert_eq ! ( file, HELLO ) ;
@@ -62,6 +65,7 @@ async fn basic_write_all() {
6265
6366 let file = File :: create ( tempfile. path ( ) ) . await . unwrap ( ) ;
6467 file. write_all_at ( HELLO , 0 ) . await . 0 . unwrap ( ) ;
68+ file. sync_all ( ) . await . unwrap ( ) ;
6569
6670 let file = std:: fs:: read ( tempfile. path ( ) ) . unwrap ( ) ;
6771 assert_eq ! ( file, HELLO ) ;
@@ -71,6 +75,7 @@ async fn basic_write_all() {
7175async fn cancel_read ( ) {
7276 let mut tempfile = tempfile ( ) ;
7377 tempfile. write_all ( HELLO ) . unwrap ( ) ;
78+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
7479
7580 let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
7681
@@ -84,6 +89,7 @@ async fn cancel_read() {
8489async fn explicit_close ( ) {
8590 let mut tempfile = tempfile ( ) ;
8691 tempfile. write_all ( HELLO ) . unwrap ( ) ;
92+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
8793
8894 let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
8995 #[ cfg( unix) ]
@@ -93,7 +99,7 @@ async fn explicit_close() {
9399
94100 file. close ( ) . await . unwrap ( ) ;
95101
96- assert_invalid_fd ( fd) ;
102+ assert_invalid_fd ( fd, tempfile . as_file ( ) . metadata ( ) . unwrap ( ) ) ;
97103}
98104
99105#[ monoio:: test_all]
@@ -103,6 +109,7 @@ async fn drop_open() {
103109 // Do something else
104110 let file_w = File :: create ( tempfile. path ( ) ) . await . unwrap ( ) ;
105111 file_w. write_at ( HELLO , 0 ) . await . 0 . unwrap ( ) ;
112+ file_w. sync_all ( ) . await . unwrap ( ) ;
106113
107114 let file = std:: fs:: read ( tempfile. path ( ) ) . unwrap ( ) ;
108115 assert_eq ! ( file, HELLO ) ;
@@ -127,7 +134,7 @@ fn drop_off_runtime() {
127134 let fd = file. as_raw_handle ( ) ;
128135 drop ( file) ;
129136
130- assert_invalid_fd ( fd) ;
137+ assert_invalid_fd ( fd, tempfile . as_file ( ) . metadata ( ) . unwrap ( ) ) ;
131138}
132139
133140#[ monoio:: test_all]
@@ -160,13 +167,29 @@ async fn poll_once(future: impl std::future::Future) {
160167 . await ;
161168}
162169
163- fn assert_invalid_fd ( fd : RawFd ) {
170+ fn assert_invalid_fd ( fd : RawFd , base : std :: fs :: Metadata ) {
164171 use std:: fs:: File ;
165172 #[ cfg( unix) ]
166- let mut f = unsafe { File :: from_raw_fd ( fd) } ;
173+ let f = unsafe { File :: from_raw_fd ( fd) } ;
167174 #[ cfg( windows) ]
168- let mut f = unsafe { File :: from_raw_handle ( fd) } ;
169- let mut buf = vec ! [ ] ;
170-
171- assert ! ( f. read_to_end( & mut buf) . is_err( ) ) ;
175+ let f = unsafe { File :: from_raw_handle ( fd) } ;
176+
177+ let meta = f. metadata ( ) ;
178+ std:: mem:: forget ( f) ;
179+
180+ if let Ok ( meta) = meta {
181+ if !meta. is_file ( ) {
182+ return ;
183+ }
184+
185+ #[ cfg( unix) ]
186+ {
187+ use std:: os:: unix:: fs:: MetadataExt ;
188+ let inode = meta. ino ( ) ;
189+ let actual = base. ino ( ) ;
190+ if inode == actual {
191+ panic ! ( ) ;
192+ }
193+ }
194+ }
172195}
0 commit comments