public void Clean()
        {
            var    c = new WeakList <object>();
            object x = new object();

            using (NoGCRegion.Enter(1000)) {
                AddCollectableItems(c, 3);

                c.InsertFirst(x);
                c.InsertAfter(x, x);
                c.InsertBefore(x, x);

                Assert.AreEqual(6, c.AddCountSinceLastClean);
                Assert.AreEqual(6, c.UnsafeCount);
                Assert.IsTrue(c.Take(3).SequenceEqual(new[] { x, x, x }));
            }

            Helpers.CollectAndWait();

            c.Clean();
            Assert.AreEqual(0, c.AddCountSinceLastClean);
            Assert.AreEqual(3, c.UnsafeCount);

            c.Remove(x);
            c.Remove(x);
            Assert.AreEqual(1, c.UnsafeCount);

            GC.KeepAlive(x);
        }
        public void EnumerationCleaning()
        {
            var    c = new WeakCollection <object>();
            object x = new object();

            using (NoGCRegion.Enter(1000)) {
                c.Add(x);
                c.Add(x);
                c.Add(x);

                AddCollectableItems(c, 3);

                Assert.AreEqual(6, c.AddCountSinceLastClean);
                Assert.AreEqual(6, c.UnsafeCount);
            }

            Helpers.CollectAndWait();

            foreach (object o in c)
            {
            }

            Assert.IsTrue(c.Remove(x));
            Assert.IsTrue(c.Remove(x));

            #if NETCOREAPP2_2 // NS2.0 target does not support removing stale entries as items are encountered.
            Assert.AreEqual(6, c.AddCountSinceLastClean);
            Assert.AreEqual(4, c.UnsafeCount);
            #else
            Assert.AreEqual(0, c.AddCountSinceLastClean);
            Assert.AreEqual(1, c.UnsafeCount);
            #endif

            GC.KeepAlive(x);
        }
Пример #3
0
        public void EnterNoGCRegionAndCollect()
        {
            var weakRef = Helpers.GetWeakRef();

            using (NoGCRegion.Enter(1000)) { }

            Helpers.CollectAndWait();
            Assert.IsFalse(weakRef.TryGetTarget(out _));
        }
        public void EnumerationCleaning()
        {
            var    c = new WeakValueDictionary <int, object>();
            object x = new object();

            using (NoGCRegion.Enter(1000)) {
                c.Add(0, x);
                c.Add(1, x);
                c.Add(2, x);

                AddCollectableItems(c, 3, 3);

                Assert.AreEqual(6, c.AddCountSinceLastClean);
                Assert.AreEqual(6, c.UnsafeCount);
                Assert.IsTrue(c.ContainsKey(1));

                #if DEBUG || !NETFRAMEWORK // Causes entry with key 4 not to collection on NETFW release builds
                Assert.IsTrue(c.ContainsKey(4));
                #endif
            }

            Helpers.CollectAndWait();

            Assert.IsTrue(c.ContainsKey(1));
            Assert.IsFalse(c.ContainsKey(4));

            foreach (object o in c)
            {
            }

            Assert.IsTrue(c.Remove(0));
            Assert.IsTrue(c.Remove(1));
            Assert.IsFalse(c.Remove(4));

            #if NETCOREAPP2_2 // NS2.0 target does not support removing stale entries as items are encountered.
            Assert.AreEqual(6, c.AddCountSinceLastClean);
            Assert.AreEqual(4, c.UnsafeCount);
            #else
            Assert.AreEqual(0, c.AddCountSinceLastClean);
            Assert.AreEqual(1, c.UnsafeCount);
            #endif

            GC.KeepAlive(x);
        }
        public void Clean()
        {
            var    c = new WeakValueDictionary <int, object>();
            object x = new object();

            using (NoGCRegion.Enter(1000)) {
                c.Add(0, x);
                c.Add(1, x);
                c.Add(2, x);

                AddCollectableItems(c, 3, 3);

                Assert.AreEqual(6, c.AddCountSinceLastClean);
                Assert.AreEqual(6, c.UnsafeCount);
            }

            Helpers.CollectAndWait();

            c.Clean();
            Assert.AreEqual(0, c.AddCountSinceLastClean);
            Assert.AreEqual(3, c.UnsafeCount);

            GC.KeepAlive(x);
        }