Пример #1
0
        private async Task CreateDatabaseIfNotExists(CosmosClient client)
        {
            string databaseId = Guid.NewGuid().ToString();

            // Create the database with this unique id
            Database createdDatabase = await client.CreateDatabaseIfNotExistsAsync(databaseId);

            // CreateDatabaseIfNotExistsAsync should create the new database
            Assert.AreEqual(databaseId, createdDatabase.Id);

            string databaseId2 = Guid.NewGuid().ToString();

            // Pre-create the database with this unique id
            Database createdDatabase2 = await client.CreateDatabaseAsync(databaseId2);

            Database readDatabase = await client.CreateDatabaseIfNotExistsAsync(databaseId2);

            // CreateDatabaseIfNotExistsAsync should return the same database
            Assert.AreEqual(createdDatabase2.Id, readDatabase.Id);

            // cleanup created databases
            await createdDatabase.DeleteAsync();

            await readDatabase.DeleteAsync();
        }
Пример #2
0
        public async Task VerifyKnownHeaders()
        {
            HeaderValidationHandler headerValidationHandler = new HeaderValidationHandler();

            using CosmosClient client = TestCommon.CreateCosmosClient(x => x.AddCustomHandlers(headerValidationHandler));
            Database database = null;

            try
            {
                database = await client.CreateDatabaseAsync(nameof(VerifyKnownHeaders) + Guid.NewGuid().ToString());

                Container container = await database.CreateContainerAsync(
                    Guid.NewGuid().ToString(),
                    "/pk");

                ToDoActivity toDoActivity = ToDoActivity.CreateRandomToDoActivity();
                await container.CreateItemAsync(toDoActivity, new PartitionKey(toDoActivity.pk));

                await container.ReadItemAsync <ToDoActivity>(toDoActivity.id, new PartitionKey(toDoActivity.pk));

                await container.UpsertItemAsync <ToDoActivity>(toDoActivity, new PartitionKey(toDoActivity.pk));

                toDoActivity.cost = 8923498;
                await container.ReplaceItemAsync <ToDoActivity>(toDoActivity, toDoActivity.id, new PartitionKey(toDoActivity.pk));

                await container.DeleteItemAsync <ToDoActivity>(toDoActivity.id, new PartitionKey(toDoActivity.pk));
            }
            finally
            {
                if (database != null)
                {
                    await database.DeleteStreamAsync();
                }
            }
        }
        public async Task CustomHandlersDiagnostic()
        {
            TimeSpan     delayTime    = TimeSpan.FromSeconds(2);
            CosmosClient cosmosClient = TestCommon.CreateCosmosClient(builder =>
                                                                      builder.AddCustomHandlers(new RequestHandlerSleepHelper(delayTime)));

            DatabaseResponse databaseResponse = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());

            string diagnostics = databaseResponse.Diagnostics.ToString();

            Assert.IsNotNull(diagnostics);
            JObject jObject       = JObject.Parse(diagnostics);
            JArray  contextList   = jObject["Context"].ToObject <JArray>();
            JObject customHandler = GetJObjectInContextList(contextList, typeof(RequestHandlerSleepHelper).FullName);

            Assert.IsNotNull(customHandler);
            double elapsedTime = customHandler["HandlerElapsedTimeInMs"].ToObject <double>();

            Assert.IsTrue(elapsedTime > 1);

            customHandler = GetJObjectInContextList(contextList, typeof(RequestHandlerSleepHelper).FullName);
            Assert.IsNotNull(customHandler);
            elapsedTime = customHandler["HandlerElapsedTimeInMs"].ToObject <double>();
            Assert.IsTrue(elapsedTime > delayTime.TotalMilliseconds);

            await databaseResponse.Database.DeleteAsync();
        }
Пример #4
0
        private static void InitializeSharedThroughputContainer()
        {
            CosmosClient client = TestCommon.CreateCosmosClient();

            Cosmos.Database db = client.CreateDatabaseAsync(string.Format("Shared_{0}", Guid.NewGuid().ToString("N")), throughput: 20000).GetAwaiter().GetResult().Database;

            for (int index = 0; index < 5; index++)
            {
                ContainerResponse containerResponse = db.CreateContainerAsync(
                    new ContainerProperties
                {
                    Id           = Guid.NewGuid().ToString(),
                    PartitionKey = BatchTestBase.PartitionKeyDefinition
                })
                                                      .GetAwaiter().GetResult();

                Assert.AreEqual(true, bool.Parse(containerResponse.Headers.Get(WFConstants.BackendHeaders.ShareThroughput)));

                if (index == 2)
                {
                    BatchTestBase.SharedThroughputContainer = containerResponse.Container;
                }
            }

            BatchTestBase.SharedThroughputDatabase = db;
        }
        public async Task ValidateCurrentWriteQuorumAndReplicaSetHeader()
        {
            CosmosClient client = TestCommon.CreateCosmosClient(false);

            Cosmos.Database db = null;
            try
            {
                db = await client.CreateDatabaseAsync(Guid.NewGuid().ToString());

                PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition {
                    Paths = new System.Collections.ObjectModel.Collection <string>(new[] { "/id" }), Kind = PartitionKind.Hash
                };
                ContainerProperties containerSetting = new ContainerProperties()
                {
                    Id           = Guid.NewGuid().ToString(),
                    PartitionKey = partitionKeyDefinition
                };
                Container coll = await db.CreateContainerAsync(containerSetting);

                Document documentDefinition = new Document {
                    Id = Guid.NewGuid().ToString()
                };
                ItemResponse <Document> docResult = await coll.CreateItemAsync <Document>(documentDefinition);

                Assert.IsTrue(int.Parse(docResult.Headers[WFConstants.BackendHeaders.CurrentWriteQuorum], CultureInfo.InvariantCulture) > 0);
                Assert.IsTrue(int.Parse(docResult.Headers[WFConstants.BackendHeaders.CurrentReplicaSetSize], CultureInfo.InvariantCulture) > 0);
            }
            finally
            {
                await db.DeleteAsync();
            }
        }
