You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by bl...@apache.org on 2022/06/14 10:17:14 UTC

[pulsar-dotpulsar] branch master updated: Adding compression tests both for runtime and testing

This is an automated email from the ASF dual-hosted git repository.

blankensteiner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-dotpulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 94daac3  Adding compression tests both for runtime and testing
94daac3 is described below

commit 94daac37a573937d2abcc73f3d355d5f22293cec
Author: Daniel Blankensteiner <db...@vmail.dk>
AuthorDate: Tue Jun 14 12:17:05 2022 +0200

    Adding compression tests both for runtime and testing
---
 .../Internal/Compression/CompressionTester.cs      | 33 ++++++++++++++++++
 .../Internal/Compression/Lz4Compression.cs         |  3 +-
 .../Internal/Compression/SnappyCompression.cs      |  3 +-
 .../Internal/Compression/ZlibCompression.cs        |  3 +-
 .../Internal/Compression/ZstdCompression.cs        |  2 +-
 tests/DotPulsar.Tests/DotPulsar.Tests.csproj       |  4 +++
 .../Internal/Compression/Lz4CompressionTests.cs    | 40 ++++++++++++++++++++++
 .../Internal/Compression/SnappyCompressionTests.cs | 40 ++++++++++++++++++++++
 .../Internal/Compression/ZlibCompressionTests.cs   | 40 ++++++++++++++++++++++
 .../Internal/Compression/ZstdCompressionTests.cs   | 40 ++++++++++++++++++++++
 10 files changed, 204 insertions(+), 4 deletions(-)

diff --git a/src/DotPulsar/Internal/Compression/CompressionTester.cs b/src/DotPulsar/Internal/Compression/CompressionTester.cs
new file mode 100644
index 0000000..2155924
--- /dev/null
+++ b/src/DotPulsar/Internal/Compression/CompressionTester.cs
@@ -0,0 +1,33 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace DotPulsar.Internal.Compression;
+
+using DotPulsar.Internal.Abstractions;
+using System;
+using System.Buffers;
+using System.Linq;
+
+public static class CompressionTester
+{
+    public static bool TestCompression(ICompressorFactory compressorFactory, IDecompressorFactory decompressorFactory)
+    {
+        using var compressor = compressorFactory.Create();
+        using var decompressor = decompressorFactory.Create();
+        var data = System.Text.Encoding.UTF8.GetBytes("Test data");
+        var compressedData = compressor.Compress(new ReadOnlySequence<byte>(data));
+        var decompressed = decompressor.Decompress(compressedData, data.Length).ToArray();
+        return data.SequenceEqual(decompressed);
+    }
+}
diff --git a/src/DotPulsar/Internal/Compression/Lz4Compression.cs b/src/DotPulsar/Internal/Compression/Lz4Compression.cs
index 4809484..9b50d47 100644
--- a/src/DotPulsar/Internal/Compression/Lz4Compression.cs
+++ b/src/DotPulsar/Internal/Compression/Lz4Compression.cs
@@ -47,7 +47,8 @@ public static class Lz4Compression
 
             compressorFactory = new CompressorFactory(PulsarApi.CompressionType.Lz4, () => new Compressor(CreateCompressor(encode, maximumOutputSize)));
             decompressorFactory = new DecompressorFactory(PulsarApi.CompressionType.Lz4, () => new Decompressor(CreateDecompressor(decode)));
