コード例 #1
0
        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);
            });
        }
コード例 #2
0
        public void Only_allows_1_BgThread_to_run_at_a_time()
        {
            using (var mqServer = CreateMqServer()) {
                mqServer.RegisterHandler <Reverse>(x => {
                    return(new ReverseResponse {
                        Value = string.Join(",", x.GetBody().Value.Reverse())
                    });
                });
                mqServer.RegisterHandler <NothingHere>(x => {
                    return(new NothingHereResponse {
                        Value = x.GetBody().Value
                    });
                });

                ExecUtils.ExecMultiThreading(5, () => mqServer.Start());
                Assert.That(mqServer.GetStatus(), Is.EqualTo("Started"));
                Assert.That(mqServer.BgThreadCount, Is.EqualTo(1));

                ExecUtils.ExecMultiThreading(10, () => mqServer.Stop());
                Assert.That(mqServer.GetStatus(), Is.EqualTo("Stopped"));

                ExecUtils.ExecMultiThreading(1, () => mqServer.Start());
                Assert.That(mqServer.GetStatus(), Is.EqualTo("Started"));
                Assert.That(mqServer.BgThreadCount, Is.EqualTo(2));

                Console.WriteLine(mqServer.GetStats());
            }
        }
コード例 #3
0
        public void Can_consume_messages_with_BasicConsumer()
        {
            RabbitMqConfig.UsingChannel(channel => {
                OperationInterruptedException lastEx = null;

                channel.Close();

                void CallBack()
                {
                    try {
                        PublishHelloRabbit(channel);
                    } catch (Exception ex) {
                        lastEx = ex as OperationInterruptedException;
                        Console.WriteLine("Caught {0}: {1}", ex.GetType().Name, ex);
                    }
                }

                ExecUtils.ExecMultiThreading(1, CallBack);
                Assert.That(lastEx, Is.Not.Null);
            });
        }
コード例 #4
0
        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));
                }
            }
        }