Esempio n. 1
0
        public async Task LruRemoveExpired()
        {
            const int n       = 10;
            const int maxSize = n * 2;
            var       maxAge  = TimeSpan.FromMilliseconds(500);

            LRU <string, string> .FetchValueDelegate f = null;
            var flushCounter = 0;

            var target = new LRU <string, string>(maxSize, maxAge, f);

            target.RaiseFlushEvent += (object o, LRU <string, string> .FlushEventArgs args) => flushCounter++;

            for (int i = 0; i < n; i++)
            {
                var s = i.ToString();
                target.Add(s, $"item {s}");
            }

            target.RemoveExpired();
            Assert.Equal(0, flushCounter);
            Assert.Equal(n, target.Count);

            await Task.Delay(maxAge.Add(maxAge));

            target.Add("expected", "value");
            target.RemoveExpired();

            Assert.Equal(n, flushCounter);
            Assert.Equal(1, target.Count);
            Assert.True(target.TryGetValue("expected", out var value));
            Assert.Equal("value", value);
        }
Esempio n. 2
0
        public void overload_shoul_keep_same_lenght()
        {
            LRU sut = new LRU(1);

            sut.Add(5);
            sut.Add(6);

            Assert.That(sut.Elements.Count, Is.EqualTo(1));
        }
Esempio n. 3
0
        public void every_element_should_be_first_item()
        {
            LRU sut = new LRU();

            sut.Add(5);
            sut.Add(6);

            Assert.That(sut.Elements.First(), Is.EqualTo(6));
        }
Esempio n. 4
0
        public void Add_ExistValut_Should_Move_To_Top()
        {
            LRU sut = new LRU(4);

            sut.Add(5);
            sut.Add(4);
            sut.Add(3);
            sut.Add(5);

            Assert.That(sut.Elements.Last, Is.Not.EqualTo(5));
        }
Esempio n. 5
0
        public void overload_should_no_keep_last_element()
        {
            LRU sut = new LRU(1);

            sut.Add(5);
            sut.Add(6);
            List <object> expected = new List <object>()
            {
                6
            };

            CollectionAssert.AreEqual(sut.Elements, expected);
        }
Esempio n. 6
0
        public void LruCountTest()
        {
            const int maxSize = 10;
            var       maxAge  = new TimeSpan(0, 1, 0, 0);

            var target = new LRU <string, string>(maxSize, maxAge);

            Assert.Equal(0, target.Count);  // "Count wrong after construction"

            target.Add("1", "one");
            Assert.Equal(1, target.Count);  // "Count wrong after adding one item"

            target.Add("2", "two");
            Assert.Equal(2, target.Count);  // "Count wrong after adding two items"
        }
Esempio n. 7
0
        public void LruCountTest()
        {
            const int maxSize = 10;
            var maxAge = new TimeSpan(0, 1, 0, 0);
            LRU<string, string>.FetchValueDelegate f = null;

            var target = new LRU<string, string>(maxSize, maxAge, f);
            Assert.Equal(0, target.Count);  // "Count wrong after construction"

            target.Add("1", "one");
            Assert.Equal(1, target.Count);  // "Count wrong after adding one item"
            
            target.Add("2", "two");
            Assert.Equal(2, target.Count);  // "Count wrong after adding two items"
        }
        public void AddOrUpdate(ActivationAddress value, int version)
        {
            var entry = new GrainDirectoryCacheEntry(value, version, initialExpirationTimer);

            // Notice that LRU should know how to throw the oldest entry if the cache is full
            cache.Add(value.Grain, entry);
        }
Esempio n. 9
0
        public void AddOrUpdate(GrainId key, IReadOnlyList <Tuple <SiloAddress, ActivationId> > value, int version)
        {
            var entry = new GrainDirectoryCacheEntry(value, version, DateTime.UtcNow, initialExpirationTimer);

            // Notice that LRU should know how to throw the oldest entry if the cache is full
            cache.Add(key, entry);
        }
        public void AddOrUpdate(GrainId key, TValue value, int version)
        {
            var entry = new GrainDirectoryCacheEntry(value, version, DateTime.UtcNow, initialExpirationTimer);

            // Notice that LRU should know how to throw the oldest entry if the cache is full
            cache.Add(key, entry);
        }
Esempio n. 11
0
        public void add_element_should_contains_in_list()
        {
            LRU sut = new LRU();

            sut.Add(5);

            Assert.That(sut.Elements.First(), Is.EqualTo(5));
        }
Esempio n. 12
0
        public void LruCountTest()
        {
            const int maxSize = 10;
            var       maxAge  = new TimeSpan(0, 1, 0, 0);

            LRU <string, string> .FetchValueDelegate f = null;

            var target = new LRU <string, string>(maxSize, maxAge, f);

            Assert.AreEqual(0, target.Count, "Count wrong after construction");

            target.Add("1", "one");
            Assert.AreEqual(1, target.Count, "Count wrong after adding one item");

            target.Add("2", "two");
            Assert.AreEqual(2, target.Count, "Count wrong after adding two items");
        }
