Exemplo n.º 1
0
        private WeakDictionary <object, int> CreateDict(out List <KeyObject> keys)
        {
            WeakDictionary <object, int> weakDict = new WeakDictionary <object, int>(this.comparer, refreshInterval: 5);

            keys = new List <KeyObject>();
            for (int i = 0; i < 10; i++)
            {
                KeyObject key = new KeyObject(i);
                keys.Add(key);
                weakDict.Add(key, i);
            }
            return(weakDict);
        }
Exemplo n.º 2
0
        private object CreateObj(KeyObject obj, RefType valueType)
        {
            switch (valueType)
            {
            case RefType.StrongRef:
                return(obj);

            case RefType.AliveWeak:
                return(new WeakKeyReference <KeyObject>(obj, this.comparer));

            default:
                return(new WeakKeyReference <KeyObject>(this.CreateObj(obj.ID), this.comparer));
            }
        }
Exemplo n.º 3
0
        public void TestRemoveDeadItemWhenAddMoreThanCurrentRefreshIntervalItems()
        {
            WeakDictionary <KeyObject, int> weakDict = new WeakDictionary <KeyObject, int>(this.comparer, 5);

            for (int i = 0; i < 4; i++)
            {
                AddAnItem(weakDict, i);
            }

            // Force gc to collect dead items
            GC.Collect();
            if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
            {
                var key1 = new KeyObject(11);
                weakDict.Add(key1, 11);

                var key2 = new KeyObject(12);

                // Weak dictionary will remove dead items during this adding operation.
                // Then the currentRefreshInterval will be 1(still alive)+5=6.
                weakDict.Add(key2, 12);

                Assert.Equal(2, weakDict.Count);

                for (int i = 0; i < 3; i++)
                {
                    AddAnItem(weakDict, i);
                }

                // Force gc to collect dead items
                GC.Collect();
                if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
                {
                    // Add the six item to dictionary.
                    var key6 = new KeyObject(16);

                    // Weak dictionary will not clean dead entities.
                    weakDict.Add(key6, 16);
                    Assert.Equal(6, weakDict.Count);

                    var key14 = new KeyObject(114);

                    // Weak dictionary will remove dead items during this adding operation.
                    // Then the currentRefreshInterval will be 3(still alive)+5=8.
                    weakDict.Add(key14, 114);
                    Assert.Equal(4, weakDict.Count);
                }
            }
        }
Exemplo n.º 4
0
        public void TestWeakKeyComparerForSameRef()
        {
            var array = Enum.GetValues(typeof(RefType));

            foreach (RefType refType in array)
            {
                KeyObject keyObj = new KeyObject(1);
                var obj = CreateObj(keyObj, refType);
                GC.Collect();
                if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
                {
                    Assert.IsTrue(this.comparer.Equals(obj, obj));
                }
            }
        }
Exemplo n.º 5
0
        // Net Core 1.0 is missing GC APIs
        private void TestWeakKeyComparerForRefImplement(RefType leftRefType, RefType rightRefType)
        {
            KeyObject obj = new KeyObject(1);
            var left = CreateObj(obj, leftRefType);
            var right = CreateObj(obj, rightRefType);

            if (leftRefType != RefType.DeadWeak && rightRefType != RefType.DeadWeak)
            {
                Assert.IsTrue(this.comparer.Equals(left, right));
            }
            else
            {
                GC.Collect();
                if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
                {
                    Assert.IsFalse(this.comparer.Equals(left, right));
                }
            }
        }
Exemplo n.º 6
0
        public void TestRemoveDeadItemWhenAddMoreThanRefreshIntervalItems()
        {
            WeakDictionary<KeyObject, int> weakDict = new WeakDictionary<KeyObject, int>(this.comparer, 5);
            for (int i = 0; i < 5; i++)
            {
                AddAnItem(weakDict, i);
            }

            // Force gc to collect dead items
            GC.Collect();
            if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
            {
                var key = new KeyObject(6);
                weakDict.Add(key, 6);

                // The countLimit after the first clean is 6 now.
                Assert.AreEqual(1, weakDict.Count);
            }
        }
Exemplo n.º 7
0
 private object CreateObj(KeyObject obj, RefType valueType)
 {
     switch (valueType)
     {
         case RefType.StrongRef:
             return obj;
         case RefType.AliveWeak:
             return new WeakKeyReference<KeyObject>(obj, this.comparer);
         default:
             return new WeakKeyReference<KeyObject>(this.CreateObj(obj.ID), this.comparer);
     }
 }
