Queue <ExpressionBuilder> DoAdd(WorkflowRuntimeException value)
        {
            var key = value.InnerException;

            if (key != null)
            {
                var nestedKey = key as WorkflowRuntimeException;
                if (nestedKey != null)
                {
                    var callStack = DoAdd(nestedKey);
                    if (callStack != null)
                    {
                        callStack.Enqueue(value.Builder);
                        return(callStack);
                    }
                }
                else
                {
                    var weakKey = new WeakKey <Exception>(key);
                    if (exceptions.ContainsKey(weakKey))
                    {
                        return(null);
                    }
                    CollectUnusedExceptions();

                    var callStack = new Queue <ExpressionBuilder>();
                    callStack.Enqueue(value.Builder);
                    exceptions.Add(weakKey, callStack);
                    return(callStack);
                }
            }

            return(null);
        }
Esempio n. 2
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion



        /// <summary>
        ///A test for GetValue
        ///</summary>
        public void GetValueTestHelper <E>(E e)
            where E : class
        {
            WeakKey <E> target = new WeakKey <E>()
            {
                _elementReference = new WeakReference(e)
            };
            E    value         = null;
            E    valueExpected = e;
            bool actual;

            actual = target.GetValue(out value, true);
            Assert.AreEqual(valueExpected, value);
            Assert.IsTrue(actual);

            ((WeakReference)target._elementReference).Target = null;
            actual = target.GetValue(out value, true);
            Assert.IsFalse(actual);

            ((WeakReference)target._elementReference).Target = WeakKey <E> .NullValue;
            actual = target.GetValue(out value, true);
            Assert.IsTrue(actual);
            Assert.AreEqual(null, value);

            target._elementReference = e;
            actual = target.GetValue(out value, false);
            Assert.AreEqual(valueExpected, value);
            Assert.IsTrue(actual);
        }
Esempio n. 3
0
        /// <summary>
        ///     Gets or adds the attached values container.
        /// </summary>
        protected override LightDictionaryBase <string, object> GetOrAddAttachedDictionary(object item, bool addNew)
        {
            var dependencyObject = item as DependencyObject;

            if (dependencyObject != null)
            {
                var dict = (AttachedValueDictionary)dependencyObject.GetValue(AttachedValueDictionaryProperty);
                if (dict == null && addNew)
                {
                    dict = new AttachedValueDictionary();
                    dependencyObject.SetValue(AttachedValueDictionaryProperty, dict);
                }
                return(dict);
            }

            lock (_internalDictionary)
            {
                WeakReference reference;
                if (!_internalDictionary.TryGetValue(new WeakKey(item, false), out reference))
                {
                    if (!addNew)
                    {
                        return(null);
                    }
                    var weakKey = new WeakKey(item, true);
                    reference = new WeakReference(new AttachedValueDictionary(weakKey, this), true);
                    _internalDictionary.Add(weakKey, reference);
                }
                return((LightDictionaryBase <string, object>)reference.Target);
            }
        }
Esempio n. 4
0
            /// <summary>
            /// Equality between keys.
            /// </summary>
            /// <param name="obj">The object to compare to.</param>
            /// <returns>True if they are equivalent.</returns>
            public override bool Equals(object obj)
            {
                WeakKey other = obj as WeakKey;

                if (other == null)
                {
                    return(false);
                }

                object a = this.Target;
                object b = other.Target;

                if (a == null && b == null)
                {
                    return(true);
                }
                else if (a == null || b == null)
                {
                    return(false);
                }
                else
                {
                    return(a.Equals(b));
                }
            }
Esempio n. 5
0
        public bool Remove(KeyValuePair <K, V> item)
        {
            WeakKey key = new WeakKey(item.Key);

            lock (_sync)
            {
                return(_internalDictionary.Remove(key));
            }
        }
Esempio n. 6
0
        public bool TryGetValue(K key, out V value)
        {
            WeakKey k = new WeakKey(key);

            lock (_sync)
            {
                return(_internalDictionary.TryGetValue(k, out value));
            }
        }
