Ejemplo n.º 1
0
        public void RabbitMQ_deve_lancar_erro_ao_deserializar_messagem_de_tipos_diferentes()
        {
            var    guid      = Guid.NewGuid().ToString();
            string queueName = $"fila-teste-rabbitmq-{guid}";
            var    config    = new MessagingConfigBuilder("RabbitMQ", BrokerType.RabbitMQ, new Dictionary <string, string>()
            {
                { "HostName", ServerName }
            }).Create();

            // garantir que a fila tem 0 mensagens
            var vazia = Messaging.Dequeue(queueName, config);

            Assert.IsNull(vazia);

            // enviar um objeto Invoice
            Messaging.Enqueue(queueName, _invoiceMessage, config);

            // receber objeto convertendo pra AnotherClass
            Assert.ThrowsException <InvalidCastException>(() => Messaging.Dequeue <AnotherClass>(queueName, config));

            // garantir que ainda está na fila recebendo uma mensagem
            var recebida = Messaging.Dequeue(queueName, config);

            Assert.IsNotNull(recebida);
        }
Ejemplo n.º 2
0
        public void RabbitMQ_deve_consumir_mensagem_lancando_exception_e_verificar_que_ela_continua_na_fila()
        {
            var    guid      = Guid.NewGuid().ToString();
            string queueName = $"fila-teste-rabbitmq-{guid}";
            string message   = $"Mensagem que deve retornar {guid}";
            var    config    = new FileMessagingConfig(LoadFileConfig(Broker.RabbitMQ));

            Messaging.Enqueue(queueName, message, config);

            using (var receiver = new Messaging(config))
            {
                receiver.StartListening(queueName, (args) =>
                {
                    throw new Exception("Vai para fila de error.");
                });
                System.Threading.Thread.Sleep(100);
            }

            var received = Messaging.Dequeue(queueName, config);

            Assert.IsNull(received);

            var errorMessage = Messaging.Dequeue($"{queueName}-error", config);

            Assert.AreEqual($"Vai para fila de error.\r\n{message}", errorMessage);
        }
Ejemplo n.º 3
0
        public void Client_deve_ser_case_insensitive_ao_lidar_com_broker_name()
        {
            var fileContent = @"<?xml version='1.0' encoding='utf-8' ?>
                    <configuration><configSections>
                    <section name='MessagingConfigSection' type='Benner.Messaging.MessagingFileConfigSection, Benner.Messaging' />
                    </configSections><MessagingConfigSection>
                        <queues>
                            <queue name='teste-case-insensitive-brokername' broker='mockmq' />
                        </queues>
                        <brokerList default='MockMQ'>
                            <broker name='RabbitMQ' type='Benner.Messaging.RabbitMQConfig, Benner.Messaging'>
                                <add key='chave' value='valor' />
                            </broker>
                            <broker name='MockmQ' type='Benner.Messaging.Tests.MockMQConfig, Benner.Messaging.Tests'>
                                <add key='chave' value='valor' />
                            </broker>
                        </brokerList>
                    </MessagingConfigSection></configuration>";

            var path = Path.Combine(folder, "teste.config");

            File.AppendAllText(path, fileContent);

            string queueName = "teste-CASE-insensitive-BROKERname";
            var    sentMsg   = "Teste de broker case insensitive";

            var config = new FileMessagingConfig(path);

            Messaging.Enqueue(queueName, sentMsg, config);

            string received = Messaging.Dequeue(queueName, config);

            Assert.AreEqual(sentMsg, received);
        }
Ejemplo n.º 4
0
        public void EnqueueSingleMessage_and_StartListening_must_deal_with_generic_objects()
        {
            QueueMock.ClearAllQueues();

            var message = new Invoice
            {
                AccountReference    = Guid.NewGuid().ToString(),
                CurrencyUsed        = false,
                CustomerOrderNumber = int.MaxValue,
                ForeignRate         = 352.65m,
                InvoiceDate         = DateTime.Now,
                InvoiceNumber       = int.MaxValue,
                ItemsNet            = int.MinValue,
                ItemsTax            = 65645.98m,
                ItemsTotal          = 321564m,
                OrderNumber         = 321123,
            };

            var mockMQConfig = new MemoryMessageConfig(defaultBroker: "MockMQ", defaultBrokerConfigType: typeof(MockMQConfig));

            Messaging.Enqueue("invoices-b", message, mockMQConfig);

            var typedMessage = Messaging.Dequeue <Invoice>("invoices-b", mockMQConfig);

            Assert.AreEqual(message.AccountReference, typedMessage.AccountReference);
            Assert.AreEqual(message.CurrencyUsed, typedMessage.CurrencyUsed);
            Assert.AreEqual(message.CustomerOrderNumber, typedMessage.CustomerOrderNumber);
            Assert.AreEqual(message.ForeignRate, typedMessage.ForeignRate);
            Assert.AreEqual(message.InvoiceDate, typedMessage.InvoiceDate);
            Assert.AreEqual(message.InvoiceNumber, typedMessage.InvoiceNumber);
            Assert.AreEqual(message.ItemsNet, typedMessage.ItemsNet);
            Assert.AreEqual(message.ItemsTax, typedMessage.ItemsTax);
            Assert.AreEqual(message.ItemsTotal, typedMessage.ItemsTotal);
            Assert.AreEqual(message.OrderNumber, typedMessage.OrderNumber);
        }
