Beispiel #1
0
        public void Constructor_CompletesWithExceptionIfInnerStreamThrows()
        {
            // Arrange
            Mock <Stream> mockInnerStream = new Mock <Stream>();

            mockInnerStream
            .Setup(
                s =>
                s.BeginWrite(
                    It.IsAny <byte[]>(),
                    It.IsAny <int>(),
                    It.IsAny <int>(),
                    It.IsAny <AsyncCallback>(),
                    It.IsAny <object>()
                    )
                )
            .Throws <ApplicationException>();
            ProgressStream progressStream = ProgressStreamTest.CreateProgressStream();

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

            // Assert
            Assert.True(result.IsCompleted);
            Assert.Throws <ApplicationException>(() => ProgressWriteAsyncResult.End(result));
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public void Constructor_BeginWriteOnInnerStream()
        {
            // Arrange
            Mock <Stream>  mockInnerStream = new Mock <Stream>();
            ProgressStream progressStream  = ProgressStreamTest.CreateProgressStream();

            // Act
            IAsyncResult result = new ProgressWriteAsyncResult(
                mockInnerStream.Object, progressStream, sampleData, 2, 4, null, null);

            // Assert
            mockInnerStream.Verify(s => s.BeginWrite(sampleData, 2, 4, It.IsAny <AsyncCallback>(), It.IsAny <object>()),
                                   Times.Once());
        }
        public void Constructor_BeginWriteOnInnerStream()
        {
            // Arrange
            Mock<Stream> mockInnerStream = new Mock<Stream>();
            ProgressStream progressStream = ProgressStreamTest.CreateProgressStream();

            // Act
            IAsyncResult result = new ProgressWriteAsyncResult(
                mockInnerStream.Object, progressStream, sampleData, 2, 4, null, null);

            // Assert 
            mockInnerStream.Verify(s => s.BeginWrite(sampleData, 2, 4, It.IsAny<AsyncCallback>(), It.IsAny<object>()),
                Times.Once());
        }
        public void Constructor_CompletesWithExceptionIfInnerStreamThrows()
        {
            // Arrange
            Mock<Stream> mockInnerStream = new Mock<Stream>();
            mockInnerStream.Setup(s => s.BeginWrite(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<AsyncCallback>(), It.IsAny<object>()))
                .Throws<ApplicationException>();
            ProgressStream progressStream = ProgressStreamTest.CreateProgressStream();

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

            // Assert 
            Assert.True(result.IsCompleted);
            Assert.Throws<ApplicationException>(() => ProgressWriteAsyncResult.End(result));
        }
        private static void WriteCompletedCallback(IAsyncResult result)
        {
            if (result.CompletedSynchronously)
            {
                return;
            }

            ProgressWriteAsyncResult thisPtr = (ProgressWriteAsyncResult)result.AsyncState;

            try
            {
                thisPtr.WriteCompleted(result);
            }
            catch (Exception e)
            {
                thisPtr.Complete(false, e);
            }
        }
        public void Constructor_CompletesSynchronouslyIfInnerStreamCompletesSynchronously()
        {
            // 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);
            ProgressStream progressStream = ProgressStreamTest.CreateProgressStream();

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

            // Assert 
            Assert.True(result.IsCompleted);
            Assert.True(result.CompletedSynchronously);
            Assert.Same(userState, result.AsyncState);
        }
Beispiel #8
0
        public void Constructor_CompletesSynchronouslyIfInnerStreamCompletesSynchronously()
        {
            // 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);
            ProgressStream progressStream = ProgressStreamTest.CreateProgressStream();

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

            // Assert
            Assert.True(result.IsCompleted);
            Assert.True(result.CompletedSynchronously);
            Assert.Same(userState, result.AsyncState);
        }
 public override void EndWrite(IAsyncResult asyncResult)
 {
     ProgressWriteAsyncResult.End(asyncResult);
 }
        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);
        }