public void DisposeUnregistersCallback()
        {
            var queueName    = "My.QueueName";
            var topicFilters = new List <string> {
                "My.*.Filter", "MVM.#"
            };
            var context = new InMemoryContext();
            var target  = new InMemoryMessageReceiver(context, queueName, topicFilters);

            target.StartReceivingMessages();
            EventMessageReceivedCallback callback = m => { };

            target.StartHandlingMessages(callback);

            // Check Arrange
            var queue = context.Connection.GetNamedMessageQueue(queueName);

            Assert.AreEqual(true, queue.HasConsumer);

            // Act
            target.Dispose();

            // Assert
            queue = context.Connection.GetNamedMessageQueue(queueName);
            Assert.AreEqual(false, queue.HasConsumer);
        }
        public void CannotCall_StartHandlingMessages_Twice()
        {
            var queueName    = "My.QueueName";
            var topicFilters = new List <string> {
                "My.*.Filter", "MVM.#"
            };
            var context = new InMemoryContext();
            var target  = new InMemoryMessageReceiver(context, queueName, topicFilters);

            target.StartReceivingMessages();
            target.StartHandlingMessages(em => { });

            Action act = () =>
            {
                target.StartHandlingMessages(em => { });
            };

            var ex = Assert.ThrowsException <BusException>(act);

            Assert.AreEqual("Cannot call 'StartHandlingMessages' multiple times.",
                            ex.Message);
        }
        public void CannotCall_StartReceivingMessages_WithoutCallingStartReceivingMessagesFirst()
        {
            var queueName    = "My.QueueName";
            var topicFilters = new List <string> {
                "My.*.Filter", "MVM.#"
            };
            var context = new InMemoryContext();
            var target  = new InMemoryMessageReceiver(context, queueName, topicFilters);

            Action act = () =>
            {
                target.StartHandlingMessages(em => { });
            };

            var ex = Assert.ThrowsException <BusException>(act);

            Assert.AreEqual("Before calling 'StartHandlingMessages', call 'StartReceivingMessages' first to declare queue and topics.",
                            ex.Message);
        }
        public async Task StartHandlingMessagesHandlesMessages()
        {
            var queueName    = "My.QueueName";
            var topicFilters = new List <string> {
                "My.*.Filter", "MVM.#"
            };
            var context = new InMemoryContext();
            var target  = new InMemoryMessageReceiver(context, queueName, topicFilters);

            target.StartReceivingMessages();

            EventMessage receivedMessage = null;

            target.StartHandlingMessages(m => { receivedMessage = m; });

            var queue = context.Connection.GetNamedMessageQueue(queueName);
            await queue.PublishAsync(new EventMessage { Topic = "Some.Topic" });

            Assert.AreEqual("Some.Topic", receivedMessage.Topic);
        }
        public void CannotCall_StartReceivingMessages_AfterDispose()
        {
            var queueName    = "My.QueueName";
            var topicFilters = new List <string> {
                "My.*.Filter", "MVM.#"
            };
            var context = new InMemoryContext();
            var target  = new InMemoryMessageReceiver(context, queueName, topicFilters);

            target.StartReceivingMessages();
            target.Dispose();

            Action act = () =>
            {
                target.StartHandlingMessages(em => { });
            };

            var ex = Assert.ThrowsException <ObjectDisposedException>(act);

            Assert.AreEqual("Cannot access a disposed object.\r\nObject name: 'InMemoryMessageReceiver'.",
                            ex.Message);
        }