예제 #1
0
        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();
            }
        }
예제 #2
0
        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++]);
                }
            }
        }
예제 #3
0
        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);
        }
예제 #4
0
        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++]);
                }
            }
        }