예제 #1
0
        public void Properties_WithSameNameButDifferentScopes_HaveIndependentValues()
        {
            var carrier = new object();
            var name    = Guid.NewGuid().ToString("N");
            var other   = new ConnectedPropertyScope();

            ConnectedProperty.GetConnectedProperty(carrier, name).Set(17);
            other.GetConnectedProperty(carrier, name).Set(13);
            Assert.Equal(17, ConnectedProperty.GetConnectedProperty(carrier, name).Get());
            Assert.Equal(13, other.GetConnectedProperty(carrier, name).Get());
        }
        public void Property_ReferencingScope_ActsAsStrongReference()
        {
            var(valueRef, scopeRef) = Part1();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            Assert.False(valueRef.IsAlive);
            Assert.False(scopeRef.IsAlive);

            (WeakReference, WeakReference, object) Create()
            {
                var        name     = Guid.NewGuid().ToString("N");
                var        scope    = new ConnectedPropertyScope();
                var        scopeRef = new WeakReference(scope);
                var        carrier  = new object();
                Func <int> value    = scope.GetHashCode;
                var        valueRef = new WeakReference(value);

                scope.GetConnectedProperty(carrier, name).Set(value);
                return(valueRef, scopeRef, carrier);
            }

            (WeakReference, WeakReference) Part1()
            {
                var(valueRef, scopeRef, carrier) = Create();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                Assert.True(valueRef.IsAlive);
                Assert.True(scopeRef.IsAlive);
                GC.KeepAlive(carrier);
                return(valueRef, scopeRef);
            }
        }
        public void Properties_CrossReferencingCarriersAcrossScopes_WhenOnePropertyRemainsAlive_AreNotCollected()
        {
            var(carrierRef1, valueRef1, carrierRef2, valueRef2, value1) = Create();
            GC.Collect();
            Assert.True(carrierRef1.IsAlive);
            Assert.True(valueRef1.IsAlive);
            Assert.True(carrierRef2.IsAlive);
            Assert.True(valueRef2.IsAlive);
            GC.KeepAlive(value1);

            (WeakReference, WeakReference, WeakReference, WeakReference, Func <int>) Create()
            {
                var        name        = Guid.NewGuid().ToString("N");
                var        other       = new ConnectedPropertyScope();
                var        carrier1    = new object();
                var        carrierRef1 = new WeakReference(carrier1);
                var        carrier2    = new object();
                var        carrierRef2 = new WeakReference(carrier1);
                Func <int> value1      = carrier2.GetHashCode;
                var        valueRef1   = new WeakReference(value1);
                Func <int> value2      = carrier1.GetHashCode;
                var        valueRef2   = new WeakReference(value2);

                ConnectedProperty.GetConnectedProperty(carrier1, name).Set(value1);
                other.GetConnectedProperty(carrier2, name).Set(value2);
                return(carrierRef1, valueRef1, carrierRef2, valueRef2, value1);
            }
        }
예제 #4
0
        public void Property_WhenScopeIsCollected_IsCollected()
        {
            var name     = Guid.NewGuid().ToString("N");
            var scope    = new ConnectedPropertyScope();
            var carrier  = new object();
            var value    = new object();
            var valueRef = new WeakReference(value);

            scope.GetConnectedProperty(carrier, name).Set(value);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            Assert.False(valueRef.IsAlive);
            GC.KeepAlive(carrier);
        }
예제 #5
0
        public void Properties_CrossReferencingCarriersAcrossScopes_WhenCarriersAreCollected_AreCollected()
        {
            var        name        = Guid.NewGuid().ToString("N");
            var        other       = new ConnectedPropertyScope();
            var        carrier1    = new object();
            var        carrierRef1 = new WeakReference(carrier1);
            var        carrier2    = new object();
            var        carrierRef2 = new WeakReference(carrier1);
            Func <int> value1      = carrier2.GetHashCode;
            var        valueRef1   = new WeakReference(value1);
            Func <int> value2      = carrier1.GetHashCode;
            var        valueRef2   = new WeakReference(value2);

            ConnectedProperty.GetConnectedProperty(carrier1, name).Set(value1);
            other.GetConnectedProperty(carrier2, name).Set(value2);
            GC.Collect();
            Assert.False(carrierRef1.IsAlive);
            Assert.False(valueRef1.IsAlive);
            Assert.False(carrierRef2.IsAlive);
            Assert.False(valueRef2.IsAlive);
        }