コード例 #1
0
        public async Task CopyToAsync_AutoFlushing(bool autoFlush, int expectedFlushes)
        {
            // Arrange
            const int SourceSize = (128 * 1024) - 3;

            var sourceBytes = Enumerable.Range(0, SourceSize).Select(i => (byte)(i % 256)).ToArray();
            var source      = new MemoryStream(sourceBytes);
            var destination = new MemoryStream();
            var flushCountingDestination = new FlushCountingStream(destination);

            var streamCopierMock = new Mock <IStreamCopier>();

            streamCopierMock
            .Setup(s => s.CopyAsync(source, It.IsAny <Stream>(), It.IsAny <CancellationToken>()))
            .Returns(async(Stream source_, Stream destination_, CancellationToken cancellation_) =>
            {
                await source_.CopyToAsync(destination_, cancellation_);
                return(StreamCopyResult.Success, null);
            });

            var sut = new StreamCopyHttpContent(source, streamCopierMock.Object, autoFlushHttpClientOutgoingStream: autoFlush, CancellationToken.None);

            // Act & Assert
            Assert.False(sut.ConsumptionTask.IsCompleted);
            Assert.False(sut.Started);
            await sut.CopyToAsync(flushCountingDestination);

            Assert.True(sut.Started);
            Assert.True(sut.ConsumptionTask.IsCompleted);
            Assert.Equal(sourceBytes, destination.ToArray());
            Assert.Equal(expectedFlushes, flushCountingDestination.NumFlushes);
        }
コード例 #2
0
        public async Task CopyToAsync_AutoFlushing(bool autoFlush)
        {
            // Must be same as StreamCopier constant.
            const int DefaultBufferSize = 65536;
            const int SourceSize        = (128 * 1024) - 3;

            var expectedFlushes = 0;

            if (autoFlush)
            {
                // How many buffers is needed to send the source rounded up.
                expectedFlushes = (SourceSize - 1) / DefaultBufferSize + 1;
            }
            // Explicit flush after headers are sent.
            expectedFlushes++;

            var sourceBytes = Enumerable.Range(0, SourceSize).Select(i => (byte)(i % 256)).ToArray();
            var source      = new MemoryStream(sourceBytes);
            var destination = new MemoryStream();
            var flushCountingDestination = new FlushCountingStream(destination);

            var sut = CreateContent(source, autoFlushHttpClientOutgoingStream: autoFlush);

            Assert.False(sut.ConsumptionTask.IsCompleted);
            Assert.False(sut.Started);
            await sut.CopyToWithCancellationAsync(flushCountingDestination);

            Assert.True(sut.Started);
            Assert.True(sut.ConsumptionTask.IsCompleted);
            Assert.Equal(sourceBytes, destination.ToArray());
            Assert.Equal(expectedFlushes, flushCountingDestination.NumFlushes);
        }
コード例 #3
0
        public async Task CopyToAsync_AutoFlushing(bool autoFlush, int expectedFlushes)
        {
            const int SourceSize = (128 * 1024) - 3;

            var sourceBytes = Enumerable.Range(0, SourceSize).Select(i => (byte)(i % 256)).ToArray();
            var source      = new MemoryStream(sourceBytes);
            var destination = new MemoryStream();
            var flushCountingDestination = new FlushCountingStream(destination);

            var sut = new StreamCopyHttpContent(source, autoFlushHttpClientOutgoingStream: autoFlush, new Clock(), CancellationToken.None);

            Assert.False(sut.ConsumptionTask.IsCompleted);
            Assert.False(sut.Started);
            await sut.CopyToAsync(flushCountingDestination);

            Assert.True(sut.Started);
            Assert.True(sut.ConsumptionTask.IsCompleted);
            Assert.Equal(sourceBytes, destination.ToArray());
            Assert.Equal(expectedFlushes, flushCountingDestination.NumFlushes);
        }