Example #1
0
        public void ClearsWhenLarge()
        {
            var dict = new SimpleLruDictionary <object, object>(10, TimeSpan.FromMilliseconds(100));

            for (int i = 0; i < 25; i++)
            {
                dict.GetOrCreate(i, _ => i);
            }
            GC.Collect(2, GCCollectionMode.Forced);
            Assert.IsTrue(dict.TryGetValue(24, out _));
            Assert.IsFalse(dict.TryGetValue(1, out _));
        }
Example #2
0
        public void ClearsOnTimeout()
        {
            var dict = new SimpleLruDictionary <object, object>(10, TimeSpan.FromMilliseconds(1));

            for (int i = 0; i < 20; i++)
            {
                dict.GetOrCreate(i, _ => new object());
            }
            Thread.Sleep(30); // let's give the timer some margin so this test does not fail randomly
            GC.Collect(2, GCCollectionMode.Forced);
            Assert.IsFalse(dict.TryGetValue(1, out _));
        }
Example #3
0
        public void KeepsOldValue()
        {
            var dict = new SimpleLruDictionary <object, object>(100, TimeSpan.FromMilliseconds(100));
            var a    = dict.GetOrCreate("a", _ => 1);

            Assert.AreEqual(1, a);
            var b = dict.GetOrCreate("b", _ => 2);

            Assert.AreEqual(2, b);
            var c = dict.GetOrCreate("a", _ => "value");

            Assert.AreEqual(1, c);
        }