Beispiel #1
0
        public static async Task Main(string[] args)
        {
            using CosmosClient client = new CosmosClient(endpoint, apikey);
            Database  db             = client.GetDatabase("StoreDB");
            Container container      = db.GetContainer("CartContainer");
            Container leasecontainer = await db.CreateContainerIfNotExistsAsync("Lease", "/id");

            ChangeFeedProcessorBuilder builder = container.GetChangeFeedProcessorBuilder(
                "sampleProcessor",
                async(IReadOnlyCollection <Item> changes, CancellationToken cancellationToken) =>
            {
                await Console.Out.WriteLineAsync("***Changes occured***");
                foreach (Item change in changes)
                {
                    await Console.Out.WriteLineAsync(change.id + " - " + change.location + " - " + change.name);
                }
            });

            ChangeFeedProcessor processor = builder
                                            .WithInstanceName("firstinstance")
                                            .WithLeaseContainer(leasecontainer)
                                            .Build();

            await processor.StartAsync();

            Console.ReadKey();

            await processor.StopAsync();
        }
Beispiel #2
0
        public async Task StopAsync()
        {
            await processor
            .StopAsync()
            .ConfigureAwait(false);

            activity?.SubscriptionStopped();
        }
        public async Task CountPendingDocuments()
        {
            ChangeFeedProcessor processor = this.Container
                                            .GetChangeFeedProcessorBuilder(
                processorName: "test",
                onChangesDelegate: (IReadOnlyCollection <dynamic> docs, CancellationToken token) =>
            {
                return(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();

            long?receivedEstimation       = null;
            ChangeFeedProcessor estimator = this.Container
                                            .GetChangeFeedEstimatorBuilder(
                processorName: "test",
                estimationDelegate: (long estimation, CancellationToken token) =>
            {
                receivedEstimation = estimation;
                return(Task.CompletedTask);
            }, TimeSpan.FromSeconds(1))
                                            .WithLeaseContainer(this.LeaseContainer)
                                            .Build();

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

            await estimator.StartAsync();

            await Task.Delay(BaseChangeFeedClientHelper.ChangeFeedCleanupTime);

            await estimator.StopAsync();

            Assert.IsTrue(receivedEstimation.HasValue);
            Assert.AreEqual(10, receivedEstimation);
        }
Beispiel #4
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);
        }