Example #1
0
        public void RemoveExpiredValues()
        {
            int size              = 10;
            int expiredInSeconds  = 1;
            int waitInMiliSeconds = 2 * 1000 * expiredInSeconds;

            TestUtilities.WriteHeader($"{this}.RemoveExpiredValues");
            var context = new CompareContext($"{this}.RemoveExpiredValues");
            var cache   = new EventBasedLRUCache <int, string>(11, removeExpiredValues: true, maintainLRU: false);

            AddItemsToCache(cache, size, expiredInSeconds);

            cache.WaitForProcessing();
            Thread.Sleep(waitInMiliSeconds);
            cache.RemoveExpiredValues();

            AssertCache(cache, size, context);
        }
        public void RemoveExpiredValues()
        {
            TestUtilities.WriteHeader($"{this}.RemoveExpiredValues");
            var context = new CompareContext($"{this}.RemoveExpiredValues");

            using (var cache = new EventBasedLRUCache <int, string>(11, TaskCreationOptions.LongRunning, tryTakeTimeout: 50, removeExpiredValues: true))
            {
                for (int i = 0; i <= 10; i++)
                {
                    // Only even values should expire.
                    if (i % 2 == 0)
                    {
                        cache.SetValue(i, i.ToString(), DateTime.UtcNow + TimeSpan.FromSeconds(5));
                    }
                    else
                    {
                        cache.SetValue(i, i.ToString());
                    }
                }

                Thread.Sleep(5000);
                cache.RemoveExpiredValues();

                for (int i = 0; i <= 10; i++)
                {
                    // Only even values should expire.
                    if (i % 2 == 0)
                    {
                        if (cache.Contains(i))
                        {
                            context.AddDiff("The key value pair {" + i + ", '" + i.ToString() + "'} should have expired and been removed, but the Contains() method returned true.");
                        }
                    }
                    else
                    {
                        if (!cache.Contains(i))
                        {
                            context.AddDiff("The key value pair {" + i + ", '" + i.ToString() + "'} should remain in the cache, but the Contains() method returned false.");
                        }
                    }
                }
                TestUtilities.AssertFailIfErrors(context);
            }
        }