示例#1
0
        private void ReadStreamTest(IStorage storage)
        {
            // arrange
            var streamSize    = 20480;
            var randomlyBytes = DummyData.GenerateRandomBytes(streamSize);
            var chunk         = new Chunk(0, streamSize - 1)
            {
                Timeout = 100, Storage = storage
            };
            var chunkDownloader = new ChunkDownloader(chunk, _configuration);

            using var memoryStream = new MemoryStream(randomlyBytes);

            // act
            chunkDownloader.ReadStream(memoryStream, new CancellationToken()).Wait();

            // assert
            Assert.AreEqual(memoryStream.Length, chunkDownloader.Chunk.Storage.GetLength());
            var chunkStream = chunkDownloader.Chunk.Storage.OpenRead();

            for (int i = 0; i < streamSize; i++)
            {
                Assert.AreEqual(randomlyBytes[i], chunkStream.ReadByte());
            }

            chunkDownloader.Chunk.Clear();
        }
示例#2
0
        private void ReadStreamProgressEventsTest(IStorage storage)
        {
            // arrange
            var eventCount    = 0;
            var receivedBytes = new List <byte>();
            var streamSize    = 9 * _configuration.BufferBlockSize;
            var source        = DummyData.GenerateRandomBytes(streamSize);

            using var sourceMemoryStream = new MemoryStream(source);
            var chunk = new Chunk(0, streamSize - 1)
            {
                Timeout = 100, Storage = storage
            };
            var chunkDownloader = new ChunkDownloader(chunk, _configuration);

            chunkDownloader.DownloadProgressChanged += (s, e) => {
                eventCount++;
                receivedBytes.AddRange(e.ReceivedBytes);
            };

            // act
            chunkDownloader.ReadStream(sourceMemoryStream, new CancellationToken()).Wait();

            // assert
            Assert.AreEqual(streamSize / _configuration.BufferBlockSize, eventCount);
            Assert.AreEqual(chunkDownloader.Chunk.Length, receivedBytes.Count);
            Assert.IsTrue(source.SequenceEqual(receivedBytes));

            chunkDownloader.Chunk.Clear();
        }
示例#3
0
        public void ReadStreamTimeoutExceptionTest()
        {
            // arrange
            var streamSize    = 20480;
            var randomlyBytes = DummyData.GenerateRandomBytes(streamSize);
            var chunk         = new Chunk(0, streamSize - 1)
            {
                Timeout = 100
            };
            var chunkDownloader = new ChunkDownloader(chunk, _configuration);

            using var memoryStream = new MemoryStream(randomlyBytes);
            var canceledToken = new CancellationToken(true);

            // act
            async Task CallReadStream() => await chunkDownloader.ReadStream(new MemoryStream(), canceledToken).ConfigureAwait(false);

            // assert
            Assert.ThrowsExceptionAsync <OperationCanceledException>(CallReadStream);
        }