Пример #6
0
        public async Task CosmosHttpClientRetryValidation()
        {
            TransientHttpClientCreatorHandler handler = new TransientHttpClientCreatorHandler();
            HttpClient httpClient = new HttpClient(handler);

            using (CosmosClient client = TestCommon.CreateCosmosClient(builder =>
                                                                       builder.WithConnectionModeGateway()
                                                                       .WithHttpClientFactory(() => httpClient)))
            {
                // Verify the failure has the required info
                try
                {
                    await client.CreateDatabaseAsync("TestGatewayTimeoutDb" + Guid.NewGuid().ToString());

                    Assert.Fail("Operation should have timed out:");
                }
                catch (CosmosException rte)
                {
                    Assert.IsTrue(handler.Count > 7);
                    string message = rte.ToString();
                    Assert.IsTrue(message.Contains("Start Time"), "Start Time:" + message);
                    Assert.IsTrue(message.Contains("Total Duration"), "Total Duration:" + message);
                    Assert.IsTrue(message.Contains("Http Client Timeout"), "Http Client Timeout:" + message);
                }
            }
        }
        public async Task GatewayStoreClientTimeout()
        {
            // Cause http client to throw a TaskCanceledException to simulate a timeout
            HttpClient httpClient = new HttpClient(new TimeOutHttpClientHandler())
            {
                Timeout = TimeSpan.FromSeconds(1)
            };

            using (CosmosClient client = TestCommon.CreateCosmosClient(x => x.WithConnectionModeGateway().WithHttpClientFactory(() => httpClient)))
            {
                // Verify the failure has the required info
                try
                {
                    await client.CreateDatabaseAsync("TestGatewayTimeoutDb" + Guid.NewGuid().ToString());

                    Assert.Fail("Operation should have timed out:");
                }
                catch (CosmosException rte)
                {
                    string message = rte.ToString();
                    Assert.IsTrue(message.Contains("Start Time"), "Start Time:" + message);
                    Assert.IsTrue(message.Contains("Total Duration"), "Total Duration:" + message);
                    Assert.IsTrue(message.Contains("Http Client Timeout"), "Http Client Timeout:" + message);
                    Assert.IsTrue(message.Contains("Activity id"), "Activity id:" + message);
                }
            }
        }
Пример #8
0
        private async Task <Database> CreateDatabaseAsync()
        {
            var databaseName = RandomName();
            var response     = await _client.CreateDatabaseAsync(databaseName);

            return(response.Database);
        }
        public async static Task Initialize(TestContext textContext)
        {
            client = TestCommon.CreateCosmosClient(true);

            string dbName = $"{nameof(LinqAttributeContractBaselineTests)}-{Guid.NewGuid().ToString("N")}";

            testDb = await client.CreateDatabaseAsync(dbName);
        }
