private void Init()
        {
            this.collectionCache = new Mock <ClientCollectionCache>(new ServerStoreModel(null), null, null);
            this.collectionCache.Setup
                (m =>
                m.ResolveCollectionAsync(
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(Task.FromResult(CosmosContainerProperties.CreateWithResourceId("test")));

            this.partitionKeyRangeCache = new Mock <PartitionKeyRangeCache>(null, null, null);
            this.partitionKeyRangeCache.Setup(
                m => m.TryLookupAsync(
                    It.IsAny <string>(),
                    It.IsAny <CollectionRoutingMap>(),
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(Task.FromResult <CollectionRoutingMap>(null));


            this.globalEndpointManager = new Mock <GlobalEndpointManager>(this, new ConnectionPolicy());

            this.InitStoreModels();
        }
Exemple #2
0
        private async Task RefreshAsync(DocumentServiceRequest request, CancellationToken cancellationToken)
        {
            System.Diagnostics.Debug.Assert(request.IsNameBased);
            InternalCache cache            = this.GetCache(request.Headers[HttpConstants.HttpHeaders.Version]);
            string        resourceFullName = PathsHelper.GetCollectionPath(request.ResourceAddress);

            if (request.RequestContext.ResolvedCollectionRid != null)
            {
                // Here we will issue backend call only if cache wasn't already refreshed (if whatever is there corresponds to presiously resolved collection rid).
                await cache.collectionInfoByName.GetAsync(
                    resourceFullName,
                    CosmosContainerProperties.CreateWithResourceId(request.RequestContext.ResolvedCollectionRid),
                    async() =>
                {
                    DateTime currentTime = DateTime.UtcNow;
                    CosmosContainerProperties collection = await this.GetByNameAsync(request.Headers[HttpConstants.HttpHeaders.Version], resourceFullName, cancellationToken);
                    cache.collectionInfoById.Set(collection.ResourceId, collection);
                    cache.collectionInfoByNameLastRefreshTime.AddOrUpdate(resourceFullName, currentTime,
                                                                          (string currentKey, DateTime currentValue) => currentTime);
                    cache.collectionInfoByIdLastRefreshTime.AddOrUpdate(collection.ResourceId, currentTime,
                                                                        (string currentKey, DateTime currentValue) => currentTime);
                    return(collection);
                },
                    cancellationToken);
            }
            else
            {
                // In case of ForceRefresh directive coming from client, there will be no ResolvedCollectionRid, so we
                // need to refresh unconditionally.
                this.Refresh(request.ResourceAddress, request.Headers[HttpConstants.HttpHeaders.Version]);
            }

            request.RequestContext.ResolvedCollectionRid = null;
        }
        private void Init()
        {
            this.collectionCache = new Mock <ClientCollectionCache>(new SessionContainer("testhost"), new ServerStoreModel(null), null, null);
            const string pkPath = "/pk";

            this.collectionCache.Setup
                (m =>
                m.ResolveCollectionAsync(
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(() =>
            {
                CosmosContainerProperties cosmosContainerSetting = CosmosContainerProperties.CreateWithResourceId("test");
                cosmosContainerSetting.PartitionKey = new PartitionKeyDefinition()
                {
                    Kind  = PartitionKind.Hash,
                    Paths = new Collection <string>()
                    {
                        pkPath
                    }
                };

                return(Task.FromResult(cosmosContainerSetting));
            });
            this.collectionCache.Setup
                (m =>
                m.ResolveByNameAsync(
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(() => {
                CosmosContainerProperties containerSettings = CosmosContainerProperties.CreateWithResourceId("test");
                containerSettings.PartitionKey.Paths        = new Collection <string>()
                {
                    pkPath
                };
                return(Task.FromResult(containerSettings));
            });

            this.collectionCache.Setup
                (m =>
                m.ResolveByNameAsync(
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(() =>
            {
                CosmosContainerProperties cosmosContainerSetting = CosmosContainerProperties.CreateWithResourceId("test");
                cosmosContainerSetting.PartitionKey = new PartitionKeyDefinition()
                {
                    Kind  = PartitionKind.Hash,
                    Paths = new Collection <string>()
                    {
                        pkPath
                    }
                };

                return(Task.FromResult(cosmosContainerSetting));
            });

            this.partitionKeyRangeCache = new Mock <PartitionKeyRangeCache>(null, null, null);
            this.partitionKeyRangeCache.Setup(
                m => m.TryLookupAsync(
                    It.IsAny <string>(),
                    It.IsAny <CollectionRoutingMap>(),
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(Task.FromResult <CollectionRoutingMap>(null));

            this.globalEndpointManager = new Mock <GlobalEndpointManager>(this, new ConnectionPolicy());

            var sessionContainer = new SessionContainer(this.ServiceEndpoint.Host);

            this.sessionContainer = sessionContainer;
        }