Example #1
0
        public void Bus_PublishedMessage_FilterByHeader()
        {
            Person actualT1 = new Person
                {
                    Id = 5
                };

            Person actualT2 = new Person
                {
                    Id = 15
                };

            Person b1 = null, b2 = null, c1 = null;

            const string header = "type";

            ManualResetEvent ev1 = new ManualResetEvent(false), ev2 = new ManualResetEvent(false), ev3 = new ManualResetEvent(false);

            using (MessageBus.Core.RabbitMQBus busA = new MessageBus.Core.RabbitMQBus(), busB = new MessageBus.Core.RabbitMQBus(), busC = new MessageBus.Core.RabbitMQBus())
            {
                using (ISubscriber subscriberB1 = busB.CreateSubscriber(), subscriberB2 = busB.CreateSubscriber(), subscriberC1 = busC.CreateSubscriber())
                {
                    subscriberB1.Subscribe<Person>(p => { b1 = p; ev1.Set(); }, filter: new[] { new BusHeader { Name = header, Value = "T1"} });
                    subscriberB2.Subscribe<Person>(p => { b2 = p; ev2.Set(); }, filter: new[] { new BusHeader { Name = header, Value = "T2"} });
                    subscriberC1.Subscribe<Person>(p => { c1 = p; ev3.Set(); }, filter: new[] { new BusHeader { Name = header, Value = "T1" } });

                    subscriberB1.Open();
                    subscriberB2.Open();
                    subscriberC1.Open();

                    using (IPublisher publisher = busA.CreatePublisher())
                    {
                        BusMessage<Person> m1 = new BusMessage<Person> {Data = actualT1};
                        m1.Headers.Add(new BusHeader { Name = header, Value = "T1"});

                        publisher.Send(m1);

                        BusMessage<Person> m2 = new BusMessage<Person> { Data = actualT2 };
                        m2.Headers.Add(new BusHeader { Name = header, Value = "T2" });

                        publisher.Send(m2);
                    }

                    bool wait = ev1.WaitOne(TimeSpan.FromSeconds(5)) &&
                                ev2.WaitOne(TimeSpan.FromSeconds(5)) &&
                                ev3.WaitOne(TimeSpan.FromSeconds(5));

                    wait.Should().BeTrue("Message should arrive to all subscribers");

                    b1.Should().NotBeNull();
                    b2.Should().NotBeNull();
                    c1.Should().NotBeNull();

                    b1.ShouldBeEquivalentTo(actualT1);
                    b2.ShouldBeEquivalentTo(actualT2);
                    c1.ShouldBeEquivalentTo(actualT1);

                }
            }
        }
Example #2
0
        public void Bus_PublishedMessage_FilterByHeader()
        {
            Person actualT1 = new Person
                {
                    Id = 5
                };

            Person actualT2 = new Person
                {
                    Id = 15
                };

            const string header = "type";

            using (MessageBus.Core.RabbitMQBus busA = new MessageBus.Core.RabbitMQBus(), busB = new MessageBus.Core.RabbitMQBus(), busC = new MessageBus.Core.RabbitMQBus())
            {
                using (IReceiver receiverB1 = busB.CreateReceiver(), receiverB2 = busB.CreateReceiver(), receiverC1 = busC.CreateReceiver())
                {
                    receiverB1.Subscribe<Person>(filter: new[] { new BusHeader { Name = header, Value = "T1" } });
                    receiverB2.Subscribe<Person>(filter: new[] { new BusHeader { Name = header, Value = "T2" } });
                    receiverC1.Subscribe<Person>(filter: new[] { new BusHeader { Name = header, Value = "T1" } });

                    receiverB1.Open();
                    receiverB2.Open();
                    receiverC1.Open();

                    using (IPublisher publisher = busA.CreatePublisher())
                    {
                        BusMessage<Person> m1 = new BusMessage<Person> { Data = actualT1 };
                        m1.Headers.Add(new BusHeader { Name = header, Value = "T1" });

                        publisher.Send(m1);

                        BusMessage<Person> m2 = new BusMessage<Person> { Data = actualT2 };
                        m2.Headers.Add(new BusHeader { Name = header, Value = "T2" });

                        publisher.Send(m2);
                    }

                    Person b1 = receiverB1.Receive<Person>();
                    Person b2 = receiverB2.Receive<Person>();
                    Person c1 = receiverC1.Receive<Person>();

                    b1.Should().NotBeNull();
                    b2.Should().NotBeNull();
                    c1.Should().NotBeNull();

                    b1.ShouldBeEquivalentTo(actualT1);
                    b2.ShouldBeEquivalentTo(actualT2);
                    c1.ShouldBeEquivalentTo(actualT1);
                }
            }
        }