Пример #10
0
        private static void CreateDBContainer(string dbName, string containerName, CosmosClient _client)
        {
            _client.CreateDatabaseAsync(dbName).GetAwaiter().GetResult();

            Database db = _client.GetDatabase(dbName);

            db.CreateContainerAsync(new ContainerProperties(containerName, "/CourseId")).GetAwaiter().GetResult();
        }
        public async Task AadMockTest()
        {
            string databaseId  = Guid.NewGuid().ToString();
            string containerId = Guid.NewGuid().ToString();

            using (CosmosClient cosmosClient = TestCommon.CreateCosmosClient())
            {
                Database database = await cosmosClient.CreateDatabaseAsync(databaseId);

                Container container = await database.CreateContainerAsync(
                    containerId,
                    "/id");
            }

            (string endpoint, string authKey) = TestCommon.GetAccountInfo();
            LocalEmulatorTokenCredential simpleEmulatorTokenCredential = new LocalEmulatorTokenCredential(authKey);
            CosmosClientOptions          clientOptions = new CosmosClientOptions()
            {
                ConnectionMode     = ConnectionMode.Gateway,
                ConnectionProtocol = Protocol.Https
            };

            using CosmosClient aadClient = new CosmosClient(
                      endpoint,
                      simpleEmulatorTokenCredential,
                      clientOptions);

            TokenCredentialCache tokenCredentialCache = ((AuthorizationTokenProviderTokenCredential)aadClient.AuthorizationTokenProvider).tokenCredentialCache;

            // The refresh interval changes slightly based on how fast machine calculate the interval based on the expire time.
            Assert.IsTrue(15 <= tokenCredentialCache.BackgroundTokenCredentialRefreshInterval.Value.TotalMinutes, "Default background refresh should be 25% of the token life which is defaulted to 1hr");
            Assert.IsTrue(tokenCredentialCache.BackgroundTokenCredentialRefreshInterval.Value.TotalMinutes > 14.7, "Default background refresh should be 25% of the token life which is defaulted to 1hr");

            Database aadDatabase = await aadClient.GetDatabase(databaseId).ReadAsync();

            Container aadContainer = await aadDatabase.GetContainer(containerId).ReadContainerAsync();

            ToDoActivity toDoActivity = ToDoActivity.CreateRandomToDoActivity();
            ItemResponse <ToDoActivity> itemResponse = await aadContainer.CreateItemAsync(
                toDoActivity,
                new PartitionKey(toDoActivity.id));

            toDoActivity.cost = 42.42;
            await aadContainer.ReplaceItemAsync(
                toDoActivity,
                toDoActivity.id,
                new PartitionKey(toDoActivity.id));

            await aadContainer.ReadItemAsync <ToDoActivity>(
                toDoActivity.id,
                new PartitionKey(toDoActivity.id));

            await aadContainer.UpsertItemAsync(toDoActivity);

            await aadContainer.DeleteItemAsync <ToDoActivity>(
                toDoActivity.id,
                new PartitionKey(toDoActivity.id));
        }
        public async Task TestDispose()
        {
            CosmosClient       cosmosClient = new CosmosClient(ConnectionString);
            Database           database     = cosmosClient.GetDatabase("asdf");
            Container          container    = cosmosClient.GetContainer("asdf", "asdf");
            TransactionalBatch batch        = container.CreateTransactionalBatch(new PartitionKey("asdf"));

            batch.ReadItem("Test");

            FeedIterator <dynamic> feedIterator1 = container.GetItemQueryIterator <dynamic>();
            FeedIterator <dynamic> feedIterator2 = container.GetItemQueryIterator <dynamic>(queryText: "select * from T");
            FeedIterator <dynamic> feedIterator3 = database.GetContainerQueryIterator <dynamic>(queryText: "select * from T");

            string userAgent = cosmosClient.ClientContext.UserAgent;

            // Dispose should be idempotent
            cosmosClient.Dispose();
            cosmosClient.Dispose();

            List <Func <Task> > validateAsync = new List <Func <Task> >()
            {
                () => cosmosClient.ReadAccountAsync(),
                () => cosmosClient.CreateDatabaseAsync("asdf"),
                () => database.CreateContainerAsync("asdf", "/pkpathasdf", 200),
                () => container.ReadItemAsync <dynamic>("asdf", new PartitionKey("test")),
                () => container.Scripts.ReadStoredProcedureAsync("asdf"),
                () => container.Scripts.ReadTriggerAsync("asdf"),
                () => container.Scripts.ReadUserDefinedFunctionAsync("asdf"),
                () => batch.ExecuteAsync(),
                () => feedIterator1.ReadNextAsync(),
                () => feedIterator2.ReadNextAsync(),
                () => feedIterator3.ReadNextAsync(),
            };

            foreach (Func <Task> asyncFunc in validateAsync)
            {
                try
                {
                    await asyncFunc();

                    Assert.Fail("Should throw ObjectDisposedException");
                }
                catch (CosmosObjectDisposedException e)
                {
                    string expectedMessage = $"Cannot access a disposed 'CosmosClient'. Follow best practices and use the CosmosClient as a singleton." +
                                             $" CosmosClient was disposed at: {cosmosClient.DisposedDateTimeUtc.Value.ToString("o", CultureInfo.InvariantCulture)}; CosmosClient Endpoint: https://localtestcosmos.documents.azure.com/; Created at: {cosmosClient.ClientConfigurationTraceDatum.ClientCreatedDateTimeUtc.ToString("o", CultureInfo.InvariantCulture)}; UserAgent: {userAgent};";
                    Assert.IsTrue(e.Message.Contains(expectedMessage));
                    string diagnostics = e.Diagnostics.ToString();
                    Assert.IsNotNull(diagnostics);
                    Assert.IsFalse(diagnostics.Contains("NoOp"));
                    Assert.IsTrue(diagnostics.Contains("Client Configuration"));
                    string exceptionString = e.ToString();
                    Assert.IsTrue(exceptionString.Contains(diagnostics));
                    Assert.IsTrue(exceptionString.Contains(e.Message));
                    Assert.IsTrue(exceptionString.Contains(e.StackTrace));
                }
            }
        }
        public async Task ValidateContainerNotFoundResponse()
        {
            CosmosDatabase dbDoesNotExist = CosmosNotFoundTests.client.GetDatabase(DoesNotExist);

            await this.ContainerOperations(database : dbDoesNotExist, dbNotExist : true);

            CosmosDatabase dbExists = await client.CreateDatabaseAsync("NotFoundTest" + Guid.NewGuid().ToString());

            await this.ContainerOperations(database : dbExists, dbNotExist : false);
        }
        public void OnConfigureAppConfiguration(IConfigurationBuilder configurationBuilder)
        {
            _cosmosDbName = $"SuperSafeBank_tests_{Guid.NewGuid()}";
            var topicsBaseName = $"aggregate_tests_{Guid.NewGuid()}";

            _topics = new List <string>();

            configurationBuilder.AddInMemoryCollection(new[]
            {
                new KeyValuePair <string, string>("dbName", _cosmosDbName),
                new KeyValuePair <string, string>("topicsBaseName", topicsBaseName)
            });

            var configuration = configurationBuilder.Build();

            _cosmosConnStr = configuration.GetConnectionString("cosmos");
            if (string.IsNullOrWhiteSpace(_cosmosConnStr))
            {
                throw new ArgumentException("invalid cosmos connection string");
            }
            _cosmosClient = new CosmosClient(_cosmosConnStr);

            _serviceBusConnStr = configuration.GetConnectionString("producer");
            if (string.IsNullOrWhiteSpace(_serviceBusConnStr))
            {
                throw new ArgumentException("invalid servicebus producer connection string");
            }

            _serviceBusClient = new ManagementClient(_serviceBusConnStr);

            _topics.Add($"{topicsBaseName}-{typeof(Customer).Name}");
            _topics.Add($"{topicsBaseName}-{typeof(Account).Name}");

            var tasks = _topics.Select(topic => _serviceBusClient.CreateTopicAsync(topic))
                        .Union(new Task[]
            {
                _cosmosClient.CreateDatabaseAsync(_cosmosDbName)
            }).ToArray();

            Task.WaitAll(tasks);

            _db = _cosmosClient.GetDatabase(_cosmosDbName);

            tasks = _topics.Select(topic => _serviceBusClient.CreateSubscriptionAsync(topic, "created"))
                    .Union(new Task[]
            {
                _db.CreateContainerAsync("Events", $"/{nameof(IDomainEvent<Guid>.AggregateId)}"),
                _db.CreateContainerAsync("CustomerEmails", "/id"),
                _db.CreateContainerAsync("CustomersArchive", "/id"),
                _db.CreateContainerAsync("CustomersDetails", "/id"),
                _db.CreateContainerAsync("AccountsDetails", "/id"),
            }).ToArray();
            Task.WaitAll(tasks);
        }
        public async Task TransportExceptionValidationTest(bool injectCpuMonitor)
        {
            CosmosClient cosmosClient = TestCommon.CreateCosmosClient(
                builder =>
            {
                builder.WithTransportClientHandlerFactory(transportClient => new TransportClientWrapper(
                                                              transportClient,
                                                              TransportWrapperTests.ThrowTransportExceptionOnItemOperation,
                                                              injectCpuMonitor));
            });

            Cosmos.Database database = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());

            Container container = await database.CreateContainerAsync(Guid.NewGuid().ToString(), "/id");

            try
            {
                TestPayload payload1 = await container.CreateItemAsync <TestPayload>(new TestPayload { id = "bad" }, new Cosmos.PartitionKey("bad"));

                Assert.Fail("Create item should fail with TransportException");
            }
            catch (CosmosException ce)
            {
                this.ValidateTransportException(ce, injectCpuMonitor);
            }

            try
            {
                FeedIterator <TestPayload> feedIterator = container.GetItemQueryIterator <TestPayload>("select * from T where T.Random = 19827 ");
                await feedIterator.ReadNextAsync();

                Assert.Fail("Create item should fail with TransportException");
            }
            catch (CosmosException ce)
            {
                this.ValidateTransportException(ce, injectCpuMonitor);
            }

            using (ResponseMessage responseMessage = await container.CreateItemStreamAsync(
                       TestCommon.SerializerCore.ToStream(new TestPayload {
                id = "bad"
            }),
                       new Cosmos.PartitionKey("bad")))
            {
                this.ValidateTransportException(responseMessage);
            }

            FeedIterator streamIterator = container.GetItemQueryStreamIterator("select * from T where T.Random = 19827 ");

            using (ResponseMessage responseMessage = await streamIterator.ReadNextAsync())
            {
                this.ValidateTransportException(responseMessage);
            }
        }
