-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Shuttle/async
Async
- Loading branch information
Showing
17 changed files
with
285 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,76 @@ | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
|
||
namespace Shuttle.Core.Compression.Tests | ||
namespace Shuttle.Core.Compression.Tests; | ||
|
||
[TestFixture] | ||
public class Fixture | ||
{ | ||
[TestFixture] | ||
public class Fixture | ||
[Test] | ||
public void Should_be_able_to_compress_and_decompress_using_gzip() | ||
{ | ||
[Test] | ||
public void Should_be_able_to_compress_and_decompress_using_gzip() | ||
{ | ||
var algorithm = new GZipCompressionAlgorithm(); | ||
var algorithm = new GZipCompressionAlgorithm(); | ||
|
||
const string text = "gzip compression algorithm"; | ||
|
||
const string text = "gzip compression algorithm"; | ||
AssertAlgorithm(algorithm, text); | ||
} | ||
|
||
AssertAlgorithm(algorithm, text); | ||
} | ||
private static void AssertAlgorithm(ICompressionAlgorithm algorithm, string text) | ||
{ | ||
Assert.AreEqual(text, Encoding.UTF8.GetString(algorithm.Decompress(algorithm.Compress(Encoding.UTF8.GetBytes(text))))); | ||
|
||
private static void AssertAlgorithm(ICompressionAlgorithm algorithm, string text) | ||
{ | ||
Assert.AreEqual(text, | ||
Encoding.UTF8.GetString(algorithm.Decompress(algorithm.Compress(Encoding.UTF8.GetBytes(text))))); | ||
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)); | ||
using var compressed = algorithm.Compress(stream); | ||
using var decompressed = algorithm.Decompress(compressed); | ||
using var decompressedStream = new MemoryStream(); | ||
|
||
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text))) | ||
using (var compressed = algorithm.Compress(stream)) | ||
using (var decompressed = algorithm.Decompress(compressed)) | ||
using (var decompressedStream = new MemoryStream()) | ||
{ | ||
decompressed.CopyTo(decompressedStream); | ||
decompressed.CopyTo(decompressedStream); | ||
|
||
Assert.AreEqual(text, Encoding.UTF8.GetString(decompressedStream.ToArray())); | ||
} | ||
} | ||
Assert.AreEqual(text, Encoding.UTF8.GetString(decompressedStream.ToArray())); | ||
} | ||
|
||
[Test] | ||
public void Should_be_able_to_compress_and_decompress_using_deflate() | ||
{ | ||
var algorithm = new DeflateCompressionAlgorithm(); | ||
[Test] | ||
public void Should_be_able_to_compress_and_decompress_using_deflate() | ||
{ | ||
var algorithm = new DeflateCompressionAlgorithm(); | ||
|
||
const string text = "deflate compression algorithm"; | ||
|
||
AssertAlgorithm(algorithm, text); | ||
} | ||
|
||
[Test] | ||
public async Task Should_be_able_to_compress_and_decompress_using_gzip_async() | ||
{ | ||
var algorithm = new GZipCompressionAlgorithm(); | ||
|
||
const string text = "gzip compression algorithm"; | ||
|
||
await AssertAlgorithmAsync(algorithm, text); | ||
} | ||
|
||
private static async Task AssertAlgorithmAsync(ICompressionAlgorithm algorithm, string text) | ||
{ | ||
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)); | ||
await using var compressed = await algorithm.CompressAsync(stream); | ||
await using var decompressed = await algorithm.DecompressAsync(compressed); | ||
using var decompressedStream = new MemoryStream(); | ||
|
||
await decompressed.CopyToAsync(decompressedStream); | ||
|
||
Assert.AreEqual(text, Encoding.UTF8.GetString(decompressedStream.ToArray())); | ||
} | ||
|
||
[Test] | ||
public async Task Should_be_able_to_compress_and_decompress_using_deflate_async() | ||
{ | ||
var algorithm = new DeflateCompressionAlgorithm(); | ||
|
||
const string text = "deflate compression algorithm"; | ||
const string text = "deflate compression algorithm"; | ||
|
||
AssertAlgorithm(algorithm, text); | ||
} | ||
await AssertAlgorithmAsync(algorithm, text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Threading.Tasks; | ||
using Shuttle.Core.Contract; | ||
|
||
namespace Shuttle.Core.Compression | ||
{ | ||
public static class CompressionServiceExtensions | ||
{ | ||
public static byte[] Compress(this ICompressionService compressionService, string name, byte[] bytes) | ||
{ | ||
return Guard.AgainstNull(compressionService, nameof(compressionService)).Get(name).Compress(bytes); | ||
} | ||
|
||
public static byte[] Decompress(this ICompressionService compressionService, string name, byte[] bytes) | ||
{ | ||
return Guard.AgainstNull(compressionService, nameof(compressionService)).Get(name).Decompress(bytes); | ||
} | ||
|
||
public static async Task<byte[]> CompressAsync(this ICompressionService compressionService, string name, byte[] bytes) | ||
{ | ||
return await Guard.AgainstNull(compressionService, nameof(compressionService)).Get(name).CompressAsync(bytes); | ||
} | ||
|
||
public static async Task<byte[]> DecompressAsync(this ICompressionService compressionService, string name, byte[] bytes) | ||
{ | ||
return await Guard.AgainstNull(compressionService, nameof(compressionService)).Get(name).DecompressAsync(bytes); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Shuttle.Core.Contract; | ||
|
||
namespace Shuttle.Core.Compression | ||
{ | ||
public static class DeflateCompressionExtensions | ||
{ | ||
public static CompressionBuilder AddDeflate(this CompressionBuilder builder) | ||
{ | ||
Guard.AgainstNull(builder, nameof(builder)).Services.AddSingleton<ICompressionAlgorithm, DeflateCompressionAlgorithm>(); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
Oops, something went wrong.