Example #3
0
        public void RabbitMQBus_Dialog_WithOrderedDelivery_AllSubscriptionTypes()
        {
            using (MessageBus.Core.RabbitMQBus entityA = new MessageBus.Core.RabbitMQBus(), entityB = new MessageBus.Core.RabbitMQBus())
            {
                Data messageA = new Person
                    {
                        Id = 5
                    };

                Data messageB = new Car
                    {
                        Number = "39847239847"
                    };

                List<Data> received = new List<Data>();

                ManualResetEvent ev1 = new ManualResetEvent(false), ev2 = new ManualResetEvent(false);

                using (ISubscriber subscriberA = entityA.CreateSubscriber(), subscriberB = entityB.CreateSubscriber())
                {
                    subscriberA.Subscribe<Data>(received.Add, true);

                    subscriberA.Subscribe(typeof(OK), (Action<object>) (data => ev1.Set()));

                    subscriberB.Subscribe((Action<OK>) (data => ev2.Set()));

                    subscriberA.Open();
                    subscriberB.Open();

                    using (IPublisher publisher = entityB.CreatePublisher())
                    {
                        publisher.Send(messageA);

                        publisher.Send(messageB);

                        publisher.Send(new OK());
                    }

                    using (IPublisher publisher = entityA.CreatePublisher())
                    {
                        publisher.Send(new OK());
                    }

                    bool waitOne = ev1.WaitOne(TimeSpan.FromSeconds(20)) && ev2.WaitOne(TimeSpan.FromSeconds(20));

                    waitOne.Should().BeTrue("Message not received");

                    received.Should().ContainInOrder(messageA, messageB);
                }
            }
        }