Пример #16
0
        /// <summary>
        /// Create the database and the required number of collections.
        /// </summary>
        private static async Task <InitializationResult> CreateDatabaseAndContainersAsync(
            CTLConfig config,
            CosmosClient cosmosClient)
        {
            List <string>        createdContainers = new List <string>();
            List <Container>     containers        = new List <Container>();
            InitializationResult result            = new InitializationResult()
            {
                CreatedDatabase = false
            };

            Database database;

            try
            {
                database = await cosmosClient.GetDatabase(config.Database).ReadAsync();
            }
            catch (CosmosException exception) when(exception.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                DatabaseResponse databaseResponse = await cosmosClient.CreateDatabaseAsync(config.Database, config.Throughput);

                result.CreatedDatabase = true;
                database = databaseResponse.Database;
            }

            int collectionCount = config.CollectionCount;

            if (collectionCount <= 0)
            {
                collectionCount = 1;
            }

            for (int i = 1; i <= collectionCount; i++)
            {
                string    containerName = $"{config.Collection}_{i}";
                Container container;
                try
                {
                    container = await database.GetContainer(containerName).ReadContainerAsync();
                }
                catch (CosmosException exception) when(exception.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    container = await database.CreateContainerAsync(containerName, ReadWriteQueryScenario.DefaultPartitionKeyPath);

                    createdContainers.Add(containerName);
                }

                containers.Add(container);
            }

            result.CreatedContainers = createdContainers;
            result.Containers        = containers;
            return(result);
        }
        /// <summary>
        /// Creates a CosmosClient with a mock TransportHandler that will capture and detect Exceptions happening inside ProcessChangesAsync.
        /// Since we want to be Exception-less, this will help detect if the Store Model is throwing or not.
        /// </summary>
        /// <param name="goThroughGateway">Whether or not to run the scenario using Gateway. If false, Direct will be used.</param>
        private static async Task <MockTransportHandler> TransportHandlerRunScenario(int responseStatusCode, bool goThroughGateway = true)
        {
            async Task <HttpResponseMessage> sendFunc(HttpRequestMessage httpRequest)
            {
                return(await Task.FromResult(new HttpResponseMessage((HttpStatusCode)responseStatusCode)
                {
                    Content = new StringContent("{}"),
                    RequestMessage = httpRequest
                }));
            }

            StoreResponse sendDirectFunc(TransportAddressUri uri, DocumentServiceRequest request)
            {
                return(new StoreResponse()
                {
                    ResponseBody = Stream.Null,
                    Status = responseStatusCode,
                    Headers = new StoreResponseNameValueCollection()
                });
            }

            // This is needed because in order to Mock a TransportClient we previously need an instance of CosmosClient
            using CosmosClient internalClient = MockCosmosUtil.CreateMockCosmosClient();
            internalClient.DocumentClient.GatewayStoreModel = MockGatewayStoreModel(sendFunc);
            internalClient.DocumentClient.StoreModel        = MockServerStoreModel(internalClient.DocumentClient.Session, sendDirectFunc);


            RetryHandler         retryHandler     = new RetryHandler(internalClient);
            MockTransportHandler transportHandler = new MockTransportHandler(internalClient);

            using CosmosClient client = MockCosmosUtil.CreateMockCosmosClient(
                      (builder) => builder.AddCustomHandlers(retryHandler, transportHandler));

            try
            {
                if (goThroughGateway)
                {
                    DatabaseResponse response = await client.CreateDatabaseAsync("test");
                }
                else
                {
                    ItemResponse <dynamic> response = await client.GetContainer("test", "test").CreateItemAsync <dynamic>(item: new { id = "id" }, partitionKey: new Cosmos.PartitionKey("id"));
                }
            }
            catch (CosmosException)
            {
                // Swallow CosmosExceptions as the point is to test the TransportHandler
            }

            return(transportHandler);
        }