Esempio n. 13
0
        public void LruUsageTest()
        {
            const int maxSize = 10;
            var       maxAge  = new TimeSpan(0, 1, 0, 0);

            LRU <string, string> .FetchValueDelegate f = null;

            var target = new LRU <string, string>(maxSize, maxAge, f);

            // Fill the LRU with "1" through "10"
            for (var i = 1; i <= maxSize; i++)
            {
                var s = i.ToString();
                target.Add(s, "item " + s);
                Thread.Sleep(10);
            }

            // Use "10", then "9", etc.
            for (var i = maxSize; i >= 1; i--)
            {
                var    s = i.ToString();
                string val;
                target.TryGetValue(s, out val);
            }

            // Add a new item to push the least recently used out -- which should be item "10"
            var s1 = (maxSize + 1).ToString();

            target.Add(s1, "item " + s1);

            Assert.Equal(maxSize, target.Count);  // "Cache has exceeded maximum size"
            var s0 = maxSize.ToString();

            Assert.False(target.ContainsKey(s0), "Least recently used item was not expelled");
            for (var i = 1; i < maxSize; i++)
            {
                var s = i.ToString();
                Assert.True(target.ContainsKey(s), "Recently used item " + s + " was incorrectly expelled");
            }
        }
Esempio n. 14
0
        public void LruUsageTest()
        {
            const int maxSize = 10;
            var maxAge = new TimeSpan(0, 1, 0, 0);
            LRU<string, string>.FetchValueDelegate f = null;

            var target = new LRU<string, string>(maxSize, maxAge, f);

            // Fill the LRU with "1" through "10"
            for (var i = 1; i <= maxSize; i++)
            {
                var s = i.ToString();
                target.Add(s, "item " + s);
                Thread.Sleep(10);
            }

            // Use "10", then "9", etc.
            for (var i = maxSize; i >= 1; i--)
            {
                var s = i.ToString();
                string val;
                target.TryGetValue(s, out val);
            }
            
            // Add a new item to push the least recently used out -- which should be item "10"
            var s1 = (maxSize + 1).ToString();
            target.Add(s1, "item " + s1);

            Assert.Equal(maxSize, target.Count);  // "Cache has exceeded maximum size"
            var s0 = maxSize.ToString();
            Assert.False(target.ContainsKey(s0), "Least recently used item was not expelled");
            for (var i = 1; i < maxSize; i++)
            {
                var s = i.ToString();
                Assert.True(target.ContainsKey(s), "Recently used item " + s + " was incorrectly expelled");
            }
        }
Esempio n. 15
0
        public void LruMaximumSizeTest()
        {
            const int maxSize = 10;
            var maxAge = new TimeSpan(0, 1, 0, 0);
            LRU<string, string>.FetchValueDelegate f = null;

            var target = new LRU<string, string>(maxSize, maxAge, f);
            for (var i = 1; i <= maxSize + 5; i++)
            {
                var s = i.ToString();
                target.Add(s, "item " + s);
                Thread.Sleep(10);                
            }

            Assert.Equal(maxSize, target.Count);  // "LRU grew larger than maximum size"
            for (var i = 1; i <= 5; i++)
            {
                var s = i.ToString();
                Assert.False(target.ContainsKey(s), "'Older' entry is still in cache");
            }
        }
Esempio n. 16
0
        public void LruMaximumSizeTest()
        {
            const int maxSize = 10;
            var       maxAge  = new TimeSpan(0, 1, 0, 0);

            var target = new LRU <string, string>(maxSize, maxAge);

            for (var i = 1; i <= maxSize + 5; i++)
            {
                var s = i.ToString();
                target.Add(s, "item " + s);
                Thread.Sleep(10);
            }

            Assert.Equal(maxSize, target.Count);  // "LRU grew larger than maximum size"
            for (var i = 1; i <= 5; i++)
            {
                var s = i.ToString();
                Assert.False(target.ContainsKey(s), "'Older' entry is still in cache");
            }
        }
Esempio n. 17
0
        public void Add_null_Should_Throw_ArgumentNullException()
        {
            LRU sut = new LRU();

            Assert.Throws <ArgumentNullException>(() => sut.Add(null));
        }
Esempio n. 18
0
 public void AddOrUpdate(GrainId key, TValue value, int version)
 {
     // ignore the version number
     cache.Add(key, value);
 }
Esempio n. 19
0
 public void AddOrUpdate(GrainId key, IReadOnlyList <Tuple <SiloAddress, ActivationId> > value, int version)
 {
     // ignore the version number
     cache.Add(key, value);
 }