public void MergeChunksByMemoryStorageTest() { // arrange var fileSize = 1024; var chunkCount = 8; var counter = 0; var mergedFilename = FileHelper.GetTempFile(""); Chunk[] chunks = _chunkHub.ChunkFile(fileSize, chunkCount); foreach (Chunk chunk in chunks) { chunk.Storage = new MemoryStorage(chunk.Length); var dummyBytes = DummyData.GenerateRandomBytes((int)chunk.Length); chunk.Storage.WriteAsync(dummyBytes, 0, dummyBytes.Length).Wait(); } // act _chunkHub.MergeChunks(chunks, mergedFilename).Wait(); // assert Assert.IsTrue(File.Exists(mergedFilename)); var mergedData = File.ReadAllBytes(mergedFilename); foreach (Chunk chunk in chunks) { var chunkStream = chunk.Storage.OpenRead(); for (int i = 0; i < chunkStream.Length; i++) { Assert.AreEqual(chunkStream.ReadByte(), mergedData[counter++]); } chunk.Clear(); } }
private void MergeChunksTest(bool onTheFly) { // arrange var fileSize = 10240; var chunkCount = 8; var counter = 0; _configuration.OnTheFlyDownload = onTheFly; var chunkHub = new ChunkHub(_configuration); Chunk[] chunks = chunkHub.ChunkFile(fileSize, chunkCount); List <byte[]> chunksData = new List <byte[]>(); foreach (Chunk chunk in chunks) { var dummyBytes = DummyData.GenerateRandomBytes((int)chunk.Length); chunksData.Add(dummyBytes); chunk.Storage.WriteAsync(dummyBytes, 0, dummyBytes.Length).Wait(); } // act using MemoryStream destinationStream = new MemoryStream(); chunkHub.MergeChunks(chunks, destinationStream, new CancellationToken()).Wait(); // assert var mergedData = destinationStream.ToArray(); foreach (byte[] chunkData in chunksData) { foreach (var byteOfChunkData in chunkData) { Assert.AreEqual(byteOfChunkData, mergedData[counter++]); } } }
public void MergeChunksCancellationExceptionTest() { // arrange var chunkHub = new ChunkHub(_configuration); Chunk[] chunks = chunkHub.ChunkFile(10240, 8); // act async Task MergeAct() => await chunkHub.MergeChunks(chunks, new MemoryStream(), CancellationToken.None).ConfigureAwait(false); // assert Assert.ThrowsExceptionAsync <OperationCanceledException>(MergeAct); }
private void MergeChunksTest(bool onTheFly) { // arrange var fileSize = 10240; var chunkCount = 8; var counter = 0; _configuration.OnTheFlyDownload = onTheFly; var chunkHub = new ChunkHub(_configuration); var mergedFilename = FileHelper.GetTempFile(""); Chunk[] chunks = chunkHub.ChunkFile(fileSize, chunkCount); List <byte[]> chunksData = new List <byte[]>(); foreach (Chunk chunk in chunks) { var dummyBytes = DummyData.GenerateRandomBytes((int)chunk.Length); chunksData.Add(dummyBytes); chunk.Storage.WriteAsync(dummyBytes, 0, dummyBytes.Length).Wait(); } // act chunkHub.MergeChunks(chunks, mergedFilename).Wait(); // assert Assert.IsTrue(File.Exists(mergedFilename)); var mergedData = File.ReadAllBytes(mergedFilename); foreach (byte[] chunkData in chunksData) { foreach (var byteOfChunkData in chunkData) { Assert.AreEqual(byteOfChunkData, mergedData[counter++]); } } }