Ejemplo n.º 1
0
        public void LeastRecentlyUsedCacheInsertUpdatesCacheWithMostRecentInstanceOfItemIfItAlreadyExistsInCache()
        {
            const double expectedValue = 5.5;

            var cache = new LeastRecentlyUsedCache <int, TestCacheStub>(5);

            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            cache.Insert(2, new TestCacheStub {
                Id = 2, Value = 1.2
            });
            cache.Insert(3, new TestCacheStub {
                Id = 3, Value = 1.3
            });
            cache.Insert(4, new TestCacheStub {
                Id = 4, Value = 1.4
            });
            cache.Insert(5, new TestCacheStub {
                Id = 5, Value = 1.5
            });
            //should move 1 to most recently used
            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 5.5
            });

            TestCacheStub item;

            cache.TryGetItem(1, out item);

            Assert.AreEqual(expectedValue, item.Value);
        }
Ejemplo n.º 2
0
        public void LeasttRecentlyUsedCacheClearEmptiesCache()
        {
            const int expectedCount = 0;

            var cache = new LeastRecentlyUsedCache <int, TestCacheStub>(5);

            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            cache.Insert(2, new TestCacheStub {
                Id = 2, Value = 1.2
            });
            cache.Insert(3, new TestCacheStub {
                Id = 3, Value = 1.3
            });
            cache.Insert(4, new TestCacheStub {
                Id = 4, Value = 1.4
            });
            cache.Insert(5, new TestCacheStub {
                Id = 5, Value = 1.5
            });

            cache.Clear();

            Assert.AreEqual(expectedCount, cache.Count);
        }
Ejemplo n.º 3
0
        public void LeastRecentlyUsedCacheRemovesLeastRecentlyUsedItemWhenCapacityReached()
        {
            var cache = new LeastRecentlyUsedCache <int, TestCacheStub>(5);

            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            cache.Insert(2, new TestCacheStub {
                Id = 2, Value = 1.2
            });
            cache.Insert(3, new TestCacheStub {
                Id = 3, Value = 1.3
            });
            cache.Insert(4, new TestCacheStub {
                Id = 4, Value = 1.4
            });
            cache.Insert(5, new TestCacheStub {
                Id = 5, Value = 1.5
            });
            cache.Insert(6, new TestCacheStub {
                Id = 6, Value = 1.6
            });

            TestCacheStub value;
            bool          result = cache.TryGetItem(1, out value);

            Assert.IsFalse(result);
        }
Ejemplo n.º 4
0
        public void LeastRecentlyUsedCacheInsertMovesItemToMostRecentlyUsedIfItAlreadyExistsInCache()
        {
            var cache = new LeastRecentlyUsedCache <int, TestCacheStub>(5);

            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            cache.Insert(2, new TestCacheStub {
                Id = 2, Value = 1.2
            });
            cache.Insert(3, new TestCacheStub {
                Id = 3, Value = 1.3
            });
            cache.Insert(4, new TestCacheStub {
                Id = 4, Value = 1.4
            });
            cache.Insert(5, new TestCacheStub {
                Id = 5, Value = 1.5
            });
            //should move 1 to most recently used
            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            //should force removal of LRU
            cache.Insert(6, new TestCacheStub {
                Id = 6, Value = 1.6
            });

            TestCacheStub value;
            bool          result1 = cache.TryGetItem(1, out value);
            bool          result2 = cache.TryGetItem(2, out value);

            Assert.IsTrue(result1);
            Assert.IsFalse(result2);
        }
