public async Task GetAsyncString_ItemExistsInCache() { // Arrange var cacheValue = "Test"; stringCache.Set(new CacheItem <string>(CacheKey, cacheValue, TimeSpan.FromSeconds(5))); // Act var result = await stringCache.GetAsync(CacheKey); // Assert Assert.Equal(result, cacheValue); }
public async Task GetSessionData() { const string sessionId = "sessionId"; const int ttl = 1400; const int throughput = 2000; byte[] data = new byte[1] { 1 }; CosmosClientBuilder builder = new CosmosClientBuilder(ConfigurationManager.AppSettings["Endpoint"], ConfigurationManager.AppSettings["MasterKey"]); IOptions <CosmosCacheOptions> options = Options.Create(new CosmosCacheOptions() { ContainerName = "session", DatabaseName = CosmosCacheEmulatorTests.databaseName, ContainerThroughput = throughput, CreateIfNotExists = true, ClientBuilder = builder }); CosmosCache cache = new CosmosCache(options); DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttl); await cache.SetAsync(sessionId, data, cacheOptions); Assert.Equal(data, await cache.GetAsync(sessionId)); }
public async Task GetReturnsNullIfKeyDoesNotExist() { Mock <CosmosClient> mockedClient = new Mock <CosmosClient>(); Mock <Container> mockedContainer = new Mock <Container>(); Mock <Database> mockedDatabase = new Mock <Database>(); Mock <ContainerResponse> mockedResponse = new Mock <ContainerResponse>(); mockedResponse.Setup(c => c.StatusCode).Returns(HttpStatusCode.OK); mockedContainer.Setup(c => c.ReadContainerAsync(It.IsAny <ContainerRequestOptions>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockedResponse.Object); mockedContainer.Setup(c => c.ReadItemAsync <CosmosCacheSession>(It.Is <string>(id => id == "key"), It.IsAny <PartitionKey>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>())).ThrowsAsync(new CosmosException("test", HttpStatusCode.NotFound, 0, "", 0)); mockedContainer.Setup(c => c.ReplaceItemAsync <CosmosCacheSession>(It.IsAny <CosmosCacheSession>(), It.Is <string>(id => id == "key"), It.IsAny <PartitionKey?>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>())).ThrowsAsync(new CosmosException("test", HttpStatusCode.NotFound, 0, "", 0)); mockedClient.Setup(c => c.GetContainer(It.IsAny <string>(), It.IsAny <string>())).Returns(mockedContainer.Object); mockedClient.Setup(c => c.GetDatabase(It.IsAny <string>())).Returns(mockedDatabase.Object); mockedClient.Setup(x => x.Endpoint).Returns(new Uri("http://localhost")); CosmosCache cache = new CosmosCache(Options.Create(new CosmosCacheOptions() { DatabaseName = "something", ContainerName = "something", CosmosClient = mockedClient.Object })); Assert.Null(await cache.GetAsync("key")); mockedContainer.Verify(c => c.ReadItemAsync <CosmosCacheSession>(It.Is <string>(id => id == "key"), It.IsAny <PartitionKey>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>()), Times.Once); mockedContainer.Verify(c => c.ReplaceItemAsync <CosmosCacheSession>(It.IsAny <CosmosCacheSession>(), It.Is <string>(id => id == "key"), It.IsAny <PartitionKey?>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>()), Times.Never); }
public async Task GetSessionData_WhenNotExists_CustomPartitionKey() { const string sessionId = "sessionId"; const int ttl = 1400; const int throughput = 2000; const string partitionKeyAttribute = "notTheId"; CosmosClientBuilder builder = new CosmosClientBuilder(ConfigurationManager.AppSettings["Endpoint"], ConfigurationManager.AppSettings["MasterKey"]); IOptions <CosmosCacheOptions> options = Options.Create(new CosmosCacheOptions() { ContainerName = "session", DatabaseName = CosmosCacheEmulatorTests.databaseName, ContainerThroughput = throughput, CreateIfNotExists = true, ClientBuilder = builder, ContainerPartitionKeyAttribute = partitionKeyAttribute, }); CosmosCache cache = new CosmosCache(options); DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttl); Assert.Null(await cache.GetAsync(sessionId)); }
public async Task GetSessionData_WhenNotExists() { DiagnosticsSink diagnosticsSink = new DiagnosticsSink(); const string sessionId = "sessionId"; const int ttl = 1400; const int throughput = 2000; CosmosClientBuilder builder = new CosmosClientBuilder(ConfigurationManager.AppSettings["Endpoint"], ConfigurationManager.AppSettings["MasterKey"]); IOptions <CosmosCacheOptions> options = Options.Create(new CosmosCacheOptions() { ContainerName = "session", DatabaseName = CosmosCacheEmulatorTests.databaseName, ContainerThroughput = throughput, CreateIfNotExists = true, ClientBuilder = builder, DiagnosticsHandler = diagnosticsSink.CaptureDiagnostics }); CosmosCache cache = new CosmosCache(options); DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttl); Assert.Null(await cache.GetAsync(sessionId)); Assert.Equal(4, diagnosticsSink.CapturedDiagnostics.Count); foreach (CosmosDiagnostics diagnostics in diagnosticsSink.CapturedDiagnostics) { Assert.NotNull(diagnostics?.ToString()); } }
public async Task ConnectAsyncThrowsIfContainerDoesNotExist() { Mock <CosmosClient> mockedClient = new Mock <CosmosClient>(); Mock <Container> mockedContainer = new Mock <Container>(); mockedContainer.Setup(c => c.ReadContainerAsync(It.IsAny <ContainerRequestOptions>(), It.IsAny <CancellationToken>())).ThrowsAsync(new CosmosException("test", HttpStatusCode.NotFound, 0, "", 0)); mockedClient.Setup(c => c.GetContainer(It.IsAny <string>(), It.IsAny <string>())).Returns(mockedContainer.Object); mockedClient.Setup(x => x.Endpoint).Returns(new Uri("http://localhost")); CosmosCache cache = new CosmosCache(Options.Create(new CosmosCacheOptions() { DatabaseName = "something", ContainerName = "something", CosmosClient = mockedClient.Object })); await Assert.ThrowsAsync <InvalidOperationException>(() => cache.GetAsync("key")); }
public async Task SlidingAndAbsoluteExpiration() { const string sessionId = "sessionId"; const int ttl = 10; const int absoluteTtl = 15; const int throughput = 400; CosmosClientBuilder builder = new CosmosClientBuilder(ConfigurationManager.AppSettings["Endpoint"], ConfigurationManager.AppSettings["MasterKey"]); IOptions <CosmosCacheOptions> options = Options.Create(new CosmosCacheOptions() { ContainerName = "session", DatabaseName = CosmosCacheEmulatorTests.databaseName, ContainerThroughput = throughput, CreateIfNotExists = true, ClientBuilder = builder }); CosmosCache cache = new CosmosCache(options); DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttl); cacheOptions.AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(absoluteTtl); byte[] data = new byte[4] { 1, 2, 3, 4 }; await cache.SetAsync(sessionId, data, cacheOptions); // Verify that container has been created CosmosCacheSession storedSession = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadItemAsync <CosmosCacheSession>(sessionId, new PartitionKey(sessionId)); Assert.Equal(ttl, storedSession.TimeToLive); await Task.Delay(8000); // Wait await cache.GetAsync(sessionId); storedSession = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadItemAsync <CosmosCacheSession>(sessionId, new PartitionKey(sessionId)); // Since the absolute expiration is closer than the sliding value, the TTL should be lower Assert.True(storedSession.TimeToLive < ttl); }
public async Task GetObtainsSessionAndUpdatesCacheForSlidingExpirationWithAbsoluteExpirationWithHigherTime() { const int ttlSliding = 20; const int ttlAbsolute = 50; string etag = "etag"; CosmosCacheSession existingSession = new CosmosCacheSession(); existingSession.SessionKey = "key"; existingSession.Content = new byte[0]; existingSession.IsSlidingExpiration = true; existingSession.TimeToLive = ttlSliding; existingSession.AbsoluteSlidingExpiration = DateTimeOffset.UtcNow.AddSeconds(ttlAbsolute).ToUnixTimeSeconds(); Mock <ItemResponse <CosmosCacheSession> > mockedItemResponse = new Mock <ItemResponse <CosmosCacheSession> >(); Mock <CosmosClient> mockedClient = new Mock <CosmosClient>(); Mock <Container> mockedContainer = new Mock <Container>(); Mock <Database> mockedDatabase = new Mock <Database>(); Mock <ContainerResponse> mockedResponse = new Mock <ContainerResponse>(); mockedItemResponse.Setup(c => c.Resource).Returns(existingSession); mockedItemResponse.Setup(c => c.ETag).Returns(etag); mockedResponse.Setup(c => c.StatusCode).Returns(HttpStatusCode.OK); mockedContainer.Setup(c => c.ReadContainerAsync(It.IsAny <ContainerRequestOptions>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockedResponse.Object); mockedContainer.Setup(c => c.ReadItemAsync <CosmosCacheSession>(It.Is <string>(id => id == "key"), It.IsAny <PartitionKey>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockedItemResponse.Object); mockedContainer.Setup(c => c.ReplaceItemAsync <CosmosCacheSession>(It.Is <CosmosCacheSession>(item => item == existingSession), It.Is <string>(id => id == "key"), It.IsAny <PartitionKey?>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockedItemResponse.Object); mockedClient.Setup(c => c.GetContainer(It.IsAny <string>(), It.IsAny <string>())).Returns(mockedContainer.Object); mockedClient.Setup(c => c.GetDatabase(It.IsAny <string>())).Returns(mockedDatabase.Object); mockedClient.Setup(x => x.Endpoint).Returns(new Uri("http://localhost")); CosmosCache cache = new CosmosCache(Options.Create(new CosmosCacheOptions() { DatabaseName = "something", ContainerName = "something", CreateIfNotExists = true, CosmosClient = mockedClient.Object })); Assert.Same(existingSession.Content, await cache.GetAsync("key")); // Checks for Db existence due to CreateIfNotExists mockedClient.Verify(c => c.CreateDatabaseIfNotExistsAsync(It.IsAny <string>(), It.IsAny <int?>(), It.IsAny <RequestOptions>(), It.IsAny <CancellationToken>()), Times.Once); mockedContainer.Verify(c => c.ReadItemAsync <CosmosCacheSession>(It.Is <string>(id => id == "key"), It.IsAny <PartitionKey>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>()), Times.Once); mockedContainer.Verify(c => c.ReplaceItemAsync <CosmosCacheSession>(It.Is <CosmosCacheSession>(item => item.TimeToLive == ttlSliding), It.Is <string>(id => id == "key"), It.IsAny <PartitionKey?>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>()), Times.Once); }
public async Task GetDoesRetryUpdateIfRetrySlidingExpirationUpdatesIsTrue() { string etag = "etag"; CosmosCacheSession existingSession = new CosmosCacheSession(); existingSession.SessionKey = "key"; existingSession.Content = new byte[0]; existingSession.IsSlidingExpiration = true; Mock <ItemResponse <CosmosCacheSession> > mockedItemResponse = new Mock <ItemResponse <CosmosCacheSession> >(); Mock <CosmosClient> mockedClient = new Mock <CosmosClient>(); Mock <Container> mockedContainer = new Mock <Container>(); Mock <Database> mockedDatabase = new Mock <Database>(); Mock <ContainerResponse> mockedResponse = new Mock <ContainerResponse>(); mockedItemResponse.Setup(c => c.Resource).Returns(existingSession); mockedItemResponse.Setup(c => c.ETag).Returns(etag); mockedResponse.Setup(c => c.StatusCode).Returns(HttpStatusCode.OK); mockedContainer.Setup(c => c.ReadContainerAsync(It.IsAny <ContainerRequestOptions>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockedResponse.Object); mockedContainer.Setup(c => c.ReadItemAsync <CosmosCacheSession>(It.Is <string>(id => id == "key"), It.IsAny <PartitionKey>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockedItemResponse.Object); mockedContainer.SetupSequence(c => c.ReplaceItemAsync <CosmosCacheSession>(It.Is <CosmosCacheSession>(item => item == existingSession), It.Is <string>(id => id == "key"), It.IsAny <PartitionKey?>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>())) .ThrowsAsync(new CosmosException("test", HttpStatusCode.PreconditionFailed, 0, "", 0)) .ReturnsAsync(mockedItemResponse.Object); mockedClient.Setup(c => c.GetContainer(It.IsAny <string>(), It.IsAny <string>())).Returns(mockedContainer.Object); mockedClient.Setup(c => c.GetDatabase(It.IsAny <string>())).Returns(mockedDatabase.Object); mockedClient.Setup(x => x.Endpoint).Returns(new Uri("http://localhost")); CosmosCache cache = new CosmosCache(Options.Create(new CosmosCacheOptions() { DatabaseName = "something", ContainerName = "something", CreateIfNotExists = true, CosmosClient = mockedClient.Object, RetrySlidingExpirationUpdates = true })); Assert.Same(existingSession.Content, await cache.GetAsync("key")); // Checks for Db existence due to CreateIfNotExists mockedClient.Verify(c => c.CreateDatabaseIfNotExistsAsync(It.IsAny <string>(), It.IsAny <int?>(), It.IsAny <RequestOptions>(), It.IsAny <CancellationToken>()), Times.Once); mockedContainer.Verify(c => c.ReadItemAsync <CosmosCacheSession>(It.Is <string>(id => id == "key"), It.IsAny <PartitionKey>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>()), Times.Exactly(2)); mockedContainer.Verify(c => c.ReplaceItemAsync <CosmosCacheSession>(It.Is <CosmosCacheSession>(item => item == existingSession), It.Is <string>(id => id == "key"), It.IsAny <PartitionKey?>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>()), Times.Exactly(2)); }