public static IReturnsResult<CloudBlobStream> ReturnsCompletingAsynchronously(
            this ISetup<CloudBlobStream, ICancellableAsyncResult> setup,
            CancellableAsyncCompletionSource completionSource)
        {
            if (setup == null)
            {
                throw new ArgumentNullException("setup");
            }

            return setup.Returns<AsyncCallback, object>((callback, state) =>
            {
                CompletingCancellableAsyncResult result = new CompletingCancellableAsyncResult(callback, state);
                completionSource.SetAsyncResult(result);
                return result;
            });
        }
예제 #2
0
        public static IReturnsResult <CloudBlobStream> ReturnsCompletingAsynchronously(
            this ISetup <CloudBlobStream, ICancellableAsyncResult> setup,
            CancellableAsyncCompletionSource completionSource)
        {
            if (setup == null)
            {
                throw new ArgumentNullException("setup");
            }

            return(setup.Returns <AsyncCallback, object>((callback, state) =>
            {
                CompletingCancellableAsyncResult result = new CompletingCancellableAsyncResult(callback, state);
                completionSource.SetAsyncResult(result);
                return result;
            }));
        }
        public void EndFlush_DuringCallback_WhenInnerStreamThrows_PropogatesException()
        {
            // Arrange
            Exception expectedException = new Exception();
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource completion = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginFlush()
                .ReturnsCompletingAsynchronously(completion);
            innerStreamMock
                .SetupEndFlush()
                .Throws(expectedException);
            CloudBlobStream innerStream = innerStreamMock.Object;
            CloudBlobStream product = CreateProductUnderTest(innerStream);

            bool callbackCalled = false;
            IAsyncResult result = null;
            AsyncCallback callback = (_) =>
            {
                Exception exception = Assert.Throws<Exception>(() => product.EndFlush(result));
                Assert.Same(expectedException, exception);
                callbackCalled = true;
            };
            object state = null;
            result = product.BeginFlush(callback, state);

            // Act & Assert
            completion.Complete();
            Assert.True(callbackCalled);
        }
        public void EndFlush_DuringCallback_DelegatesToInnerStreamEndFlush()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource completion = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginFlush()
                .ReturnsCompletingAsynchronously(completion);
            innerStreamMock
                .Setup(s => s.EndFlush(It.Is<IAsyncResult>((ar) => ar == completion.AsyncResult)))
                .Verifiable();
            CloudBlobStream innerStream = innerStreamMock.Object;
            CloudBlobStream product = CreateProductUnderTest(innerStream);

            bool callbackCalled = false;
            AsyncCallback callback = (ar) =>
            {
                product.EndFlush(ar);
                callbackCalled = true;
            };
            object state = null;

            IAsyncResult result = product.BeginFlush(callback, state);

            // Act
            completion.Complete();

            // Assert
            Assert.True(callbackCalled);
            innerStreamMock.Verify();
        }
        public void BeginFlush_WhenCompletedAsynchronously_CallsCallbackAndCompletesResult()
        {
            // Arrange
            object expectedState = new object();
            ExpectedAsyncResult expectedResult = new ExpectedAsyncResult
            {
                AsyncState = expectedState,
                CompletedSynchronously = false,
                IsCompleted = true
            };

            bool callbackCalled = false;
            IAsyncResult callbackResult = null;
            CloudBlobStream product = null;

            AsyncCallback callback = (ar) =>
            {
                callbackResult = ar;
                AssertEqual(expectedResult, ar);
                callbackCalled = true;
            };

            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource completion = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginFlush()
                .ReturnsCompletingAsynchronously(completion);
            innerStreamMock
                .SetupEndRead()
                .Returns(-1);
            CloudBlobStream innerStream = innerStreamMock.Object;
            product = CreateProductUnderTest(innerStream);

            IAsyncResult result = product.BeginFlush(callback, expectedState);

            // Act
            completion.Complete();

            // Assert
            Assert.True(callbackCalled);
            // An AsyncCallback must be called with the same IAsyncResult instance as the Begin method returned.
            Assert.Same(result, callbackResult);
            AssertEqual(expectedResult, result, disposeActual: true);
        }
        public void BeginFlush_Cancel_WhenNotYetCompleted_CallsInnerCancellableAsyncResultCancel()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource spy = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginFlush()
                .ReturnsCompletingAsynchronously(spy);
            CloudBlobStream innerStream = innerStreamMock.Object;
            CloudBlobStream product = CreateProductUnderTest(innerStream);

            AsyncCallback callback = null;
            object state = null;

            ICancellableAsyncResult result = product.BeginFlush(callback, state);
            Assert.NotNull(result); // Guard

            // Act
            result.Cancel();

            // Assert
            Assert.True(spy.Canceled);

            // Cleanup
            result.AsyncWaitHandle.Dispose();
        }
        public void EndCommit_WhenInnerStreamThrows_PropogatesException()
        {
            // Arrange
            Exception expectedException = new Exception();
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource completion = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginCommit()
                .ReturnsCompletingAsynchronously(completion);
            innerStreamMock
                .SetupEndCommit()
                .Throws(expectedException);
            CloudBlobStream innerStream = innerStreamMock.Object;
            CloudBlobStream product = CreateProductUnderTest(innerStream);

            AsyncCallback callback = null;
            object state = null;
            IAsyncResult result = product.BeginCommit(callback, state);
            completion.Complete();

            // Act & Assert
            Exception exception = Assert.Throws<Exception>(() => product.EndCommit(result));
            Assert.Same(expectedException, exception);
        }
        public void EndCommit_DelegatesToInnerStreamEndCommit()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource completion = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginCommit()
                .ReturnsCompletingAsynchronously(completion);
            innerStreamMock
                .Setup(s => s.EndCommit(It.Is<IAsyncResult>((ar) => ar == completion.AsyncResult)))
                .Verifiable();
            CloudBlobStream innerStream = innerStreamMock.Object;
            CloudBlobStream product = CreateProductUnderTest(innerStream);

            AsyncCallback callback = null;
            object state = null;

            IAsyncResult result = product.BeginCommit(callback, state);
            completion.Complete();

            // Act
            product.EndCommit(result);

            // Assert
            innerStreamMock.Verify();
        }
        public void CompleteAsync_WhenUnchanged_DoesNotCommitAndReturnsFalse()
        {
            // Arrange
            bool committed = false;
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource spy = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginCommit()
                .Callback(() => committed = true)
                .ReturnsCompletedSynchronously();
            innerStreamMock.SetupEndCommit();
            CloudBlobStream innerStream = innerStreamMock.Object;
            WatchableCloudBlobStream product = CreateProductUnderTest(innerStream);

            CancellationToken cancellationToken = CancellationToken.None;

            // Act
            Task<bool> task = product.CompleteAsync(cancellationToken);

            // Assert
            Assert.False(committed);
            Assert.NotNull(task);
            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.False(task.Result);
        }
        public void CompleteAsync_WhenChangedAndCommitted_DoesNotCommitAgainButStillReturnsTrue()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource spy = new CancellableAsyncCompletionSource();
            bool committedAgain = false;
            innerStreamMock.Setup(s => s.WriteByte(It.IsAny<byte>()));
            innerStreamMock.Setup(s => s.Commit());
            innerStreamMock
                .SetupBeginCommit()
                .Callback(() => committedAgain = true)
                .ReturnsCompletedSynchronously();
            innerStreamMock.SetupEndCommit();
            CloudBlobStream innerStream = innerStreamMock.Object;
            WatchableCloudBlobStream product = CreateProductUnderTest(innerStream);

            product.WriteByte(0x00);
            product.Commit();

            CancellationToken cancellationToken = CancellationToken.None;

            // Act
            Task<bool> task = product.CompleteAsync(cancellationToken);

            // Assert
            Assert.False(committedAgain);
            Assert.NotNull(task);
            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.True(task.Result);
        }
        public void CompleteAsync_WhenChangedAndCancellationIsRequested_CallsCancellableAsyncResultCancel()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource spy = new CancellableAsyncCompletionSource();
            innerStreamMock.Setup(s => s.WriteByte(It.IsAny<byte>()));
            innerStreamMock
                .SetupBeginCommit()
                .ReturnsCompletingAsynchronously(spy);
            innerStreamMock.SetupEndCommit();
            CloudBlobStream innerStream = innerStreamMock.Object;
            WatchableCloudBlobStream product = CreateProductUnderTest(innerStream);

            product.WriteByte(0x00);

            CancellationToken cancellationToken = new CancellationToken(canceled: true);
            Task task = product.CompleteAsync(cancellationToken);

            // Act
            spy.Complete();

            // Assert
            Assert.True(spy.Canceled);
            Assert.NotNull(task);
            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }
        public void CommitAsync_WhenNotCompletedSynchronously_CancelTokenDuringEndCommit_DoesNotCallAsyncResultCancel()
        {
            // Arrange
            using (CancellationTokenSource tokenSource = new CancellationTokenSource())
            {
                Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
                CancellableAsyncCompletionSource spy = new CancellableAsyncCompletionSource();
                innerStreamMock
                    .SetupBeginCommit()
                    .ReturnsCompletingAsynchronously(spy);
                innerStreamMock
                    .SetupEndCommit()
                    .Callback(() => tokenSource.Cancel());
                CloudBlobStream innerStream = innerStreamMock.Object;
                WatchableCloudBlobStream product = CreateProductUnderTest(innerStream);
                CancellationToken cancellationToken = tokenSource.Token;

                Task task = product.CommitAsync(cancellationToken);
                Assert.NotNull(task); // Guard

                // Act
                spy.Complete();

                // Assert
                Assert.False(spy.Canceled);
                task.GetAwaiter().GetResult();
            }
        }
        public void CommitAsync_WhenInnerStreamBeginCommitCompletedAsynchronously_ReturnsRanToCompletionTask()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource completion = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginCommit()
                .ReturnsCompletingAsynchronously(completion);
            innerStreamMock.SetupEndCommit();
            CloudBlobStream innerStream = innerStreamMock.Object;
            WatchableCloudBlobStream product = CreateProductUnderTest(innerStream);

            CancellationToken cancellationToken = CancellationToken.None;

            Task task = product.CommitAsync(cancellationToken);

            // Act
            completion.Complete();

            // Assert
            Assert.NotNull(task);
            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }
        public void CommitAsync_DelegatesToInnerStreamBeginEndCommit()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource completion = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginCommit()
                .ReturnsCompletingAsynchronously(completion)
                .Verifiable();
            innerStreamMock
                .Setup(s => s.EndCommit(It.Is<IAsyncResult>((ar) => ar == completion.AsyncResult)))
                .Verifiable();
            CloudBlobStream innerStream = innerStreamMock.Object;
            WatchableCloudBlobStream product = CreateProductUnderTest(innerStream);

            CancellationToken cancellationToken = CancellationToken.None;
            Task task = product.CommitAsync(cancellationToken);

            // Act
            completion.Complete();

            // Assert
            innerStreamMock.Verify();
            task.GetAwaiter().GetResult();
        }
        public void FlushAsync_CancelToken_WhenNotCompletedSynchronously_DelegatesToCancel()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CancellableAsyncCompletionSource spy = new CancellableAsyncCompletionSource();
            innerStreamMock
                .SetupBeginFlush()
                .ReturnsCompletingAsynchronously(spy);
            innerStreamMock.SetupEndFlush();
            CloudBlobStream innerStream = innerStreamMock.Object;
            Stream product = CreateProductUnderTest(innerStream);

            using (CancellationTokenSource tokenSource = new CancellationTokenSource())
            {
                CancellationToken cancellationToken = tokenSource.Token;

                Task task = product.FlushAsync(cancellationToken);
                Assert.NotNull(task); // Guard

                // Act
                tokenSource.Cancel();

                // Assert
                Assert.True(spy.Canceled);

                // Cleanup
                spy.Complete();
                task.GetAwaiter().GetResult();
            }
        }