示例#1
0
        public void Fetch_OnSave_test()
        {
            var root          = Substitute.For <ISite>();
            var cacheRegistry = new CacheRegistry(new TestCacheConfig());
            var cacheStore    = new CacheStore();
            var cache         = new Cache(cacheStore, cacheRegistry, root);

            async Task <IEnumerable <TestItem> > Task(ICache c) => await System.Threading.Tasks.Task.Run(() => new[]
            {
                new TestItem {
                    ItemId = 1
                },
                new TestItem {
                    ItemId = 3
                },
                new TestItem {
                    ItemId = 2
                }
            }).ConfigureAwait(false);

            cacheRegistry.Add(Task)
            .Span(300)
            .OnSave(x => x.OrderBy(y => y.ItemId));

            var result = cache.Fetch <TestItem>().ToList();

            Assert.That(result, Is.Not.Null);
            Assert.That(result.First().ItemId, Is.EqualTo(1));
            Assert.That(result.Last().ItemId, Is.EqualTo(3));
        }
示例#2
0
        public void Reload_no_key()
        {
            var task          = Substitute.For <Func <ICache, Task <IEnumerable <TestItem> > > >();
            var cache         = Substitute.For <ICache>();
            var cacheRegistry = new CacheRegistry(new TestCacheConfig());

            cacheRegistry.Add(task);
            cacheRegistry.Reload(cache);

            task.Received(1);
        }
示例#3
0
        public async Task ReloadAsync_no_key()
        {
            var task          = Substitute.For <Func <ICache, Task <IEnumerable <TestItem> > > >();
            var cache         = Substitute.For <ICache>();
            var cacheRegistry = new CacheRegistry(new TestCacheConfig());

            cacheRegistry.Add(task);
            await cacheRegistry.ReloadAsync(cache).ConfigureAwait(false);

            task.Received(1);
        }
示例#4
0
        public async Task FetchAsync_lookup_test()
        {
            var root          = Substitute.For <ISite>();
            var cacheRegistry = new CacheRegistry(new TestCacheConfig());
            var cacheStore    = new CacheStore();
            var lookup        = new CacheLookup <CacheItem>().Add(x => x.LookupKey);
            var lookupOther   = new CacheLookup <CacheItem>().Add(x => x.LookupKeyOther);
            var cache         = new Cache(cacheStore, cacheRegistry, root);

            async Task <IEnumerable <CacheItem> > Task(ICache c) => await System.Threading.Tasks.Task.Run(() => new[]
            {
                new CacheItem {
                    LookupKey = 1, LookupKeyOther = "2"
                },
                new CacheItem {
                    LookupKey = 1, LookupKeyOther = "2"
                },
                new CacheItem {
                    LookupKey = 2, LookupKeyOther = "1"
                }
            }).ConfigureAwait(false);

            cacheRegistry.Add(Task).Lookup(lookup).Lookup(lookupOther).Span(300);

            var result = await cache.FetchAsync(lookup).ConfigureAwait(false);

            var resultOther = await cache.FetchAsync(lookupOther).ConfigureAwait(false);

            var item      = result["2"].First();
            var itemOther = resultOther["1"].First();

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Count, Is.EqualTo(2));
            Assert.That(result["1"].Count(), Is.EqualTo(2));
            Assert.That(result["2"].Count(), Is.EqualTo(1));

            Assert.That(resultOther, Is.Not.Null);
            Assert.That(resultOther.Count, Is.EqualTo(2));
            Assert.That(resultOther["1"].Count(), Is.EqualTo(1));
            Assert.That(resultOther["2"].Count(), Is.EqualTo(2));

            Assert.That(item.Id, Is.EqualTo(itemOther.Id));
        }