Пример #18
0
        public async Task ValidateContainerRecreateScenario()
        {
            CosmosClient cc1 = TestCommon.CreateCosmosClient();
            CosmosClient cc2 = TestCommon.CreateCosmosClient();

            Database database = null;

            try
            {
                database = await cc1.CreateDatabaseAsync("ContainerRecreateScenarioDb");

                Container containerCC1 = await database.CreateContainerAsync("ContainerRecreateContainer", "/pk");

                // Create items with different pk values
                for (int i = 0; i < 5; i++)
                {
                    ItemResponse <ToDoActivity> itemResponse = await containerCC1.CreateItemAsync(
                        ToDoActivity.CreateRandomToDoActivity("pk" + i, i.ToString()));
                }

                List <(string, PartitionKey)> itemList = new List <(string, PartitionKey)>();
                for (int i = 0; i < 5; i++)
                {
                    itemList.Add((i.ToString(), new PartitionKey("pk" + i)));
                }

                FeedResponse <ToDoActivity> feedResponse = await containerCC1.ReadManyItemsAsync <ToDoActivity>(itemList);

                Assert.AreEqual(feedResponse.Count, 5);

                Database  databaseCC2  = cc2.GetDatabase("ContainerRecreateScenarioDb");
                Container containerCC2 = cc2.GetContainer("ContainerRecreateScenarioDb", "ContainerRecreateContainer");
                await containerCC2.DeleteContainerAsync();

                // Recreate container
                containerCC2 = await databaseCC2.CreateContainerAsync("ContainerRecreateContainer", "/pk");

                // Check if recreate scenario works
                feedResponse = await containerCC1.ReadManyItemsAsync <ToDoActivity>(itemList);

                Assert.AreEqual(feedResponse.Count, 0);
                Assert.IsTrue(feedResponse.StatusCode == HttpStatusCode.OK);
            }
            finally
            {
                await database.DeleteAsync();

                cc1.Dispose();
                cc2.Dispose();
            }
        }
