@@ -24,6 +24,7 @@ async fn read_hello(file: &File) {
24
24
async fn basic_read ( ) {
25
25
let mut tempfile = tempfile ( ) ;
26
26
tempfile. write_all ( HELLO ) . unwrap ( ) ;
27
+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
27
28
28
29
let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
29
30
read_hello ( & file) . await ;
@@ -33,6 +34,7 @@ async fn basic_read() {
33
34
async fn basic_read_exact ( ) {
34
35
let mut tempfile = tempfile ( ) ;
35
36
tempfile. write_all ( HELLO ) . unwrap ( ) ;
37
+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
36
38
37
39
let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
38
40
let buf = Vec :: with_capacity ( HELLO . len ( ) ) ;
@@ -51,6 +53,7 @@ async fn basic_write() {
51
53
52
54
let file = File :: create ( tempfile. path ( ) ) . await . unwrap ( ) ;
53
55
file. write_at ( HELLO , 0 ) . await . 0 . unwrap ( ) ;
56
+ file. sync_all ( ) . await . unwrap ( ) ;
54
57
55
58
let file = std:: fs:: read ( tempfile. path ( ) ) . unwrap ( ) ;
56
59
assert_eq ! ( file, HELLO ) ;
@@ -62,6 +65,7 @@ async fn basic_write_all() {
62
65
63
66
let file = File :: create ( tempfile. path ( ) ) . await . unwrap ( ) ;
64
67
file. write_all_at ( HELLO , 0 ) . await . 0 . unwrap ( ) ;
68
+ file. sync_all ( ) . await . unwrap ( ) ;
65
69
66
70
let file = std:: fs:: read ( tempfile. path ( ) ) . unwrap ( ) ;
67
71
assert_eq ! ( file, HELLO ) ;
@@ -71,6 +75,7 @@ async fn basic_write_all() {
71
75
async fn cancel_read ( ) {
72
76
let mut tempfile = tempfile ( ) ;
73
77
tempfile. write_all ( HELLO ) . unwrap ( ) ;
78
+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
74
79
75
80
let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
76
81
@@ -84,6 +89,7 @@ async fn cancel_read() {
84
89
async fn explicit_close ( ) {
85
90
let mut tempfile = tempfile ( ) ;
86
91
tempfile. write_all ( HELLO ) . unwrap ( ) ;
92
+ tempfile. as_file_mut ( ) . sync_data ( ) . unwrap ( ) ;
87
93
88
94
let file = File :: open ( tempfile. path ( ) ) . await . unwrap ( ) ;
89
95
#[ cfg( unix) ]
@@ -93,7 +99,7 @@ async fn explicit_close() {
93
99
94
100
file. close ( ) . await . unwrap ( ) ;
95
101
96
- assert_invalid_fd ( fd) ;
102
+ assert_invalid_fd ( fd, tempfile . as_file ( ) . metadata ( ) . unwrap ( ) ) ;
97
103
}
98
104
99
105
#[ monoio:: test_all]
@@ -103,6 +109,7 @@ async fn drop_open() {
103
109
// Do something else
104
110
let file_w = File :: create ( tempfile. path ( ) ) . await . unwrap ( ) ;
105
111
file_w. write_at ( HELLO , 0 ) . await . 0 . unwrap ( ) ;
112
+ file_w. sync_all ( ) . await . unwrap ( ) ;
106
113
107
114
let file = std:: fs:: read ( tempfile. path ( ) ) . unwrap ( ) ;
108
115
assert_eq ! ( file, HELLO ) ;
@@ -127,7 +134,7 @@ fn drop_off_runtime() {
127
134
let fd = file. as_raw_handle ( ) ;
128
135
drop ( file) ;
129
136
130
- assert_invalid_fd ( fd) ;
137
+ assert_invalid_fd ( fd, tempfile . as_file ( ) . metadata ( ) . unwrap ( ) ) ;
131
138
}
132
139
133
140
#[ monoio:: test_all]
@@ -160,13 +167,29 @@ async fn poll_once(future: impl std::future::Future) {
160
167
. await ;
161
168
}
162
169
163
- fn assert_invalid_fd ( fd : RawFd ) {
170
+ fn assert_invalid_fd ( fd : RawFd , base : std :: fs :: Metadata ) {
164
171
use std:: fs:: File ;
165
172
#[ cfg( unix) ]
166
- let mut f = unsafe { File :: from_raw_fd ( fd) } ;
173
+ let f = unsafe { File :: from_raw_fd ( fd) } ;
167
174
#[ 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
+ }
172
195
}
0 commit comments