Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add behavioral test for Write::abort #2018

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/types/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use futures::AsyncWrite;
use futures::FutureExt;

use crate::ops::OpWrite;
use crate::raw::oio::Write;
use crate::raw::*;
use crate::*;

Expand Down Expand Up @@ -75,6 +76,18 @@ impl Writer {
}
}

/// Abort inner writer.
pub async fn abort(&mut self) -> Result<()> {
if let State::Idle(Some(w)) = &mut self.state {
w.abort().await
} else {
unreachable!(
"writer state invalid while abort, expect Idle, actual {}",
self.state
);
}
}

/// Close the writer and make sure all data have been stored.
pub async fn close(&mut self) -> Result<()> {
if let State::Idle(Some(w)) = &mut self.state {
Expand Down
20 changes: 20 additions & 0 deletions core/tests/behavior/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ macro_rules! behavior_write_tests {
test_delete_not_existing,
test_delete_stream,
test_append,
test_abort_writer,
);
)*
};
Expand Down Expand Up @@ -578,6 +579,25 @@ pub async fn test_read_with_special_chars(op: Operator) -> Result<()> {
Ok(())
}

// Delete existing file should succeed.
pub async fn test_abort_writer(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();
let (content, _) = gen_bytes();

let mut writer = op.writer(&path).await.unwrap();
if let Err(e) = writer.append(content).await {
assert_eq!(e.kind(), ErrorKind::Unsupported);
v0y4g3r marked this conversation as resolved.
Show resolved Hide resolved
}

if let Err(e) = writer.abort().await {
assert_eq!(e.kind(), ErrorKind::Unsupported);
}

// Aborted writer should not write actual file.
assert!(!op.is_exist(&path).await?);
Ok(())
}

// Delete existing file should succeed.
pub async fn test_delete(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();
Expand Down