Esempio n. 7
0
        public bool TryGetValue(TKey key, out TValue value)
        {
            WeakKey tkey = new WeakKey(key);

            lock (_internalObject)
            {
                return(_internalDictionary.TryGetValue(tkey, out value));
            }
        }
Esempio n. 8
0
        public bool Remove(KeyValuePair <TKey, TValue> item)
        {
            WeakKey key = new WeakKey(item.Key);

            lock (_internalObject)
            {
                return(_internalDictionary.Remove(key));
            }
        }
Esempio n. 9
0
        /// <summary>
        ///A test for SetValue
        ///</summary>
        public void SetValueTestHelper <E>(E e)
            where E : class
        {
            WeakKey <E> target = new WeakKey <E>();
            E           value  = e;

            target.SetValue(value, false);
            Assert.AreEqual(e, target._elementReference);
            target.SetValue(value, true);
            Assert.AreEqual(e, ((WeakReference)target._elementReference).Target);
        }
Esempio n. 10
0
        public void Add(TKey key, TValue value)
        {
            WeakKey tkey = new WeakKey(key);

            lock (_internalObject)
            {
                _internalDictionary.Add(tkey, value);
            }
            // This looks a bit weird but the purpose of the lost key finder is to execute
            // code in some future garbage collection phase so we immediately create some garbage.
            new LostKeyFinder(this, tkey);
        }
Esempio n. 11
0
        /// <summary>
        ///A test for IsGarbage
        ///</summary>
        public void IsGarbageTestHelper <E>(E e)
            where E : class
        {
            WeakKey <E> target = new WeakKey <E>()
            {
                _elementReference = new WeakReference(e)
            };                                                                                 // TODO: Initialize to an appropriate value

            Assert.IsFalse(target.IsGarbage);

            ((WeakReference)target._elementReference).Target = null;
            Assert.IsTrue(target.IsGarbage);
        }
Esempio n. 12
0
        /// <summary>
        ///     Clears all attached properties in the specified item.
        /// </summary>
        protected override bool ClearInternal(object item)
        {
            var dependencyObject = item as DependencyObject;

            if (dependencyObject != null)
            {
                dependencyObject.ClearValue(AttachedValueDictionaryProperty);
                return(true);
            }

            var weakKey = new WeakKey(item, false);

            return(ClearInternal(ref weakKey));
        }
Esempio n. 13
0
        public void TestWeakKeyHashBehavior()
        {
            var controlA = new TestControlA();
            var controlB = new TestControlA();

            var weakA  = new WeakKey <Control>(controlA);
            var weakA2 = new WeakKey <Control>(controlA);

            Assert.IsTrue(weakA.Equals(weakA));
            Assert.IsTrue(weakA.GetHashCode() == weakA2.GetHashCode());
            Assert.IsTrue(weakA.Equals(weakA2));
            Assert.IsTrue(weakA != weakA2);
            Assert.IsTrue(!weakA.Equals(controlA));
            Assert.IsTrue(!controlA.Equals(weakA));

            var weakB = new WeakKey <Control>(controlB);

            Assert.IsTrue(!weakA.Equals(weakB));
            Assert.IsTrue(weakA.GetHashCode() != weakB.GetHashCode()); //Always true? Not sure, but I hope so!

            var set = new HashSet <WeakKey <Control> >();

            set.Add(weakA);
            Assert.IsTrue(set.Count == 1);
            set.Add(weakA2);
            Assert.IsTrue(set.Count == 1);
            set.Add(weakB);
            Assert.IsTrue(set.Count == 2);

            // Couldn't get this to work.
            //controlA = null;
            //controlB = null;
            //GC.Collect(); //defaults are all generation and 'forced' collection
            //foreach (SkinService.WeakKey<Control> key in set)
            //    Assert.IsTrue(!key.IsAlive);

            // Proves that WeakReference can't do the job!
            //var weakRefA = new WeakReference(controlA);
            //var weakRefA2 = new WeakReference(controlA);
            //var weakRefB = new WeakReference(controlB);

            //var set2 = new HashSet<WeakReference>();
            //set2.Add(weakRefA);
            //Assert.IsTrue(set2.Count == 1);
            //set2.Add(weakRefA2);
            //Assert.IsTrue(set2.Count == 1); //it's 2!
            //set2.Add(weakRefB);
            //Assert.IsTrue(set2.Count == 2); //it's 3!
        }
