Ejemplo n.º 1
0
        public void LruCacheStorage_Dispose()
        {
            var storage = new LruCacheStorage <int>(1);
            var entry   = storage.GetEntry(42);

            storage.ReleaseEntry(entry);
            storage.Dispose();
            Assert.ThrowsException <ObjectDisposedException>(() => storage.GetEntry(42));
            storage.Dispose();
        }
Ejemplo n.º 2
0
        public void LruCacheStorage_Count()
        {
            var cacheStorage = new LruCacheStorage <string>(1);

            Assert.AreEqual(0, cacheStorage.Count);

            var e1 = cacheStorage.GetEntry("foo");
            var e2 = cacheStorage.GetEntry("bar");

            Assert.AreEqual(1, cacheStorage.Count);

            cacheStorage.ReleaseEntry(e1);
            Assert.AreEqual(1, cacheStorage.Count);

            cacheStorage.ReleaseEntry(e2);
            Assert.AreEqual(0, cacheStorage.Count);
        }
Ejemplo n.º 3
0
        public void LruCacheStorage_ArgumentChecks()
        {
            var cacheStorage = new LruCacheStorage <string>(1);

            AssertEx.ThrowsException <ArgumentOutOfRangeException>(() => new LruCacheStorage <string>(-1), ex => Assert.AreEqual("size", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => new LruCacheStorage <string>(1, comparer: null), ex => Assert.AreEqual("comparer", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cacheStorage.GetEntry(value: null), ex => Assert.AreEqual("value", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cacheStorage.ReleaseEntry(entry: null), ex => Assert.AreEqual("entry", ex.ParamName));
            AssertEx.ThrowsException <ArgumentException>(() => cacheStorage.ReleaseEntry(new NullEntry()), ex => Assert.AreEqual("entry", ex.ParamName));
        }
Ejemplo n.º 4
0
        public void LruCacheStorage_ReleaseAfterEviction()
        {
            var storage = new LruCacheStorage <string>(1);
            var e1      = storage.GetEntry("foo"); // "foo" is in cache
            var e2      = storage.GetEntry("bar"); // "bar" is in cache; "foo" is evicted
            var e3      = storage.GetEntry("foo"); // "foo" is in cache; "bar" is evicted
            var e4      = storage.GetEntry("foo"); // "foo" cache hit

            Assert.AreNotSame(e1, e3);
            Assert.AreSame(e3, e4);

            storage.ReleaseEntry(e1); // first "foo" is released, no-op (not in cache)
            storage.ReleaseEntry(e2); // "bar" is released, no-op
            storage.ReleaseEntry(e3); // "foo" is released, decremented

            var e5 = storage.GetEntry("foo");

            Assert.AreSame(e4, e5); // "foo" cache hit
            storage.ReleaseEntry(e4);
            storage.ReleaseEntry(e5);
        }