public async Task WHEN_Passing_Any_Culture_Resulting_TreeStructure_SHOULD_Be_Cached_ByCulture()
        {
            //Arrange
            CacheProviderFactory.CacheHistMonitor monitor = new CacheProviderFactory.CacheHistMonitor();
            LocalizationTree cachedTree = new LocalizationTree(CultureInfo.CurrentCulture);

            AutoMocker container = new AutoMocker();

            container.Use(CacheProviderFactory.CreateWithMonitor(monitor, cachedTree));
            container.Use(ComposerEnvironmentFactory.Create());

            ILocalizationProvider localizationProvider = container.CreateInstance <ResourceLocalizationProvider>();

            CultureInfo cultureA = CultureInfo.GetCultureInfo("fr-CA");
            CultureInfo cultureB = CultureInfo.GetCultureInfo("fr-FR");

            //Act
            monitor.Reset();
            monitor.CacheMissCount.ShouldBeEquivalentTo(0, "Otherwise this test is irrelevent");
            monitor.CacheHitCount.ShouldBeEquivalentTo(0, "Otherwise this test is irrelevent");

            LocalizationTree tree1A = await localizationProvider.GetLocalizationTreeAsync(cultureA);

            monitor.CacheMissCount.ShouldBeEquivalentTo(1, "First attempt to load the CultureA should cache miss");

            LocalizationTree tree2A = await localizationProvider.GetLocalizationTreeAsync(cultureA);

            monitor.CacheHitCount.ShouldBeEquivalentTo(1, "Second attempt to load the CultureA should cache hit");

            monitor.Reset();
            for (int i = 0; i < 10; i++)
            {
                LocalizationTree tree3A = await localizationProvider.GetLocalizationTreeAsync(cultureA);
            }
            monitor.CacheMissCount.ShouldBeEquivalentTo(0, "Subsequent attempt to load the CultureA should not cache miss");
            monitor.CacheHitCount.Should().BeGreaterOrEqualTo(10, "Subsequent attempt to load the CultureA should cache hit");

            //--
            monitor.Reset();
            LocalizationTree tree1B = await localizationProvider.GetLocalizationTreeAsync(cultureB);

            monitor.CacheMissCount.ShouldBeEquivalentTo(1, "First attempt to load the CultureB should cache miss, key is culture dependant");
            monitor.CacheHitCount.ShouldBeEquivalentTo(0, "First attempt to load the CultureB should not cache hit, key is culture dependant");
        }
Пример #2
0
        public void WHEN_Passing_Any_TemplateName_Resulting_View_SHOULD_Be_Cached_ByTemplateName()
        {
            //Arrange
            var controllerContext = new Mock <ControllerContext>(MockBehavior.Strict);
            var cachedView        = new HandlebarsView((w, o) => { }, GetRandom.String(32), new Dictionary <string, HandlebarsView>());
            var monitor           = new CacheProviderFactory.CacheHistMonitor();
            var cacheProvider     = CacheProviderFactory.CreateForHandlebarsWithMonitor(monitor, cachedView, cachedView);
            var viewEngine        = new UnitTestableHandlebarsViewEngine(cacheProvider.Object);

            string templateNameA = "SubLevel2";
            string templateNameB = "OtherLeaf";

            //Act
            monitor.Reset();
            monitor.CacheMissCount.ShouldBeEquivalentTo(0, "Otherwise this test is irrelevent");
            monitor.CacheHitCount.ShouldBeEquivalentTo(0, "Otherwise this test is irrelevent");

            var result1A = viewEngine.FindPartialView(controllerContext.Object, templateNameA, false);

            monitor.CacheMissCount.ShouldBeEquivalentTo(1, "First attempt to load the TemplateA should cache miss");

            var result2A = viewEngine.FindPartialView(controllerContext.Object, templateNameA, false);

            monitor.CacheHitCount.ShouldBeEquivalentTo(1, "Second attempt to load the TemplateA should cache hit");

            monitor.Reset();
            for (int i = 0; i < 10; i++)
            {
                var result3A = viewEngine.FindPartialView(controllerContext.Object, templateNameA, false);
            }
            monitor.CacheMissCount.ShouldBeEquivalentTo(0, "Subsequent attempt to load the TemplateA should not cache miss");
            monitor.CacheHitCount.Should().BeGreaterOrEqualTo(10, "Subsequent attempt to load the TemplateA should cache hit");

            //--
            monitor.Reset();
            var result1B = viewEngine.FindPartialView(controllerContext.Object, templateNameB, false);

            monitor.CacheMissCount.ShouldBeEquivalentTo(1, "First attempt to load the CultureB should cache miss, key is culture dependant");
            monitor.CacheHitCount.ShouldBeEquivalentTo(0, "First attempt to load the CultureB should not cache hit, key is culture dependant");
        }