Skip to content

Commit

Permalink
feat: add abort API to opendal::types::writer::Writer. Add behavoiral…
Browse files Browse the repository at this point in the history
… test for abort
  • Loading branch information
v0y4g3r committed Apr 17, 2023
1 parent 7525ccd commit d485a72
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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);
}

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

0 comments on commit d485a72

Please sign in to comment.