Esempio n. 1
0
        public void SubscribePublishMessagesInheritanceTest()
        {
            var listener1 = new Listener1();

            _messageBus.Subscribe(listener1);

            var listener2 = new Listener2();

            _messageBus.Subscribe(listener2);

            var message1 = new Message1();

            _messageBus.Publish(message1);

            var message2 = new Message2();

            _messageBus.Publish(message2);

            Assert.That(listener1.ProcessedMessages, Has.Exactly(2).Items);
            Assert.AreSame(message1, listener1.ProcessedMessages[0]);
            Assert.AreSame(message2, listener1.ProcessedMessages[1]);

            Assert.That(listener2.ProcessedMessages, Has.Exactly(1).Items);
            Assert.AreSame(message2, listener2.ProcessedMessages[0]);
        }
Esempio n. 2
0
        public void PublishFromHandlerNotRecursiveTest()
        {
            var listener = new Listener6(_messageBus);

            _messageBus.Subscribe(listener);

            var anotherListener = new Listener1();

            _messageBus.Subscribe(anotherListener);

            var message = new Message1();

            Assert.DoesNotThrow(() => _messageBus.Publish(message));

            Assert.That(listener.ProcessedMessages, Has.Exactly(1).Items);
            Assert.AreSame(message, listener.ProcessedMessages[0]);

            Assert.That(anotherListener.ProcessedMessages, Has.Exactly(1).Items);
            Assert.AreSame(message, anotherListener.ProcessedMessages[0]);

            var anotherMessage = new Message1();

            _messageBus.Publish(anotherMessage);

            Assert.That(listener.ProcessedMessages, Has.Exactly(2).Items);
            Assert.AreSame(message, listener.ProcessedMessages[0]);
            Assert.AreSame(anotherMessage, listener.ProcessedMessages[1]);

            Assert.That(anotherListener.ProcessedMessages, Has.Exactly(2).Items);
            Assert.AreSame(message, anotherListener.ProcessedMessages[0]);
            Assert.AreSame(anotherMessage, anotherListener.ProcessedMessages[1]);
        }
Esempio n. 3
0
        public void SubscribePublishGCTest()
        {
            WeakReference <Listener1> CreateWeakReferenceToListener()
            {
                var listener = new Listener1();

                _messageBus.Subscribe(listener);
                return(new WeakReference <Listener1>(listener));
            }

            var reference = CreateWeakReferenceToListener();

            GC.Collect();

            if (reference.TryGetTarget(out _))
            {
                Assert.Fail();
            }

            Assert.DoesNotThrow(() => _messageBus.Publish(new Message1()));

            if (reference.TryGetTarget(out _))
            {
                Assert.Fail();
            }
        }
Esempio n. 4
0
        public void SubscribePublishBasicTest()
        {
            var listener = new Listener1();

            _messageBus.Subscribe(listener);

            var message = new Message1();

            _messageBus.Publish(message);

            Assert.That(listener.ProcessedMessages, Has.Exactly(1).Items);
            Assert.AreSame(message, listener.ProcessedMessages[0]);
        }
Esempio n. 5
0
        public void DoubleUnsubscribeTest()
        {
            var listener = new Listener1();

            _messageBus.Subscribe(listener);
            _messageBus.Unsubscribe(listener);

            Assert.DoesNotThrow(() => _messageBus.Unsubscribe(listener));

            _messageBus.Publish(new Message1());

            Assert.IsEmpty(listener.ProcessedMessages);
        }
Esempio n. 6
0
        public void DoubleSubscribeTest()
        {
            var listener = new Listener1();

            _messageBus.Subscribe(listener);

            Assert.DoesNotThrow(() => _messageBus.Subscribe(listener));

            var message = new Message1();

            _messageBus.Publish(message);

            Assert.That(listener.ProcessedMessages, Has.Exactly(1).Items);
            Assert.AreSame(message, listener.ProcessedMessages[0]);
        }