public async Task BeginEndRead_ReportsBytesRead()
        {
            // Arrange
            HttpResponseMessage response = CreateResponse();
            Stream innerStream           = await response.Content.ReadAsStreamAsync();

            long?expectedLength = response.Content.Headers.ContentLength;
            MockProgressEventHandler mockProgressEventHandler;
            ProgressMessageHandler   progressMessageHandler = MockProgressEventHandler.CreateProgressMessageHandler(out mockProgressEventHandler, sendProgress: false);
            ProgressStream           progressStream         = CreateProgressStream(innerStream: innerStream, progressMessageHandler: progressMessageHandler, response: response);
            object userState = new object();

            // Act/Assert
            int totalBytesRead = 0;
            int bytesRead      = 0;

            do
            {
                byte[]       buffer = new byte[8];
                IAsyncResult result = progressStream.BeginRead(buffer, 0, buffer.Length, null, userState);
                bytesRead       = progressStream.EndRead(result);
                totalBytesRead += bytesRead;

                Assert.Same(userState, mockProgressEventHandler.EventArgs.UserState);
                Assert.Equal(totalBytesRead, mockProgressEventHandler.EventArgs.BytesTransferred);
                Assert.Equal((100L * totalBytesRead) / expectedLength, mockProgressEventHandler.EventArgs.ProgressPercentage);
            }while (bytesRead > 0);

            Assert.Equal(expectedLength, mockProgressEventHandler.EventArgs.TotalBytes);
            Assert.Equal(100, mockProgressEventHandler.EventArgs.ProgressPercentage);
        }
        public async Task WriteAsync_ReportsBytesWritten()
        {
            // Arrange
            using (ManualResetEvent writeComplete = new ManualResetEvent(false))
            {
                HttpRequestMessage request     = CreateRequest();
                Stream             innerStream = new MemoryStream();
                byte[]             buffer      = CreateBufferContent();
                long?expectedLength            = request.Content.Headers.ContentLength;
                MockProgressEventHandler mockProgressEventHandler;
                ProgressMessageHandler   progressMessageHandler = MockProgressEventHandler.CreateProgressMessageHandler(out mockProgressEventHandler, sendProgress: true);
                ProgressStream           progressStream         = CreateProgressStream(innerStream: innerStream, progressMessageHandler: progressMessageHandler, request: request);
                object userState = new object();

                // Act/Assert
                int totalBytesWritten = 0;
                int bytesWritten      = 0;
                while (totalBytesWritten < expectedLength)
                {
                    bytesWritten = Math.Min(8, (int)expectedLength - totalBytesWritten);
                    await progressStream.WriteAsync(buffer, totalBytesWritten, bytesWritten);

                    totalBytesWritten += bytesWritten;

                    Assert.Equal(totalBytesWritten, mockProgressEventHandler.EventArgs.BytesTransferred);
                    Assert.Equal((100L * totalBytesWritten) / expectedLength, mockProgressEventHandler.EventArgs.ProgressPercentage);
                }

                Assert.Equal(expectedLength, mockProgressEventHandler.EventArgs.TotalBytes);
                Assert.Equal(100, mockProgressEventHandler.EventArgs.ProgressPercentage);
            }
        }
        public async Task ReadByte_ReportsBytesRead()
        {
            // Arrange
            HttpResponseMessage response = CreateResponse();
            Stream innerStream           = await response.Content.ReadAsStreamAsync();

            long?expectedLength = response.Content.Headers.ContentLength;
            MockProgressEventHandler mockProgressEventHandler;
            ProgressMessageHandler   progressMessageHandler = MockProgressEventHandler.CreateProgressMessageHandler(out mockProgressEventHandler, sendProgress: false);
            ProgressStream           progressStream         = CreateProgressStream(innerStream: innerStream, progressMessageHandler: progressMessageHandler, response: response);

            // Act/Assert
            int totalBytesRead = 0;

            while (progressStream.ReadByte() != -1)
            {
                totalBytesRead += 1;

                Assert.Equal(totalBytesRead, mockProgressEventHandler.EventArgs.BytesTransferred);
                Assert.Equal((100L * totalBytesRead) / expectedLength, mockProgressEventHandler.EventArgs.ProgressPercentage);
            }

            Assert.Equal(expectedLength, mockProgressEventHandler.EventArgs.TotalBytes);
            Assert.Equal(100, mockProgressEventHandler.EventArgs.ProgressPercentage);
        }
        public void WriteByte_ReportsBytesWritten()
        {
            // Arrange
            HttpRequestMessage request     = CreateRequest();
            Stream             innerStream = new MemoryStream();

            byte[] buffer         = CreateBufferContent();
            long?  expectedLength = request.Content.Headers.ContentLength;
            MockProgressEventHandler mockProgressEventHandler;
            ProgressMessageHandler   progressMessageHandler = MockProgressEventHandler.CreateProgressMessageHandler(out mockProgressEventHandler, sendProgress: true);
            ProgressStream           progressStream         = CreateProgressStream(innerStream: innerStream, progressMessageHandler: progressMessageHandler, request: request);

            // Act/Assert
            int totalBytesWritten = 0;

            while (totalBytesWritten < expectedLength)
            {
                progressStream.WriteByte(buffer[totalBytesWritten]);
                totalBytesWritten += 1;

                Assert.Equal(totalBytesWritten, mockProgressEventHandler.EventArgs.BytesTransferred);
                Assert.Equal((100L * totalBytesWritten) / expectedLength, mockProgressEventHandler.EventArgs.ProgressPercentage);
            }

            Assert.Equal(expectedLength, mockProgressEventHandler.EventArgs.TotalBytes);
            Assert.Equal(100, mockProgressEventHandler.EventArgs.ProgressPercentage);
        }
