From e527e0049cfb75f939b39e233885030811a57a3b Mon Sep 17 00:00:00 2001 From: Owen Byomuhangi Date: Tue, 2 Dec 2025 15:51:58 -0500 Subject: [PATCH] Add pattern matching to handle writing failure in compression --- lib/z_stream/lz4.ex | 4 ++++ test/z_stream/lz4_test.exs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/z_stream/lz4.ex b/lib/z_stream/lz4.ex index f0a3855..6339be8 100644 --- a/lib/z_stream/lz4.ex +++ b/lib/z_stream/lz4.ex @@ -60,6 +60,10 @@ defmodule ZStream.LZ4 do {:erlang.iolist_to_iovec(finish), ref} end + defp do_compress_exit({:start, ref}) do + :lz4f.compress_end(ref) + end + defp do_compress_exit(ref) do :lz4f.compress_end(ref) end diff --git a/test/z_stream/lz4_test.exs b/test/z_stream/lz4_test.exs index f759947..65c6f54 100644 --- a/test/z_stream/lz4_test.exs +++ b/test/z_stream/lz4_test.exs @@ -30,4 +30,21 @@ defmodule ZStream.LZ4Test do assert Enum.into(stream, "") == "" end + + test "compress empty stream" do + empty_data = [] + stream = ZStream.compress(empty_data, LZ4) + result = Enum.into(stream, []) + + assert [_ | _] = result + end + + test "decompress empty stream" do + empty_data = [] + compressed = ZStream.compress(empty_data, LZ4) + compressed_bytes = Enum.into(compressed, []) + decompressed = ZStream.decompress(compressed_bytes, LZ4) + + assert Enum.into(decompressed, "") == "" + end end