コード例 #1
0
        async void Remove_LastItemInLinkedDictionary_SetSecondItemFromLastToBeLast(
            [Frozen] Mock <Func <string, Task <ConditionalValue <CachedItem> > > > getCacheItem,
            CacheStoreMetadata cacheStoreMetadata,
            LinkedDictionaryHelper linkedDictionaryHelper)
        {
            var cachedValue = Encoding.UTF8.GetBytes("some value");
            var totalSize   = cacheStoreMetadata.Size - cachedValue.Length;

            var items = new Dictionary <string, CachedItem> {
                { "1", new CachedItem(cachedValue, null, "2") },
                { "2", new CachedItem(cachedValue, "1", "3", TimeSpan.FromMilliseconds(100), new DateTime(1000)) },
                { "3", new CachedItem(cachedValue, "2", null) }
            };

            getCacheItem.Setup(mock => mock(It.IsAny <string>())).ReturnsAsync(await Task.FromResult(new ConditionalValue <CachedItem>(true, items["2"])));

            var result = await linkedDictionaryHelper.Remove(cacheStoreMetadata, items["3"]);

            Assert.Single(result.CachedItemsToUpdate);
            var newLastItem = result.CachedItemsToUpdate["2"];

            Assert.Equal(items["2"].BeforeCacheKey, newLastItem.BeforeCacheKey);
            Assert.Null(newLastItem.AfterCacheKey);
            Assert.Equal(cachedValue, newLastItem.Value);
            Assert.Equal(items["2"].SlidingExpiration, newLastItem.SlidingExpiration);
            Assert.Equal(items["2"].AbsoluteExpiration, newLastItem.AbsoluteExpiration);

            Assert.Equal(totalSize, result.CacheStoreMetadata.Size);
            Assert.Equal(cacheStoreMetadata.FirstCacheKey, result.CacheStoreMetadata.FirstCacheKey);
            Assert.Equal("2", result.CacheStoreMetadata.LastCacheKey);
        }
コード例 #2
0
        async void Remove_OnlyItemInLinkedDictionary_SetCacheItemNotCalled(
            CacheStoreMetadata cacheStoreMetadata,
            LinkedDictionaryHelper linkedDictionaryHelper)
        {
            var cachedValue = Encoding.UTF8.GetBytes("some value");
            var totalSize   = cacheStoreMetadata.Size - cachedValue.Length;
            var c           = new CachedItem(cachedValue, null, null);

            var result = await linkedDictionaryHelper.Remove(cacheStoreMetadata, c);

            Assert.Empty(result.CachedItemsToUpdate);

            Assert.Equal(totalSize, result.CacheStoreMetadata.Size);
            Assert.Null(result.CacheStoreMetadata.FirstCacheKey);
            Assert.Null(result.CacheStoreMetadata.LastCacheKey);
        }
コード例 #3
0
        async void Remove_MiddleItemInLinkedDictionary_ItemBeforeAndAfterNeedTobeLinked(
            [Frozen] Mock <Func <string, Task <ConditionalValue <CachedItem> > > > getCacheItem,
            CacheStoreMetadata cacheStoreMetadata,
            LinkedDictionaryHelper linkedDictionaryHelper)
        {
            var cachedValue = Encoding.UTF8.GetBytes("some value");
            var totalSize   = cacheStoreMetadata.Size - cachedValue.Length;

            var items = new Dictionary <string, CachedItem> {
                { "1", new CachedItem(cachedValue, null, "2", TimeSpan.FromMilliseconds(10), new DateTimeOffset(50, TimeSpan.Zero)) },
                { "2", new CachedItem(cachedValue, "1", "3") },
                { "3", new CachedItem(cachedValue, "2", null, TimeSpan.FromMilliseconds(100), new DateTimeOffset(1000, TimeSpan.Zero)) }
            };

            getCacheItem.Setup(mock => mock("1")).ReturnsAsync(await Task.FromResult(new ConditionalValue <CachedItem>(true, items["1"])));
            getCacheItem.Setup(mock => mock("3")).ReturnsAsync(await Task.FromResult(new ConditionalValue <CachedItem>(true, items["3"])));


            var result = await linkedDictionaryHelper.Remove(cacheStoreMetadata, items["2"]);

            Assert.Equal(2, result.CachedItemsToUpdate.Count);

            var newFirstItem = result.CachedItemsToUpdate["1"];

            Assert.Null(newFirstItem.BeforeCacheKey);
            Assert.Equal("3", newFirstItem.AfterCacheKey);
            Assert.Equal(cachedValue, newFirstItem.Value);
            Assert.Equal(items["1"].SlidingExpiration, newFirstItem.SlidingExpiration);
            Assert.Equal(items["1"].AbsoluteExpiration, newFirstItem.AbsoluteExpiration);

            var newLastItem = result.CachedItemsToUpdate["3"];

            Assert.Equal("1", newLastItem.BeforeCacheKey);
            Assert.Null(newLastItem.AfterCacheKey);
            Assert.Equal(cachedValue, newLastItem.Value);
            Assert.Equal(items["3"].SlidingExpiration, newLastItem.SlidingExpiration);
            Assert.Equal(items["3"].AbsoluteExpiration, newLastItem.AbsoluteExpiration);

            Assert.Equal(totalSize, result.CacheStoreMetadata.Size);
            Assert.Equal(cacheStoreMetadata.FirstCacheKey, result.CacheStoreMetadata.FirstCacheKey);
            Assert.Equal(cacheStoreMetadata.LastCacheKey, result.CacheStoreMetadata.LastCacheKey);
        }