Exemple #1
0
        public override async Task <CosmosDatabaseResponse> CreateDatabaseIfNotExistsAsync(
            string id,
            int?throughput = null,
            CosmosRequestOptions requestOptions = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // Doing a Read before Create will give us better latency for existing databases
            CosmosDatabase         database = this[id];
            CosmosDatabaseResponse cosmosDatabaseResponse = await database.ReadAsync(cancellationToken : cancellationToken);

            if (cosmosDatabaseResponse.StatusCode != HttpStatusCode.NotFound)
            {
                return(cosmosDatabaseResponse);
            }

            cosmosDatabaseResponse = await this.CreateDatabaseAsync(id, throughput, requestOptions, cancellationToken : cancellationToken);

            if (cosmosDatabaseResponse.StatusCode != HttpStatusCode.Conflict)
            {
                return(cosmosDatabaseResponse);
            }

            // This second Read is to handle the race condition when 2 or more threads have Read the database and only one succeeds with Create
            // so for the remaining ones we should do a Read instead of throwing Conflict exception
            return(await database.ReadAsync(cancellationToken : cancellationToken));
        }
 internal Task <string> GetRID(CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.ReadAsync(cancellationToken: cancellationToken)
            .ContinueWith(task =>
     {
         CosmosDatabaseResponse response = task.Result;
         return response.Resource.ResourceId;
     }, cancellationToken));
 }
 internal CosmosDatabaseResponse CreateDatabaseResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosDatabase database)
 {
     return(CosmosDatabaseResponse.CreateResponse(
                cosmosResponseMessage,
                this.jsonSerializer,
                database));
 }
Exemple #4
0
        /// <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)
        {
            Func <HttpRequestMessage, Task <HttpResponseMessage> > sendFunc = async httpRequest => await Task.FromResult(new HttpResponseMessage((HttpStatusCode)responseStatusCode) {
                Content        = new StringContent("{}"),
                RequestMessage = httpRequest
            });

            Func <Uri, DocumentServiceRequest, StoreResponse> sendDirectFunc = (uri, request) => new StoreResponse()
            {
                ResponseBody         = Stream.Null,
                Status               = responseStatusCode,
                ResponseHeaderNames  = Array.Empty <string>(),
                ResponseHeaderValues = Array.Empty <string>()
            };

            // This is needed because in order to Mock a TransportClient we previously need an instance of CosmosClient
            CosmosClient internalClient = MockDocumentClient.CreateMockCosmosClient();

            internalClient.DocumentClient.GatewayStoreModel = MockGatewayStoreModel(sendFunc);
            internalClient.DocumentClient.StoreModel        = MockServerStoreModel(internalClient.DocumentClient.Session, sendDirectFunc);


            RetryHandler         retryHandler     = new RetryHandler(internalClient.DocumentClient.ResetSessionTokenRetryPolicy);
            MockTransportHandler transportHandler = new MockTransportHandler(internalClient);

            CosmosClient client = MockDocumentClient.CreateMockCosmosClient(
                (builder) => {
                builder
                .AddCustomHandlers(retryHandler, transportHandler);
            });

            try
            {
                if (goThroughGateway)
                {
                    CosmosDatabaseResponse response = await client.Databases.CreateDatabaseAsync("test");
                }
                else
                {
                    CosmosItemResponse <dynamic> response = await client.Databases["test"].Containers["test"].Items.CreateItemAsync <dynamic>(partitionKey: "id", item: new { id = "id" });
                }
            }
            catch (CosmosException)
            {
                // Swallow CosmosExceptions as the point is to test the TransportHandler
            }

            return(transportHandler);
        }