Ejemplo n.º 5
0
        public void Configuracao_deve_devolver_broker_especifico_para_fila_informada()
        {
            var fileContent = @"<?xml version='1.0' encoding='utf-8' ?><configuration><configSections>
                    <section name='MessagingConfigSection' type='Benner.Messaging.MessagingFileConfigSection, Benner.Messaging' />
                    </configSections><MessagingConfigSection>
                    <queues>
                        <queue name='broker-especifico-para-fila-informada' broker='MockMQ' />
                    </queues>
                    <brokerList default='RabbitMQ'> 
                        <broker name='RabbitMQ' type='Benner.Messaging.RabbitMQConfig, Benner.Messaging'>
                            <add key='chave' value='valor' />
                        </broker>
                        <broker name='MockMQ' type='Benner.Messaging.Tests.MockMQConfig, Benner.Messaging.Tests'>
                            <add key='chave' value='valor' />
                        </broker>
                    </brokerList>
                    </MessagingConfigSection></configuration>";

            File.AppendAllText(_testeConfigPath, fileContent);

            string queueName = "broker-especifico-para-fila-informada";
            var    sentMsg   = "Teste automatizado da fila broker_especifico_para_fila_informada";

            var config = new FileMessagingConfig(_testeConfigPath);

            Messaging.Enqueue(queueName, sentMsg, config);
            string received = Messaging.Dequeue(queueName, config);

            Assert.AreEqual(sentMsg, received);
        }
Ejemplo n.º 6
0
        public void Client_deve_suportar_injecao_de_configuracao_sem_arquivo()
        {
            var mmc = new MemoryMessageConfig(defaultBroker: "MockMQ", defaultBrokerConfigType: typeof(MockMQConfig))
                      .WithQueue("teste1", "MockMQ")
                      .WithQueue("teste2", "RabbitMQ");

            string queueName = "teste1";
            string sentMsg   = "Teste de configuração em memória";


            Messaging.Enqueue(queueName, sentMsg, mmc);

            string received = "";

            using (var client = new Messaging(mmc))
            {
                client.StartListening(queueName, (args) =>
                {
                    received = args.AsString;
                    return(true);
                });
            }

            Assert.AreEqual(sentMsg, received);
        }
Ejemplo n.º 7
0
        public void ActiveMQ_deve_enviar_e_receber_mensagem()
        {
            var    guid      = Guid.NewGuid().ToString();
            string queueName = $"fila-teste-activemq-{guid}";
            string message   = $"Mensagem teste para ActiveMQ with guid {guid}";
            var    config    = new FileMessagingConfig(LoadFileConfig(Broker.ActiveMQ));

            Messaging.Enqueue(queueName, message, config);
            string received = Messaging.Dequeue(queueName, config);

            Assert.AreEqual(message, received);
        }
Ejemplo n.º 8
0
        public void ActiveMQ_deve_enviar_e_receber_mensagens_com_configuracao_em_memoria()
        {
            var    guid      = Guid.NewGuid().ToString();
            string queueName = $"fila-teste-activemq-{guid}";
            string message   = $"Mensagem que deve retornar {guid}";
            var    config    = new MessagingConfigBuilder("ActiveMQ", BrokerType.ActiveMQ, new Dictionary <string, string>()
            {
                { "Hostname", ServerName }
            })
                               .Create();

            Messaging.Enqueue(queueName, message, config);
            var received = Messaging.Dequeue(queueName, config);

            Assert.AreEqual(message, received);
        }
Ejemplo n.º 9
0
        public void AmazonSQS_deve_enviar_e_receber_mensagens_com_configuracao_em_memoria()
        {
            var    guid      = Guid.NewGuid().ToString();
            string queueName = $"fila-teste-amazonsqs-{guid}";
            string message   = $"Mensagem que deve retornar {guid}";
            var    config    = new MessagingConfigBuilder("AmazonSQS", BrokerType.AmazonSQS, new Dictionary <string, string>()
            {
                { "InvisibilityTime", "15" }
            })
                               .Create();

            Messaging.Enqueue(queueName, message, config);
            var received = Messaging.Dequeue(queueName, config);

            Assert.AreEqual(message, received);
        }
