public async void WriteEntry_WritesEntryUsingCorrectAbsoluteExpiration_ReadBeforeExpirationTime() { const string cacheKey = "test-cache-key"; const string value = "test-value"; DateTime absoluteExpiration = new DateTime(year: 2018, month: 3, day: 17, hour: 8, minute: 0, second: 0); using (IMemoryCache memoryCache = GetMemoryCache()) { MemoryCacheAdapter memoryCacheAdapter = new MemoryCacheAdapter(memoryCache, slidingExpiration: TimeSpan.FromDays(10)); using (CurrentTime.UseMockUtcNow(absoluteExpiration.AddHours(-1))) { await memoryCacheAdapter.WriteEntry(cacheKey, new CacheEntry <string>(value, absoluteExpiration)); } using (CurrentTime.UseMockUtcNow(absoluteExpiration.AddSeconds(-1))) { CacheEntry <string> cacheEntry = memoryCache.Get <CacheEntry <string> >(cacheKey); Assert.NotNull(cacheEntry); Assert.Equal(actual: cacheEntry.Value, expected: value); Assert.Equal(actual: cacheEntry.AbsoluteExpiration, expected: absoluteExpiration); } } }
public async void WriteEntry_WritesEntryUsingCorrectSlidingExpiration_ReadAfterExpirationTime() { const string cacheKey = "test-cache-key"; const string value = "test-value"; DateTime absoluteExpiration = new DateTime(year: 2018, month: 3, day: 17, hour: 8, minute: 0, second: 0); using (IMemoryCache memoryCache = GetMemoryCache()) { TimeSpan slidingExpiration = TimeSpan.FromMinutes(10); MemoryCacheAdapter memoryCacheAdapter = new MemoryCacheAdapter(memoryCache, slidingExpiration: slidingExpiration); DateTime writeTime = absoluteExpiration.AddHours(-1); using (CurrentTime.UseMockUtcNow(writeTime)) { await memoryCacheAdapter.WriteEntry(cacheKey, new CacheEntry <string>(value, absoluteExpiration)); } using (CurrentTime.UseMockUtcNow(writeTime.Add(slidingExpiration).AddSeconds(1))) { CacheEntry <string> cacheEntry = memoryCache.Get <CacheEntry <string> >(cacheKey); Assert.Null(cacheEntry); } } }