Example #4
0
        public void Bus_PublishedMessage_EachSubscriberGetsMessageCopy()
        {
            Person actual = new Person
                {
                    Id = 5
                };

            Person b1 = null, b2 = null, c1 = null;

            ManualResetEvent ev1 = new ManualResetEvent(false), ev2 = new ManualResetEvent(false), ev3 = new ManualResetEvent(false);

            using (MessageBus.Core.RabbitMQBus busA = new MessageBus.Core.RabbitMQBus(), busB = new MessageBus.Core.RabbitMQBus(), busC = new MessageBus.Core.RabbitMQBus())
            {
                using (ISubscriber subscriberB1 = busB.CreateSubscriber(), subscriberB2 = busB.CreateSubscriber(), subscriberC1 = busC.CreateSubscriber())
                {
                    subscriberB1.Subscribe<Person>(p => { b1 = p; ev1.Set(); });
                    subscriberB2.Subscribe<Person>(p => { p.Id *= 2; b2 = p; ev2.Set(); });
                    subscriberC1.Subscribe<Person>(p => { c1 = p; ev3.Set(); });

                    subscriberB1.Open();
                    subscriberB2.Open();
                    subscriberC1.Open();

                    using (IPublisher publisher = busA.CreatePublisher())
                    {
                        publisher.Send(actual);
                    }

                    bool wait = ev1.WaitOne(TimeSpan.FromSeconds(5)) &&
                                ev2.WaitOne(TimeSpan.FromSeconds(5)) &&
                                ev3.WaitOne(TimeSpan.FromSeconds(5));

                    wait.Should().BeTrue("Message should arrive to all subscribers");

                    b1.Should().NotBeNull();
                    b2.Should().NotBeNull();
                    c1.Should().NotBeNull();

                    b1.ShouldBeEquivalentTo(actual);
                    b2.ShouldBeEquivalentTo(new Person
                        {
                            Id = actual.Id * 2
                        });
                    c1.ShouldBeEquivalentTo(actual);

                }
            }
        }
        public void Bus_SendReceive_BunchOfMessages()
        {
            //RabbitMQConnectionString connectionString = new RabbitMQConnectionString(new Uri("amqp://*****:*****@10.0.8.100"));

            using (RabbitMQBus entityA = new RabbitMQBus(), entityB = new RabbitMQBus())
            {
                Data messageA = new Person
                    {
                        Id = 5
                    };

                ManualResetEvent ev1 = new ManualResetEvent(false);

                int counter = 0;

                using (ISubscriber subscriberA = entityA.CreateSubscriber())
                {
                    subscriberA.Subscribe((Action<Data>)(d => Counter(ref counter)), true);

                    subscriberA.Subscribe(typeof(OK), (Action<object>)(data => ev1.Set()));

                    subscriberA.Open();

                    const int expected = 100000;

                    using (IPublisher publisher = entityB.CreatePublisher())
                    {
                        for (int i = 0; i < expected; i++)
                        {
                            publisher.Send(messageA);
                        }

                        publisher.Send(new OK());
                    }

                    bool waitOne = ev1.WaitOne(TimeSpan.FromSeconds(20));

                    waitOne.Should().BeTrue("Message not received");

                    counter.Should().Be(expected);
                }
            }
        }
        public void Bus_MessageBaseSubscription_CallReceived()
        {
            ManualResetEvent ev = new ManualResetEvent(false);

            MessageBasedImplementation implementation = new MessageBasedImplementation(ev);

            using (IBus bus = new MessageBus.Core.RabbitMQBus())
            {
                using (ISubscription subscription = bus.RegisterSubscription(implementation, c => c.SetReceiveSelfPublish()))
                {
                    subscription.Open();

                    using (IPublisher publisher = bus.CreatePublisher())
                    {
                        Person person = new Person {Id = 5};

                        BusMessage<Person> busMessage = new BusMessage<Person>
                            {
                                Data = person
                            };

                        busMessage.Headers.Add(new BusHeader
                            {
                                Name = "Header",
                                Value = "RightValue"
                            });

                        publisher.Send(busMessage);

                        bool waitOne = ev.WaitOne(TimeSpan.FromSeconds(5000));

                        waitOne.Should().BeTrue();

                        busMessage.ShouldBeEquivalentTo(implementation.Message,
                                                        options =>
                                                        options.Excluding(message => message.BusId)
                                                               .Excluding(message => message.Sent));
                    }
                }
            }
        }
        public void MessageProcessFailedOnce_CheckMessageRedelivered()
        {
            using (RabbitMQBus entityA = new RabbitMQBus(), entityB = new RabbitMQBus())
            {
                Person message = new Person
                {
                    Id = 5
                };

                ManualResetEvent ev = new ManualResetEvent(false);

                int counter = 0;

                using (ISubscriber subscriberA = entityA.CreateSubscriber(c => c.UseTransactionalDelivery()))
                {
                    subscriberA.Subscribe((Action<Person>)(d =>
                    {
                        counter++;

                        if (counter == 1)
                        {
                            throw new Exception();
                        }
                    }));

                    subscriberA.Open();

                    const int expected = 2;

                    using (IPublisher publisher = entityB.CreatePublisher())
                    {
                        publisher.Send(message);
                    }

                    ev.WaitOne(TimeSpan.FromSeconds(5));

                    counter.Should().Be(expected);
                }
            }
        }
