예제 #1
0
        public void UseXmlFormat_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.UseXmlSerializer()))
                    {
                        publisher.Send(person);
                    }

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

                    actual.ShouldBeEquivalentTo(person);
                }
            }
        }
예제 #2
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);
                }
            }
        }
예제 #3
0
        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(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(typeof(Person), A <byte[]> ._)).MustHaveHappened();
            }
        }
        public void Bus_SubscribtionWithFilter_CallReceived()
        {
            ManualResetEvent ev = new ManualResetEvent(false);

            FilterImplementation implementation = new FilterImplementation(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 = "RightValue"
                        });

                        publisher.Send(busMessage);

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

                        waitOne.Should().BeTrue();

                        person.ShouldBeEquivalentTo(implementation.Person);
                    }
                }
            }
        }
예제 #5
0
        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);
                }
            }
        }
예제 #6
0
        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);
                }
            }
        }
예제 #7
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);
                }
            }
        }
예제 #8
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);
                }
            }
        }
예제 #9
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);
                }
            }
        }
예제 #10
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);
                }
            }
        }
        public void Bus_SimpleSubscription_CallReceivedOnImplementationInstace()
        {
            ManualResetEvent ev = new ManualResetEvent(false);

            SimpleImplementation implementation = new SimpleImplementation(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 = "Value"
                            });

                        publisher.Send(busMessage);

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

                        waitOne.Should().BeTrue();

                        person.ShouldBeEquivalentTo(implementation.Person);
                    }
                }
            }
        }