public async Task SetAsyncString_DoesNotThrow() { // Arrange // Act await stringCache.SetAsync(new CacheItem <string>(CacheKey, "Test", TimeSpan.FromSeconds(5))); // Assert }
public async Task StoreSessionData() { 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 }); CosmosCache cache = new CosmosCache(options); DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttl); await cache.SetAsync(sessionId, new byte[0], cacheOptions); // Verify that container has been created CosmosCacheSession storedSession = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadItemAsync <CosmosCacheSession>(sessionId, new PartitionKey(sessionId)); Assert.Equal(sessionId, storedSession.SessionKey); }
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 ValidatesAbsoluteExpiration() { DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.AbsoluteExpiration = DateTime.UtcNow.AddHours(-1); CosmosCacheSession existingSession = new CosmosCacheSession(); existingSession.SessionKey = "key"; existingSession.Content = new byte[0]; 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>(); mockedResponse.Setup(c => c.StatusCode).Returns(HttpStatusCode.OK); mockedContainer.Setup(c => c.ReadContainerAsync(It.IsAny <ContainerRequestOptions>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockedResponse.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", CosmosClient = mockedClient.Object })); await Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => cache.SetAsync(existingSession.SessionKey, existingSession.Content, cacheOptions)); }
public async Task SetAsyncCallsUpsert() { int ttl = 10; DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttl); CosmosCacheSession existingSession = new CosmosCacheSession(); existingSession.SessionKey = "key"; existingSession.Content = new byte[0]; 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>(); 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.UpsertItemAsync <CosmosCacheSession>(It.Is <CosmosCacheSession>(item => item.SessionKey == existingSession.SessionKey && item.TimeToLive == ttl), 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", CosmosClient = mockedClient.Object })); await cache.SetAsync(existingSession.SessionKey, existingSession.Content, cacheOptions); mockedContainer.Verify(c => c.UpsertItemAsync <CosmosCacheSession>(It.Is <CosmosCacheSession>(item => item.SessionKey == existingSession.SessionKey && item.TimeToLive == ttl), It.IsAny <PartitionKey?>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>()), Times.Once); }
public async Task InitializeContainerIfNotExists() { 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 }); CosmosCache cache = new CosmosCache(options); DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttl); await cache.SetAsync(sessionId, new byte[0], cacheOptions); // Verify that container has been created ContainerResponse response = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadContainerAsync(); Assert.NotEqual(HttpStatusCode.NotFound, response.StatusCode); Assert.NotEqual(ttl, response.Resource.DefaultTimeToLive); int?throughputContainer = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadThroughputAsync(); Assert.Equal(throughput, throughputContainer); }
public async Task RemoveSessionData_CustomPartitionKey() { const string sessionId = "sessionId"; const int ttl = 1400; const int throughput = 2000; byte[] data = new byte[1] { 1 }; 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); await cache.SetAsync(sessionId, data, cacheOptions); await cache.RemoveAsync(sessionId); CosmosException exception = await Assert.ThrowsAsync <CosmosException>(() => this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadItemAsync <dynamic>(sessionId, new PartitionKey(sessionId))); Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode); }
public async Task InitializeContainerIfNotExists() { DiagnosticsSink diagnosticsSink = new DiagnosticsSink(); const string sessionId = "sessionId"; const int ttl = 2000; const int ttlInSeconds = ttl / 1000; 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, DefaultTimeToLiveInMs = ttl, DiagnosticsHandler = diagnosticsSink.CaptureDiagnostics }); CosmosCache cache = new CosmosCache(options); DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttlInSeconds); await cache.SetAsync(sessionId, new byte[0], cacheOptions); // Verify that container has been created ContainerResponse response = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadContainerAsync(); Assert.NotEqual(HttpStatusCode.NotFound, response.StatusCode); Assert.Equal(ttlInSeconds, response.Resource.DefaultTimeToLive); Assert.True(response.Resource.IndexingPolicy.ExcludedPaths.Any(e => e.Path.Equals("/*"))); int?throughputContainer = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadThroughputAsync(); Assert.Equal(throughput, throughputContainer); Assert.Equal(4, diagnosticsSink.CapturedDiagnostics.Count); foreach (CosmosDiagnostics diagnostics in diagnosticsSink.CapturedDiagnostics) { Assert.NotNull(diagnostics?.ToString()); } }
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 RemoveSessionData() { DiagnosticsSink diagnosticsSink = new DiagnosticsSink(); 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, DiagnosticsHandler = diagnosticsSink.CaptureDiagnostics }); CosmosCache cache = new CosmosCache(options); DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttl); await cache.SetAsync(sessionId, data, cacheOptions); await cache.RemoveAsync(sessionId); CosmosException exception = await Assert.ThrowsAsync <CosmosException>(() => this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadItemAsync <dynamic>(sessionId, new PartitionKey(sessionId))); Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode); Assert.Equal(5, diagnosticsSink.CapturedDiagnostics.Count); foreach (CosmosDiagnostics diagnostics in diagnosticsSink.CapturedDiagnostics) { Assert.NotNull(diagnostics?.ToString()); } }
public async Task InitializeContainerIfNotExists_CustomPartitionKey() { const string sessionId = "sessionId"; const int ttl = 2000; const int ttlInSeconds = ttl / 1000; 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, DefaultTimeToLiveInMs = ttl, ContainerPartitionKeyAttribute = partitionKeyAttribute, }); CosmosCache cache = new CosmosCache(options); DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions(); cacheOptions.SlidingExpiration = TimeSpan.FromSeconds(ttlInSeconds); await cache.SetAsync(sessionId, new byte[0], cacheOptions); // Verify that container has been created ContainerResponse response = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadContainerAsync(); Assert.NotEqual(HttpStatusCode.NotFound, response.StatusCode); Assert.Equal(ttlInSeconds, response.Resource.DefaultTimeToLive); Assert.True(response.Resource.IndexingPolicy.ExcludedPaths.Any(e => e.Path.Equals("/*"))); Assert.Equal($"/{partitionKeyAttribute}", response.Resource.PartitionKeyPath); int?throughputContainer = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadThroughputAsync(); Assert.Equal(throughput, throughputContainer); }
public async Task StoreSessionData_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); 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(sessionId, storedSession.SessionKey); Assert.Equal(data, storedSession.Content); ItemResponse <dynamic> dynamicSession = await this.testClient.GetContainer(CosmosCacheEmulatorTests.databaseName, "session").ReadItemAsync <dynamic>(sessionId, new PartitionKey(sessionId)); Assert.NotNull(dynamicSession.Resource.notTheId); Assert.Equal(sessionId, (string)dynamicSession.Resource.notTheId); }
public async Task GetSessionData() { DiagnosticsSink diagnosticsSink = new DiagnosticsSink(); 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, DiagnosticsHandler = diagnosticsSink.CaptureDiagnostics }); 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)); Assert.Equal(6, diagnosticsSink.CapturedDiagnostics.Count); foreach (CosmosDiagnostics diagnostics in diagnosticsSink.CapturedDiagnostics) { Assert.NotNull(diagnostics?.ToString()); } }