public void when_sending_message_with_session_then_session_receiver_gets_it()
        {
            var sender = this.Settings.CreateTopicClient(this.Topic);
            var signal = new ManualResetEventSlim();
            var body = Guid.NewGuid().ToString();

            var receiver = new SessionSubscriptionReceiver(this.Settings, this.Topic, this.Subscription);

            sender.Send(new BrokeredMessage(Guid.NewGuid().ToString()));
            sender.Send(new BrokeredMessage(body) { SessionId = "foo" });

            var received = "";

            receiver.Start(
                m =>
                {
                    received = m.GetBody<string>();
                    signal.Set();
                    return MessageReleaseAction.CompleteMessage;
                });

            signal.Wait();

            receiver.Stop();

            Assert.Equal(body, received);
        }
        public void when_sending_message_with_session_then_session_receiver_gets_both_messages_fast()
        {
            var sender = this.Settings.CreateTopicClient(this.Topic);
            var signal = new AutoResetEvent(false);
            var body1 = Guid.NewGuid().ToString();
            var body2 = Guid.NewGuid().ToString();
            var stopWatch = new Stopwatch();

            var receiver = new SessionSubscriptionReceiver(this.Settings, this.Topic, this.Subscription);

            sender.Send(new BrokeredMessage(body1) { SessionId = "foo" });
            sender.Send(new BrokeredMessage(body2) { SessionId = "bar" });

            var received = new ConcurrentBag<string>();

            receiver.Start(
                m =>
                {
                    received.Add(m.GetBody<string>());
                    signal.Set();
                    return MessageReleaseAction.CompleteMessage;
                });

            signal.WaitOne();
            stopWatch.Start();
            signal.WaitOne();
            stopWatch.Stop();

            receiver.Stop();

            Assert.Contains(body1, received);
            Assert.Contains(body2, received);
            Assert.InRange(stopWatch.Elapsed, TimeSpan.Zero, TimeSpan.FromSeconds(2));
        }
        public void when_disposing_not_started_then_no_op()
        {
            var receiver = new SessionSubscriptionReceiver(this.Settings, this.Topic, this.Subscription);

            receiver.Dispose();
        }
        public void when_sending_message_to_different_sessions_then_processes_concurrently()
        {
            var sender = this.Settings.CreateTopicClient(this.Topic);
            var message1received = new ManualResetEvent(false);
            var message2received = new ManualResetEvent(false);
            var body1 = Guid.NewGuid().ToString();
            var body2 = Guid.NewGuid().ToString();

            var receiver = new SessionSubscriptionReceiver(this.Settings, this.Topic, this.Subscription);
            var stopWatch = new Stopwatch();

            receiver.Start(
                m =>
                {
                    var msg = m.GetBody<string>();
                    if (msg == body1)
                    {
                        message1received.Set();
                        // do not continue until we verify that the 2nd message is received, hence implying parallelism
                        message2received.WaitOne();
                    }
                    else
                    {
                        message2received.Set();
                    }

                    return MessageReleaseAction.CompleteMessage;
                });

            sender.Send(new BrokeredMessage(body1) { SessionId = "foo" });
            stopWatch.Start();
            Assert.True(message1received.WaitOne(10000));

            sender.Send(new BrokeredMessage(body2) { SessionId = "bar" });
            Assert.True(message2received.WaitOne(10000));
            stopWatch.Stop();

            receiver.Stop();

            Assert.InRange(stopWatch.Elapsed, TimeSpan.Zero, TimeSpan.FromSeconds(10));
        }
        public void when_stopping_without_starting_then_ignores_request()
        {
            var receiver = new SessionSubscriptionReceiver(this.Settings, this.Topic, this.Subscription);

            receiver.Stop();
        }
        public void when_starting_twice_then_ignores_second_request()
        {
            var receiver = new SessionSubscriptionReceiver(this.Settings, this.Topic, this.Subscription);

            receiver.Start(m => MessageReleaseAction.CompleteMessage);
            receiver.Start(m => MessageReleaseAction.CompleteMessage);

            receiver.Stop();
        }
        public void when_starting_twice_then_ignores_second_request()
        {
            var receiver = new SessionSubscriptionReceiver(this.Settings, this.Topic, this.Subscription);

            receiver.Start();
            receiver.Start();

            receiver.Stop();
        }
        public void when_sending_message_with_same_session_at_different_times_then_session_receiver_gets_all()
        {
            var client = this.Settings.CreateSubscriptionClient(this.Topic, this.Subscription);
            var sender = this.Settings.CreateTopicClient(this.Topic);
            var signal = new AutoResetEvent(false);
            var body1 = Guid.NewGuid().ToString();
            var body2 = Guid.NewGuid().ToString();
            var body3 = Guid.NewGuid().ToString();

            var receiver = new SessionSubscriptionReceiver(this.Settings, this.Topic, this.Subscription);
            var stopWatch = new Stopwatch();
            var received = new ConcurrentBag<string>();

            receiver.MessageReceived += (s, e) =>
            {
                received.Add(e.Message.GetBody<string>());
                e.Message.Complete();
                signal.Set();
            };

            receiver.Start();

            sender.Send(new BrokeredMessage(body1) { SessionId = "foo" });
            stopWatch.Start();
            signal.WaitOne();

            sender.Send(new BrokeredMessage(body2) { SessionId = "bar" });
            signal.WaitOne();

            sender.Send(new BrokeredMessage(body3) { SessionId = "foo" });
            signal.WaitOne();
            stopWatch.Stop();

            receiver.Stop();

            Assert.Contains(body1, received);
            Assert.Contains(body2, received);
            Assert.Contains(body3, received);
            Assert.InRange(stopWatch.Elapsed, TimeSpan.Zero, TimeSpan.FromSeconds(10));
        }
Example #9
0
        public EventProcessor CreateEventProcessor(string subscription, IEventHandler handler, ITextSerializer serializer, bool instrumentationEnabled = false)
        {
            if (!initialized)
            {
                throw new InvalidOperationException("Service bus configuration has not been initialized.");
            }

            TopicSettings        topicSettings        = null;
            SubscriptionSettings subscriptionSettings = null;

            foreach (var settings in this.settings.Topics.Where(t => t.IsEventBus))
            {
                subscriptionSettings = settings.Subscriptions.Find(s => s.Name == subscription);
                if (subscriptionSettings != null)
                {
                    topicSettings = settings;
                    break;
                }
            }

            if (subscriptionSettings == null)
            {
                throw new ArgumentOutOfRangeException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              "Subscription '{0}' has not been registered for an event bus topic in the service bus configuration.",
                              subscription));
            }

            IMessageReceiver receiver;

            if (subscriptionSettings.RequiresSession)
            {
                var instrumentation = new SessionSubscriptionReceiverInstrumentation(subscription, instrumentationEnabled);
                try {
                    receiver = new SessionSubscriptionReceiver(settings, topicSettings.Path, subscription, true, instrumentation);
                } catch {
                    instrumentation.Dispose();
                    throw;
                }
            }
            else
            {
                var instrumentation = new SubscriptionReceiverInstrumentation(subscription, instrumentationEnabled);
                try {
                    receiver = new SubscriptionReceiver(settings, topicSettings.Path, subscription, true, instrumentation);
                } catch {
                    instrumentation.Dispose();
                    throw;
                }
            }

            EventProcessor processor;

            try {
                processor = new EventProcessor(receiver, serializer);
            } catch {
                using (receiver as IDisposable) { }
                throw;
            }

            try {
                processor.Register(handler);

                return(processor);
            } catch {
                processor.Dispose();
                throw;
            }
        }