private void VerifyListenerOnWriteAttempt(ICacheKey key) { DuplicatingCacheEventListener listener = (DuplicatingCacheEventListener)_cacheEventListener; Assert.IsTrue(listener.GetEvents("OnWriteAttempt").Count != 0); SettableCacheEvent cacheEvent = (SettableCacheEvent)listener.GetLastEvent("OnWriteAttempt"); Assert.IsNotNull(cacheEvent); Assert.AreEqual(cacheEvent.CacheKey, key); }
public void TestCacheFileWithIOException() { ICacheKey key1 = new SimpleCacheKey("aaa"); // Before inserting, make sure files not exist. IBinaryResource resource1 = GetResource(key1); Assert.IsNull(resource1); // 1. Should not create cache files if IOException happens in the middle. IOException writeException = new IOException(); try { _cache.Insert(key1, new WriterCallbackImpl(os => { throw writeException; })); Assert.Fail(); } catch (IOException) { Assert.IsNull(GetResource(key1)); } VerifyListenerOnWriteAttempt(key1); VerifyListenerOnWriteException(key1, writeException); // 2. Test a read failure from DiskStorage ICacheKey key2 = new SimpleCacheKey("bbb"); int value2Size = 42; byte[] value2 = new byte[value2Size]; value2[25] = 98; // 'b' _cache.Insert(key2, WriterCallbacks.From(value2)); VerifyListenerOnWriteAttempt(key2); string resourceId2 = VerifyListenerOnWriteSuccessAndGetResourceId(key2, value2Size); ((DiskStorageWithReadFailures)_storage).SetPoisonResourceId(resourceId2); Assert.IsNull(_cache.GetResource(key2)); VerifyListenerOnReadException(key2, new IOException("Poisoned resource requested")); Assert.IsFalse(_cache.Probe(key2)); VerifyListenerOnReadException(key2, new IOException("Poisoned resource requested")); DuplicatingCacheEventListener listener = (DuplicatingCacheEventListener)_cacheEventListener; Assert.IsTrue(listener.GetEvents("OnCleared").Count == 0); Assert.IsTrue(listener.GetEvents("OnHit").Count == 0); Assert.IsTrue(listener.GetEvents("OnMiss").Count == 0); Assert.IsTrue(listener.GetEvents("OnEviction").Count == 0); }
private void VerifyListenerOnReadException(ICacheKey key, IOException exception) { DuplicatingCacheEventListener listener = (DuplicatingCacheEventListener)_cacheEventListener; Assert.IsTrue(listener.GetEvents("OnReadException").Count != 0); SettableCacheEvent cacheEvent = (SettableCacheEvent)listener.GetLastEvent("OnReadException"); Assert.IsNotNull(cacheEvent); Assert.AreEqual(cacheEvent.CacheKey, key); Assert.IsNotNull(cacheEvent.Exception); }
private void VerifyListenerOnHit(ICacheKey key, string resourceId) { DuplicatingCacheEventListener listener = (DuplicatingCacheEventListener)_cacheEventListener; Assert.IsTrue(listener.GetEvents("OnHit").Count != 0); SettableCacheEvent cacheEvent = (SettableCacheEvent)listener.GetLastEvent("OnHit"); Assert.IsNotNull(cacheEvent); Assert.AreEqual(cacheEvent.CacheKey, key); Assert.AreEqual(cacheEvent.ResourceId, resourceId); }
private string VerifyListenerOnWriteSuccessAndGetResourceId( ICacheKey key, long itemSize) { DuplicatingCacheEventListener listener = (DuplicatingCacheEventListener)_cacheEventListener; Assert.IsTrue(listener.GetEvents("OnWriteSuccess").Count != 0); SettableCacheEvent cacheEvent = (SettableCacheEvent)listener.GetLastEvent("OnWriteSuccess"); Assert.IsNotNull(cacheEvent); Assert.AreEqual(cacheEvent.CacheKey, key); Assert.AreEqual(cacheEvent.ItemSize, itemSize); Assert.IsNotNull(cacheEvent.ResourceId); return(cacheEvent.ResourceId); }
public void TestCacheEventListener() { // 1. Add first cache file ICacheKey key1 = new SimpleCacheKey("foo"); int value1Size = 101; byte[] value1 = new byte[value1Size]; value1[80] = 99; // 'c', just so it's not all zeros for the equality test below. IBinaryResource resource1 = _cache.Insert(key1, WriterCallbacks.From(value1)); VerifyListenerOnWriteAttempt(key1); string resourceId1 = VerifyListenerOnWriteSuccessAndGetResourceId(key1, value1Size); IBinaryResource resource1Again = _cache.GetResource(key1); Assert.AreEqual(((FileBinaryResource)resource1).File.FullName, ((FileBinaryResource)resource1Again).File.FullName); VerifyListenerOnHit(key1, resourceId1); IBinaryResource resource1Again2 = _cache.GetResource(key1); Assert.AreEqual(((FileBinaryResource)resource1).File.FullName, ((FileBinaryResource)resource1Again2).File.FullName); VerifyListenerOnHit(key1, resourceId1); SimpleCacheKey missingKey = new SimpleCacheKey("nonexistent_key"); IBinaryResource res2 = _cache.GetResource(missingKey); Assert.IsNull(res2); VerifyListenerOnMiss(missingKey); DuplicatingCacheEventListener listener = (DuplicatingCacheEventListener)_cacheEventListener; listener.Clear(); _cache.ClearAll(); Assert.IsTrue(listener.GetEvents("OnCleared").Count != 0); Assert.IsTrue(listener.GetEvents("OnHit").Count == 0); Assert.IsTrue(listener.GetEvents("OnMiss").Count == 0); Assert.IsTrue(listener.GetEvents("OnWriteAttempt").Count == 0); Assert.IsTrue(listener.GetEvents("OnWriteSuccess").Count == 0); Assert.IsTrue(listener.GetEvents("OnReadException").Count == 0); Assert.IsTrue(listener.GetEvents("OnWriteException").Count == 0); Assert.IsTrue(listener.GetEvents("OnEviction").Count == 0); }
private void VerifyListenerOnEviction( string[] resourceIds, long[] itemSizes, EvictionReason reason, long cacheSizeBeforeEviction) { int numberItems = resourceIds.Length; DuplicatingCacheEventListener listener = (DuplicatingCacheEventListener)_cacheEventListener; IList <ICacheEvent> cacheEvents = listener.GetEvents("OnEviction"); Assert.IsTrue(cacheEvents.Count == numberItems); bool[] found = new bool[numberItems]; long runningCacheSize = cacheSizeBeforeEviction; // The eviction order is unknown so make allowances for them coming in different orders foreach (var cacheEvent in cacheEvents) { Assert.IsNotNull(cacheEvent); SettableCacheEvent settableCacheEvent = (SettableCacheEvent)cacheEvent; for (int i = 0; i < numberItems; i++) { if (!found[i] && resourceIds[i].Equals(settableCacheEvent.ResourceId)) { found[i] = true; Assert.IsTrue(settableCacheEvent.ItemSize != 0); Assert.IsNotNull(settableCacheEvent.EvictionReason); } } runningCacheSize -= settableCacheEvent.ItemSize; Assert.IsTrue(settableCacheEvent.CacheSize == runningCacheSize); } // Ensure all resources were found for (int i = 0; i < numberItems; i++) { Assert.IsTrue(found[i], $"Expected eviction of resource { resourceIds[i] } but wasn't evicted"); } }