예제 #1
0
        public void TestDisposeOnSlidingExpiration()
        {
            this.app.GetConfig <ScheduledEventsConfig>().ExecutionCheck = TimeSpan.FromMilliseconds(1);
            this.app.GetConfig <MemoryCachingConfig>().ExpirationCheck  = TimeSpan.FromMilliseconds(1);
            this.app.Reinitialize();
            var timeService = new TimeServiceMock(this.app);

            this.app.Container.Register(timeService);

            var disposed = false;

            var cache = this.app.GetMemoryCache();

            cache.Set(nameof(TestDisposeOnSlidingExpiration), this, dispose: () => disposed = true, slidingExpiration: TimeSpan.FromMilliseconds(50));
            var item = cache.Get(nameof(TestDisposeOnSlidingExpiration));

            item.Should().Be(this, "returned item should be the same as the original");

            for (int i = 0; i < 75; i++)
            {
                item = cache.Get(nameof(TestDisposeOnSlidingExpiration));
                item.Should().Be(this, $"returned item should be the same as the original after {i} retries");
                disposed.Should().BeFalse($"the item should not have expired after {i} retries");
                Thread.Sleep(1);
            }

            timeService.SetTime(timeService.GetTime().AddMilliseconds(51));
            new Func <bool>(() => disposed).ShouldReturn(true, TimeSpan.FromMilliseconds(1000), "the item should have expired");
            item = cache.Get(nameof(TestDisposeOnSlidingExpiration));
            item.Should().BeNull("the item shold have been removed from the cache");
        }
예제 #2
0
        public void TestMixedExpiration()
        {
            this.app.GetConfig <ScheduledEventsConfig>().ExecutionCheck = TimeSpan.FromMilliseconds(1);
            this.app.GetConfig <MemoryCachingConfig>().ExpirationCheck  = TimeSpan.FromMilliseconds(1);
            this.app.Reinitialize();
            var timeService = new TimeServiceMock(this.app);

            this.app.Container.Register(timeService);

            var cache = this.app.GetMemoryCache();

            cache.Set(nameof(TestMixedExpiration), this, absoluteExpiration: TimeSpan.FromMilliseconds(50), slidingExpiration: TimeSpan.FromMilliseconds(25));
            var item = cache.Get(nameof(TestMixedExpiration));

            item.Should().Be(this, "returned item should be the same as the original");

            timeService.SetTime(timeService.GetTime().AddMilliseconds(51));
            new Func <object>(() => cache.Get(nameof(TestMixedExpiration)))
            .ShouldReturn(null, TimeSpan.FromMilliseconds(1000), "the item shold have been removed from the cache");
        }