public void Can_consume_messages_from_RabbitMQ_with_BasicGet() { RabbitMqConfig.UsingChannel(channel => { PublishHelloRabbit(channel); while (true) { var basicGetMsg = channel.BasicGet(MqQueueNames <HelloRabbit> .Direct, false); if (basicGetMsg == null) { Console.WriteLine("End of the road..."); return; } var msg = MqMessageExtensions.FromJsonBytes <HelloRabbit>(basicGetMsg.Body); Console.WriteLine(msg); Thread.Sleep(1000); channel.BasicAck(basicGetMsg.DeliveryTag, false); } }); }
public void TestFixtureTearDown() { string[] exchangeNames = { _exchange, _exchangeDlq, _exchangeTopic, _exchangeFanout, MqQueueNames.Exchange, MqQueueNames.ExchangeDlq, MqQueueNames.ExchangeTopic }; RabbitMqConfig.UsingChannel(channel => { foreach (var value in exchangeNames) { channel.ExchangeDelete(value); } channel.DeleteQueue <AlwaysThrows>(); channel.DeleteQueue <HelloRabbit>(); channel.DeleteQueue <Incr>(); channel.DeleteQueue <Reverse>(); channel.DeleteQueue <NothingHere>(); }); }
public void Does_process_messages_sent_before_it_was_started() { var reverseCalled = 0; using (var mqServer = RabbitMqServerTests.CreateMqServer()) { void Action0(IModel channel) { channel.PurgeQueue <Reverse>(); channel.PurgeQueue <ReverseResponse>(); } RabbitMqConfig.UsingChannel(mqServer.ConnectionFactory, Action0); mqServer.RegisterHandler <Reverse>(x => { Interlocked.Increment(ref reverseCalled); return(new ReverseResponse { Value = string.Join(",", x.GetBody().Value.Reverse()) }); }); using (var mqClient = mqServer.CreateMessageQueueClient()) { RabbitMqServerTests.Publish_4_messages(mqClient); mqServer.Start(); Thread.Sleep(2000); Assert.That(mqServer.GetStats().TotalMessagesProcessed, Is.EqualTo(4)); Assert.That(reverseCalled, Is.EqualTo(4)); } } }
public void Can_consume_messages_from_RabbitMQ_with_BasicConsume() { RabbitMqConfig.UsingChannel(channel => { var consumer = new RabbitMqBasicConsumer(channel); channel.BasicConsume(MqQueueNames <HelloRabbit> .Direct, false, consumer); string recvMsg = null; ExecUtils.ExecMultiThreading(1, () => PublishHelloRabbit(channel)); while (true) { try { var e = consumer.Queue.Dequeue(); Console.WriteLine("Dequeued"); recvMsg = e.Body.FromUtf8Bytes(); // ... process the message Console.WriteLine(recvMsg); channel.BasicAck(e.DeliveryTag, false); break; } catch (OperationInterruptedException) { // The consumer was removed, either through // channel or connection closure, or through the // action of IModel.BasicCancel(). Console.WriteLine("End of the road..."); break; } } Assert.That(recvMsg, Is.Not.Null); }); }
private static void RunHandlerOnMultipleThreads(int noOfThreads, int msgs) { using (var mqServer = CreateMqServer()) { var timesCalled = 0; RabbitMqConfig.UsingChannel(mqServer.ConnectionFactory, channel => channel.PurgeQueue <Wait>()); object WaitProcessMessageFn(IMqMessage <Wait> m) { Interlocked.Increment(ref timesCalled); Thread.Sleep(m.GetBody().ForMs); return(null); } mqServer.RegisterHandler <Wait>(WaitProcessMessageFn, noOfThreads); mqServer.Start(); using (var mqClient = mqServer.CreateMessageQueueClient()) { var dto = new Wait { ForMs = 100 }; for (var i1 = 0; i1 < msgs; i1++) { mqClient.Publish(dto); } Thread.Sleep(1000); Assert.That(timesCalled, Is.EqualTo(msgs)); } } }
public void Can_receive_and_process_same_reply_responses() { var called = 0; using (var mqServer = CreateMqServer()) { RabbitMqConfig.UsingChannel(mqServer.ConnectionFactory, channel => channel.PurgeQueue <Incr>()); Incr ProcessMessageFn(IMqMessage <Incr> m) { Console.WriteLine("In Incr #" + m.GetBody().Value); Interlocked.Increment(ref called); return(m.GetBody().Value > 0 ? new Incr { Value = m.GetBody().Value - 1 } : null); } mqServer.RegisterHandler <Incr>(ProcessMessageFn); mqServer.Start(); var incr = new Incr { Value = 5 }; using (var mqClient = mqServer.CreateMessageQueueClient()) { mqClient.Publish(incr); } Thread.Sleep(1000); Assert.That(called, Is.EqualTo(1 + incr.Value)); } }
public void Does_publish_to_dead_letter_exchange() { RabbitMqConfig.UsingChannel(channel => { PublishHelloRabbit(channel); var basicGetMsg = channel.BasicGet(MqQueueNames <HelloRabbit> .Direct, true); var dlqBasicMsg = channel.BasicGet(MqQueueNames <HelloRabbit> .Dlq, true); Assert.That(basicGetMsg, Is.Not.Null); Assert.That(dlqBasicMsg, Is.Null); PublishHelloRabbit(channel); basicGetMsg = channel.BasicGet(MqQueueNames <HelloRabbit> .Direct, false); Thread.Sleep(500); dlqBasicMsg = channel.BasicGet(MqQueueNames <HelloRabbit> .Dlq, false); Assert.That(basicGetMsg, Is.Not.Null); Assert.That(dlqBasicMsg, Is.Null); channel.BasicNack(basicGetMsg.DeliveryTag, false, false); Thread.Sleep(500); dlqBasicMsg = channel.BasicGet(MqQueueNames <HelloRabbit> .Dlq, true); Assert.That(dlqBasicMsg, Is.Not.Null); }); }
public void Publishing_message_with_routingKey_sends_only_to_registered_queue() { RabbitMqConfig.UsingChannel(channel => { PublishHelloRabbit(channel); var basicGetMsg = channel.BasicGet(MqQueueNames <HelloRabbit> .Direct, true); Assert.That(basicGetMsg, Is.Not.Null); }); }
public void TestFixtureTearDown() { RabbitMqConfig.UsingChannel(channel => { channel.ExchangeDelete(MqQueueNames.Exchange); channel.ExchangeDelete(MqQueueNames.ExchangeDlq); channel.ExchangeDelete(MqQueueNames.ExchangeTopic); channel.DeleteQueue <HelloIntro>(); channel.DeleteQueue <HelloIntroResponse>(); channel.DeleteQueues(ReplyToMq); }); }
public void TestFixtureTearDown() { RabbitMqConfig.UsingChannel(channel => { channel.ExchangeDelete(MqQueueNames.Exchange); channel.ExchangeDelete(MqQueueNames.ExchangeDlq); channel.ExchangeDelete(MqQueueNames.ExchangeTopic); channel.DeleteQueue <Reverse>(); channel.DeleteQueue <ReverseResponse>(); channel.DeleteQueue <NothingHere>(); channel.DeleteQueue <NothingHereResponse>(); channel.DeleteQueue <AlwaysThrows>(); }); }
public void TestFixtureTearDown() { MqQueueNames.SetQueuePrefix(QueuePrefix); RabbitMqConfig.UsingChannel(channel => { channel.ExchangeDelete(MqQueueNames.Exchange); channel.ExchangeDelete(MqQueueNames.ExchangeDlq); channel.ExchangeDelete(MqQueueNames.ExchangeTopic); channel.DeleteQueue <HelloIntro>(); channel.DeleteQueue <HelloIntroResponse>(); }); MqQueueNames.SetQueuePrefix(""); }
public void Can_interrupt_BasicConsumer_in_bgthread_by_closing_channel() { RabbitMqConfig.UsingChannel(channel => { string recvMsg = null; EndOfStreamException lastEx = null; var bgThread = new Thread(() => { try { var consumer = new RabbitMqBasicConsumer(channel); channel.BasicConsume(MqQueueNames <HelloRabbit> .Direct, false, consumer); while (true) { try { var e = consumer.Queue.Dequeue(); recvMsg = e.Body.FromUtf8Bytes(); } catch (EndOfStreamException ex) { // The consumer was cancelled, the model closed, or the // connection went away. Console.WriteLine("EndOfStreamException in bgthread: {0}", ex.Message); lastEx = ex; return; } catch (Exception ex) { Assert.Fail("Unexpected exception in bgthread: " + ex.Message); } } } catch (Exception ex) { Console.WriteLine("Exception in bgthread: {0}: {1}", ex.GetType().Name, ex.Message); } }) { Name = "Closing Channel Test", IsBackground = true }; bgThread.Start(); PublishHelloRabbit(channel); Thread.Sleep(100); // closing either throws EndOfStreamException in bgthread channel.Close(); // connection.Close(); Thread.Sleep(2000); Assert.That(recvMsg, Is.Not.Null); Assert.That(lastEx, Is.Not.Null); }); }
public void TestFixtureSetUp() { RabbitMqConfig.UsingChannel(channel => { channel.RegisterDirectExchange(_exchange); channel.RegisterDlqExchange(_exchangeDlq); channel.RegisterTopicExchange(_exchangeTopic); RegisterQueue(channel, MqQueueNames <HelloRabbit> .Direct); RegisterDlq(channel, MqQueueNames <HelloRabbit> .Dlq); RegisterTopic(channel, MqQueueNames <HelloRabbit> .Topic); RegisterQueue(channel, MqQueueNames <HelloRabbit> .Direct, _exchangeTopic); channel.PurgeQueue <HelloRabbit>(); }); }
public void Can_publish_messages_to_RabbitMQ() { RabbitMqConfig.UsingChannel(channel => { for (var i1 = 0; i1 < 5; i1++) { var payload = MqMessageExtensions.ToJsonBytes(new HelloRabbit { Name = $"World! #{i1}" }); var props = channel.CreateBasicProperties(); props.Persistent = true; channel.BasicPublish(_exchange, MqQueueNames <HelloRabbit> .Direct, props, payload); Console.WriteLine("Sent Message " + i1); Thread.Sleep(1000); } }); }
public void TestFixtureTearDown() { RabbitMqConfig.UsingChannel(channel => { channel.ExchangeDelete(MqQueueNames.Exchange); channel.ExchangeDelete(MqQueueNames.ExchangeDlq); channel.ExchangeDelete(MqQueueNames.ExchangeTopic); channel.DeleteQueue <Hello>(); channel.DeleteQueue <HelloResponse>(); channel.DeleteQueue <HelloNull>(); channel.DeleteQueue <Incr>(); channel.DeleteQueue <IncrResponse>(); channel.DeleteQueue <Reverse>(); channel.DeleteQueue <ReverseResponse>(); channel.DeleteQueue <NothingHere>(); channel.DeleteQueue <NothingHereResponse>(); channel.DeleteQueue <Wait>(); }); }
public void Publishing_message_to_fanout_exchange_publishes_to_all_queues() { RabbitMqConfig.UsingChannel(channel => { channel.RegisterFanoutExchange(_exchangeFanout); RegisterQueue(channel, MqQueueNames <HelloRabbit> .Direct, _exchangeFanout); var body = MqMessageExtensions.ToJsonBytes(new HelloRabbit { Name = "World!" }); var props = channel.CreateBasicProperties(); props.Persistent = true; channel.BasicPublish(_exchangeFanout, MqQueueNames <HelloRabbit> .Direct, props, body); var basicGetMsg = channel.BasicGet(MqQueueNames <HelloRabbit> .Direct, true); Assert.That(basicGetMsg, Is.Not.Null); }); }
public void Can_receive_and_process_standard_request_reply_combo() { using (var mqServer = CreateMqServer()) { void Action0(IModel channel) { channel.PurgeQueue <Hello>(); channel.PurgeQueue <HelloResponse>(); } RabbitMqConfig.UsingChannel(mqServer.ConnectionFactory, Action0); string messageReceived = null; mqServer.RegisterHandler <Hello>(m => new HelloResponse { Result = "Hello, " + m.GetBody().Name }); mqServer.RegisterHandler <HelloResponse>(m => { messageReceived = m.GetBody().Result; return(null); }); mqServer.Start(); using (var mqClient = mqServer.CreateMessageQueueClient()) { var dto = new Hello { Name = "TheOne" }; mqClient.Publish(dto); Thread.Sleep(1000); Assert.That(messageReceived, Is.EqualTo("Hello, TheOne")); } } }
public void Does_process_all_messages_and_Starts_Stops_correctly_with_multiple_threads_racing() { using (var mqServer = RabbitMqServerTests.CreateMqServer()) { void Action0(IModel channel) { channel.PurgeQueue <Reverse>(); channel.PurgeQueue <NothingHere>(); } RabbitMqConfig.UsingChannel(mqServer.ConnectionFactory, Action0); var reverseCalled = 0; var nothingHereCalled = 0; mqServer.RegisterHandler <Reverse>(x => { Console.WriteLine("Processing Reverse {0}...", Interlocked.Increment(ref reverseCalled)); return(new ReverseResponse { Value = string.Join(",", x.GetBody().Value.Reverse()) }); }); mqServer.RegisterHandler <NothingHere>(x => { Console.WriteLine("Processing NothingHere {0}...", Interlocked.Increment(ref nothingHereCalled)); return(new NothingHereResponse { Value = x.GetBody().Value }); }); using (var mqClient = mqServer.CreateMessageQueueClient()) { mqClient.Publish(new Reverse { Value = "Hello" }); mqClient.Publish(new Reverse { Value = "World" }); mqClient.Publish(new NothingHere { Value = "HelloWorld" }); mqServer.Start(); Thread.Sleep(1500); Assert.That(mqServer.GetStatus(), Is.EqualTo("Started")); Assert.That(mqServer.GetStats().TotalMessagesProcessed, Is.EqualTo(3)); mqClient.Publish(new Reverse { Value = "Foo" }); mqClient.Publish(new NothingHere { Value = "Bar" }); ExecUtils.ExecMultiThreading(10, () => mqServer.Start()); Assert.That(mqServer.GetStatus(), Is.EqualTo("Started")); ExecUtils.ExecMultiThreading(5, () => mqServer.Stop()); Assert.That(mqServer.GetStatus(), Is.EqualTo("Stopped")); ExecUtils.ExecMultiThreading(10, () => mqServer.Start()); Assert.That(mqServer.GetStatus(), Is.EqualTo("Started")); Console.WriteLine("\n" + mqServer.GetStats()); Assert.That(mqServer.GetStats().TotalMessagesProcessed, Is.GreaterThanOrEqualTo(5)); Assert.That(reverseCalled, Is.EqualTo(3)); Assert.That(nothingHereCalled, Is.EqualTo(2)); } } }
public void Does_retry_messages_with_errors_by_RetryCount() { var retryCount = 1; // in total, inc. first try var totalRetries = 1 + retryCount; using (var mqServer = RabbitMqServerTests.CreateMqServer(retryCount)) { void Action0(IModel channel) { channel.PurgeQueue <Reverse>(); channel.PurgeQueue <NothingHere>(); channel.PurgeQueue <AlwaysThrows>(); } RabbitMqConfig.UsingChannel(mqServer.ConnectionFactory, Action0); var reverseCalled = 0; var nothingHereCalled = 0; mqServer.RegisterHandler <Reverse>(x => { Interlocked.Increment(ref reverseCalled); return(new ReverseResponse { Value = string.Join(",", x.GetBody().Value.Reverse()) }); }); mqServer.RegisterHandler <NothingHere>(x => { Interlocked.Increment(ref nothingHereCalled); return(new NothingHereResponse { Value = x.GetBody().Value }); }); mqServer.RegisterHandler <AlwaysThrows>(x => throw new Exception("Always Throwing! " + x.GetBody().Value)); mqServer.Start(); using (var mqClient = mqServer.CreateMessageQueueClient()) { mqClient.Publish(new AlwaysThrows { Value = "1st" }); mqClient.Publish(new Reverse { Value = "Hello" }); mqClient.Publish(new Reverse { Value = "World" }); mqClient.Publish(new NothingHere { Value = "TheOne" }); Thread.Sleep(1000); Assert.That(mqServer.GetStats().TotalMessagesFailed, Is.EqualTo(1 * totalRetries)); Assert.That(mqServer.GetStats().TotalMessagesProcessed, Is.EqualTo(2 + 1)); for (var i = 0; i < 5; i++) { mqClient.Publish(new AlwaysThrows { Value = "#" + i }); } mqClient.Publish(new Reverse { Value = "Hello" }); mqClient.Publish(new Reverse { Value = "World" }); mqClient.Publish(new NothingHere { Value = "TheOne" }); } Console.WriteLine(mqServer.GetStatsDescription()); Thread.Sleep(1000); Assert.That(mqServer.GetStats().TotalMessagesFailed, Is.EqualTo((1 + 5) * totalRetries)); Assert.That(mqServer.GetStats().TotalMessagesProcessed, Is.EqualTo(6)); Assert.That(reverseCalled, Is.EqualTo(2 + 2)); Assert.That(nothingHereCalled, Is.EqualTo(1 + 1)); } }
public void Can_filter_published_and_received_messages() { string receivedMsgApp = null; string receivedMsgType = null; RabbitMqExtensions.CreateQueueFilter = (s, args) => { if (s == MqQueueNames <Hello> .Direct) { args.Remove(RabbitMqExtensions.XMaxPriority); } }; using (var mqServer = CreateMqServer()) { mqServer.PublishMessageFilter = (queueName, properties, msg) => { properties.AppId = $"app:{queueName}"; }; mqServer.GetMessageFilter = (queueName, basicMsg) => { var props = basicMsg.BasicProperties; // automatically added by RabbitMqProducer receivedMsgType = props.Type; receivedMsgApp = props.AppId; }; mqServer.RequestFilter = message => { Console.WriteLine(message); return(message); }; mqServer.ResponseFilter = o => { Console.WriteLine(o); return(o); }; mqServer.RegisterHandler <Hello>(m => { return(new HelloResponse { Result = string.Format("Hello, {0}!", m.GetBody().Name) }); }); mqServer.Start(); using (var mqClient = mqServer.CreateMessageQueueClient()) { mqClient.Publish(new Hello { Name = "Bugs Bunny" }); } Thread.Sleep(100); Assert.That(receivedMsgApp, Is.EqualTo(string.Format("app:{0}", MqQueueNames <Hello> .Direct))); Assert.That(receivedMsgType, Is.EqualTo(typeof(Hello).Name)); RabbitMqConfig.UsingChannel(channel => { var queueName = MqQueueNames <HelloResponse> .Direct; channel.RegisterQueue(queueName); var basicMsg = channel.BasicGet(queueName, true); var props = basicMsg.BasicProperties; Assert.That(props.Type, Is.EqualTo(typeof(HelloResponse).Name)); Assert.That(props.AppId, Is.EqualTo($"app:{queueName}")); var msg = basicMsg.ToMessage <HelloResponse>(); Assert.That(msg.GetBody().Result, Is.EqualTo("Hello, Bugs Bunny!")); }); } }