-            return true;
+
+            return CompressionTester.TestCompression(compressorFactory, decompressorFactory);
         }
         catch
         {
diff --git a/src/DotPulsar/Internal/Compression/SnappyCompression.cs b/src/DotPulsar/Internal/Compression/SnappyCompression.cs
index 5d82747..0f8edf3 100644
--- a/src/DotPulsar/Internal/Compression/SnappyCompression.cs
+++ b/src/DotPulsar/Internal/Compression/SnappyCompression.cs
@@ -43,7 +43,8 @@ public static class SnappyCompression
 
             compressorFactory = new CompressorFactory(PulsarApi.CompressionType.Snappy, () => new Compressor(CreateCompressor(encode)));
             decompressorFactory = new DecompressorFactory(PulsarApi.CompressionType.Snappy, () => new Decompressor(CreateDecompressor(decode)));
-            return true;
+
+            return CompressionTester.TestCompression(compressorFactory, decompressorFactory);
         }
         catch
         {
diff --git a/src/DotPulsar/Internal/Compression/ZlibCompression.cs b/src/DotPulsar/Internal/Compression/ZlibCompression.cs
index f4bdcbe..4838ab6 100644
--- a/src/DotPulsar/Internal/Compression/ZlibCompression.cs
+++ b/src/DotPulsar/Internal/Compression/ZlibCompression.cs
@@ -43,7 +43,8 @@ public static class ZlibCompression
 
             compressorFactory = new CompressorFactory(PulsarApi.CompressionType.Zlib, () => new Compressor(CreateCompressor(compressBuffer)));
             decompressorFactory = new DecompressorFactory(PulsarApi.CompressionType.Zlib, () => new Decompressor(CreateDecompressor(uncompressBuffer)));
-            return true;
+
+            return CompressionTester.TestCompression(compressorFactory, decompressorFactory);
         }
         catch
         {
diff --git a/src/DotPulsar/Internal/Compression/ZstdCompression.cs b/src/DotPulsar/Internal/Compression/ZstdCompression.cs
index b9857d1..34b1fc1 100644
--- a/src/DotPulsar/Internal/Compression/ZstdCompression.cs
+++ b/src/DotPulsar/Internal/Compression/ZstdCompression.cs
@@ -63,7 +63,7 @@ public static class ZstdCompression
                 return new Decompressor(CreateDecompressor(unwrap), (IDisposable) decompressor);
             });
 
