public void IndirectComputedObjectCanBeGarbageCollected() { GC.Collect(); SourceData observable = new SourceData(); DirectComputed intermediate = new DirectComputed(observable); IndirectComputed indirectComputed = new IndirectComputed(intermediate); observable.SourceProperty = 42; Assert.AreEqual(42, indirectComputed.ComputedProperty); WeakReference weakIndirectComputed = new WeakReference(indirectComputed); GC.Collect(); Assert.IsTrue(weakIndirectComputed.IsAlive, "Since we hold a strong reference to the dependent, the object should still be alive."); // This assertion here to make sure the dependent is not optimized away. Assert.AreEqual(42, indirectComputed.ComputedProperty); indirectComputed = null; GC.Collect(); Assert.IsFalse(weakIndirectComputed.IsAlive, "Since we released the strong reference to the dependent, the object should not be alive."); // Make sure we can still modify the observable, and that the intermediate still depends upon it. observable.SourceProperty = 32; Assert.AreEqual(32, observable.SourceProperty); Assert.AreEqual(32, intermediate.ComputedProperty); }
public void Initialize() { _source = new SourceData(); _intermediateComputed = new DirectComputed(_source); _computed = new IndirectComputed(_intermediateComputed); }