public async Task RemoveAsyncDeletesItem()
        {
            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.DeleteItemAsync <CosmosCacheSession>(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",
                CosmosClient  = mockedClient.Object
            }));

            await cache.RemoveAsync("key");

            mockedContainer.Verify(c => c.DeleteItemAsync <CosmosCacheSession>(It.Is <string>(id => id == "key"), It.IsAny <PartitionKey>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>()), Times.Once);
        }
Ejemplo n.º 2
0
        public async Task RemoveSessionData_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);
            await cache.RemoveAsync(sessionId);
        }
Ejemplo n.º 3
0
        public async Task RemoveSessionData_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);
            await cache.RemoveAsync(sessionId);

            Assert.Equal(4, diagnosticsSink.CapturedDiagnostics.Count);
            foreach (CosmosDiagnostics diagnostics in diagnosticsSink.CapturedDiagnostics)
            {
                Assert.NotNull(diagnostics?.ToString());
            }
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        public async Task RemoveAsync_AfterRemove_GetReturnsNull()
        {
            // Arrange
            var cacheValue = "Test";


            await stringCache.SetAsync(new CacheItem <string>(CacheKey, cacheValue, TimeSpan.FromSeconds(5)));

            // Act
            await stringCache.RemoveAsync(CacheKey);

            // Assert
            var result = await stringCache.GetAsync(CacheKey);

            Assert.Null(result);
        }
Ejemplo n.º 6
0
        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());
            }
        }