Пример #19
0
        public async Task GatewayStoreClientTimeout()
        {
            using (CosmosClient client = TestCommon.CreateCosmosClient(useGateway: true))
            {
                // Creates the store clients in the document client
                await client.DocumentClient.EnsureValidClientAsync();

                // Get the GatewayStoreModel
                GatewayStoreModel gatewayStore;
                using (DocumentServiceRequest serviceRequest = new DocumentServiceRequest(
                           operationType: OperationType.Read,
                           resourceIdOrFullName: null,
                           resourceType: ResourceType.Database,
                           body: null,
                           headers: null,
                           isNameBased: false,
                           authorizationTokenType: AuthorizationTokenType.PrimaryMasterKey))
                {
                    serviceRequest.UseGatewayMode = true;
                    gatewayStore = (GatewayStoreModel)client.DocumentClient.GetStoreProxy(serviceRequest);
                }

                DocumentClient   documentClient           = client.DocumentClient;
                FieldInfo        cosmosHttpClientProperty = client.DocumentClient.GetType().GetField("httpClient", BindingFlags.NonPublic | BindingFlags.Instance);
                CosmosHttpClient cosmosHttpClient         = (CosmosHttpClient)cosmosHttpClientProperty.GetValue(documentClient);

                // Set the http request timeout to 10 ms to cause a timeout exception
                HttpClient httpClient         = new HttpClient(new TimeOutHttpClientHandler());
                FieldInfo  httpClientProperty = cosmosHttpClient.GetType().GetField("httpClient", BindingFlags.NonPublic | BindingFlags.Instance);
                httpClientProperty.SetValue(cosmosHttpClient, httpClient);

                FieldInfo gatewayRequestTimeoutProperty = cosmosHttpClient.GetType().GetField("GatewayRequestTimeout", BindingFlags.NonPublic | BindingFlags.Static);
                gatewayRequestTimeoutProperty.SetValue(cosmosHttpClient, TimeSpan.FromSeconds(1));

                // Verify the failure has the required info
                try
                {
                    await client.CreateDatabaseAsync("TestGatewayTimeoutDb" + Guid.NewGuid().ToString());

                    Assert.Fail("Operation should have timed out:");
                }
                catch (CosmosException rte)
                {
                    string message = rte.ToString();
                    Assert.IsTrue(message.Contains("Start Time"), "Start Time:" + message);
                    Assert.IsTrue(message.Contains("Total Duration"), "Total Duration:" + message);
                    Assert.IsTrue(message.Contains("Http Client Timeout"), "Http Client Timeout:" + message);
                    Assert.IsTrue(message.Contains("Activity id"), "Activity id:" + message);
                }
            }
        }
Пример #20
0
        public async Task HttpClientConnectionLimitTest()
        {
            int gatewayConnectionLimit = 1;

            IReadOnlyList <string> excludeConnections = GetActiveConnections();
            CosmosClient           cosmosClient       = new CosmosClient(
                ConfigurationManager.AppSettings["GatewayEndpoint"],
                ConfigurationManager.AppSettings["MasterKey"],
                new CosmosClientOptions
            {
                ApplicationName = "test",
                GatewayModeMaxConnectionLimit = gatewayConnectionLimit,
                ConnectionMode     = ConnectionMode.Gateway,
                ConnectionProtocol = Protocol.Https
            }
                );

            FieldInfo         httpClient        = cosmosClient.DocumentClient.GetType().GetField("httpClient", BindingFlags.NonPublic | BindingFlags.Instance);
            CosmosHttpClient  cosmosHttpClient  = (CosmosHttpClient)httpClient.GetValue(cosmosClient.DocumentClient);
            HttpClientHandler httpClientHandler = (HttpClientHandler)cosmosHttpClient.HttpMessageHandler;

            Assert.AreEqual(gatewayConnectionLimit, httpClientHandler.MaxConnectionsPerServer);

            Cosmos.Database database = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());

            Container container = await database.CreateContainerAsync(
                "TestConnections",
                "/pk",
                throughput : 20000);

            List <Task> creates = new List <Task>();

            for (int i = 0; i < 100; i++)
            {
                creates.Add(container.CreateItemAsync <dynamic>(new { id = Guid.NewGuid().ToString(), pk = Guid.NewGuid().ToString() }));
            }

            await Task.WhenAll(creates);

            // Verify the handler still exists after client warm up
            //Assert.AreEqual(gatewayConnectionLimit, httpClientHandler.MaxConnectionsPerServer);
            IReadOnlyList <string> afterConnections = GetActiveConnections();

            // Clean up the database and container
            await database.DeleteAsync();

            int connectionDiff = afterConnections.Count - excludeConnections.Count;

            Assert.IsTrue(connectionDiff <= gatewayConnectionLimit, $"Connection before : {excludeConnections.Count}, after {afterConnections.Count}");
        }
