Beispiel #1
0
        WeakReference SubscribeUnsubsribeCoreAlloc(EventOwner e, object sender, PropertyChangedEventArgs args, Action raise,
                                                   Action <EventOwner, EventArgs> raiseMyEvent1, Action <EventOwner, object, EventArgs> raiseMyEvent2,
                                                   Action <Subscriber, EventOwner> subscribe, Action <Subscriber, EventOwner> subscribeTwice, Action <Subscriber, EventOwner> unsubscribe)
        {
            var s = new Subscriber();

            subscribe(s, e);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(1, s.Count);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(2, s.Count);

            s.Clear();
            subscribe(s, e);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(2, s.Count);

            s.Clear();
            unsubscribe(s, e);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(1, s.Count);

            s.Clear();
            unsubscribe(s, e);
            raise();
            Assert.AreEqual(null, s.Sender);
            Assert.AreEqual(null, s.E);
            Assert.AreEqual(0, s.Count);

            s.Clear();
            subscribeTwice(s, e);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(2, s.Count);
            unsubscribe(s, e);
            unsubscribe(s, e);

            unsubscribe(s, e);
            subscribe(s, e);
            return(new WeakReference(s));
        }
        void SubscribeUnsubsribeCore(bool passSender, Action <EventOwner, EventArgs> raiseMyEvent1, Action <EventOwner, object, EventArgs> raiseMyEvent2,
                                     Action <Subscriber, EventOwner> subscribe, Action <Subscriber, EventOwner> subscribeTwice, Action <Subscriber, EventOwner> unsubscribe)
        {
            var    e      = new EventOwner();
            var    s      = new Subscriber();
            var    sender = passSender ? new object() : e;
            var    args   = new PropertyChangedEventArgs("Test");
            Action raise  = () => {
                if (passSender)
                {
                    raiseMyEvent2(e, sender, args);
                }
                else
                {
                    raiseMyEvent1(e, args);
                }
            };

            subscribe(s, e);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(1, s.Count);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(2, s.Count);

            s.Clear();
            subscribe(s, e);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(2, s.Count);

            s.Clear();
            unsubscribe(s, e);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(1, s.Count);

            s.Clear();
            unsubscribe(s, e);
            raise();
            Assert.AreEqual(null, s.Sender);
            Assert.AreEqual(null, s.E);
            Assert.AreEqual(0, s.Count);

            s.Clear();
            subscribeTwice(s, e);
            raise();
            Assert.AreEqual(sender, s.Sender);
            Assert.AreEqual(args, s.E);
            Assert.AreEqual(2, s.Count);
            unsubscribe(s, e);
            unsubscribe(s, e);

            unsubscribe(s, e);
            subscribe(s, e);
            WeakReference sRef = new WeakReference(s);

            s = null;
            MemoryLeaksHelper.CollectOptional(sRef);
            MemoryLeaksHelper.EnsureCollected(sRef);
            raise();
        }