Esempio n. 14
0
        public void TestWeakKeyHashBehavior()
        {
            var controlA = new TestControlA();
            var controlB = new TestControlA();

            var weakA = new WeakKey<Control>(controlA);
            var weakA2 = new WeakKey<Control>(controlA);

            Assert.IsTrue(weakA.Equals(weakA));
            Assert.IsTrue(weakA.GetHashCode() == weakA2.GetHashCode());
            Assert.IsTrue(weakA.Equals(weakA2));
            Assert.IsTrue(weakA != weakA2);
            Assert.IsTrue(!weakA.Equals(controlA));
            Assert.IsTrue(!controlA.Equals(weakA));

            var weakB = new WeakKey<Control>(controlB);
            Assert.IsTrue(!weakA.Equals(weakB));
            Assert.IsTrue(weakA.GetHashCode() != weakB.GetHashCode()); //Always true? Not sure, but I hope so!

            var set = new HashSet<WeakKey<Control>>();
            set.Add(weakA);
            Assert.IsTrue(set.Count == 1);
            set.Add(weakA2);
            Assert.IsTrue(set.Count == 1);
            set.Add(weakB);
            Assert.IsTrue(set.Count == 2);

            // Couldn't get this to work.
            //controlA = null;
            //controlB = null;
            //GC.Collect(); //defaults are all generation and 'forced' collection
            //foreach (SkinService.WeakKey<Control> key in set)
            //    Assert.IsTrue(!key.IsAlive);

            // Proves that WeakReference can't do the job!
            //var weakRefA = new WeakReference(controlA);
            //var weakRefA2 = new WeakReference(controlA);
            //var weakRefB = new WeakReference(controlB);

            //var set2 = new HashSet<WeakReference>();
            //set2.Add(weakRefA);
            //Assert.IsTrue(set2.Count == 1);
            //set2.Add(weakRefA2);
            //Assert.IsTrue(set2.Count == 1); //it's 2!
            //set2.Add(weakRefB);
            //Assert.IsTrue(set2.Count == 2); //it's 3!
        }
Esempio n. 15
0
 private bool ClearInternal(ref WeakKey key)
 {
     lock (_internalDictionary)
     {
         WeakReference value;
         if (!_internalDictionary.TryGetValue(key, out value) || !_internalDictionary.Remove(key))
         {
             return(false);
         }
         var dictionary = (AttachedValueDictionary)value.Target;
         if (dictionary != null)
         {
             dictionary.Clear();
         }
         key.Free();
         return(true);
     }
 }
Esempio n. 16
0
 public V this[K key]
 {
     get {
         lock (_sync) {
             return(_internalDictionary[new WeakKey(key)]);
         }
     }
     set
     {
         WeakKey k = new WeakKey(key);
         lock (_sync)
         {
             _internalDictionary[k] = value;
         }
         // This looks a bit weird but the purpose of the lost key finder is to execute
         // code in some future garbage collection phase so we immediately create some garbage.
         new LostKeyFinder(this, k);
     }
 }
        /// <summary>
        ///A test for GetHashCode
        ///</summary>
        void GetHashCodeTestHelper <E>(E e)
            where E : class
        {
            WeakKeyComparer <E> target = new WeakKeyComparer <E>()
            {
                _equalityComparer = EqualityComparer <E> .Default
            };
            var key = new WeakKey <E>()
            {
                _elementReference = new WeakReference(e)
            };

            int hash1 = target.GetHashCode(ref key, true);

            key.SetValue(e, false);

            int hash2 = target.GetHashCode(ref key, false);

            Assert.AreEqual(hash1, hash2);
        }
Esempio n. 18
0
 public TValue this[TKey key]
 {
     get {
         lock (_internalObject) {
             return(_internalDictionary[new WeakKey(key)]);
         }
     }
     set
     {
         WeakKey Tkey = new WeakKey(key);
         lock (_internalObject)
         {
             //_internalDictionary[Tkey] = value;
             _internalDictionary.Add(Tkey, value);
         }
         // This looks a bit weird but the purpose of the lost key finder is to execute
         // code in some future garbage collection phase so we immediately create some garbage.
         new LostKeyFinder(this, Tkey);
     }
 }