Ejemplo n.º 5
0
        public void LeastRecentlyUsedCacheSizeDoesNotExceedCapacity()
        {
            const int expectedCount = 5;

            var cache = new LeastRecentlyUsedCache <int, TestCacheStub>(5);

            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            cache.Insert(2, new TestCacheStub {
                Id = 2, Value = 1.2
            });
            cache.Insert(3, new TestCacheStub {
                Id = 3, Value = 1.3
            });
            cache.Insert(4, new TestCacheStub {
                Id = 4, Value = 1.4
            });
            cache.Insert(5, new TestCacheStub {
                Id = 5, Value = 1.5
            });
            cache.Insert(6, new TestCacheStub {
                Id = 6, Value = 1.6
            });

            Assert.AreEqual(expectedCount, cache.Count);
        }
        public RestfulClientFactory()
        {
            LeastRecentlyUsedCacheSettings settings = new LeastRecentlyUsedCacheSettings();

            settings.Staleness        = TimeSpan.FromHours(1);
            m_oAuthEvoHttpClientCache = new LeastRecentlyUsedCache <OAuthTokenIdentifier, IRestfulClient>(settings);
        }
Ejemplo n.º 7
0
        public void LeastRecentlyUsedCacheTryGetItemThrowsIfKeyIsNull()
        {
            var cache = new LeastRecentlyUsedCache <int?, TestCacheStub>(5);

            TestCacheStub cacheItem;

            Assert.Throws <ArgumentNullException>(() => cache.TryGetItem(null, out cacheItem));
        }
Ejemplo n.º 8
0
        public void LeastRecentlyUsedCacheInsertThrowsIfKeyIsNull()
        {
            var cache     = new LeastRecentlyUsedCache <int?, TestCacheStub>(5);
            var cacheItem = new TestCacheStub {
                Id = 1, Value = 1.1
            };

            Assert.Throws <ArgumentNullException>(() => cache.Insert(null, cacheItem));
        }
        public void WhenItemExists_ThenRemoveSucceeds()
        {
            var cache = new LeastRecentlyUsedCache <string, string>(2);

            cache.Add("one", "ONE");
            cache.Remove("one");
            cache.Remove("doesnotexist");

            Assert.IsNull(cache.Lookup("one"));
        }
        public void WhenLookupCachedItem_ThenItemIsReturned()
        {
            var cache = new LeastRecentlyUsedCache <string, string>(2);

            cache.Add("one", "ONE");
            cache.Add("two", "TWO");

            Assert.AreEqual("ONE", cache.Lookup("one"));
            Assert.AreEqual("TWO", cache.Lookup("two"));
        }
        private void AssertContentsPerStep<K, V>(LeastRecentlyUsedCache<K, V> sut, Func<K, V> valueCreator, K[] keys, K[][] stepValues)
        {
            stepValues.Length.Should().Be(keys.Length, "one result is needed per step key");

            for(int i=0;i<keys.Length;++i)
            {
                sut.GetOrAdd(keys[i], valueCreator);

                AssertContents(sut, valueCreator, stepValues[i]);
            }
        }
Ejemplo n.º 12
0
        public void AddEntryShouldReturnEntryUsingGetEntry(int key, string actual, LeastRecentlyUsedCache <int, string> sut)
        {
            // Arrange

            // Act
            sut.AddEntry(key, actual);
            var expected = sut.GetEntry(key);

            // Assert
            actual.ShouldBe(expected);
        }
        public void WhenItemAddedTwice_ThenSecondCallIsIgnored()
        {
            var cache = new LeastRecentlyUsedCache <string, string>(2);

            cache.Add("one", "ONE");
            cache.Add("one", "ONE");
            cache.Add("two", "TWO");

            Assert.AreEqual("ONE", cache.Lookup("one"));
            Assert.AreEqual("TWO", cache.Lookup("two"));
        }
Ejemplo n.º 14
0
        public void CtorShouldSetCapacitySpecifiedIfValid(int capacity)
        {
            // Arrange

            // Act
            var sut    = new LeastRecentlyUsedCache <int, string>(capacity);
            var actual = sut.Capacity;

            // Assert
            actual.ShouldBe(capacity);
        }
        public void WhenAddingItemsBeyondCapacity_ThenLeastReentlyUsedItemIsPurged()
        {
            var cache = new LeastRecentlyUsedCache <string, string>(2);

            cache.Add("one", "ONE");
            cache.Add("two", "TWO");
            cache.Add("three", "THREE");

            Assert.IsNull(cache.Lookup("one"));
            Assert.AreEqual("TWO", cache.Lookup("two"));
            Assert.AreEqual("THREE", cache.Lookup("three"));
        }
