@@ -22,6 +22,7 @@ async fn read_hello(file: &File) {
22
22
async fn basic_read ( ) {
23
23
let mut tempfile = tempfile ( ) ;
24
24
tempfile. write_all ( HELLO ) . unwrap ( ) ;
25
+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
25
26
26
27
let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
27
28
read_hello ( & file) . await ;
@@ -31,6 +32,7 @@ async fn basic_read() {
31
32
async fn basic_read_exact ( ) {
32
33
let mut tempfile = tempfile ( ) ;
33
34
tempfile. write_all ( HELLO ) . unwrap ( ) ;
35
+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
34
36
35
37
let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
36
38
let buf = Vec :: with_capacity ( HELLO . len ( ) ) ;
@@ -49,6 +51,7 @@ async fn basic_write() {
49
51
50
52
let file = File :: create ( tempfile. path ( ) ) . await . unwrap ( ) ;
51
53
file. write_at ( HELLO , 0 ) . await . 0 . unwrap ( ) ;
54
+ file. sync_all ( ) . await . unwrap ( ) ;
52
55
53
56
let file = std:: fs:: read ( tempfile. path ( ) ) . unwrap ( ) ;
54
57
assert_eq ! ( file, HELLO ) ;
@@ -60,6 +63,7 @@ async fn basic_write_all() {
60
63
61
64
let file = File :: create ( tempfile. path ( ) ) . await . unwrap ( ) ;
62
65
file. write_all_at ( HELLO , 0 ) . await . 0 . unwrap ( ) ;
66
+ file. sync_all ( ) . await . unwrap ( ) ;
63
67
64
68
let file = std:: fs:: read ( tempfile. path ( ) ) . unwrap ( ) ;
65
69
assert_eq ! ( file, HELLO ) ;
@@ -69,6 +73,7 @@ async fn basic_write_all() {
69
73
async fn cancel_read ( ) {
70
74
let mut tempfile = tempfile ( ) ;
71
75
tempfile. write_all ( HELLO ) . unwrap ( ) ;
76
+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
72
77
73
78
let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
74
79
@@ -82,6 +87,7 @@ async fn cancel_read() {
82
87
async fn explicit_close ( ) {
83
88
let mut tempfile = tempfile ( ) ;
84
89
tempfile. write_all ( HELLO ) . unwrap ( ) ;
90
+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
85
91
86
92
let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
87
93
#[ cfg( unix) ]
@@ -91,7 +97,7 @@ async fn explicit_close() {
91
97
92
98
file. close ( ) . await . unwrap ( ) ;
93
99
94
- assert_invalid_fd ( fd) ;
100
+ assert_invalid_fd ( fd, tempfile . as_file ( ) . metadata ( ) . unwrap ( ) ) ;
95
101
}
96
102
97
103
#[ monoio:: test_all]
@@ -101,6 +107,7 @@ async fn drop_open() {
101
107
// Do something else
102
108
let file_w = File :: create ( tempfile. path ( ) ) . await . unwrap ( ) ;
103
109
file_w. write_at ( HELLO , 0 ) . await . 0 . unwrap ( ) ;
110
+ file_w. sync_all ( ) . await . unwrap ( ) ;
104
111
105
112
let file = std:: fs:: read ( tempfile. path ( ) ) . unwrap ( ) ;
106
113
assert_eq ! ( file, HELLO ) ;
@@ -125,7 +132,7 @@ fn drop_off_runtime() {
125
132
let fd = file. as_raw_handle ( ) ;
126
133
drop ( file) ;
127
134
128
- assert_invalid_fd ( fd) ;
135
+ assert_invalid_fd ( fd, tempfile . as_file ( ) . metadata ( ) . unwrap ( ) ) ;
129
136
}
130
137
131
138
#[ monoio:: test_all]
@@ -158,13 +165,29 @@ async fn poll_once(future: impl std::future::Future) {
158
165
. await ;
159
166
}
160
167
161
- fn assert_invalid_fd ( fd : RawFd ) {
168
+ fn assert_invalid_fd ( fd : RawFd , base : std :: fs :: Metadata ) {
162
169
use std:: fs:: File ;
163
170
#[ cfg( unix) ]
164
- let mut f = unsafe { File :: from_raw_fd ( fd) } ;
171
+ let f = unsafe { File :: from_raw_fd ( fd) } ;
165
172
#[ cfg( windows) ]
166
- let mut f = unsafe { File :: from_raw_handle ( fd) } ;
167
- let mut buf = vec ! [ ] ;
168
-
169
- assert ! ( f. read_to_end( & mut buf) . is_err( ) ) ;
173
+ let f = unsafe { File :: from_raw_handle ( fd) } ;
174
+
175
+ let meta = f. metadata ( ) ;
176
+ std:: mem:: forget ( f) ;
177
+
178
+ if let Ok ( meta) = meta {
179
+ if !meta. is_file ( ) {
180
+ return ;
181
+ }
182
+
183
+ #[ cfg( unix) ]
184
+ {
185
+ use std:: os:: unix:: fs:: MetadataExt ;
186
+ let inode = meta. ino ( ) ;
187
+ let actual = base. ino ( ) ;
188
+ if inode == actual {
189
+ panic ! ( ) ;
190
+ }
191
+ }
192
+ }
170
193
}
0 commit comments