Exemplo n.º 1
0
        public void Queue_NonDurableQueue_TwoExclusiveSubscribers_BothShouldGetMessage()
        {
            NonImportantData data = new NonImportantData {
                Info = "Non-Valuable information"
            };

            NonImportantData expected1 = null;
            NonImportantData expected2 = null;
            ManualResetEvent ev1       = new ManualResetEvent(false);
            ManualResetEvent ev2       = new ManualResetEvent(false);
            bool             wait1;
            bool             wait2;
            IBus             subscriberBus = null;
            ISubscriber      subscriber1 = null, subscriber2 = null;

            try
            {
                subscriberBus = new MessageBus.Core.RabbitMQBus();
                subscriber1   = subscriberBus.CreateSubscriber(c => { });
                subscriber1.Subscribe <NonImportantData>(p =>
                {
                    expected1 = p;
                    ev1.Set();
                });
                subscriber1.Open();

                subscriber2 = subscriberBus.CreateSubscriber(c => { });
                subscriber2.Subscribe <NonImportantData>(p =>
                {
                    expected2 = p;
                    ev2.Set();
                });
                subscriber2.Open();

                // Dispatch message
                using (var bus = new MessageBus.Core.RabbitMQBus())
                {
                    using (IPublisher publisher = bus.CreatePublisher())
                    {
                        publisher.Send(data);
                    }
                }

                wait1 = ev1.WaitOne(TimeSpan.FromMilliseconds(100));
                wait2 = ev2.WaitOne(TimeSpan.FromMilliseconds(100));
            }
            finally
            {
                subscriber2?.Dispose();
                subscriber1?.Dispose();
                subscriberBus?.Dispose();
            }

            wait1.Should().BeTrue();
            wait2.Should().BeTrue();

            expected1.Should().NotBeNull();
            expected1.ShouldBeEquivalentTo(data);

            expected2.Should().NotBeNull();
            expected2.ShouldBeEquivalentTo(data);
        }
Exemplo n.º 2
0
        public void Queue_NonDurableQueue_TwoSharedQueueSubscribers_OneShouldGetMessage()
        {
            NonImportantData data = new NonImportantData {
                Info = "Non-Valuable information"
            };

            NonImportantData expected1 = null;
            NonImportantData expected2 = null;
            ManualResetEvent ev1       = new ManualResetEvent(false);
            ManualResetEvent ev2       = new ManualResetEvent(false);
            bool             wait1;
            bool             wait2;
            IBus             subscriberBus = null;
            ISubscriber      subscriber1 = null, subscriber2 = null;

            try
            {
                subscriberBus = new MessageBus.Core.RabbitMQBus();
                subscriber1   = subscriberBus.CreateSubscriber(c => c.UseNonDurableNamedQueue(NonDurableTestQueueSuffix));
                subscriber1.Subscribe <NonImportantData>(p =>
                {
                    expected1 = p;
                    ev1.Set();
                });
                subscriber1.Open();

                subscriber2 = subscriberBus.CreateSubscriber(c => c.UseNonDurableNamedQueue(NonDurableTestQueueSuffix));
                subscriber2.Subscribe <NonImportantData>(p =>
                {
                    expected2 = p;
                    ev2.Set();
                });
                subscriber2.Open();

                // Dispatch message
                using (var bus = new MessageBus.Core.RabbitMQBus())
                {
                    using (IPublisher publisher = bus.CreatePublisher())
                    {
                        publisher.Send(data);
                    }
                }

                wait1 = ev1.WaitOne(TimeSpan.FromMilliseconds(100));
                wait2 = ev2.WaitOne(TimeSpan.FromMilliseconds(100));
            }
            finally
            {
                subscriber2?.Dispose();
                subscriber1?.Dispose();
                subscriberBus?.Dispose();
            }

            // Wait for reset events to timeout
            Thread.Sleep(500);

            wait1.ShouldBeEquivalentTo(!wait2);

            if (wait1)
            {
                expected2.Should().BeNull();
                expected1.Should().NotBeNull();
                expected1.ShouldBeEquivalentTo(data);
            }
            else
            {
                expected1.Should().BeNull();
                expected2.Should().NotBeNull();
                expected2.ShouldBeEquivalentTo(data);
            }
        }