Ejemplo n.º 16
0
        public void RemoveEntryShouldRemove(int key, string entry, LeastRecentlyUsedCache <int, string> sut)
        {
            // Arrange
            sut.AddEntry(key, entry);

            // Act
            sut.RemoveEntry(key);
            var actual = sut.GetEntry(key);

            // Assert
            actual.ShouldBeNull();
        }
Ejemplo n.º 17
0
        public void CtorShouldUseDefaultMinCapacityIfSpecifiedCapacityIsLessThanDefault()
        {
            // Arrange
            var expected = 10;

            // Act
            var sut    = new LeastRecentlyUsedCache <int, string>(5);
            var actual = sut.Capacity;

            // Assert
            actual.ShouldBe(expected);
        }
        private void AssertContents<K, V>(LeastRecentlyUsedCache<K, V> sut, Func<K, V> valueCreator, K[] k)
        {
            int index = 0;
            foreach(var kvp in sut)
            {
                var expectedKey = k[index];
                var expectedValue = valueCreator(expectedKey);

                kvp.Should().BeEquivalentTo(new KeyValuePair<K, V>(expectedKey, expectedValue));
                index++;
            }
        }
        public void WhenLookingUpItem_ThenItemIsMarkedAsUsed()
        {
            var cache = new LeastRecentlyUsedCache <string, string>(2);

            cache.Add("one", "ONE");
            cache.Add("two", "TWO");
            cache.Lookup("one");
            cache.Add("three", "THREE");

            Assert.IsNull(cache.Lookup("two"));
            Assert.AreEqual("ONE", cache.Lookup("one"));
            Assert.AreEqual("THREE", cache.Lookup("three"));
        }
        public void Get_ReturnsCachedItem()
        {
            // Arrange
            var cache = new LeastRecentlyUsedCache <string>(1);

            cache.Set(KeyOne, ValueOne);

            // Act
            var result = cache.Get(KeyOne);

            // Assert
            Assert.AreEqual(ValueOne, result);
        }
Ejemplo n.º 21
0
        public void LeastRecentlyUsedCacheInsertsItemIntoCache()
        {
            const int expectedCount = 1;

            var cache     = new LeastRecentlyUsedCache <int, TestCacheStub>(5);
            var cacheItem = new TestCacheStub {
                Id = 1, Value = 1.1
            };

            cache.Insert(cacheItem.Id, cacheItem);

            Assert.AreEqual(expectedCount, cache.Count);
        }
        public void Set_EvictsOldestElement()
        {
            // Arrange
            var cache = new LeastRecentlyUsedCache <string>(1);

            cache.Set(KeyOne, ValueOne);
            cache.Set(KeyTwo, ValueTwo);

            // Act
            var result = cache.Get(KeyOne);

            // Assert
            Assert.IsNull(result);
        }
Ejemplo n.º 23
0
        public void LeastRecentlyUsedCacheTryGetItemReturnsFalseIfItemIsInCache()
        {
            var cache     = new LeastRecentlyUsedCache <int, TestCacheStub>(5);
            var cacheItem = new TestCacheStub {
                Id = 1, Value = 1.1
            };

            cache.Insert(cacheItem.Id, cacheItem);

            TestCacheStub value;
            bool          result = cache.TryGetItem(2, out value);

            Assert.IsFalse(result);
        }
