public async Task TestInitializeAsync()
        {
            if (this.ClassData.monitoredCollectionInfo == null)
            {
                try
                {
                    if (this.ClassData.monitoredCollectionInfo == null)
                    {
                        this.ClassData.leaseCollectionInfoTemplate = await TestClassInitializeAsync(this, $"data_{this.GetType().Name}");
                    }
                }
                catch (Exception ex)
                {
                    Debug.Write(ex);
                    throw;
                }
            }

            this.LeaseCollectionInfo = new DocumentCollectionInfo(this.ClassData.leaseCollectionInfoTemplate);
            this.LeaseCollectionInfo.CollectionName = $"leases_{this.GetType().Name}_{Guid.NewGuid().ToString()}";

            var leaseCollection = new DocumentCollection {
                Id = this.LeaseCollectionInfo.CollectionName
            };

            using (var client = new DocumentClient(this.LeaseCollectionInfo.Uri, this.LeaseCollectionInfo.MasterKey, this.LeaseCollectionInfo.ConnectionPolicy))
            {
                await IntegrationTestsHelper.CreateDocumentCollectionAsync(client, this.LeaseCollectionInfo.DatabaseName, leaseCollection, leaseOfferThroughput);
            }
        }
        public async Task InitializeAsync()
        {
            try
            {
                await this.CreateMonitoredCollectionAsync($"data_{this.GetType().Name}");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw;
            }

            this.LeaseCollectionInfo.CollectionName = $"leases_{this.GetType().Name}_{Guid.NewGuid().ToString()}";

            var leaseCollection = new DocumentCollection
            {
                Id = this.LeaseCollectionInfo.CollectionName,
            };

            if (this.IsPartitionedLeaseCollection)
            {
                leaseCollection.PartitionKey = new PartitionKeyDefinition {
                    Paths = { "/id" }
                };
            }

            using (var client = new DocumentClient(this.LeaseCollectionInfo.Uri, this.LeaseCollectionInfo.MasterKey, this.LeaseCollectionInfo.ConnectionPolicy))
            {
                await IntegrationTestsHelper.CreateDocumentCollectionAsync(client, this.LeaseCollectionInfo.DatabaseName, leaseCollection, leaseOfferThroughput);
            }
        }
        private static async Task <DocumentCollectionInfo> TestClassInitializeAsync(IntegrationTest test, string monitoredCollectionName)
        {
            Debug.Assert(test != null);
            Debug.Assert(monitoredCollectionName != null);

            DocumentCollectionInfo leaseCollectionInfo;

            IntegrationTestsHelper.GetConfigurationSettings(
                out test.ClassData.monitoredCollectionInfo,
                out leaseCollectionInfo,
                out monitoredOfferThroughput,
                out leaseOfferThroughput);

            test.ClassData.monitoredCollectionInfo.CollectionName = monitoredCollectionName;

            var monitoredCollection = new DocumentCollection
            {
                Id = test.ClassData.monitoredCollectionInfo.CollectionName,
            };

            if (test.ClassData.isPartitionedCollection)
            {
                monitoredCollection.PartitionKey = new PartitionKeyDefinition {
                    Paths = new Collection <string> {
                        "/id"
                    }
                };
            }
            else
            {
                if (monitoredOfferThroughput > 10000)
                {
                    monitoredOfferThroughput = 10000;
                }
            }

            using (var client = new DocumentClient(test.ClassData.monitoredCollectionInfo.Uri, test.ClassData.monitoredCollectionInfo.MasterKey, test.ClassData.monitoredCollectionInfo.ConnectionPolicy))
            {
                await IntegrationTestsHelper.CreateDocumentCollectionAsync(client, test.ClassData.monitoredCollectionInfo.DatabaseName, monitoredCollection, monitoredOfferThroughput);
            }

            test.FinishTestClassInitializeAsync().Wait();

            return(leaseCollectionInfo);
        }
        private async Task CreateMonitoredCollectionAsync(string monitoredCollectionName)
        {
            Debug.Assert(monitoredCollectionName != null);

            IntegrationTestsHelper.GetConfigurationSettings(
                out DocumentCollectionInfo baseMonitoredCollectionInfo,
                out DocumentCollectionInfo baseLeaseCollectionInfo,
                out monitoredOfferThroughput,
                out leaseOfferThroughput);

            this.MonitoredCollectionInfo = baseMonitoredCollectionInfo;
            this.LeaseCollectionInfo     = baseLeaseCollectionInfo;

            this.MonitoredCollectionInfo.CollectionName = monitoredCollectionName;

            var monitoredCollection = new DocumentCollection
            {
                Id = this.MonitoredCollectionInfo.CollectionName,
            };

            if (this.IsPartitionedMonitoredCollection)
            {
                monitoredCollection.PartitionKey = new PartitionKeyDefinition {
                    Paths = { "/id" }
                };
            }
            else
            {
                if (monitoredOfferThroughput > 10000)
                {
                    monitoredOfferThroughput = 10000;
                }
            }

            using (var client = new DocumentClient(this.MonitoredCollectionInfo.Uri, this.MonitoredCollectionInfo.MasterKey, this.MonitoredCollectionInfo.ConnectionPolicy))
            {
                await IntegrationTestsHelper.CreateDocumentCollectionAsync(client, this.MonitoredCollectionInfo.DatabaseName, monitoredCollection, monitoredOfferThroughput);
            }
        }
        public async Task WriteToDocDB_SouldGeneratesIncomingEvent_IfStartingFromDefaultPoint()
        {
            string sessionId = Guid.NewGuid().ToString();

            IntegrationTestsHelper.GetConfigurationSettings(out DocumentCollectionInfo feedCollectionInfo, out DocumentCollectionInfo leaseCollectionInfo, out int feedOfferThroughput, out int leaseOfferThroughput);
            feedCollectionInfo.CollectionName  = Guid.NewGuid().ToString();
            leaseCollectionInfo.CollectionName = feedCollectionInfo.CollectionName + "-lease";

            using (var feedClient = new DocumentClient(
                       feedCollectionInfo.Uri,
                       feedCollectionInfo.MasterKey,
                       feedCollectionInfo.ConnectionPolicy))
            {
                await IntegrationTestsHelper.CreateDocumentCollectionAsync(feedClient, feedCollectionInfo.DatabaseName, new DocumentCollection { Id = feedCollectionInfo.CollectionName }, 400);

                using (var client = new DocumentClient(leaseCollectionInfo.Uri, leaseCollectionInfo.MasterKey, leaseCollectionInfo.ConnectionPolicy))
                {
                    await IntegrationTestsHelper.CreateDocumentCollectionAsync(
                        client,
                        leaseCollectionInfo.DatabaseName,
                        new DocumentCollection { Id = leaseCollectionInfo.CollectionName },
                        400);
                }

                TaskCompletionSource <bool> documentReceived = new TaskCompletionSource <bool>();
                Task Process(IChangeFeedObserverContext changeFeedObserverContext, IReadOnlyList <Document> readOnlyList)
                {
                    if (readOnlyList.Any(d => d.GetPropertyValue <string>("payload") == sessionId))
                    {
                        documentReceived.SetResult(true);
                    }
                    return(Task.CompletedTask);
                }

                FeedProcessing.IChangeFeedObserverFactory observerFactory = new DelegatingMemoryObserverFactory(
                    ctx => Task.CompletedTask,
                    (ctx, reason) => Task.CompletedTask,
                    Process);

                IChangeFeedProcessor changeFeedProcessor = await new ChangeFeedProcessorBuilder()
                                                           .WithObserverFactory(observerFactory)
                                                           .WithHostName("smoke_test")
                                                           .WithFeedCollection(feedCollectionInfo)
                                                           .WithLeaseCollection(leaseCollectionInfo)
                                                           .BuildAsync();

                await changeFeedProcessor.StartAsync().ConfigureAwait(false);

                try
                {
                    var generateDocsTask = await Task.Factory.StartNew(async() =>
                    {
                        while (!documentReceived.Task.IsCompleted)
                        {
                            var document = new Document();
                            document.SetPropertyValue("payload", sessionId);
                            var collectionUri = UriFactory.CreateDocumentCollectionUri(
                                feedCollectionInfo.DatabaseName,
                                feedCollectionInfo.CollectionName);
                            await feedClient.CreateDocumentAsync(collectionUri, document);
                            await Task.Delay(TimeSpan.FromMilliseconds(100));
                        }
                    });

                    await documentReceived.Task;
                    await generateDocsTask;
                }
                finally
                {
                    await changeFeedProcessor.StopAsync();
                }
            }
        }