public void TestCollect4() { var dictionary = new WeakKeyDictionary <object, int>(); var key1 = new Key(1, 9001); var key4 = new Key(4, 9001); new Action(() => { dictionary.Add(key1, 1); dictionary.Add(new Key(2, 9001), 2); dictionary.Add(new Key(3, 9001), 3); dictionary.Add(key4, 4); })(); GC.Collect(); GC.WaitForPendingFinalizers(); dictionary.Count.Should().Be(4); dictionary.Collect(); dictionary.Count.Should().Be(2); dictionary[key1].Should().Be(1); dictionary[key4].Should().Be(4); dictionary._freeCount.Should().Be(2, "Because 2 previously used entries were reclaimed"); EnsureIntegrity(dictionary); }
public void TestCollect8() { var dictionary = new WeakKeyDictionary <object, int>(); var key1 = new Key(1, 9001); var key4 = new Key(4, 9001); new Action(() => { dictionary.Add(key1, 1); dictionary.Add(new Key(2, 9001), 2); dictionary.Add(new Key(3, 9001), 3); dictionary.Add(key4, 4); })(); GC.Collect(); GC.WaitForPendingFinalizers(); dictionary.Collect(); dictionary.Count.Should().Be(2); var key2 = new Key(2, 9001); var key3 = new Key(3, 9001); dictionary.Add(key2, 2); dictionary.Add(key3, 3); dictionary.Count.Should().Be(4); dictionary[key2].Should().Be(2); dictionary[key3].Should().Be(3); EnsureIntegrity(dictionary); }
public void TestCollect1() { var dictionary = new WeakKeyDictionary <object, int>(); WeakReference <object> weakKey = null; new Action(() => { var key = new ByReferenceClass(); weakKey = new WeakReference <object>(key); dictionary.Add(key, 42); dictionary.Count.Should().Be(1); })(); GC.Collect(); GC.WaitForPendingFinalizers(); weakKey.Should().NotBeNull(); dictionary.Collect(); dictionary.Count.Should().Be(0); object unused; weakKey.TryGetTarget(out unused).Should().BeFalse("Because the dictionary shouldn't keep the key alive"); EnsureIntegrity(dictionary); }
public int RemoveUnusedServants() { lock (_syncRoot) { var collectedServants = _servantsBySubject.Collect(returnCollectedValues: true); if (collectedServants != null) { foreach (var servant in collectedServants) { if (Log.IsDebugEnabled) { Log.DebugFormat( "{0}: Removing servant '#{1}' from list of available servants because it's subject is no longer reachable (it has been garbage collected)", _remotingEndPoint.Name, servant.ObjectId); } _servantsById.Remove(servant.ObjectId); } _numServantsCollected += collectedServants.Count; return(collectedServants.Count); } return(0); } }
public void TestCollect5() { var dictionary = new WeakKeyDictionary <object, int>(); dictionary.Version.Should().Be(0); dictionary.Collect(); dictionary.Version.Should().Be(0, "Because no entry should've been modified"); EnsureIntegrity(dictionary); }
public void TestCollect6() { var dictionary = new WeakKeyDictionary <object, int>(); var key1 = new object(); var key2 = new object(); dictionary.Add(key1, 1337); dictionary.Add(key2, 42); dictionary.Version.Should().Be(2); dictionary.Count.Should().Be(2); dictionary.Collect(); dictionary.Version.Should().Be(2); dictionary.Count.Should().Be(2); dictionary[key1].Should().Be(1337); dictionary[key2].Should().Be(42); EnsureIntegrity(dictionary); }
public void TestCollect2() { var dictionary = new WeakKeyDictionary <object, int>(); new Action(() => { dictionary.Add(new Key(1, 9001), 1); dictionary.Add(new Key(2, 9001), 2); dictionary.Add(new Key(3, 9001), 3); })(); GC.Collect(); GC.WaitForPendingFinalizers(); dictionary.Count.Should().Be(3); dictionary.Collect(); dictionary.Count.Should().Be(0); EnsureIntegrity(dictionary); }