Exemplo n.º 1
0
        public void GetValue_WhenTheCacheIsFull_ShouldPushTheLeastRecentlyUsedOff()
        {
            var service = MockRepository.GenerateMock<IService>();
            var cache = new RefreshingCache(service, 2, 1);


            service.Expect(x => x.GetValue("key1"))
                .Return(string.Empty)
                .Repeat.Times(2);

            service.Expect(x => x.GetValue("key2"))
                .Return(string.Empty)
                .Repeat.Times(1);

            service.Expect(x => x.GetValue("key3"))
                .Return(string.Empty)
                .Repeat.Times(1);

            cache.GetValue("key1");
            cache.GetValue("key2");
            cache.GetValue("key3");
            cache.GetValue("key1");

            service.VerifyAllExpectations();



        }
Exemplo n.º 2
0
        public void RefreshingCacheIndexer_SimpleIntegerValue_ShouldPass()
        {
            var systemTime = new Time();
            var cache      = new RefreshingCache <int, string>(CacheSize, ExpirationTime, new DataStorage(), systemTime);
            var actual     = cache[0];

            Assert.Equal("0", actual);
        }
Exemplo n.º 3
0
        public void WhenGetValueTheFirstTime_ShouldCallTheRemoteService()
        {
            var service = MockRepository.GenerateMock<IService>();
            var cache = new RefreshingCache(service, 5, 5);
            cache.GetValue("Test");

            service.AssertWasCalled(x => x.GetValue("Test"));
        }
Exemplo n.º 4
0
        public void WhenGetValueMoreThanOneTime_ShouldOnCallTheProxyOnce()
        {
            var service = MockRepository.GenerateMock<IService>();
            var cache = new RefreshingCache(service, 5, 5);

            service.Expect(x => x.GetValue("Test"))
                .Return(string.Empty)
                .Repeat.Once();

            cache.GetValue("Test");

            service.VerifyAllExpectations();
        }
Exemplo n.º 5
0
        public void RefreshingCacheIndexer_WhenCacheSizeMoreThanMaxSize_ShouldPass()
        {
            var systemTime = new Time();

            var cache = new RefreshingCache <int, string>(CacheSize, ExpirationTime, new DataStorage(), systemTime);

            for (int i = 0; i < 20; ++i)
            {
                ++systemTime.CurrentTime;
                var temp = cache[i];
            }

            Assert.Equal(CacheSize, cache.Count);
        }
Exemplo n.º 6
0
        public void GetValue_WhenTheCacheIsStale_ShouldCallTheRemoteProxy()
        {
            var service = MockRepository.GenerateMock<IService>();
            var cache = new RefreshingCache(service, 5, 1);


            service.Expect(x => x.GetValue("Test"))
                .Return(string.Empty)
                .Repeat.Twice();

            cache.GetValue("Test");

            Thread.Sleep(1000);

            cache.GetValue("Test");

            service.VerifyAllExpectations();
        }
Exemplo n.º 7
0
        public void RefreshingCacheIndexer_WhenTimeIsUp_ShouldPass()
        {
            var systemTime  = new Time();
            var dataStorage = new DataStorage();

            string temp;
            var    cache = new RefreshingCache <int, string>(CacheSize, ExpirationTime, dataStorage, systemTime);

            for (int i = 0; i < 16; ++i)
            {
                ++systemTime.CurrentTime;
                temp = cache[i];
            }

            systemTime.CurrentTime = TimeMoreThanMax;

            //we add new data with key "17". It replaces data with key "0" because our time expired.
            temp = cache[17];

            //And when we try get data with key "0", cache gets it from dataStorage, because it was deleted early.
            temp = cache[0];

            Assert.Equal("0", dataStorage.LastData);
        }