Example #1
0
 public void TryRemove_not_existing_returns_false()
 {
     using (var timedDictionary = new TimedDictionary <string, object>())
     {
         Assert.IsFalse(timedDictionary.TryRemove("test"));
     }
 }
Example #2
0
 public void Constructor_DefaultExpiry_ConcurrencyLevel_Capacity()
 {
     using (var timedDictionary = new TimedDictionary <object, object>(TimeSpan.FromSeconds(123), 5, 6))
     {
         Assert.AreEqual(TimeSpan.FromSeconds(123), timedDictionary.DefaultExpiry);
     }
 }
Example #3
0
 public void Constructor_DefaultExpiry()
 {
     using (var timedDictionary = new TimedDictionary <object, object>(TimeSpan.FromSeconds(123)))
     {
         Assert.AreEqual(TimeSpan.FromSeconds(123), timedDictionary.DefaultExpiry);
     }
 }
Example #4
0
        public void Values_enumeration()
        {
            var timedDictionary = new TimedDictionary <string, object> {
                { "key", "value" }
            };

            Assert.That(timedDictionary.Values, Is.EquivalentTo(new [] { "value" }));
        }
Example #5
0
 public void CleanUp_HasOneExpiredItem_CountIsOne()
 {
     using (var timedDictionary = new TimedDictionary <int, int>(TimeSpan.MaxValue))
     {
         timedDictionary.TryAdd(1, 1);
         timedDictionary.CleanUp();
         Assert.AreEqual(1, timedDictionary.Count);
     }
 }
Example #6
0
 public void CleanUp_HasOneExpiredItem_CountIsZero()
 {
     using (var timedDictionary = new TimedDictionary <int, int>(TimeSpan.FromMilliseconds(0)))
     {
         timedDictionary.TryAdd(1, 1);
         Thread.Sleep(1);
         timedDictionary.CleanUp();
         Assert.AreEqual(0, timedDictionary.Count);
     }
 }
Example #7
0
        public void GetOrAdd_value_called_first_creates_value()
        {
            using (var timedDictionary = new TimedDictionary <string, object>())
            {
                var value = timedDictionary.GetOrAdd("test", "value");

                Assert.AreEqual("value", value);
                Assert.AreEqual("value", timedDictionary["test"]);
            }
        }
Example #8
0
        public void Indexer_TryGetValue()
        {
            using (var timedDictionary = new TimedDictionary <string, object>())
            {
                timedDictionary["test"] = 1;

                object value;
                Assert.IsTrue(timedDictionary.TryGetValue("test", out value));
            }
        }
Example #9
0
        public void AddOrUpdate_value_called_first_creates_value()
        {
            using (var timedDictionary = new TimedDictionary <string, object>())
            {
                var value = timedDictionary.AddOrUpdate("test", "value", (k, v) => "value2");

                Assert.AreEqual("value", value);
                Assert.AreEqual("value", timedDictionary["test"]);
            }
        }
Example #10
0
        public void Constructor_DefaultExpiry_CleanUpPeriod()
        {
            var defaultExpiry = TimeSpan.FromSeconds(123);
            var cleanUpPeriod = TimeSpan.FromSeconds(234);

            using (var timedDictionary = new TimedDictionary <object, object>(defaultExpiry, cleanUpPeriod))
            {
                Assert.AreEqual(defaultExpiry, timedDictionary.DefaultExpiry);
                Assert.AreEqual(cleanUpPeriod, timedDictionary.CleanUpPeriod);
            }
        }
Example #11
0
        public void Indexer_TryGetValue_StringComparer_InvariantCultureIgnoreCase()
        {
            using (var timedDictionary = new TimedDictionary <string, object>(1, 1, StringComparer.InvariantCultureIgnoreCase))
            {
                timedDictionary["Test"] = 1;

                object value;
                Assert.IsTrue(timedDictionary.TryGetValue("test", out value));
                Assert.AreEqual(1, timedDictionary["test"]);
            }
        }