Esempio n. 19
0
        public bool TryGetValue(Exception key, out WorkflowException value)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            value = null;
            var weakKey = new WeakKey <Exception>(key);
            var result  = exceptions.TryGetValue(weakKey, out Queue <ExpressionBuilder> callStack);

            if (result)
            {
                while (callStack.Count > 0)
                {
                    var builder = callStack.Dequeue();
                    value = new WorkflowRuntimeException(key.Message, builder, value ?? key);
                }
            }

            return(result);
        }
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Equals
        ///</summary>
        void EqualsTestHelper <E>(E e1, E e2)
            where E : class
        {
            WeakKeyComparer <E> target = new WeakKeyComparer <E>()
            {
                _equalityComparer = EqualityComparer <E> .Default
            };
            var key1 = new WeakKey <E>()
            {
                _elementReference = new WeakReference(e1)
            };
            var key2 = new WeakKey <E>()
            {
                _elementReference = new WeakReference(e2)
            };

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, true));
            Assert.IsFalse(target.Equals(ref key2, true, ref key1, true));

            Assert.IsTrue(target.Equals(ref key1, true, ref key1, true));
            Assert.IsTrue(target.Equals(ref key2, true, ref key2, true));

            key2.SetValue(e1, true);

            Assert.IsTrue(target.Equals(ref key1, true, ref key2, true));
            Assert.IsTrue(target.Equals(ref key2, true, ref key1, true));

            ((WeakReference)key1._elementReference).Target = null;

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, true));
            Assert.IsFalse(target.Equals(ref key2, true, ref key1, true));

            ((WeakReference)key2._elementReference).Target = null;

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, true));
            Assert.IsFalse(target.Equals(ref key2, true, ref key1, true));

            Assert.IsTrue(target.Equals(ref key1, true, ref key1, true));
            Assert.IsTrue(target.Equals(ref key2, true, ref key2, true));

            key1.SetValue(e1, true);
            key2.SetValue(e2, false);

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, false));
            Assert.IsFalse(target.Equals(ref key2, false, ref key1, true));

            Assert.IsTrue(target.Equals(ref key1, true, ref key1, true));
            Assert.IsTrue(target.Equals(ref key2, false, ref key2, false));

            key2.SetValue(e1, false);

            Assert.IsTrue(target.Equals(ref key1, true, ref key2, false));
            Assert.IsTrue(target.Equals(ref key2, false, ref key1, true));

            key2.SetValue(null, false);

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, false));
            Assert.IsFalse(target.Equals(ref key2, false, ref key1, true));

            ((WeakReference)key1._elementReference).Target = null;

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, false));
            Assert.IsFalse(target.Equals(ref key2, false, ref key1, true));

            key1.SetValue(e1, false);
            key2.SetValue(e2, false);

            Assert.IsFalse(target.Equals(ref key1, false, ref key2, false));
            Assert.IsFalse(target.Equals(ref key2, false, ref key1, false));

            key2.SetValue(e1, false);

            Assert.IsTrue(target.Equals(ref key1, false, ref key2, false));
            Assert.IsTrue(target.Equals(ref key2, false, ref key1, false));
        }
Esempio n. 21
0
 public LostKeyFinder(WeakKeyDictionary <TKey, TValue> dictionary, WeakKey key)
 {
     _dictionary = dictionary;
     _key        = key;
 }
Esempio n. 22
0
 protected bool ShouldEntryBeRemoved(WeakKey <Key> key, Value val)
 {
     return(!key.IsAlive);
 }
Esempio n. 23
0
 public AttachedValueDictionary(WeakKey reference, WeakReferenceAttachedValueProvider provider)
     : base(true)
 {
     _reference = reference;
     Add(ProviderKey, provider);
 }
Esempio n. 24
0
 public LostKeyFinder(WeakKeyDictionary <K, V> dictionary, WeakKey key)
 {
     _dictionary = dictionary;
     _key        = key;
 }