Esempio n. 1
0
 public void PerTestSetUp()
 {
     EventReceiver1 = new EventReceiver1();
     EventReceiver2 = new EventReceiver2();
     _myTestHandler = new WeakEvent<EventHandler<EventArgs1>>();
 }
Esempio n. 2
0
        public void IgnoresGarbageCollectedListeners()
        {
            WeakEvent<EventHandler<EventArgs1>> handler = new WeakEvent<EventHandler<EventArgs1>>();

            EventReceiver1 receiver = new EventReceiver1();
            WeakReference receiverReference = new WeakReference(receiver);

            EventArgs1 args = new EventArgs1(2);
            object sender = new object();

            handler.Add(receiver.Callback);

            handler.Raise(sender, args);

            // ReSharper disable RedundantAssignment
            receiver = null;
            // ReSharper restore RedundantAssignment

            GC.Collect();

            Assert.That(receiverReference.IsAlive, Is.False);

            handler.Raise(sender, args);

            Assert.That(handler.ListenersCount, Is.EqualTo(0));
        }
Esempio n. 3
0
        public void LetsPublishersBeGarbageCollected()
        {
            Publisher1 pub = new Publisher1();

            WeakReference pubReference = new WeakReference(pub);

            EventReceiver1 receiver1 = new EventReceiver1();

            pub.MyTestEvent += receiver1.Callback;

            pub.Publish();

            Assert.That(receiver1.CallCounter, Is.EqualTo(1));

            // ReSharper disable RedundantAssignment
            pub = null;
            // ReSharper restore RedundantAssignment

            GC.Collect();

            Assert.That(pubReference.IsAlive, Is.False);
        }