Exemplo n.º 1
0
        public void TestDeadSubscriber()
        {
            StringBuilder sb = new StringBuilder();

            var hub = new MsgBroker();

            var subscriberA = new TestSubscriber(sb);
            var subscriberB = new TestSubscriber(sb);

            hub.Subscribe <string>(subscriberA, s => sb.Append($"A({s})"));
            hub.Subscribe <string>(subscriberB, s => sb.Append($"B({s})"));

            hub.PublishObject("x");
            hub.PublishObject("y");

            Assert.That(sb.ToString(), Is.EqualTo("A(x)B(x)A(y)B(y)"));

            subscriberA = null;

            GC.Collect();

            sb.Clear();

            hub.PublishObject("x");
            hub.PublishObject("y");

            Assert.That(sb.ToString(), Is.EqualTo("B(x)B(y)"));

            subscriberB.Reset(); // make sure subscriberB stays alive
        }
Exemplo n.º 2
0
        void Check(MsgBroker hub, TestSubscriber subscriber, Action <MsgBroker> action, int expectedCalls, string expectedResult = null)
        {
            subscriber.Reset();

            action(hub);

            Assert.That(subscriber.Calls, Is.EqualTo(expectedCalls));

            if (expectedResult != null)
            {
                Assert.That(subscriber.Result, Is.EqualTo(expectedResult));
            }
        }