コード例 #1
0
        public void RetrieveItemFromCachePostExpiration()
        {
            // Arrange
            const int zipCode       = 95661;
            var       rawTestData   = GenerateTestJSONRawData();
            var       cacheSettings = GetTestWeatherCacheSettings(1);
            //var mockIMemoryCache = new Mock<IMemoryCache>();
            MemoryCache memoryCache      = new MemoryCache(new MemoryCacheOptions());
            var         weatherDataCache = new WeatherDataCache(cacheSettings, memoryCache);

            weatherDataCache.SaveDataToCache(zipCode, rawTestData);

            // Add Delay to force Cache Expiration
            var mockDelay = new Mock <ITestDelay>();

            mockDelay.Setup(f => f.PerformTask()).Callback(() => Thread.Sleep(2000)).Returns("Test");
            mockDelay.Object.PerformTask();

            // Act
            var cachedResults = weatherDataCache.RetrieveDataFromCache(zipCode);

            // Assert
            Assert.IsTrue(string.IsNullOrWhiteSpace(cachedResults), "Cached Results were NOT Null or Empty!");
            Assert.AreNotEqual(rawTestData, cachedResults);
        }
コード例 #2
0
        public void RetrieveItemFromCachePreExpiration()
        {
            // Arrange
            const int zipCode       = 95661;
            var       rawTestData   = GenerateTestJSONRawData();
            var       cacheSettings = GetTestWeatherCacheSettings(30);
            //var mockIMemoryCache = new Mock<IMemoryCache>();
            MemoryCache memoryCache      = new MemoryCache(new MemoryCacheOptions());
            var         weatherDataCache = new WeatherDataCache(cacheSettings, memoryCache);

            weatherDataCache.SaveDataToCache(zipCode, rawTestData);

            // Act
            var cachedResults = weatherDataCache.RetrieveDataFromCache(zipCode);

            // Assert
            Assert.IsNotNull(cachedResults, "Cached Results were Null!");
            Assert.AreEqual(rawTestData, cachedResults);
        }