Skip to content

Commit

Permalink
Add test that ensures the Drop implementation for BzEncoder will not …
Browse files Browse the repository at this point in the history
…regress

Without the `impl<W: Write> Drop for BzEncoder<W>` the test fails with
the error message:
    called `Result::unwrap()` on an `Err` value: Custom { kind: UnexpectedEof, error: "Input EOF reached before logical stream end" }
  • Loading branch information
jonasbb committed Dec 17, 2024
1 parent c3be758 commit 1539a77
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,29 @@ mod tests {
.into_inner()
}
}

#[test]
fn terminate_on_drop() {
// Test that dropping the BzEncoder will result in a valid, decompressable datastream
let s = "12345".repeat(100000);

let mut compressed = Vec::new();
{
let mut c: Box<dyn std::io::Write> =
Box::new(BzEncoder::new(&mut compressed, Compression::default()));
c.write_all(b"12834").unwrap();
c.write_all(s.as_bytes()).unwrap();
c.flush().unwrap();
}
assert!(!compressed.is_empty());

let uncompressed = {
let mut d = BzDecoder::new(Vec::new());
d.write_all(&compressed).unwrap();
d.finish().unwrap()
};
assert_eq!(&uncompressed[0..5], b"12834");
assert_eq!(uncompressed.len(), 500005);
assert!(format!("12834{}", s).as_bytes() == &*uncompressed);
}
}

0 comments on commit 1539a77

Please sign in to comment.