public void should_call_function_once() { _cachedString.Get("Test", _worker.GetString); _cachedString.Get("Test", _worker.GetString); _worker.HitCount.Should().Be(1); }
public T Get <T>() { var result = Cached.Get(typeof(T)); if (result == null) { return(default(T)); } return((T)result); }
public void Get_RefreshesTheValueIfExpired() { var called = 0; var temp = new Cached <int>(1); var x = temp.Get(() => { called++; return(-1); }); Thread.Sleep(10); x = temp.Get(() => { called++; return(-1); }); Assert.IsTrue(called == 2); }
public void Get_RefreshesTheValueOnFirstRead() { var called = false; var temp = new Cached <int>(1); var x = temp.Get(() => { called = true; return(-1); }); Assert.IsTrue(called); }
public void Get_UsesTheSuppliedFunctionInsteadOfTheDefaultOne() { var called = 0; var temp = new Cached <int>(() => { called += 10; return(-1); }, 1); var x = temp.Get(() => { called++; return(-1); }); Assert.IsTrue(called == 1); }
public void should_honor_ttl() { int hitCount = 0; _cachedString = new Cached<string>(); for (int i = 0; i < 10; i++) { _cachedString.Get("key", () => { hitCount++; return null; }, TimeSpan.FromMilliseconds(300)); Thread.Sleep(100); } hitCount.Should().BeInRange(3, 6); }
public void should_honor_ttl() { int hitCount = 0; _cachedString = new Cached <string>(); for (int i = 0; i < 100; i++) { _cachedString.Get("key", () => { hitCount++; return(null); }, TimeSpan.FromMilliseconds(300)); Thread.Sleep(10); } hitCount.Should().BeInRange(3, 6); }
public void should_clear_expired_when_they_expire() { int hitCount = 0; _cachedString = new Cached <string>(); for (int i = 0; i < 10; i++) { _cachedString.Get("key", () => { hitCount++; return(null); }, TimeSpan.FromMilliseconds(300)); Thread.Sleep(100); } Thread.Sleep(1000); hitCount.Should().BeInRange(3, 7); _cachedString.Values.Should().HaveCount(0); }