Fix memory leak: Dispose IronCompressResult in Zstd codec (#7193)
* Initial plan * Fix memory leak: Dispose IronCompressResult in Zstd codec and add tests Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com> * Refactor tests to be more DRY using Theory and InlineData Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>
This commit is contained in:
parent
bc70beff12
commit
05d40e3a2a
|
|
@ -15,7 +15,7 @@ public class Zstd : ICompressionCodec
|
|||
{
|
||||
var inputBytes = Encoding.UTF8.GetBytes(input);
|
||||
var span = inputBytes.AsSpan();
|
||||
var result = Iron.Compress(Codec.Zstd, span);
|
||||
using var result = Iron.Compress(Codec.Zstd, span);
|
||||
var compressedBytes = result.AsSpan();
|
||||
var compressedString = Convert.ToBase64String(compressedBytes);
|
||||
|
||||
|
|
@ -27,7 +27,7 @@ public class Zstd : ICompressionCodec
|
|||
{
|
||||
var inputBytes = Convert.FromBase64String(input);
|
||||
var span = inputBytes.AsSpan();
|
||||
var result = Iron.Decompress(Codec.Zstd, span);
|
||||
using var result = Iron.Decompress(Codec.Zstd, span);
|
||||
var decompressedBytes = result.AsSpan();
|
||||
var decompressedString = Encoding.UTF8.GetString(decompressedBytes);
|
||||
|
||||
|
|
|
|||
67
test/unit/Elsa.Common.UnitTests/Codecs/ZstdTests.cs
Normal file
67
test/unit/Elsa.Common.UnitTests/Codecs/ZstdTests.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
using Elsa.Common.Codecs;
|
||||
|
||||
namespace Elsa.Common.UnitTests.Codecs;
|
||||
|
||||
public class ZstdTests
|
||||
{
|
||||
private readonly Zstd _codec = new();
|
||||
|
||||
[Fact]
|
||||
public async Task CompressAsync_WithSimpleString_ReturnsCompressedString()
|
||||
{
|
||||
// Arrange
|
||||
var input = "Hello, World!";
|
||||
|
||||
// Act
|
||||
var result = await _codec.CompressAsync(input);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.NotEmpty(result);
|
||||
Assert.NotEqual(input, result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Hello, World!")]
|
||||
[InlineData("")]
|
||||
[InlineData("Hello! 你好! مرحبا! Здравствуйте! 🎉🎊")]
|
||||
[InlineData("{\"name\":\"John Doe\",\"age\":30,\"city\":\"New York\",\"items\":[1,2,3,4,5]}")]
|
||||
public async Task CompressDecompress_RoundTrip_PreservesOriginalData(string original)
|
||||
{
|
||||
// Act
|
||||
var compressed = await _codec.CompressAsync(original);
|
||||
var decompressed = await _codec.DecompressAsync(compressed);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(original, decompressed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompressDecompress_WithLargeString_WorksCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
var original = string.Join("", Enumerable.Repeat("This is a test string that will be compressed. ", 1000));
|
||||
|
||||
// Act
|
||||
var compressed = await _codec.CompressAsync(original);
|
||||
var decompressed = await _codec.DecompressAsync(compressed);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(original, decompressed);
|
||||
Assert.True(compressed.Length < original.Length, "Compressed string should be smaller than original");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompressAsync_MultipleCallsWithSameInput_ProducesConsistentResults()
|
||||
{
|
||||
// Arrange
|
||||
var input = "Test string for consistency";
|
||||
|
||||
// Act
|
||||
var result1 = await _codec.CompressAsync(input);
|
||||
var result2 = await _codec.CompressAsync(input);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(result1, result2);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue