예제 #1
0
        public static IReturnsResult <CloudBlobStream> ReturnsCompletedSynchronously(
            this ISetup <CloudBlobStream, IAsyncResult> setup, CompletedCancellationSpy spy)
        {
            if (setup == null)
            {
                throw new ArgumentNullException("setup");
            }

            return(setup.Returns <AsyncCallback, object>((callback, state) =>
            {
                CompletedCancellableAsyncResult result = new CompletedCancellableAsyncResult(state);
                spy.SetAsyncResult(result);
                InvokeCallback(callback, result);
                return result;
            }));
        }
        public static IReturnsResult<CloudBlobStream> ReturnsCompletedSynchronously(
            this ISetup<CloudBlobStream, ICancellableAsyncResult> setup, CompletedCancellationSpy spy)
        {
            if (setup == null)
            {
                throw new ArgumentNullException("setup");
            }

            return setup.Returns<AsyncCallback, object>((callback, state) =>
            {
                CompletedCancellableAsyncResult result = new CompletedCancellableAsyncResult(state);
                spy.SetAsyncResult(result);
                InvokeCallback(callback, result);
                return result;
            });
        }
        public void CompleteAsync_WhenChanged_DelegatesToBeginEndCommit()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CompletedCancellationSpy spy = new CompletedCancellationSpy();
            innerStreamMock.Setup(s => s.WriteByte(It.IsAny<byte>()));
            innerStreamMock
                .SetupBeginCommit()
                .ReturnsCompletedSynchronously(spy)
                .Verifiable();
            innerStreamMock
                .Setup(s => s.EndCommit(It.Is<IAsyncResult>((ar) => ar == spy.AsyncResult)))
                .Verifiable();
            CloudBlobStream innerStream = innerStreamMock.Object;
            WatchableCloudBlobStream product = CreateProductUnderTest(innerStream);

            product.WriteByte(0x00);

            CancellationToken cancellationToken = CancellationToken.None;

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

            // Assert
            innerStreamMock.Verify();
            Assert.NotNull(task);
            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }
        public void CommitAsync_CancelToken_AfterCompletion_DoesNotCallCancellableAsyncResultCancel()
        {
            // Arrange
            Mock<CloudBlobStream> innerStreamMock = CreateMockInnerStream();
            CompletedCancellationSpy spy = new CompletedCancellationSpy();
            innerStreamMock
                .SetupBeginCommit()
                .ReturnsCompletedSynchronously(spy);
            innerStreamMock.SetupEndCommit();
            CloudBlobStream innerStream = innerStreamMock.Object;
            WatchableCloudBlobStream product = CreateProductUnderTest(innerStream);

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

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

                // Act
                tokenSource.Cancel();

                // Assert
                Assert.False(spy.Canceled);
                task.GetAwaiter().GetResult();
            }
        }