Example #1
0
        /// <summary>
        /// Forces a removal of a project root element from the weak cache if it is present.
        /// </summary>
        /// <param name="projectRootElement">The project root element to remove.</param>
        /// <remarks>
        /// No exception is thrown if this project root element is in use by currently loaded projects
        /// by this method.  The calling method must know that this is a safe operation.
        /// There may of course be strong references to the project root element from customer code.
        /// The assumption is that when they instruct the project collection to unload it, which
        /// leads to this being called, they are releasing their strong references too (or it doesn't matter)
        /// </remarks>
        internal override void DiscardAnyWeakReference(ProjectRootElement projectRootElement)
        {
            ErrorUtilities.VerifyThrowArgumentNull(projectRootElement, "projectRootElement");

            // A PRE may be unnamed if it was only used in memory.
            if (projectRootElement.FullPath != null)
            {
                lock (_locker)
                {
                    _weakCache.Remove(projectRootElement.FullPath);
                }
            }
        }
        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);
        }
Example #3
0
        public void When_non_existing_key_is_removed_Then_false_is_returned()
        {
            var dict = new WeakValueDictionary <int, Item> {
                [0] = new Item(),
                [1] = new Item()
            };

            bool removed = dict.Remove(3);

            Assert.That(removed, Is.False);
            Assert.That(dict.Count, Is.EqualTo(2));
        }
Example #4
0
        public void When_key_is_removed_Then_count_is_decreased()
        {
            var dict = new WeakValueDictionary <int, Item> {
                [0] = new Item(),
                [1] = new Item()
            };

            bool removed = dict.Remove(0);

            Assert.That(removed, Is.True);
            Assert.That(dict.Count, Is.EqualTo(1));
        }