Пример #21
0
        public async Task HttpClientConnectionLimitTest()
        {
            int gatewayConnectionLimit = 1;

            IReadOnlyList <string> excludeConnections = GetActiveConnections();

            using (CosmosClient cosmosClient = new CosmosClient(
                       ConfigurationManager.AppSettings["GatewayEndpoint"],
                       ConfigurationManager.AppSettings["MasterKey"],
                       new CosmosClientOptions
            {
                ApplicationName = "test",
                GatewayModeMaxConnectionLimit = gatewayConnectionLimit,
                ConnectionMode = ConnectionMode.Gateway,
                ConnectionProtocol = Protocol.Https
            }
                       ))
            {
                CosmosHttpClient  cosmosHttpClient  = cosmosClient.DocumentClient.httpClient;
                HttpClientHandler httpClientHandler = (HttpClientHandler)cosmosHttpClient.HttpMessageHandler;
                Assert.AreEqual(gatewayConnectionLimit, httpClientHandler.MaxConnectionsPerServer);

                Cosmos.Database database = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());

                Container container = await database.CreateContainerAsync(
                    "TestConnections",
                    "/pk",
                    throughput : 20000);

                List <Task> creates = new List <Task>();
                for (int i = 0; i < 100; i++)
                {
                    creates.Add(container.CreateItemAsync <dynamic>(new { id = Guid.NewGuid().ToString(), pk = Guid.NewGuid().ToString() }));
                }

                await Task.WhenAll(creates);

                // Clean up the database and container
                await database.DeleteAsync();
            }


            IReadOnlyList <string> afterConnections = GetActiveConnections();

            int connectionDiff = afterConnections.Count - excludeConnections.Count;

            Assert.IsTrue(connectionDiff <= gatewayConnectionLimit, $"Connection before : {excludeConnections.Count}, after {afterConnections.Count};" +
                          $"Before connections: {JsonConvert.SerializeObject(excludeConnections)}; After connections: {JsonConvert.SerializeObject(afterConnections)}");
        }
        public async static Task Initialize(TestContext textContext)
        {
            cosmosClient = TestCommon.CreateCosmosClient((cosmosClientBuilder) => {
                cosmosClientBuilder.WithCustomJsonSerializer(new CustomJsonSerializer(new JsonSerializerSettings()
                {
                    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                    // We want to simulate the property not exist so ignoring the null value
                    NullValueHandling = NullValueHandling.Ignore
                })).WithConnectionModeGateway();
            });

            await cosmosClient.GetDatabase(id : nameof(LinqSQLTranslationBaselineTest)).DeleteAsync();

            testDb = await cosmosClient.CreateDatabaseAsync(nameof(LinqSQLTranslationBaselineTest));
        }
Пример #23
0
        public async Task <IDbContainerProvider> CreateTestDatabaseAsync()
        {
            var dbName   = $"{_dbNamePrefix}{Guid.NewGuid()}";
            var response = await _client.CreateDatabaseAsync(dbName);

            if (null == response || response.StatusCode != HttpStatusCode.Created)
            {
                throw new Exception($"unable to create db {dbName}");
            }

            await response.Database.CreateContainerIfNotExistsAsync(_eventsContainerName, $"/{nameof(DummyEvent.AggregateId)}");

            _dbs.Add(response.Database);
            return(new DbContainerProvider(response.Database));
        }
Пример #24
0
        public async Task VerifyNegativeWebProxySettings()
        {
            //proxy will be bypassed if the endpoint is localhost or 127.0.0.1
            string endpoint = $"https://{Environment.MachineName}";

            IWebProxy proxy = new WebProxy
            {
                Address            = new Uri("http://www.cosmostestproxyshouldfail.com"),
                BypassProxyOnLocal = false,
                BypassList         = new string[] { },
            };

            CosmosClient cosmosClient = new CosmosClient(
                endpoint,
                ConfigurationManager.AppSettings["MasterKey"],
                new CosmosClientOptions
            {
                ConnectionMode     = ConnectionMode.Gateway,
                ConnectionProtocol = Protocol.Https,
                WebProxy           = proxy
            }
                );

            await Assert.ThrowsExceptionAsync <HttpRequestException>(async() =>
            {
                DatabaseResponse databaseResponse = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());
            });

            proxy = new TestWebProxy {
                Credentials = new NetworkCredential("test", "test")
            };

            cosmosClient = new CosmosClient(
                endpoint,
                ConfigurationManager.AppSettings["MasterKey"],
                new CosmosClientOptions
            {
                ConnectionMode     = ConnectionMode.Gateway,
                ConnectionProtocol = Protocol.Https,
                WebProxy           = proxy
            }
                );

            await Assert.ThrowsExceptionAsync <HttpRequestException>(async() =>
            {
                DatabaseResponse databaseResponse = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());
            });
        }