Example #8
0
        public void Bus_PublishedMessage_EachReceiverGetsMessageCopy()
        {
            Person actual = new Person
                {
                    Id = 5
                };

            using (MessageBus.Core.RabbitMQBus busA = new MessageBus.Core.RabbitMQBus(), busB = new MessageBus.Core.RabbitMQBus(), busC = new MessageBus.Core.RabbitMQBus())
            {
                using (IReceiver receiverB1 = busB.CreateReceiver(), receiverB2 = busB.CreateReceiver(), receiverC1 = busC.CreateReceiver())
                {
                    receiverB1.Subscribe<Person>();
                    receiverB2.Subscribe<Person>();
                    receiverC1.Subscribe<Person>();

                    receiverB1.Open();
                    receiverB2.Open();
                    receiverC1.Open();

                    using (IPublisher publisher = busA.CreatePublisher())
                    {
                        publisher.Send(actual);
                    }

                    Person b1 = receiverB1.Receive<Person>();
                    Person b2 = receiverB2.Receive<Person>();
                    Person c1 = receiverC1.Receive<Person>();

                    b1.Should().NotBeNull();
                    b2.Should().NotBeNull();
                    c1.Should().NotBeNull();

                    b1.ShouldBeEquivalentTo(actual);
                    b2.ShouldBeEquivalentTo(actual);
                    c1.ShouldBeEquivalentTo(actual);

                }
            }
        }
        public void Bus_UndeliverableMessages_ReturnedToSubscriber()
        {
            using (IBus bus = new MessageBus.Core.RabbitMQBus())
            {
                using (IPublisher publisher = bus.CreatePublisher(c => c.SetMandatoryDelivery().UseErrorHandler(this)))
                {
                    Person person = new Person {Id = 5};

                    BusMessage<Person> busMessage = new BusMessage<Person>
                        {
                            Data = person
                        };

                    busMessage.Headers.Add(new BusHeader
                        {
                            Name = "Header",
                            Value = "Value"
                        });

                    publisher.Send(busMessage);

                    bool waitOne = _ev.WaitOne(TimeSpan.FromSeconds(5));

                    waitOne.Should().BeTrue();

                    _errorCode.Should().Be(312);
                    _text.Should().Be("NO_ROUTE");

                    _message.BusId.Should().Be(bus.BusId);
                    _message.Name.Should().Be("Person");
                    _message.Sent.Should().BeCloseTo(DateTime.Now, 2000);
                    _message.Data.Should().BeOfType<Person>();

                    _message.Headers.OfType<BusHeader>().Should().OnlyContain(header => header.Name == "Header" && header.Value == "Value");

                    person.ShouldBeEquivalentTo(_message.Data);
                }
            }
        }
        public void Bus_SimpleSubscribtion_CallReceivedOnImplementationInstace()
        {
            ManualResetEvent ev = new ManualResetEvent(false);

            SimpleImplementation implementation = new SimpleImplementation(ev);

            using (IBus bus = new RabbitMQBus())
            {
                using (ISubscription subscription = bus.RegisterSubscription(implementation))
                {
                    subscription.Open();

                    using (IPublisher publisher = bus.CreatePublisher())
                    {
                        Person person = new Person {Id = 5};

                        BusMessage<Person> busMessage = new BusMessage<Person>
                            {
                                Data = person
                            };

                        busMessage.Headers.Add(new BusHeader
                            {
                                Name = "Header",
                                Value = "Value"
                            });

                        publisher.Send(busMessage);

                        bool waitOne = ev.WaitOne(TimeSpan.FromSeconds(5));

                        waitOne.Should().BeTrue();

                        person.ShouldBeEquivalentTo(implementation.Person);
                    }
                }
            }
        }
