コード例 #1
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);
     }
 }
コード例 #2
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);
     }
 }
コード例 #3
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);
            }
        }
コード例 #4
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);
            }
        }