Example #1
0
        private static void CountReturnsNumberOfKeysWithLiveValues_TestHelper(out object o, WeakRefDictionary <object, object> dict)
        {
            o = new object();
            dict.Add("foo1", o);
            dict.Add("foo2", o);

            Assert.AreEqual(2, dict.Count);
        }
Example #2
0
        public void AddingToSameKeyTwiceAlwaysThrows()
        {
            object o = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo1", o);
            dict.Add("foo1", o);
        }
        public void AddingToSameKeyTwiceAlwaysThrows()
        {
            object o = new object();
            WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

            dict.Add("foo1", o);
            dict.Add("foo1", o);
        }
        public void CanAddItemAfterPreviousItemIsCollected()
        {
            WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();
            dict.Add("foo", new object());

            GC.Collect();

            dict.Add("foo", new object());
        }
Example #5
0
        public void CanAddSameObjectTwiceWithDifferentKeys()
        {
            object o = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo1", o);
            dict.Add("foo2", o);

            Assert.AreSame(dict["foo1"], dict["foo2"]);
        }
Example #6
0
        public void CanAddItemAfterPreviousItemIsCollected()
        {
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo", new object());

            GC.Collect();

            dict.Add("foo", new object());
        }
		public void CanAddSameObjectTwiceWithDifferentKeys()
		{
			object o = new object();
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

			dict.Add("foo1", o);
			dict.Add("foo2", o);

			Assert.AreSame(dict["foo1"], dict["foo2"]);
		}
Example #8
0
        public void RemovingAKeyOfOneObjectDoesNotAffectOtherKeysForSameObject()
        {
            object o = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo1", o);
            dict.Add("foo2", o);
            dict.Remove("foo1");

            Assert.AreSame(o, dict["foo2"]);
        }
		public void CanRegisterTwoObjectsAndGetThemBoth()
		{
			object o1 = new object();
			object o2 = new object();
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

			dict.Add("foo1", o1);
			dict.Add("foo2", o2);

			Assert.AreSame(o1, dict["foo1"]);
			Assert.AreSame(o2, dict["foo2"]);
		}
Example #10
0
        public void CanRegisterTwoObjectsAndGetThemBoth()
        {
            object o1 = new object();
            object o2 = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo1", o1);
            dict.Add("foo2", o2);

            Assert.AreSame(o1, dict["foo1"]);
            Assert.AreSame(o2, dict["foo2"]);
        }
Example #11
0
        public void RemovingAKeyDoesNotAffectOtherKeys()
        {
            object o1 = new object();
            object o2 = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo1", o1);
            dict.Add("foo2", o2);
            dict.Remove("foo1");

            Assert.AreSame(o2, dict["foo2"]);
        }
Example #12
0
        public void CountReturnsNumberOfKeysWithLiveValues()
        {
            object o = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo1", o);
            dict.Add("foo2", o);

            Assert.AreEqual(2, dict.Count);

            o = null;
            GC.Collect();

            Assert.AreEqual(0, dict.Count);
        }
Example #13
0
        /// <summary>
        /// Retrieves an object instance by key. If the key hasn't been encountered before, a new instance will be created.
        /// </summary>
        /// <param name="key">The key used to look up the instance. If the key is encountered for the first time, a new instance will be created.</param>
        /// <returns>An existing or new instance matching the key.</returns>
        public T GetObject(TKey key)
        {
            T obj;

            if (_objects.TryGetValue(key, out obj))
            {
                return(obj);
            }

            // No or dead repository for the given key. Create a new one.
            obj = Create();
            _objects.Add(key, obj);

            return(obj);
        }
        public void CanEnumerate()
        {
            object o1 = new object();
            object o2 = new object();
            WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

            dict.Add("foo1", o1);
            dict.Add("foo2", o2);

            foreach (KeyValuePair<object, object> kvp in dict)
            {
                Assert.IsNotNull(kvp);
                Assert.IsNotNull(kvp.Key);
                Assert.IsNotNull(kvp.Value);
            }
        }
Example #15
0
        public void NullIsAValidValue()
        {
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo", null);
            Assert.IsNull(dict["foo"]);
        }
Example #16
0
        public void CanEnumerate()
        {
            object o1 = new object();
            object o2 = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo1", o1);
            dict.Add("foo2", o2);

            foreach (KeyValuePair <object, object> kvp in dict)
            {
                Assert.IsNotNull(kvp);
                Assert.IsNotNull(kvp.Key);
                Assert.IsNotNull(kvp.Value);
            }
        }
