예제 #1
0
        public async void UnitTestUpdateSizeLimitDynamicallyWithGetOrAdd()
        {
            // setup
            var configurationProviderMock = new Mock <IConfigurationProvider>();

            configurationProviderMock.Setup(x => x.Get(ConfigSectionName)).Returns(new Dictionary <string, string>()
            {
                { "SizeLimit", "2" }
            });

            // run
            var configurableMemoryCache = new ConfigurableMemoryCache <int, string>(configurationProviderMock.Object, ConfigSectionName);
            await configurableMemoryCache.GetOrAddAsync(1, () => Task.FromResult("test1")).ConfigureAwait(false);

            await configurableMemoryCache.GetOrAddAsync(2, () => Task.FromResult("test2")).ConfigureAwait(false);

            await configurableMemoryCache.GetOrAddAsync(3, () => Task.FromResult("test3")).ConfigureAwait(false);

            // change configuration dynamically
            configurationProviderMock.Setup(x => x.Get(ConfigSectionName)).Returns(new Dictionary <string, string>()
            {
                { "SizeLimit", "3" }
            });
            await configurableMemoryCache.GetOrAddAsync(4, () => Task.FromResult("test4")).ConfigureAwait(false);

            // assert
            Assert.Equal(3, configurableMemoryCache.Count);
            Assert.Null(await configurableMemoryCache.GetAsync(1).ConfigureAwait(false));
            Assert.Equal("test2", await configurableMemoryCache.GetAsync(2).ConfigureAwait(false));
            Assert.Equal("test3", await configurableMemoryCache.GetAsync(3).ConfigureAwait(false));
            Assert.Equal("test4", await configurableMemoryCache.GetAsync(4).ConfigureAwait(false));
        }