Example #11
0
        public void SendAndReciveMessageFromFanoutExchangeWithRoutingKey()
        {
            using (IBus busA = new RabbitMQBus(), busB = new RabbitMQBus())
            {
                Person expected = new Person
                {
                    Id = 5
                };

                Person actual = null;

                ManualResetEvent ev = new ManualResetEvent(false);

                using (var subscriber = busB.CreateSubscriber(c => c.SetExchange("amq.fanout").SetRoutingKey("MyKey")))
                {
                    subscriber.Subscribe((Person p) =>
                    {
                        actual = p;

                        ev.Set();
                    });

                    subscriber.Open();

                    using (var publisher = busA.CreatePublisher(c => c.SetExchange("amq.fanout").SetRoutingKey("MyKey")))
                    {
                        publisher.Send(expected);
                    }

                    bool waitOne = ev.WaitOne(TimeSpan.FromSeconds(20));

                    waitOne.Should().BeTrue("Message not received");

                    actual.ShouldBeEquivalentTo(expected);
                }
            }
        }
        public void MessageProcessFailedOnce_RejectMessage_CheckMessageDeliveredToDeadLetterQ()
        {
            using (RabbitMQBus entityA = new RabbitMQBus(), entityB = new RabbitMQBus(), entityC = new RabbitMQBus(c => c.UseConnectionString("amqp://localhost/amq.direct")))
            {
                Person message = new Person
                    {
                        Id = 5
                    };

                ManualResetEvent ev = new ManualResetEvent(false);

                int counter = 0;

                BusMessage<Person> actual = null;

                using (ISubscriber subscriberA = entityA.CreateSubscriber(c => c.UseDurableQueue(QueueName).UseTransactionalDelivery()))
                {
                    subscriberA.Subscribe((Action<Person>) (d =>
                    {
                        counter++;

                        if (counter == 1)
                        {
                            throw new Exception();
                        }

                        throw new RejectMessageException();
                    }));

                    subscriberA.Open();

                    using (ISubscriber deadLetterSubscriber = entityC.CreateSubscriber(c => c.UseDurableQueue(DeadLetterQueueName)))
                    {
                        deadLetterSubscriber.Subscribe<Person>(m =>
                        {
                            actual = m;

                            ev.Set();
                        });

                        deadLetterSubscriber.Open();

                        const int expected = 2;

                        using (IPublisher publisher = entityB.CreatePublisher())
                        {
                            publisher.Send(message);
                        }

                        bool waitOne = ev.WaitOne(TimeSpan.FromSeconds(50));

                        counter.Should().BeGreaterOrEqualTo(expected);
                        waitOne.Should().BeTrue();
                        actual.Data.ShouldBeEquivalentTo(message);

                        XDeadHeader xDeadHeader = actual.Headers.OfType<XDeadHeader>().First();

                        xDeadHeader.Exchange.Should().Be("amq.headers");
                        xDeadHeader.Reason.Should().Be("rejected");
                        xDeadHeader.Queue.Should().Be(QueueName);
                    }
                }

            }
        }
        public void Bus_SubscriptionWithFilter_CallFiltered()
        {
            ManualResetEvent ev = new ManualResetEvent(false);

            FilterImplementation implementation = new FilterImplementation(ev);

            using (IBus bus = new MessageBus.Core.RabbitMQBus())
            {
                using (ISubscription subscription = bus.RegisterSubscription(implementation, c => c.SetReceiveSelfPublish()))
                {
                    subscription.Open();

                    using (IPublisher publisher = bus.CreatePublisher())
                    {
                        Person person = new Person {Id = 5};

                        BusMessage<Person> busMessage = new BusMessage<Person>
                            {
                                Data = person
                            };

                        busMessage.Headers.Add(new BusHeader
                            {
                                Name = "Header",
                                Value = "WrongValue"
                            });

                        publisher.Send(busMessage);

                        bool waitOne = ev.WaitOne(TimeSpan.FromSeconds(5));

                        waitOne.Should().BeFalse();
                    }
                }
            }
        }