Exemplo n.º 5
0
        public async Task SerializeToStreamAsync_InsertsProgressStream()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();

            request.Content = new StringContent("HelloWorld!");

            MockProgressEventHandler progressEventHandler = new MockProgressEventHandler();
            ProgressMessageHandler   progressHandler      =
                MockProgressEventHandler.CreateProgressMessageHandler(
                    out progressEventHandler,
                    sendProgress: true
                    );
            ProgressContent progressContent = new ProgressContent(
                request.Content,
                progressHandler,
                request
                );
            MemoryStream memStream = new MemoryStream();

            // Act
            await progressContent.CopyToAsync(memStream);

            // Assert
            Assert.True(progressEventHandler.WasInvoked);
            Assert.Equal(request, progressEventHandler.Sender);
            Assert.Equal(
                request.Content.Headers.ContentLength,
                progressEventHandler.EventArgs.TotalBytes
                );
        }
Exemplo n.º 6
0
        public void Constructor_ReportsBytesWritten(int offset, int count)
        {
            // Arrange
            Mock <Stream> mockInnerStream  = new Mock <Stream>();
            object        userState        = new object();
            IAsyncResult  mockIAsyncResult = CreateMockCompletedAsyncResult(true, userState);

            mockInnerStream.Setup(s => s.BeginWrite(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <AsyncCallback>(), It.IsAny <object>()))
            .Returns(mockIAsyncResult);

            MockProgressEventHandler mockProgressHandler;
            ProgressMessageHandler   progressMessageHandler = MockProgressEventHandler.CreateProgressMessageHandler(out mockProgressHandler, sendProgress: true);
            HttpRequestMessage       request = new HttpRequestMessage();

            ProgressStream progressStream = ProgressStreamTest.CreateProgressStream(progressMessageHandler: progressMessageHandler, request: request);

            // Act
            IAsyncResult result = new ProgressWriteAsyncResult(
                mockInnerStream.Object, progressStream, sampleData, offset, count, null, userState);

            // Assert
            Assert.True(mockProgressHandler.WasInvoked);
            Assert.Same(request, mockProgressHandler.Sender);
            Assert.Equal(count, mockProgressHandler.EventArgs.BytesTransferred);
            Assert.Same(userState, mockProgressHandler.EventArgs.UserState);
        }
Exemplo n.º 7
0
        public void BeginEndWrite_ReportsBytesWritten()
        {
            // Arrange
            HttpRequestMessage request     = CreateRequest();
            Stream             innerStream = new MemoryStream();

            byte[] buffer         = CreateBufferContent();
            long?  expectedLength = request.Content.Headers.ContentLength;
            MockProgressEventHandler mockProgressEventHandler;
            ProgressMessageHandler   progressMessageHandler = MockProgressEventHandler.CreateProgressMessageHandler(out mockProgressEventHandler, sendProgress: true);
            ProgressStream           progressStream         = CreateProgressStream(innerStream: innerStream, progressMessageHandler: progressMessageHandler, request: request);
            object userState = new object();

            // Act/Assert
            int totalBytesWritten = 0;
            int bytesWritten      = 0;

            while (totalBytesWritten < expectedLength)
            {
                bytesWritten = Math.Min(8, (int)expectedLength - totalBytesWritten);
                IAsyncResult result = progressStream.BeginWrite(buffer, totalBytesWritten, bytesWritten, null, userState);
                progressStream.EndWrite(result);
                totalBytesWritten += bytesWritten;

                Assert.Same(userState, mockProgressEventHandler.EventArgs.UserState);
                Assert.Equal(totalBytesWritten, mockProgressEventHandler.EventArgs.BytesTransferred);
                Assert.Equal((100L * totalBytesWritten) / expectedLength, mockProgressEventHandler.EventArgs.ProgressPercentage);
            }

            Assert.Equal(expectedLength, mockProgressEventHandler.EventArgs.TotalBytes);
            Assert.Equal(100, mockProgressEventHandler.EventArgs.ProgressPercentage);
        }
        public Task SerializeToStreamAsync_InsertsProgressStream()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();
            request.Content = new StringContent("HelloWorld!");

            MockProgressEventHandler progressEventHandler = new MockProgressEventHandler();
            ProgressMessageHandler progressHandler = MockProgressEventHandler.CreateProgressMessageHandler(out progressEventHandler, sendProgress: true);
            ProgressContent progressContent = new ProgressContent(request.Content, progressHandler, request);
            MemoryStream memStream = new MemoryStream();

            // Act
            return progressContent.CopyToAsync(memStream).ContinueWith(
                task =>
                {
                    // Assert
                    Assert.Equal(TaskStatus.RanToCompletion, task.Status);
                    Assert.True(progressEventHandler.WasInvoked);
                    Assert.Equal(request, progressEventHandler.Sender);
                    Assert.Equal(request.Content.Headers.ContentLength, progressEventHandler.EventArgs.TotalBytes);
                });
        }