Ejemplo n.º 24
0
        public void ClearShouldRemoveAllData(int[] keys, string[] entries, LeastRecentlyUsedCache <int, string> sut)
        {
            // Arrange
            for (int i = 0; i < keys.Length; i++)
            {
                sut.AddEntry(keys[i], entries[i]);
            }

            // Act
            sut.Clear();
            var actual = sut.Count;

            // Assert
            actual.ShouldBe(0);
        }
        public void Evicts_if_maximum_capacity_reached()
        {
            // Arrange
            const int capacity = 1000;
            var       leastRecentlyUsedCache = new LeastRecentlyUsedCache <string, string>(capacity);

            // Act
            for (var i = 0; i < capacity * 2; ++i)
            {
                var entry = $"entry_{i}";
                leastRecentlyUsedCache.AddOrReplace(entry, entry);
            }

            // Assert
            leastRecentlyUsedCache.Count.Should().Be(capacity);
        }
        public void Get_MovesElementToFrontOfListForEviction()
        {
            // Arrange
            var cache = new LeastRecentlyUsedCache <string>(2);

            cache.Set(KeyOne, ValueOne);
            cache.Set(KeyTwo, ValueTwo);
            cache.Get(KeyOne);
            cache.Set(KeyThree, ValueThree);

            // Act
            var result = cache.Get(KeyOne);

            // Assert
            Assert.AreEqual(ValueOne, result);
        }
Ejemplo n.º 27
0
        public void LeastRecentlyUsedCacheDoesNotInsertDuplicateKeysWhenMultiThreaded()
        {
            const int expectedCount = 1;

            var cache = new LeastRecentlyUsedCache <int, TestCacheStub>(5);

            var options = new ParallelOptions
            {
                MaxDegreeOfParallelism = 8
            };

            Parallel.For(0, 100, options, i =>
            {
                cache.Insert(1, new TestCacheStub {
                    Id = i, Value = i * 1.1
                });
            });
            Assert.AreEqual(expectedCount, cache.Count);
        }
Ejemplo n.º 28
0
        public void GetMostRecentlyUsedEntryReturnsValidEntry(Fixture fixture, LeastRecentlyUsedCache <int, string> sut)
        {
            // Arrange
            for (int i = 0; i < sut.Capacity; i++)
            {
                var key = fixture.Create <int>();
                sut.AddEntry(key, fixture.Create <string>());
            }

            var currentLru = sut.GetLeastRecentlyUsedEntry();

            // Act
            sut.AddEntry(currentLru.Key, currentLru.Entry);
            var actualMru = sut.GetMostRecentlyUsedEntry();
            var actualLru = sut.GetLeastRecentlyUsedEntry();

            // Assert
            actualMru.ShouldBe(currentLru);
            actualLru.ShouldNotBe(currentLru);
        }
Ejemplo n.º 29
0
        public void AddingNewItemGivenCacheFullShouldRemoveLru(Fixture fixture, LeastRecentlyUsedCache <int, string> sut)
        {
            // Arrange
            for (int i = 0; i < sut.Capacity; i++)
            {
                var key = fixture.Create <int>();
                sut.AddEntry(key, fixture.Create <string>());
            }

            var currentLru = sut.GetLeastRecentlyUsedEntry();
            var newKey     = fixture.Create <int>();
            var newValue   = fixture.Create <string>();

            // Act
            sut.AddEntry(newKey, newValue);
            var actual = sut.ContainsKey(currentLru.Key);

            // Assert
            actual.ShouldBeFalse();
        }
        public void Test1(string[] keys)
        {
            var sut = new LeastRecentlyUsedCache<string, int>(5);

            AssertContentsPerStep(sut, k => k.Length, keys, new[] {
                new [] { "a" },
                new [] { "a", "bc" },
                new [] { "a", "bc", "def" },
                new [] { "bc", "def", "a" },
                new [] { "bc", "def", "a", "ghij" },
                new [] { "bc", "def", "ghij", "a" },
                new [] { "bc", "def", "ghij", "a", "klmno" },
                new [] { "def", "ghij", "a", "klmno", "bc" },
                new [] { "def", "ghij", "klmno", "bc", "a" },
                new [] { "ghij", "klmno", "bc", "a", "p" },
                new [] { "klmno", "bc", "a", "p", "qr" },
                new [] { "klmno", "a", "p", "qr", "bc" },
                new [] { "klmno", "a", "p", "qr", "bc" },
                }
            );
        }