Exemplo n.º 8
0
 private WeakDictionary<object, int> CreateDict(out List<KeyObject> keys)
 {
     WeakDictionary<object, int> weakDict = new WeakDictionary<object, int>(this.comparer, refreshInterval: 5);
     keys = new List<KeyObject>();
     for (int i = 0; i < 10; i++)
     {
         KeyObject key = new KeyObject(i);
         keys.Add(key);
         weakDict.Add(key, i);
     }
     return weakDict;
 }
Exemplo n.º 9
0
        public void TestWeakKeyComparerForSameRef()
        {
            var array = Enum.GetValues(typeof(RefType));

            foreach (RefType refType in array)
            {
                KeyObject keyObj = new KeyObject(1);
                var obj = CreateObj(keyObj, refType);
                GC.Collect();
                if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
                {
                    Assert.IsTrue(this.comparer.Equals(obj, obj));
                }
            }
        }
Exemplo n.º 10
0
        private void TestWeakKeyComparerForRefImplement(RefType leftRefType, RefType rightRefType)
        {
            KeyObject obj = new KeyObject(1);
            var left = CreateObj(obj, leftRefType);
            var right = CreateObj(obj, rightRefType);

            if (leftRefType != RefType.DeadWeak && rightRefType != RefType.DeadWeak)
            {
                Assert.IsTrue(this.comparer.Equals(left, right));
            }
            else
            {
                GC.Collect();
                if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
                {
                    Assert.IsFalse(this.comparer.Equals(left, right));
                }
            }
        }
Exemplo n.º 11
0
        public void TestAddSameKeyThrowsException()
        {
            WeakDictionary<KeyObject, int> weakDict = new WeakDictionary<KeyObject, int>(this.comparer, 5);
            KeyObject key = new KeyObject(10);
            weakDict.Add(key, 10);

            Action testAction = () => { weakDict.Add(key, 10); };
            testAction.ShouldThrow<ArgumentException>();
        }
Exemplo n.º 12
0
 private void AddAnItem(WeakDictionary<KeyObject, int> dict, int id)
 {
     KeyObject key = new KeyObject(id);
     dict.Add(key, rand.Next());
 }
Exemplo n.º 13
0
        public void TestRemoveDeadItemWhenAddMoreThanCurrentRefreshIntervalItems()
        {
            WeakDictionary<KeyObject, int> weakDict = new WeakDictionary<KeyObject, int>(this.comparer, 5);
            for (int i = 0; i < 4; i++)
            {
                AddAnItem(weakDict, i);
            }

            // Force gc to collect dead items
            GC.Collect();
            if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
            {
                var key1 = new KeyObject(11);
                weakDict.Add(key1, 11);

                var key2 = new KeyObject(12);

                // Weak dictionary will remove dead items during this adding operation.
                // Then the currentRefreshInterval will be 1(still alive)+5=6.
                weakDict.Add(key2, 12);

                Assert.AreEqual(2, weakDict.Count);

                for (int i = 0; i < 3; i++)
                {
                    AddAnItem(weakDict, i);
                }

                // Force gc to collect dead items
                GC.Collect();
                if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
                {
                    // Add the six item to dictionary.
                    var key6 = new KeyObject(16);

                    // Weak dictionary will not clean dead entities.
                    weakDict.Add(key6, 16);
                    Assert.AreEqual(6, weakDict.Count);

                    var key14 = new KeyObject(114);

                    // Weak dictionary will remove dead items during this adding operation.
                    // Then the currentRefreshInterval will be 3(still alive)+5=8.
                    weakDict.Add(key14, 114);
                    Assert.AreEqual(4, weakDict.Count);
                }
            }
        }
Exemplo n.º 14
0
        public void TestRemoveDeadItemWhenAddMoreThanRefreshIntervalItems()
        {
            WeakDictionary<KeyObject, int> weakDict = new WeakDictionary<KeyObject, int>(this.comparer, 5);
            for (int i = 0; i < 5; i++)
            {
                AddAnItem(weakDict, i);
            }

            // Force gc to collect dead items
            GC.Collect();
            if (GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded)
            {
                var key = new KeyObject(6);
                weakDict.Add(key, 6);

                // The countLimit after the first clean is 6 now.
                Assert.AreEqual(1, weakDict.Count);
            }
        }
Exemplo n.º 15
0
 private void AddAnItem(WeakDictionary<KeyObject, int> dict, int id)
 {
     KeyObject key = new KeyObject(id);
     dict.Add(key, rand.Next());
 }