コード例 #1
0
        public async Task WhenLeasesHaveContinuationTokenNullReturn0_Pull()
        {
            ChangeFeedProcessor processor = this.Container
                                            .GetChangeFeedProcessorBuilder("test", (IReadOnlyCollection <dynamic> docs, CancellationToken token) => Task.CompletedTask)
                                            .WithInstanceName("random")
                                            .WithLeaseContainer(this.LeaseContainer).Build();

            await processor.StartAsync();

            await Task.Delay(BaseChangeFeedClientHelper.ChangeFeedCleanupTime);

            await processor.StopAsync();

            long receivedEstimation       = 0;
            ChangeFeedEstimator estimator = ((ContainerInternal)this.Container)
                                            .GetChangeFeedEstimator(
                processorName: "test",
                this.LeaseContainer);

            using FeedIterator <ChangeFeedProcessorState> feedIterator = estimator.GetCurrentStateIterator();
            while (feedIterator.HasMoreResults)
            {
                FeedResponse <ChangeFeedProcessorState> response = await feedIterator.ReadNextAsync();

                receivedEstimation += response.Sum(r => r.EstimatedLag);
            }

            Assert.AreEqual(0, receivedEstimation);
        }
        public async Task ShouldReturnZeroWhenNoItems()
        {
            long globalLsnPKRange0 = 10;
            long globalLsnPKRange1 = 30;
            long expectedTotal     = 0;

            List <DocumentServiceLeaseCore> leases = new List <DocumentServiceLeaseCore>()
            {
                new DocumentServiceLeaseCore()
                {
                    LeaseToken = "0"
                },
                new DocumentServiceLeaseCore()
                {
                    LeaseToken = "1"
                }
            };

            Mock <FeedIterator> mockIteratorPKRange0 = new Mock <FeedIterator>();

            mockIteratorPKRange0.Setup(i => i.ReadNextAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(GetResponse(HttpStatusCode.NotModified, "0:" + globalLsnPKRange0.ToString()));

            Mock <FeedIterator> mockIteratorPKRange1 = new Mock <FeedIterator>();

            mockIteratorPKRange1.Setup(i => i.ReadNextAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(GetResponse(HttpStatusCode.NotModified, "1:" + globalLsnPKRange1.ToString()));

            Mock <DocumentServiceLeaseContainer> mockContainer = new Mock <DocumentServiceLeaseContainer>();

            mockContainer.Setup(c => c.GetAllLeasesAsync()).ReturnsAsync(leases);

            Func <string, string, bool, FeedIterator> feedCreator = (string partitionKeyRangeId, string continuationToken, bool startFromBeginning) =>
            {
                if (partitionKeyRangeId == "0")
                {
                    return(mockIteratorPKRange0.Object);
                }

                return(mockIteratorPKRange1.Object);
            };

            ChangeFeedEstimatorIterator remainingWorkEstimator = new ChangeFeedEstimatorIterator(
                Mock.Of <ContainerInternal>(),
                Mock.Of <ContainerInternal>(),
                mockContainer.Object,
                feedCreator,
                null);

            long estimation = 0;

            while (remainingWorkEstimator.HasMoreResults)
            {
                FeedResponse <ChangeFeedProcessorState> response = await remainingWorkEstimator.ReadNextAsync(default(CancellationToken));

                estimation += response.Sum(e => e.EstimatedLag);
            }

            Assert.AreEqual(expectedTotal, estimation);
        }
コード例 #3
0
        public async Task CountPendingDocuments_Pull()
        {
            ChangeFeedProcessor processor = this.Container
                                            .GetChangeFeedProcessorBuilder(
                processorName: "test",
                onChangesDelegate: (IReadOnlyCollection <dynamic> docs, CancellationToken token) => Task.CompletedTask)
                                            .WithInstanceName("random")
                                            .WithLeaseContainer(this.LeaseContainer)
                                            .Build();

            await processor.StartAsync();

            // Letting processor initialize
            await Task.Delay(BaseChangeFeedClientHelper.ChangeFeedSetupTime);

            // Inserting documents
            foreach (int id in Enumerable.Range(0, 10))
            {
                await this.Container.CreateItemAsync <dynamic>(new { id = id.ToString() });
            }

            // Waiting on all notifications to finish
            await Task.Delay(BaseChangeFeedClientHelper.ChangeFeedCleanupTime);

            await processor.StopAsync();

            ManualResetEvent    manualResetEvent   = new ManualResetEvent(false);
            long                receivedEstimation = 0;
            ChangeFeedEstimator estimator          = ((ContainerInternal)this.Container)
                                                     .GetChangeFeedEstimator(
                processorName: "test",
                this.LeaseContainer);

            // Inserting more documents
            foreach (int id in Enumerable.Range(11, 10))
            {
                await this.Container.CreateItemAsync <dynamic>(new { id = id.ToString() });
            }

            using FeedIterator <ChangeFeedProcessorState> feedIterator = estimator.GetCurrentStateIterator();
            while (feedIterator.HasMoreResults)
            {
                FeedResponse <ChangeFeedProcessorState> response = await feedIterator.ReadNextAsync();

                receivedEstimation += response.Sum(r => r.EstimatedLag);
                Assert.IsTrue(response.Headers.RequestCharge > 0);
                Assert.IsNotNull(response.Diagnostics);
                string asString = response.Diagnostics.ToString();
                Assert.IsTrue(asString.Length > 0);
                Assert.IsTrue(asString.Contains("cosmos-netstandard-sdk"));
            }

            Assert.AreEqual(10, receivedEstimation);
        }
コード例 #4
0
 public async Task<int> GetRegistrantCountAsync()
 {
     FeedOptions options = new FeedOptions { EnableCrossPartitionQuery = true };
     IDocumentQuery<int> query = Client.CreateDocumentQuery<int>(Collection.SelfLink, "SELECT VALUE COUNT(1) FROM registrants", options).AsDocumentQuery();
 int count = 0;
 while (query.HasMoreResults)
 {
   FeedResponse<int> results = await query.ExecuteNextAsync<int>(); 
   count += results.Sum(); 
 }
 return count;
 }
コード例 #5
0
        private async Task <long> GetEstimatedRemainingWorkAsync(CancellationToken cancellationToken)
        {
            using FeedIterator <ChangeFeedProcessorState> feedIterator = this.remainingWorkEstimator.GetCurrentStateIterator();
            FeedResponse <ChangeFeedProcessorState> estimations        = await feedIterator.ReadNextAsync(cancellationToken).ConfigureAwait(false);

            if (estimations.Count == 0)
            {
                return(1);
            }

            // Gets all results in first page
            return(estimations.Sum(estimation => estimation.EstimatedLag));
        }
コード例 #6
0
        public async Task WhenNoLeasesExist_Pull()
        {
            ChangeFeedEstimator estimator = ((ContainerInternal)this.Container)
                                            .GetChangeFeedEstimator(
                processorName: "test",
                this.LeaseContainer);

            long receivedEstimation = 0;

            using FeedIterator <ChangeFeedProcessorState> feedIterator = estimator.GetCurrentStateIterator();
            while (feedIterator.HasMoreResults)
            {
                FeedResponse <ChangeFeedProcessorState> response = await feedIterator.ReadNextAsync();

                receivedEstimation += response.Sum(r => r.EstimatedLag);
            }

            Assert.AreEqual(0, receivedEstimation);
        }