public void Cache_can_be_cleared() { var item = new Item(); var key = item.Id; var factoryMock = new Mock <Func <Item> >(); factoryMock.Setup(factory => factory()).Returns(item); var cache = new MKCache <Item>(); var foundItem = cache.GetOrCreate(key, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, foundItem); var cachedItem = cache.GetOrCreate(key, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, cachedItem); cache.Clear(); var newlyCachedItem = cache.GetOrCreate(key, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Exactly(2)); Assert.Same(item, newlyCachedItem); }
public async Task Cache_item_expires() { var cache = new MKCache <Item>(); var item = new Item(); var factoryMock = new Mock <Func <Item> >(); factoryMock.Setup(factory => factory()).Returns(item); var expirySpan = TimeSpan.FromSeconds(1); var foundItem = cache.GetOrCreate(item.Id, factoryMock.Object, expirySpan); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, foundItem); var cachedItem = cache.GetOrCreate(item.Id, () => new Item(), expirySpan); Assert.Same(item, cachedItem); await Task.Delay(expirySpan * 3); var newItem = cache.GetOrCreate(item.Id, factoryMock.Object, expirySpan); factoryMock.Verify(factory => factory(), Times.Exactly(2)); }
public void An_item_is_cached_with_the_default_key() { var item = new Item(); var key = item.Id; var factoryMock = new Mock <Func <Item> >(); factoryMock.Setup(factory => factory()).Returns(item); var cache = new MKCache <Item>(); var foundItem = cache.GetOrCreate(key, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, foundItem); var cachedItem = cache.GetOrCreate(key, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, cachedItem); var _ = cache.GetOrCreate(item.Name, factoryMock.Object, Expiration); // Delegate invoked a second time, because the item was not found factoryMock.Verify(factory => factory(), Times.Exactly(2)); }
public void An_item_is_cached_with_multiple_keys() { var item = new Item(); var key = item.Id; var factoryMock = new Mock <Func <Item> >(); factoryMock.Setup(factory => factory()).Returns(item); var cache = new MKCache <Item>( x => x.Id, x => x.Name); var foundItem = cache.GetOrCreate(key, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, foundItem); var cachedItem = cache.GetOrCreate(key, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, cachedItem); var cachedItemWithDifferentKey = cache.GetOrCreate(item.Name, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, cachedItemWithDifferentKey); }
public void Can_be_disposed() { var cache = new MKCache <Item>(); var item = cache.GetOrCreate("1", () => new Item(), Expiration); cache.Dispose(); // Dispose is not Clear. Assert.Equal(1, cache.Count); // Disposed cache cannot be used anymore. Assert.Throws <ObjectDisposedException>(() => cache.GetOrCreate("1", () => new Item(), Expiration)); }
public void Can_inspect_items_count() { var cache = new MKCache <int>(); Assert.Equal(0, cache.Count); cache.GetOrCreate("1", () => 1, Expiration); Assert.Equal(1, cache.Count); cache.GetOrCreate("2", () => 2, Expiration); Assert.Equal(2, cache.Count); cache.Clear(); Assert.Equal(0, cache.Count); }
public void Null_value_does_not_throw_and_does_not_get_cached() { var cache = new MKCache <Item>(); var factoryMock = new Mock <Func <Item> >(); factoryMock.Setup(factory => factory()).Returns((null as Item) !); var nullItem = cache.GetOrCreate("any", factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Null(nullItem); nullItem = cache.GetOrCreate("any", factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Exactly(2)); Assert.Null(nullItem); }
public void Can_inspect_items_count() { var cache = new MKCache <Item>( x => x.Id, x => x.Name); Assert.Equal(0, cache.Count); cache.GetOrCreate("1", () => new Item(), Expiration); Assert.Equal(1, cache.Count); cache.GetOrCreate("name", () => new Item(), Expiration); Assert.Equal(2, cache.Count); cache.Clear(); Assert.Equal(0, cache.Count); }
public void An_item_is_cached_with_a_single_key() { var item = new Item(); var factoryMock = new Mock <Func <Item> >(); factoryMock.Setup(factory => factory()).Returns(item); var cache = new MKCache <Item>( x => x.Name /* Name property is the key */); _ = cache.GetOrCreate(item.Id, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); var cachedItem = cache.GetOrCreate(item.Name, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, cachedItem); _ = cache.GetOrCreate(item.Id, factoryMock.Object, Expiration); // Delegate invoked a second time, because the item was not found using its Id factoryMock.Verify(factory => factory(), Times.Exactly(2)); }
public void Null_key_from_identifier_does_not_throw_and_value_does_not_get_cached() { var cache = new MKCache <Item>( x => x.Name /* The Name is the key */); var item = new Item(name: null !); var factoryMock = new Mock <Func <Item> >(); factoryMock.Setup(factory => factory()).Returns(item); var foundItem = cache.GetOrCreate(item.Id, factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, foundItem); foundItem = cache.GetOrCreate("any", factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Exactly(2)); Assert.Same(item, foundItem); foundItem = cache.GetOrCreate("any", factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Exactly(3)); Assert.Same(item, foundItem); }
public void Items_can_be_retrieved_from_the_cache() { var item = new Item(); var factoryMock = new Mock <Func <Item> >(); factoryMock.Setup(factory => factory()).Returns(item); var cache = new MKCache <Item>(); var nullItem = cache.Get("any"); Assert.Null(nullItem); var foundItem = cache.GetOrCreate("any", factoryMock.Object, Expiration); factoryMock.Verify(factory => factory(), Times.Once); Assert.Same(item, foundItem); var cachedItem = cache.Get("any"); Assert.Same(item, cachedItem); }