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); }
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)); } }
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); } } }
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)); } } }
// 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)); } } }
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); } }
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); } }
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; }
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)); } } }
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)); } } }
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>(); }
private void AddAnItem(WeakDictionary<KeyObject, int> dict, int id) { KeyObject key = new KeyObject(id); dict.Add(key, rand.Next()); }
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); } } }
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); } }
private void AddAnItem(WeakDictionary<KeyObject, int> dict, int id) { KeyObject key = new KeyObject(id); dict.Add(key, rand.Next()); }