Example #12
0
        public void DefaultExpiry_Indexer_StringComparer_InvariantCultureIgnoreCase()
        {
            var defaultExpiry = TimeSpan.FromSeconds(123);

            using (var timedDictionary = new TimedDictionary <string, object>(defaultExpiry, 1, 1, StringComparer.InvariantCultureIgnoreCase))
            {
                timedDictionary["Test"] = 1;

                Assert.AreEqual(defaultExpiry, timedDictionary.DefaultExpiry);
                Assert.AreEqual(1, timedDictionary["test"]);
            }
        }
Example #13
0
        public void AddOrUpdate_value_UpdateAccessTime_false()
        {
            using (var timedDictionary = new TimedDictionary <string, object>())
            {
                timedDictionary.AddOrUpdate("test", "value", (k, v) => v);

                TimedValue <object> timedValue;
                Assert.IsTrue(timedDictionary.TryGetValue("test", out timedValue));

                var lastAccessed = timedValue.LastAccessUtc;
                Thread.Sleep(15);

                timedDictionary.AddOrUpdate("test", "", (k, v) => v, false);
                Assert.AreEqual(timedValue.LastAccessUtc, lastAccessed);
            }
        }
Example #14
0
        public void GetOrAdd_value_UpdateAccessTime_true()
        {
            using (var timedDictionary = new TimedDictionary <string, object>())
            {
                timedDictionary.GetOrAdd("test", "value");

                TimedValue <object> timedValue;
                Assert.IsTrue(timedDictionary.TryGetValue("test", out timedValue));

                var lastAccessed = timedValue.LastAccessUtc;
                Thread.Sleep(15);

                timedDictionary.GetOrAdd("test", "");
                Assert.Greater(timedValue.LastAccessUtc, lastAccessed);
            }
        }
Example #15
0
        public void GetOrAdd_factory_UpdateAccessTime_false()
        {
            using (var timedDictionary = new TimedDictionary <string, object>())
            {
                timedDictionary.GetOrAdd("test", k => "value");

                TimedValue <object> timedValue;
                Assert.IsTrue(timedDictionary.TryGetValue("test", out timedValue));

                var lastAccessed = timedValue.LastAccessUtc;
                Thread.Sleep(15);

                timedDictionary.GetOrAdd("test", k => "value", false);
                Assert.AreEqual(timedValue.LastAccessUtc, lastAccessed);
            }
        }
Example #16
0
        public void CleanUp_HasOneExpiredItemWaitForAutomaticCleanUp_CountIsZero()
        {
            using (var timedDictionary = new TimedDictionary <int, int>(TimeSpan.FromMilliseconds(0)))
            {
                timedDictionary.TryAdd(1, 1);

                bool cancelled = false;
                using (var task = Task.Factory.StartNew(() => { while (timedDictionary.Count > 0 && !cancelled)
                                                                {
                                                                    Thread.Sleep(0);
                                                                }
                                                        }, TaskCreationOptions.LongRunning))
                {
                    task.Wait(1100);
                    cancelled = true;
                    task.Wait();
                }

                Assert.AreEqual(0, timedDictionary.Count);
            }
        }
Example #17
0
        public void ConcurrencyTest()
        {
            var exceptions = new List <Exception>();

            TimedDictionaryWorker.OnCleanUpException += (td, ex) => exceptions.Add(ex);

            var sw = Stopwatch.StartNew();

            using (var timedDictionary = new TimedDictionary <int, byte>(TimeSpan.Zero))
            {
                var added = 0;
                while (sw.ElapsedMilliseconds < 2000)
                {
                    if (timedDictionary.TryAdd(added, 0))
                    {
                        added++;
                    }
                }
                Assert.Less(timedDictionary.Count, added);
                Assert.AreEqual(0, exceptions.Count);
            }
        }