public async Task RunObserver_ShouldCancelTasks_WhenTokenCanceled()
        {
            Task renewerTask = Task.FromResult(false);

            Mock.Get(leaseRenewer)
            .Setup(renewer => renewer.RunAsync(It.IsAny <CancellationToken>()))
            .Returns <CancellationToken>(token => renewerTask = Task.Delay(TimeSpan.FromMinutes(1), token));

            Task processorTask = Task.FromResult(false);

            Mock.Get(partitionProcessor)
            .Setup(processor => processor.RunAsync(It.IsAny <CancellationToken>()))
            .Returns <CancellationToken>(token => processorTask = Task.Delay(TimeSpan.FromMinutes(1), token));

            Task supervisorTask = sut.RunAsync(shutdownToken.Token);

            Task delay    = Task.Delay(TimeSpan.FromMilliseconds(100));
            Task finished = await Task.WhenAny(supervisorTask, delay).ConfigureAwait(false);

            Assert.Equal(delay, finished);

            shutdownToken.Cancel();
            await supervisorTask.ConfigureAwait(false);

            Assert.True(renewerTask.IsCanceled);
            Assert.True(processorTask.IsCanceled);
            Mock.Get(partitionProcessor)
            .Verify(processor => processor.RunAsync(It.IsAny <CancellationToken>()), Times.Once);

            Mock.Get(observer)
            .Verify(feedObserver => feedObserver
                    .CloseAsync(It.Is <ChangeFeedObserverContext>(context => context.PartitionKeyRangeId == lease.PartitionId),
                                ChangeFeedObserverCloseReason.Shutdown));
        }
Exemplo n.º 2
0
        public async Task RunObserver_ResourceGoneCloseReason_IfProcessorFailedWithPartitionNotFoundException()
        {
            Mock.Get(partitionProcessor)
            .Setup(processor => processor.RunAsync(It.IsAny <CancellationToken>()))
            .ThrowsAsync(new FeedNotFoundException("processorException", "12345"));

            Exception exception = await Assert.ThrowsExceptionAsync <FeedNotFoundException>(() => sut.RunAsync(shutdownToken.Token)).ConfigureAwait(false);

            Assert.AreEqual("processorException", exception.Message);

            Mock.Get(observer)
            .Verify(feedObserver => feedObserver
                    .CloseAsync(It.Is <ChangeFeedObserverContext>(context => context.LeaseToken == lease.CurrentLeaseToken),
                                ChangeFeedObserverCloseReason.ResourceGone));
        }