async void GetCachedItemAsync_GetItemThatExistsWithAbsoluteExpiration_ItemIsMovedToLastItem(
            [Frozen] Mock <IReliableStateManagerReplica2> stateManager,
            [Frozen] Mock <IReliableDictionary <string, CachedItem> > cacheItemDict,
            [Frozen] Mock <IReliableDictionary <string, CacheStoreMetadata> > metadataDict,
            [Frozen] Mock <ISystemClock> systemClock,
            [Greedy] ServiceFabricDistributedCacheStoreService cacheStore)
        {
            var cacheValue  = Encoding.UTF8.GetBytes("someValue");
            var currentTime = new DateTime(2019, 2, 1, 1, 0, 0);
            var expireTime  = currentTime.AddSeconds(30);

            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime);

            SetupInMemoryStores(stateManager, cacheItemDict);
            var metadata = SetupInMemoryStores(stateManager, metadataDict);

            await cacheStore.SetCachedItemAsync("mykey1", cacheValue, null, expireTime);

            await cacheStore.SetCachedItemAsync("mykey2", cacheValue, null, expireTime);

            await cacheStore.SetCachedItemAsync("mykey3", cacheValue, null, expireTime);

            Assert.Equal("mykey3", metadata["CacheStoreMetadata"].LastCacheKey);

            await cacheStore.GetCachedItemAsync("mykey2");

            Assert.Equal("mykey2", metadata["CacheStoreMetadata"].LastCacheKey);
        }
        async void RemoveLeastRecentlyUsedCacheItemWhenOverMaxCacheSize_RemoveItemsFromLinkedDictionary_DoesNotRemoveNonExpiredItems(
            [Frozen] Mock <IReliableStateManagerReplica2> stateManager,
            [Frozen] Mock <IReliableDictionary <string, CachedItem> > cacheItemDict,
            [Frozen] Mock <IReliableDictionary <string, CacheStoreMetadata> > metadataDict,
            [Frozen] Mock <ISystemClock> systemClock,
            [Greedy] ServiceFabricDistributedCacheStoreService cacheStore)
        {
            var cacheValue  = new byte[1000000];
            var currentTime = new DateTime(2019, 2, 1, 1, 0, 0);

            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime);

            var cachedItems = SetupInMemoryStores(stateManager, cacheItemDict);
            var metadata    = SetupInMemoryStores(stateManager, metadataDict);

            await cacheStore.SetCachedItemAsync("1", cacheValue, TimeSpan.FromMinutes(10), null);

            for (var i = 2; i <= 10; i++)
            {
                await cacheStore.SetCachedItemAsync(i.ToString(), cacheValue, TimeSpan.FromSeconds(10), null);
            }

            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime.AddSeconds(10));
            await cacheStore.RemoveLeastRecentlyUsedCacheItemWhenOverMaxCacheSize();

            Assert.Single(cachedItems);
            Assert.Equal("1", metadata["CacheStoreMetadata"].FirstCacheKey);
            Assert.Equal("1", metadata["CacheStoreMetadata"].LastCacheKey);
        }
        async void GetCachedItemAsync_GetItemThatDoesHaveKeyAndIsIsSlidingExpired_SlidedExpirationUpdates(
            [Frozen] Mock <IReliableStateManagerReplica2> stateManager,
            [Frozen] Mock <IReliableDictionary <string, CachedItem> > cacheItemDict,
            [Frozen] Mock <IReliableDictionary <string, CacheStoreMetadata> > metadataDict,
            [Frozen] Mock <ISystemClock> systemClock,
            [Greedy] ServiceFabricDistributedCacheStoreService cacheStore)
        {
            var cacheValue  = Encoding.UTF8.GetBytes("someValue");
            var currentTime = new DateTime(2019, 2, 1, 1, 0, 0);

            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime);

            SetupInMemoryStores(stateManager, cacheItemDict);
            SetupInMemoryStores(stateManager, metadataDict);

            await cacheStore.SetCachedItemAsync("mykey", cacheValue, TimeSpan.FromSeconds(10), null);

            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime.AddSeconds(5));
            var resultAfter5Seconds = await cacheStore.GetCachedItemAsync("mykey");

            Assert.Equal(cacheValue, resultAfter5Seconds);
            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime.AddSeconds(8));
            var resultAfter8Seconds = await cacheStore.GetCachedItemAsync("mykey");

            Assert.Equal(cacheValue, resultAfter8Seconds);
            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime.AddSeconds(9));
            var resultAfter9Seconds = await cacheStore.GetCachedItemAsync("mykey");

            Assert.Equal(cacheValue, resultAfter9Seconds);
            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime.AddSeconds(19));
            var resultAfter19Seconds = await cacheStore.GetCachedItemAsync("mykey");

            Assert.Null(resultAfter19Seconds);
        }
        async void RemoveCachedItemAsync_RemoveItemsFromLinkedDictionary_ListStaysLinkedTogetherAfterItemsRemoved(
            [Frozen] Mock <IReliableStateManagerReplica2> stateManager,
            [Frozen] Mock <IReliableDictionary <string, CachedItem> > cacheItemDict,
            [Frozen] Mock <IReliableDictionary <string, CacheStoreMetadata> > metadataDict,
            [Frozen] Mock <ISystemClock> systemClock,
            [Greedy] ServiceFabricDistributedCacheStoreService cacheStore)
        {
            var cacheValue  = Encoding.UTF8.GetBytes("someValue");
            var currentTime = new DateTime(2019, 2, 1, 1, 0, 0);

            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime);

            var cachedItems = SetupInMemoryStores(stateManager, cacheItemDict);
            var metadata    = SetupInMemoryStores(stateManager, metadataDict);

            await cacheStore.SetCachedItemAsync("1", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.SetCachedItemAsync("2", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.SetCachedItemAsync("3", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.SetCachedItemAsync("4", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.SetCachedItemAsync("5", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.SetCachedItemAsync("6", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.SetCachedItemAsync("7", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.SetCachedItemAsync("8", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.RemoveCachedItemAsync("3");

            await cacheStore.RemoveCachedItemAsync("4");

            await cacheStore.RemoveCachedItemAsync("8");

            await cacheStore.RemoveCachedItemAsync("1");

            Assert.Null(cachedItems["2"].BeforeCacheKey);
            foreach (var item in cachedItems)
            {
                if (item.Value.BeforeCacheKey != null)
                {
                    Assert.Equal(item.Key, cachedItems[item.Value.BeforeCacheKey].AfterCacheKey);
                }
                if (item.Value.AfterCacheKey != null)
                {
                    Assert.Equal(item.Key, cachedItems[item.Value.AfterCacheKey].BeforeCacheKey);
                }
            }
            Assert.Null(cachedItems["7"].AfterCacheKey);

            Assert.Equal("2", metadata["CacheStoreMetadata"].FirstCacheKey);
            Assert.Equal("7", metadata["CacheStoreMetadata"].LastCacheKey);
            Assert.Equal((cacheValue.Length + 250) * cachedItems.Count, metadata["CacheStoreMetadata"].Size);
        }
        async void GetCachedItemAsync_GetItemThatDoesNotExist_NullResultReturned(
            [Frozen] Mock <IReliableStateManagerReplica2> stateManager,
            [Frozen] Mock <IReliableDictionary <string, CachedItem> > cacheItemDict,
            [Frozen] Mock <IReliableDictionary <string, CacheStoreMetadata> > metadataDict,
            [Frozen] Mock <ISystemClock> systemClock,
            [Greedy] ServiceFabricDistributedCacheStoreService cacheStore)
        {
            var cacheValue  = Encoding.UTF8.GetBytes("someValue");
            var currentTime = new DateTime(2019, 2, 1, 1, 0, 0);
            var expireTime  = currentTime.AddSeconds(1);

            systemClock.SetupGet(m => m.UtcNow).Returns(currentTime);

            SetupInMemoryStores(stateManager, metadataDict);
            SetupInMemoryStores(stateManager, cacheItemDict);

            var result = await cacheStore.GetCachedItemAsync("keyThatDoesNotExist");

            Assert.Null(result);
        }
Example #6
0
        async void RemoveCachedItemAsync_RemoveItemsFromLinkedDictionary_RemovalWorksWithMalformedMetadata(
            [Frozen] Mock <IReliableStateManagerReplica2> stateManager,
            [Frozen] Mock <IReliableDictionary <string, CachedItem> > cacheItemDict,
            [Frozen] Mock <IReliableDictionary <string, CacheStoreMetadata> > metadataDict,
            [Greedy] ServiceFabricDistributedCacheStoreService cacheStore)
        {
            var cacheValue = Encoding.UTF8.GetBytes("someValue");

            var cachedItems = SetupInMemoryStores(stateManager, cacheItemDict);
            var metadata    = SetupInMemoryStores(stateManager, metadataDict);

            await cacheStore.SetCachedItemAsync("1", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.SetCachedItemAsync("2", cacheValue, TimeSpan.FromSeconds(10), null);

            await cacheStore.SetCachedItemAsync("3", cacheValue, TimeSpan.FromSeconds(10), null);

            metadata["CacheStoreMetadata"] = new CacheStoreMetadata(int.MaxValue, null, "3");
            await cacheStore.RemoveLeastRecentlyUsedCacheItemWhenOverMaxCacheSize();

            metadata["CacheStoreMetadata"] = new CacheStoreMetadata(int.MaxValue, "Garbage", "3");
            await cacheStore.RemoveLeastRecentlyUsedCacheItemWhenOverMaxCacheSize();
        }