Example #14
0
 protected bool Equals(Person other)
 {
     return Id == other.Id;
 }
        public void ProcessPerson([HeaderFilter("Header", "RightValue")]Person person)
        {
            _person = person;

            _ev.Set();
        }
        public void UseCustomFormat_MessageSent_Received()
        {
            ManualResetEvent ev = new ManualResetEvent(false);

            using (IBus busA = new RabbitMQBus(), busB = new RabbitMQBus())
            {
                Person person = new Person
                {
                    Id = 5
                };

                Person actual = null;

                ISerializer serializer = A.Fake<ISerializer>();

                A.CallTo(() => serializer.Serialize(A<RawBusMessage>._)).Returns(new byte[0]);
                A.CallTo(() => serializer.Deserialize(new DataContractKey(typeof(Person).Name, typeof(Person).Namespace), typeof(Person), A<byte[]>._)).Returns(person);
                A.CallTo(() => serializer.ContentType).Returns("Custom");

                using (ISubscriber subscriber = busA.CreateSubscriber(c => c.AddCustomSerializer(serializer)))
                {
                    subscriber.Subscribe((Action<Person>) (p =>
                    {
                        actual = p;

                        ev.Set();
                    }));

                    subscriber.Open();

                    using (IPublisher publisher = busB.CreatePublisher(c => c.UseCustomSerializer(serializer)))
                    {
                        publisher.Send(new Person());
                    }

                    ev.WaitOne(TimeSpan.FromSeconds(5));

                    actual.ShouldBeEquivalentTo(person);
                }

                A.CallTo(() => serializer.Serialize(A<RawBusMessage>._)).MustHaveHappened();
                A.CallTo(() => serializer.Deserialize(new DataContractKey(typeof(Person).Name, typeof(Person).Namespace), typeof(Person), A<byte[]>._)).MustHaveHappened();
            }
        }
        public void ProcessPerson(Person person)
        {
            _person = person;

            _ev.Set();
        }
        public void UseSoapFormat_MessageSent_Received()
        {
            ManualResetEvent ev = new ManualResetEvent(false);

            using (IBus busA = new RabbitMQBus(), busB = new RabbitMQBus())
            {
                Person person = new Person
                {
                    Id = 5
                };

                Person actual = null;

                using (ISubscriber subscriber = busA.CreateSubscriber())
                {
                    subscriber.Subscribe((Action<Person>) (p =>
                    {
                        actual = p;

                        ev.Set();
                    }));

                    subscriber.Open();

                    using (IPublisher publisher = busB.CreatePublisher(c => c.UseSoapSerializer()))
                    {
                        publisher.Send(person);
                    }

                    ev.WaitOne(TimeSpan.FromSeconds(5));

                    actual.ShouldBeEquivalentTo(person);
                }
            }
        }
        public void MessageProcessFailedOnce_RejectMessage_CheckMessageDeliveredToDeadLetterQ()
        {
            using (RabbitMQBus entityA = new RabbitMQBus(), entityB = new RabbitMQBus(), entityC = new RabbitMQBus(c => c.UseConnectionString("amqp://localhost/amq.direct")))
            {
                Person message = new Person
                    {
                        Id = 5
                    };

                ManualResetEvent ev = new ManualResetEvent(false);

                int counter = 0;

                BusMessage<Person> actual = null;

                using (ISubscriber subscriberA = entityA.CreateSubscriber(c => c.UseDurableQueue(QueueName).UseTransactionalDelivery()))
                {
                    subscriberA.Subscribe((Action<Person>) (d =>
                    {
                        counter++;

                        if (counter == 1)
                        {
                            throw new Exception();
                        }

                        throw new RejectMessageException();
                    }));

                    subscriberA.Open();

                    using (ISubscriber deadLetterSubscriber = entityC.CreateSubscriber(c => c.UseDurableQueue(DeadLetterQueueName)))
                    {
                        deadLetterSubscriber.Subscribe<Person>(m =>
                        {
                            actual = m;
                            
                            ev.Set();
                        });

                        deadLetterSubscriber.Open();

                        const int expected = 2;

                        using (IPublisher publisher = entityB.CreatePublisher())
                        {
                            publisher.Send(message);
                        }

                        bool waitOne = ev.WaitOne(TimeSpan.FromSeconds(50));

                        counter.Should().BeGreaterOrEqualTo(expected);
                        waitOne.Should().BeTrue();
                        actual.Data.ShouldBeEquivalentTo(message);

                        XDeadHeader xDeadHeader = actual.Headers.OfType<XDeadHeader>().First();

                        xDeadHeader.Exchange.Should().Be("amq.headers");
                        xDeadHeader.Reason.Should().Be("rejected");
                        xDeadHeader.Queue.Should().Be(QueueName);
                    }
                }
            
            }
        }