Пример #25
0
        public async Task TestRidRefreshOnNotFoundAsync()
        {
            CosmosClient resourceClient = TestCommon.CreateCosmosClient();

            string dbName        = Guid.NewGuid().ToString();
            string containerName = Guid.NewGuid().ToString();

            Database db = await resourceClient.CreateDatabaseAsync(dbName);

            Container container = await db.CreateContainerAsync(containerName, "/_id");

            CosmosClient      testClient    = TestCommon.CreateCosmosClient();
            ContainerInternal testContainer = (ContainerInlineCore)testClient.GetContainer(dbName, containerName);

            // Populate the RID cache.
            string cachedRidAsync = await testContainer.GetCachedRIDAsync(forceRefresh : false, trace : NoOpTrace.Singleton, cancellationToken : default);
        public async static Task Initialize(TestContext textContext)
        {
            client = TestCommon.CreateCosmosClient(true);

            // Set a callback to get the handle of the last executed query to do the verification
            // This is neede because aggregate queries return type is a scalar so it can't be used
            // to verify the translated LINQ directly as other queries type.
            client.DocumentClient.OnExecuteScalarQueryCallback = q => LinqAggregateFunctionBaselineTests.lastExecutedScalarQuery = q;

            string dbName = $"{nameof(LinqAggregateFunctionBaselineTests)}-{Guid.NewGuid().ToString("N")}";

            testDb = await client.CreateDatabaseAsync(dbName);

            getQuery       = LinqTestsCommon.GenerateSimpleCosmosData(testDb);
            getQueryFamily = LinqTestsCommon.GenerateFamilyCosmosData(testDb, out _);
        }
Пример #27
0
        public async Task VerifyNegativeWebProxySettings()
        {
            CosmosClient cosmosClient = TestCommon.CreateCosmosClient((cosmosClientBuilder) => {
                cosmosClientBuilder.WithConnectionModeGateway(webProxy: new TestWebProxy());
            });

            DatabaseResponse databaseResponse = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());

            Assert.AreEqual(HttpStatusCode.Created, databaseResponse.StatusCode);

            /*
             * await Assert.ThrowsExceptionAsync<HttpRequestException>(async () => {
             *  DatabaseResponse databaseResponse = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());
             * });
             */
        }
Пример #28
0
        public async static Task Initialize(TestContext textContext)
        {
            client = TestCommon.CreateCosmosClient(true);

            var db = new Database()
            {
                Id = nameof(LinqTranslationBaselineTests)
            };

            try
            {
                var response = await client.DocumentClient.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(db.Id));
            }
            catch { }
            testDb = await client.CreateDatabaseAsync(nameof(LinqTranslationBaselineTests));
        }
        public async Task TransportInterceptorContractTest()
        {
            CosmosClient cosmosClient = TestCommon.CreateCosmosClient(
                builder =>
            {
                builder.WithTransportClientHandlerFactory(transportClient => new TransportClientWrapper(transportClient, TransportWrapperTests.Interceptor));
            });

            Cosmos.Database database = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());

            Container container = await database.CreateContainerAsync(Guid.NewGuid().ToString(), "/id");

            string      id1      = Guid.NewGuid().ToString();
            TestPayload payload1 = await container.CreateItemAsync <TestPayload>(new TestPayload { id = id1 });

            payload1 = await container.ReadItemAsync <TestPayload>(id1, new Cosmos.PartitionKey(id1));
        }
Пример #30
0
        public static async Task <InitializationResult> CreateDatabaseAndContainerAsync(
            CTLConfig config,
            CosmosClient cosmosClient)
        {
            InitializationResult result = new InitializationResult()
            {
                CreatedDatabase  = false,
                CreatedContainer = false
            };

            Database database;

            try
            {
                database = await cosmosClient.GetDatabase(config.Database).ReadAsync();
            }
            catch (CosmosException exception) when(exception.StatusCode == HttpStatusCode.NotFound)
            {
                await cosmosClient.CreateDatabaseAsync(config.Database, config.DatabaseThroughput);

                result.CreatedDatabase = true;
                database = cosmosClient.GetDatabase(config.Database);
            }

            Container container;

            try
            {
                container = await database.GetContainer(config.Collection).ReadContainerAsync();
            }
            catch (CosmosException exception) when(exception.StatusCode == HttpStatusCode.NotFound)
            {
                if (config.Throughput > 0)
                {
                    await database.CreateContainerAsync(config.Collection, $"/{config.CollectionPartitionKey}", config.Throughput);
                }
                else
                {
                    await database.CreateContainerAsync(config.Collection, $"/{config.CollectionPartitionKey}");
                }

                result.CreatedContainer = true;
            }

            return(result);
        }