Beispiel #1
0
        private void TestDocumentClientMemoryLeakPrivateNoInline(ConnectionPolicy connectionPolicy, WeakReference reference)
        {
            Uri    globalEndpointUri     = new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]);
            Uri    configurationEndPoint = DNSHelper.GetResolvedUri(ConfigurationManager.AppSettings["GatewayEndpoint"]);
            string authKey = ConfigurationManager.AppSettings["MasterKey"];

            DocumentClient client = new DocumentClient(globalEndpointUri, authKey, connectionPolicy);

            // Holding a WeakReference to client to test whether it gets garbage collected eventually
            reference.Target = client;

            // Executing any request using this client
            client.CreateDatabaseAsync(new CosmosDatabaseSettings {
                Id = Guid.NewGuid().ToString()
            }).Wait();

            // Verify that the Write and Read Endpoints point to same endpoint(since no PreferredLocations was specified)
            Assert.AreEqual(client.WriteEndpoint, configurationEndPoint);
            Assert.AreEqual(client.ReadEndpoint, configurationEndPoint);

            // Adding a preferred read location, which should trigger the event handler to update the Read and Write endpoints
            connectionPolicy.PreferredLocations.Add(ConfigurationManager.AppSettings["Location2"]);

            client.CreateDatabaseAsync(new CosmosDatabaseSettings {
                Id = Guid.NewGuid().ToString()
            }).Wait();

            // Verify that the read endpoint now changes to this new preferred location
            Assert.AreEqual(client.WriteEndpoint, configurationEndPoint);
            Assert.AreEqual(client.ReadEndpoint, configurationEndPoint);

            // Disposing the client and setting it to null to enable garbage collection
            client.Dispose();
            client = null;
        }
Beispiel #2
0
        private async Task TestPreferredRegionOrderAsync()
        {
            Uri    globalEndpointUri = new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]);
            string authKey           = ConfigurationManager.AppSettings["MasterKey"];

            ConnectionPolicy connectionPolicy = new ConnectionPolicy();

            connectionPolicy.PreferredLocations.Add(ConfigurationManager.AppSettings["Location"]);  //write region
            connectionPolicy.PreferredLocations.Add(ConfigurationManager.AppSettings["Location2"]); // read region

            DocumentClient client = new DocumentClient(globalEndpointUri, authKey, connectionPolicy);

            CosmosDatabaseSettings database = await client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString() });

            CosmosContainerSettings collection = await client.CreateDocumentCollectionAsync(database.SelfLink, new CosmosContainerSettings { Id = Guid.NewGuid().ToString() });

            // todo: SessionToken container has a bug which prevent the session consistency read. So we sleep to make sure it is replicated.
            await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication);

            Document document =
                await client.CreateDocumentAsync(collection.SelfLink, new Document { Id = Guid.NewGuid().ToString() });

            Assert.AreEqual(client.WriteEndpoint, DNSHelper.GetResolvedUri(ConfigurationManager.AppSettings["GatewayEndpoint"]));
            // Ensure that the ReadEndpoint gets set to whatever is the first region in PreferredLocations irrespective whether it's read or write region
            Assert.AreEqual(client.ReadEndpoint, DNSHelper.GetResolvedUri(ConfigurationManager.AppSettings["GatewayEndpoint"]));
        }