Example #17
0
        public void CanFindOutIfContainsAKey()
        {
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo", null);
            Assert.IsTrue(dict.ContainsKey("foo"));
            Assert.IsFalse(dict.ContainsKey("foo2"));
        }
Example #18
0
        public void RegistrationDoesNotPreventGarbageCollection()
        {
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo", new object());
            GC.Collect();
            object unused = dict["foo"];
        }
Example #19
0
        public void CanRemoveAnObjectThatWasAlreadyAdded()
        {
            object o = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo", o);
            dict.Remove("foo");
            object unused = dict["foo"];
        }
Example #20
0
        public void CanRegisterObjectAndFindItByID()
        {
            object o = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add("foo", o);
            Assert.IsNotNull(dict["foo"]);
            Assert.AreSame(o, dict["foo"]);
        }
		public void CanRegisterObjectAndFindItByID()
		{
			object o = new object();
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

			dict.Add("foo", o);
			Assert.IsNotNull(dict["foo"]);
			Assert.AreSame(o, dict["foo"]);
		}
Example #22
0
        public void KeyCanBeOfArbitraryType()
        {
            object oKey = new object();
            object oVal = new object();
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            dict.Add(oKey, oVal);

            Assert.AreSame(oVal, dict[oKey]);
        }
		public void KeyCanBeOfArbitraryType()
		{
			object oKey = new object();
			object oVal = new object();
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

			dict.Add(oKey, oVal);

			Assert.AreSame(oVal, dict[oKey]);
		}
Example #24
0
        public void CanAddItemAfterPreviousItemIsCollected()
        {
            WeakRefDictionary <object, object> dict = new WeakRefDictionary <object, object>();

            CanAddItemAfterPreviousItemIsCollected_TestHelper(dict);

            GC.Collect();
            GC.WaitForPendingFinalizers();

            dict.Add("foo", new object());
        }
Example #25
0
        public static void AddClientService(Type clientServiceType, object serviceInstance)
        {
            if (clientServiceType.FullName.ToLower().Contains("workitem"))
            {
                return;
            }
            //if (IsWCFContractType(clientServiceType))
            //    return;

            if (clientServices.ContainsKey(clientServiceType))
            {
                return;
            }
            clientServices.Add(clientServiceType, serviceInstance);
        }
Example #26
0
 private static void RegistrationDoesNotPreventGarbageCollection_TestHelper(WeakRefDictionary <object, object> dict)
 {
     dict.Add("foo", new object());
 }
Example #27
0
 private static void CanAddItemAfterPreviousItemIsCollected_TestHelper(WeakRefDictionary <object, object> dict)
 {
     dict.Add("foo", new object());
 }
		public void RemovingAKeyDoesNotAffectOtherKeys()
		{
			object o1 = new object();
			object o2 = new object();
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

			dict.Add("foo1", o1);
			dict.Add("foo2", o2);
			dict.Remove("foo1");

			Assert.AreSame(o2, dict["foo2"]);
		}
		public void RegistrationDoesNotPreventGarbageCollection()
		{
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();
			dict.Add("foo", new object());
			GC.Collect();
			object unused = dict["foo"];
		}
		public void NullIsAValidValue()
		{
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();
			dict.Add("foo", null);
			Assert.IsNull(dict["foo"]);
		}
		public void CanFindOutIfContainsAKey()
		{
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

			dict.Add("foo", null);
			Assert.IsTrue(dict.ContainsKey("foo"));
			Assert.IsFalse(dict.ContainsKey("foo2"));
		}
		public void CountReturnsNumberOfKeysWithLiveValues()
		{
			object o = new object();
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

			dict.Add("foo1", o);
			dict.Add("foo2", o);

			Assert.AreEqual(2, dict.Count);

			o = null;
			GC.Collect();

			Assert.AreEqual(0, dict.Count);
		}
		public void RemovingAKeyOfOneObjectDoesNotAffectOtherKeysForSameObject()
		{
			object o = new object();
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

			dict.Add("foo1", o);
			dict.Add("foo2", o);
			dict.Remove("foo1");

			Assert.AreSame(o, dict["foo2"]);
		}
		public void CanRemoveAnObjectThatWasAlreadyAdded()
		{
			object o = new object();
			WeakRefDictionary<object, object> dict = new WeakRefDictionary<object, object>();

			dict.Add("foo", o);
			dict.Remove("foo");
			object unused = dict["foo"];
		}