Skip to content

Commit f286bbf

Browse files
refactor(volume): share speed test file cleanup
1 parent 75b6fc8 commit f286bbf

1 file changed

Lines changed: 25 additions & 29 deletions

File tree

core/src/volume/speed.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ pub async fn run_speed_test_with_config(
8383
))
8484
}
8585

86+
async fn remove_file_best_effort(path: &std::path::Path, artifact: &'static str) -> bool {
87+
match tokio::fs::remove_file(path).await {
88+
Ok(()) => true,
89+
Err(error) if error.kind() == std::io::ErrorKind::NotFound => true,
90+
Err(error) => {
91+
warn!(
92+
error = %error,
93+
path = %path.display(),
94+
artifact = artifact,
95+
"Failed to remove speed test artifact"
96+
);
97+
false
98+
}
99+
}
100+
}
101+
86102
/// Helper for managing test files
87103
struct TestLocation {
88104
test_file: std::path::PathBuf,
@@ -107,22 +123,10 @@ impl TestLocation {
107123
/// Clean up the test file
108124
async fn cleanup(&mut self) {
109125
// Never remove a file unless this speed test successfully created it.
110-
if self.test_file_created {
111-
match tokio::fs::remove_file(&self.test_file).await {
112-
Ok(()) => {
113-
self.test_file_created = false;
114-
}
115-
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
116-
self.test_file_created = false;
117-
}
118-
Err(e) => {
119-
warn!(
120-
error = %e,
121-
path = %self.test_file.display(),
122-
"Failed to remove speed test file"
123-
);
124-
}
125-
}
126+
if self.test_file_created
127+
&& remove_file_best_effort(&self.test_file, "speed test file").await
128+
{
129+
self.test_file_created = false;
126130
}
127131
}
128132
}
@@ -272,19 +276,11 @@ async fn get_writable_directory(
272276
Ok(mut file) => {
273277
let write_result = file.write_all(b"test").await;
274278
drop(file);
275-
let cleanup_succeeded = match tokio::fs::remove_file(&permission_file).await
276-
{
277-
Ok(()) => true,
278-
Err(error) if error.kind() == std::io::ErrorKind::NotFound => true,
279-
Err(error) => {
280-
warn!(
281-
error = %error,
282-
path = %permission_file.display(),
283-
"Failed to remove speed test permission probe"
284-
);
285-
false
286-
}
287-
};
279+
let cleanup_succeeded = remove_file_best_effort(
280+
&permission_file,
281+
"speed test permission probe",
282+
)
283+
.await;
288284
write_result.is_ok() && cleanup_succeeded
289285
}
290286
Err(_) => false,

0 commit comments

Comments
 (0)