public void ShouldHandleEventWhenBothReferencesAreAlive() { var alarm = new Alarm(); var sleepy = new Sleepy(alarm); alarm.Beep(); alarm.Beep(); Assert.AreEqual(2, sleepy.SnoozeCount); }
public void SubscriberShouldNotBeUnsubscribedUntilCollection() { var alarm = new Alarm(); var sleepy = new Sleepy(alarm); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); alarm.Beep(); alarm.Beep(); Assert.AreEqual(2, sleepy.SnoozeCount); }
public void ShouldAllowSubscriberReferenceToBeCollected() { var alarm = new Alarm(); var sleepyReference = (null as WeakReference); new Action(() => { // Run this in a delegate to that the local variable gets garbage collected var sleepy = new Sleepy(alarm); alarm.Beep(); alarm.Beep(); Assert.AreEqual(2, sleepy.SnoozeCount); sleepyReference = new WeakReference(sleepy); })(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Assert.IsNull(sleepyReference.Target); }