public void Invalidate_OfCacheableClassWhenCalledInvalidatedAndCalledAgain_ReturnsSecondCachedValue() { // Arrange int cachedValue = 100; AutoRefreshingItemCache <CacheableClass> cache = new AutoRefreshingItemCache <CacheableClass>( new FakeLogger(), (ct) => { cachedValue += 25; return(Task.FromResult(new CacheableClass(cachedValue))); }, null, TimeSpan.FromMinutes(2) ); CacheableClass cachedClass; int actualResult; int expectedResult = 150; cache.Initialise(); // Act _ = cache.GetItem(); cache.Invalidate(); TimeDelay.WaitFor(50); cachedClass = cache.GetItem(); actualResult = cachedClass.GetValue(); // Assert Assert.AreEqual(expectedResult, actualResult); }
public void GetItem_OfCacheableStructRequestedMultipleTimesInSuccession_CallsRetrievalDelegateJustOnce() { // Arrange CacheableStruct cacheableStruct = new CacheableStruct(125); AutoRefreshingItemCache <CacheableStruct> cache = new AutoRefreshingItemCache <CacheableStruct>( new FakeLogger(), (ct) => { return(Task.FromResult(cacheableStruct.GetSelf())); }, new CacheableStruct(1000), TimeSpan.FromMinutes(2) ); int actualResult; int expectedResult = 1; cache.Initialise(); // Act _ = cache.GetItem(); _ = cache.GetItem(); _ = cache.GetItem(); _ = cache.GetItem(); actualResult = cacheableStruct.NumberOfGetSelfCalls; // Assert Assert.AreEqual(expectedResult, actualResult); }
public void GetItem_OfCacheableClassWhenCacheExpires_CallsRetrievalDelegateAgain() { // Arrange AutoRefreshingItemCache <CacheableClass> cache = new AutoRefreshingItemCache <CacheableClass>( new FakeLogger(), (ct) => { return(Task.FromResult(new CacheableClass(125))); }, null, TimeSpan.FromMilliseconds(100)); DateTime initialCreatedAt; DateTime finalCreatedAt; CacheableClass cacheItem; cache.Initialise(); // Act cacheItem = cache.GetItem(); initialCreatedAt = cacheItem.CreatedAt; // Wait for the cache item to expire and then retry TimeDelay.WaitFor(150); cacheItem = cache.GetItem(); finalCreatedAt = cacheItem.CreatedAt; // Assert that the creation values are different Assert.AreNotEqual(initialCreatedAt, finalCreatedAt); }
public void Invalidate_OfIntWhenCalledInvalidatedAndCalledAgain_ReturnsSecondCachedValue() { // Arrange int cachedValue = 100; AutoRefreshingItemCache <int> cache = new AutoRefreshingItemCache <int>( new FakeLogger(), new NonCloningClonerFactory <int>(), (ct) => { cachedValue += 25; return(Task.FromResult(cachedValue)); }, 0, TimeSpan.FromMinutes(2) ); int actualResult; int expectedResult = 150; cache.Initialise(); // Act _ = cache.GetItem(); cache.Invalidate(); TimeDelay.WaitFor(50); actualResult = cache.GetItem(); // Assert Assert.AreEqual(expectedResult, actualResult); }
public void GetItem_OfCacheableClassRequestedMultipleTimesInSuccession_CallsRetrievalDelegateJustOnce() { // Arrange AutoRefreshingItemCache <CacheableClass> cache = new AutoRefreshingItemCache <CacheableClass>( new FakeLogger(), (ct) => { return(Task.FromResult(new CacheableClass(125))); }, null, TimeSpan.FromMilliseconds(100)); DateTime initialCreatedAt; DateTime finalCreatedAt; CacheableClass cacheItem; cache.Initialise(); // Act cacheItem = cache.GetItem(); initialCreatedAt = cacheItem.CreatedAt; _ = cache.GetItem(); _ = cache.GetItem(); cacheItem = cache.GetItem(); finalCreatedAt = cacheItem.CreatedAt; // Assert Assert.AreEqual(initialCreatedAt, finalCreatedAt); }
public void GetItem_OfCacheableStruct_ReturnsCorrectCachedValue() { // Arrange CacheableStruct cacheableStruct = new CacheableStruct(125); AutoRefreshingItemCache <CacheableStruct> cache = new AutoRefreshingItemCache <CacheableStruct>( new FakeLogger(), (ct) => { return(Task.FromResult(cacheableStruct)); }, new CacheableStruct(1000), TimeSpan.FromMinutes(2) ); int actualResult; int expectedResult = 125; cache.Initialise(); // Act actualResult = cache.GetItem().GetValue(); // Assert Assert.AreEqual(expectedResult, actualResult); }
public void GetItem_OfInt_ReturnsCorrectCachedValue() { // Arrange AutoRefreshingItemCache <int> cache = new AutoRefreshingItemCache <int>( new FakeLogger(), new NonCloningClonerFactory <int>(), (ct) => { return(Task.FromResult(125)); }, 0, TimeSpan.FromMinutes(2) ); int actualResult; int expectedResult = 125; cache.Initialise(); // Act actualResult = cache.GetItem(); // Assert Assert.AreEqual(expectedResult, actualResult); }
public void GetItem_OfCacheableClassWhenCalledTwice_DoesNotReturnSameInstance() { // Arrange AutoRefreshingItemCache <CacheableClass> cache = new AutoRefreshingItemCache <CacheableClass>( new FakeLogger(), (ct) => { return(Task.FromResult(new CacheableClass(125))); }, null, TimeSpan.FromMilliseconds(100)); CacheableClass cacheItem1; CacheableClass cacheItem2; cache.Initialise(); // Act cacheItem1 = cache.GetItem(); cacheItem2 = cache.GetItem(); // Assert that full, deep cloning is being performed Assert.AreNotEqual(cacheItem1, cacheItem2); Assert.AreNotEqual(cacheItem1.Child, cacheItem2.Child); }
public void GetItem_OfCacheableClassWhenCalledTwice_CreatesPerfectClone() { // Arrange AutoRefreshingItemCache <CacheableClass> cache = new AutoRefreshingItemCache <CacheableClass>( new FakeLogger(), (ct) => { return(Task.FromResult(new CacheableClass(125))); }, null, TimeSpan.FromMilliseconds(100)); CacheableClass cacheItem1; CacheableClass cacheItem2; cache.Initialise(); // Act cacheItem1 = cache.GetItem(); cacheItem2 = cache.GetItem(); // Assert that the clone worked correctly Assert.AreEqual(cacheItem1.GetValue(), cacheItem2.GetValue()); Assert.AreEqual(cacheItem1.NumberOfCalls, cacheItem2.NumberOfCalls); Assert.AreEqual(cacheItem1.CreatedAt, cacheItem2.CreatedAt); }
/// <summary> /// Initialise the underlying cache so that it is ready for use. /// </summary> /// <remarks> /// Note that this method blocks until initialisation is complete. If there is a chance that /// the data source will be unavailable then use of the alternative StartInitialisation and /// TryCompleteInitialisation methods may be more appropriate /// </remarks> void IDisallowedFragmentListCacheInitialiser.Initialise() { _cache.Initialise(); }
/// <summary> /// Initialise the underlying cache so that it is ready for use. /// </summary> /// <remarks> /// Note that this method blocks until initialisation is complete. If there is a chance that /// the data source will be unavailable then use of the alternative StartInitialisation and /// TryCompleteInitialisation methods may be more appropriate /// </remarks> void ICharacterSetListCacheInitialiser.Initialise() { _cache.Initialise(); }