public void CanGetItemBeforeExpiration() { TimedEventMock timedEventMock = null; SystemClockMock clock = new SystemClockMock(); TimerFactoryMock timerFactory = new TimerFactoryMock( timedEventMockFactory: (interval, runOnce, action) => { TimedEventMock result = new TimedEventMock(interval, runOnce, action); timedEventMock = result; return(result); }, timerMockFactory: null ); const string key = "test_key"; const string value = "test_value"; clock.UtcNow = DateTime.Now; TimeSpan expiration = TimeSpan.FromHours(1); using (InMemoryCache <string> cache = new InMemoryCache <string>(clock, timerFactory)) { cache.Set(key, value, expiration); clock.UtcNow += expiration - TimeSpan.FromMinutes(30); timedEventMock.Tick(); Assert.IsTrue(cache.HasItemWithKey(key), $"Cache does not contain entry with key '{key}'."); string actual = cache.Get(key); Assert.AreEqual(value, actual); } }
public void CanNotGetItemAfterExpiration() { TimedEventMock timedEventMock = null; SystemClockMock clock = new SystemClockMock(); TimerFactoryMock timerFactory = new TimerFactoryMock( timedEventMockFactory: (interval, runOnce, action) => { TimedEventMock result = new TimedEventMock(interval, runOnce, action); timedEventMock = result; return(result); }, timerMockFactory: null ); const string key = "test_key"; const string value = "test_value"; clock.UtcNow = DateTime.Now; TimeSpan expiration = TimeSpan.FromHours(1); using (InMemoryCache <string> cache = new InMemoryCache <string>(clock, timerFactory)) { cache.Set(key, value, expiration); clock.UtcNow += expiration + TimeSpan.FromSeconds(5); timedEventMock.Tick(); Assert.IsFalse(cache.HasItemWithKey(key), $"Cache contains entry with key '{key}'."); Assert.ThrowsException <KeyNotFoundException>(() => cache.Get(key)); } }
public void CanInvokeExpirationMethodWhenItemExpires() { TimedEventMock timedEventMock = null; SystemClockMock clock = new SystemClockMock(); TimerFactoryMock timerFactory = new TimerFactoryMock( timedEventMockFactory: (interval, runOnce, action) => { TimedEventMock result = new TimedEventMock(interval, runOnce, action); timedEventMock = result; return(result); }, timerMockFactory: null ); const string key = "test_key"; const string value = "test_value"; clock.UtcNow = DateTime.Now; TimeSpan expiration = TimeSpan.FromHours(1); bool expirationMethodInvoked = false; bool expirationEventIsFiredForItem = false; using (InMemoryCache <string> cache = new InMemoryCache <string>(clock, timerFactory)) { cache.TtlExpired += (o, expiredValue) => { if (expiredValue == value) { expirationEventIsFiredForItem = true; } }; cache.Set(key, value, expiration, (x) => { expirationMethodInvoked = true; }); clock.UtcNow += expiration + TimeSpan.FromSeconds(5); timedEventMock.Tick(); Assert.IsTrue(expirationEventIsFiredForItem, $"{nameof(cache.TtlExpired)} is not fired for the expired item."); Assert.IsTrue(expirationMethodInvoked, "The method set to be execute on expiration of the item has not invoked."); } }
public void CanDisposeProperly() { TimedEventMock timedEventMock = null; SystemClockMock clock = new SystemClockMock(); TimerFactoryMock timerFactory = new TimerFactoryMock( timedEventMockFactory: (interval, runOnce, action) => { TimedEventMock result = new TimedEventMock(interval, runOnce, action); timedEventMock = result; return(result); }, timerMockFactory: null ); using (InMemoryCache <string> cache = new InMemoryCache <string>(clock, timerFactory)) { //do nothing here } Assert.IsTrue(timedEventMock.DisposeInvoked, $"Dispose method is not invoked on {nameof(timedEventMock)}."); }