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);
        }
        public void ShouldHandleEventWhenBothReferencesAreAlive()
        {
            var alarm = new Alarm();
            var sleepy = new Sleepy(alarm);
            alarm.Beep();
            alarm.Beep();

            Assert.AreEqual(2, sleepy.SnoozeCount);
        }
 public Sleepy(Alarm alarm)
 {
     _alarm = alarm;
     _alarm.Beeped += new WeakEventHandler<PropertyChangedEventArgs>(Alarm_Beeped).Handler;
 }
        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);
        }