Ejemplo n.º 10
0
        public void RabbitMQ_deve_enviar_e_receber_objeto_serializado()
        {
            var    config    = new FileMessagingConfig(LoadFileConfig(Broker.RabbitMQ));
            string queueName = $"fila-teste-rabbitmq-{Guid.NewGuid()}";

            Messaging.Enqueue(queueName, _invoiceMessage, config);
            var typedMessage = Messaging.Dequeue <Invoice>(queueName, config);

            Assert.AreEqual(_invoiceMessage.AccountReference, typedMessage.AccountReference);
            Assert.AreEqual(_invoiceMessage.CurrencyUsed, typedMessage.CurrencyUsed);
            Assert.AreEqual(_invoiceMessage.CustomerOrderNumber, typedMessage.CustomerOrderNumber);
            Assert.AreEqual(_invoiceMessage.ForeignRate, typedMessage.ForeignRate);
            Assert.AreEqual(_invoiceMessage.InvoiceDate, typedMessage.InvoiceDate);
            Assert.AreEqual(_invoiceMessage.InvoiceNumber, typedMessage.InvoiceNumber);
            Assert.AreEqual(_invoiceMessage.ItemsNet, typedMessage.ItemsNet);
            Assert.AreEqual(_invoiceMessage.ItemsTax, typedMessage.ItemsTax);
            Assert.AreEqual(_invoiceMessage.ItemsTotal, typedMessage.ItemsTotal);
            Assert.AreEqual(_invoiceMessage.OrderNumber, typedMessage.OrderNumber);
        }
Ejemplo n.º 11
0
        public void Client_deve_permitir_receber_qualquer_IMessagingConfig()
        {
            string mensagem = "Mensagem para teste de fila";
            // por memoria
            var memoryConfig = new MemoryMessageConfig(defaultBroker: "MockMQ", defaultBrokerConfigType: typeof(MockMQConfig)).WithQueue("teste", "MockMQ");

            string receivedByMemory = "";

            Messaging.Enqueue("teste", mensagem, memoryConfig);
            using (var client = new Messaging(memoryConfig))
            {
                client.StartListening("teste", (args) =>
                {
                    receivedByMemory = args.AsString;
                    return(true);
                });
            }

            Assert.AreEqual(mensagem, receivedByMemory);

            // por arquivo
            var fileContent = @"<?xml version='1.0' encoding='utf-8' ?><configuration><configSections>
                    <section name='MessagingConfigSection' type='Benner.Messaging.MessagingFileConfigSection, Benner.Messaging' />
                    </configSections><MessagingConfigSection><queues><queue name='teste-arquivo' broker='MockMQ' /></queues><brokerList default='MockMQ'>
                    <broker name='RabbitMQ' type='Benner.Messaging.RabbitMQConfig, Benner.Messaging'><add key='chave' value='valor' /></broker>
                    <broker name='MockMQ' type='Benner.Messaging.Tests.MockMQConfig, Benner.Messaging.Tests'><add key='chave' value='valor' /></broker>
                    </brokerList></MessagingConfigSection></configuration>";

            var path = Path.Combine(folder, "teste.config");

            File.AppendAllText(path, fileContent);

            var fileConfig = new FileMessagingConfig(path);

            string receivedByFile = "";

            Messaging.Enqueue("teste-arquivo", mensagem, fileConfig);
            receivedByFile = Messaging.Dequeue("teste-arquivo", fileConfig);

            Assert.AreEqual(mensagem, receivedByFile);
        }
Ejemplo n.º 12
0
        public void RabbitMQ_deve_consumir_mensagem_retornando_false_e_verificar_que_ela_continua_na_fila()
        {
            var    guid      = Guid.NewGuid().ToString();
            string queueName = $"fila-teste-rabbitmq-{guid}";
            string message   = $"Mensagem que deve retornar {guid}";
            var    config    = new FileMessagingConfig(LoadFileConfig(Broker.RabbitMQ));

            Messaging.Enqueue(queueName, message, config);

            string received = "";
            bool   isFirst  = true;
            int    attempts = 0;

            using (var receiver = new Messaging(config))
            {
                bool shouldRun = true;
                receiver.StartListening(queueName, (args) =>
                {
                    if (shouldRun)
                    {
                        attempts++;
                        if (isFirst)
                        {
                            isFirst = false;
                            return(false);
                        }
                        received  = args.AsString;
                        shouldRun = false;
                        return(true);
                    }
                    return(true);
                });
                while (shouldRun)
                {
                    System.Threading.Thread.Sleep(20);
                }
            }
            Assert.AreEqual(message, received);
            Assert.AreEqual(2, attempts);
        }