-            return true;
+            return CompressionTester.TestCompression(compressorFactory, decompressorFactory);
         }
         catch
         {
diff --git a/tests/DotPulsar.Tests/DotPulsar.Tests.csproj b/tests/DotPulsar.Tests/DotPulsar.Tests.csproj
index aee122b..f473d10 100644
--- a/tests/DotPulsar.Tests/DotPulsar.Tests.csproj
+++ b/tests/DotPulsar.Tests/DotPulsar.Tests.csproj
@@ -6,8 +6,11 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="DotNetZip" Version="1.16.0" />
     <PackageReference Include="Ductus.FluentDocker" Version="2.10.51" />
     <PackageReference Include="FluentAssertions" Version="6.7.0" />
+    <PackageReference Include="IronSnappy" Version="1.3.0" />
+    <PackageReference Include="K4os.Compression.LZ4" Version="1.2.16" />
     <PackageReference Include="NSubstitute" Version="4.3.0" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
     <PackageReference Include="xunit" Version="2.4.1" />
@@ -19,6 +22,7 @@
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
+    <PackageReference Include="ZstdNet" Version="1.4.5" />
   </ItemGroup>
 
   <ItemGroup>
diff --git a/tests/DotPulsar.Tests/Internal/Compression/Lz4CompressionTests.cs b/tests/DotPulsar.Tests/Internal/Compression/Lz4CompressionTests.cs
new file mode 100644
index 0000000..7866e95
--- /dev/null
+++ b/tests/DotPulsar.Tests/Internal/Compression/Lz4CompressionTests.cs
@@ -0,0 +1,40 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace DotPulsar.Tests.Internal.Compression;
+
+using DotPulsar.Internal.Abstractions;
+using DotPulsar.Internal.Compression;
+using FluentAssertions;
+using Xunit;
+
+[Trait("Category", "Unit")]
+public class Lz4CompressionTests
+{
+    [Fact]
+    public void Compression_GivenDataToCompressAndDecompress_ShouldReturnOriginalData()
+    {
+        // Arrange
+        var couldLoad = Lz4Compression.TryLoading(out ICompressorFactory? compressorFactory, out IDecompressorFactory? decompressorFactory);
+        couldLoad.Should().BeTrue();
+        using var compressor = compressorFactory!.Create();
+        using var decompressor = decompressorFactory!.Create();
+
+        // Act
+        var compressionWorks = CompressionTester.TestCompression(compressorFactory, decompressorFactory);
+
+        // Assert
+        compressionWorks.Should().BeTrue();
+    }
+}
diff --git a/tests/DotPulsar.Tests/Internal/Compression/SnappyCompressionTests.cs b/tests/DotPulsar.Tests/Internal/Compression/SnappyCompressionTests.cs
new file mode 100644
index 0000000..05b8d33
--- /dev/null
+++ b/tests/DotPulsar.Tests/Internal/Compression/SnappyCompressionTests.cs
@@ -0,0 +1,40 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace DotPulsar.Tests.Internal.Compression;
+
+using DotPulsar.Internal.Abstractions;
+using DotPulsar.Internal.Compression;
+using FluentAssertions;
+using Xunit;
+
+[Trait("Category", "Unit")]
+public class SnappyCompressionTests
+{
+    [Fact]
+    public void Compression_GivenDataToCompressAndDecompress_ShouldReturnOriginalData()
+    {
+        // Arrange
+        var couldLoad = SnappyCompression.TryLoading(out ICompressorFactory? compressorFactory, out IDecompressorFactory? decompressorFactory);
+        couldLoad.Should().BeTrue();
+        using var compressor = compressorFactory!.Create();
+        using var decompressor = decompressorFactory!.Create();
+
+        // Act
+        var compressionWorks = CompressionTester.TestCompression(compressorFactory, decompressorFactory);
+
+        // Assert
+        compressionWorks.Should().BeTrue();
+    }
+}
diff --git a/tests/DotPulsar.Tests/Internal/Compression/ZlibCompressionTests.cs b/tests/DotPulsar.Tests/Internal/Compression/ZlibCompressionTests.cs
new file mode 100644
index 0000000..c563844
--- /dev/null
+++ b/tests/DotPulsar.Tests/Internal/Compression/ZlibCompressionTests.cs
@@ -0,0 +1,40 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace DotPulsar.Tests.Internal.Compression;
+
+using DotPulsar.Internal.Abstractions;
+using DotPulsar.Internal.Compression;
+using FluentAssertions;
+using Xunit;
+
+[Trait("Category", "Unit")]
+public class ZlibCompressionTests
+{
+    [Fact]
+    public void Compression_GivenDataToCompressAndDecompress_ShouldReturnOriginalData()
+    {
+        // Arrange
+        var couldLoad = ZlibCompression.TryLoading(out ICompressorFactory? compressorFactory, out IDecompressorFactory? decompressorFactory);
+        couldLoad.Should().BeTrue();
+        using var compressor = compressorFactory!.Create();
+        using var decompressor = decompressorFactory!.Create();
+
+        // Act
+        var compressionWorks = CompressionTester.TestCompression(compressorFactory, decompressorFactory);
+
+        // Assert
+        compressionWorks.Should().BeTrue();
+    }
+}
diff --git a/tests/DotPulsar.Tests/Internal/Compression/ZstdCompressionTests.cs b/tests/DotPulsar.Tests/Internal/Compression/ZstdCompressionTests.cs
new file mode 100644
index 0000000..87460fa
--- /dev/null
+++ b/tests/DotPulsar.Tests/Internal/Compression/ZstdCompressionTests.cs
@@ -0,0 +1,40 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace DotPulsar.Tests.Internal.Compression;
+
+using DotPulsar.Internal.Abstractions;
+using DotPulsar.Internal.Compression;
+using FluentAssertions;
+using Xunit;
+
+[Trait("Category", "Unit")]
+public class ZstdCompressionTests
+{
+    [Fact]
+    public void Compression_GivenDataToCompressAndDecompress_ShouldReturnOriginalData()
+    {
+        // Arrange
+        var couldLoad = ZstdCompression.TryLoading(out ICompressorFactory? compressorFactory, out IDecompressorFactory? decompressorFactory);
+        couldLoad.Should().BeTrue();
+        using var compressor = compressorFactory!.Create();
+        using var decompressor = decompressorFactory!.Create();
+
+        // Act
+        var compressionWorks = CompressionTester.TestCompression(compressorFactory, decompressorFactory);
+
+        // Assert
+        compressionWorks.Should().BeTrue();
+    }
+}