Exemplo n.º 1
0
        void VerifyBestPractices(OutgoingContext context)
        {
            if (!context.DeliveryOptions.EnforceMessagingBestPractices)
            {
                return;
            }

            var sendOptions = context.DeliveryOptions as SendOptions;

            if (sendOptions == null)
            {
                MessagingBestPractices.AssertIsValidForPubSub(context.OutgoingLogicalMessage.MessageType, Conventions);
                return;
            }

            if (sendOptions.Destination == Address.Undefined)
            {
                throw new InvalidOperationException("No destination specified for message: " + context.OutgoingLogicalMessage.MessageType);
            }

            if (sendOptions is ReplyOptions)
            {
                MessagingBestPractices.AssertIsValidForReply(context.OutgoingLogicalMessage.MessageType, Conventions);
            }
            else
            {
                MessagingBestPractices.AssertIsValidForSend(context.OutgoingLogicalMessage.MessageType, Conventions);
            }
        }
Exemplo n.º 2
0
        static void VerifyBestPractices(SendLogicalMessageContext context)
        {
            if (!context.SendOptions.EnforceMessagingBestPractices)
            {
                return;
            }
            if (context.SendOptions.Destination == Address.Undefined)
            {
                throw new InvalidOperationException("No destination specified for message: " + context.MessageToSend.MessageType);
            }

            switch (context.SendOptions.Intent)
            {
            case MessageIntentEnum.Init:
            case MessageIntentEnum.Subscribe:
            case MessageIntentEnum.Unsubscribe:
                break;

            case MessageIntentEnum.Publish:
                MessagingBestPractices.AssertIsValidForPubSub(context.MessageToSend.MessageType);
                break;

            case MessageIntentEnum.Reply:
                MessagingBestPractices.AssertIsValidForReply(context.MessageToSend.MessageType);
                break;

            case MessageIntentEnum.Send:
                MessagingBestPractices.AssertIsValidForSend(context.MessageToSend.MessageType, context.SendOptions.Intent);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Check that the object sends a message of the given type complying with the given predicate.
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="check"></param>
        /// <returns></returns>
        public void ExpectSend <TMessage>(SendPredicate <TMessage> check)
        {
            MessagingBestPractices.AssertIsValidForSend(typeof(TMessage), MessageIntentEnum.Send);

            Delegate d = new HandleMessageDelegate(
                () => ExpectCallToSend <TMessage>(
                    msgs => msgs.All(msg => msg is TMessage) && msgs.OfType <TMessage>().All(msg => check(msg))));

            delegates.Add(d);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Check that the object sends the given message type to its local queue
        /// and that the message complies with the given predicate.
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="check"></param>
        /// <returns></returns>
        public void ExpectSendLocal <TMessage>(SendPredicate <TMessage> check)
        {
            MessagingBestPractices.AssertIsValidForSend(typeof(TMessage), MessageIntentEnum.Send);

            Delegate d = new HandleMessageDelegate(
                () => ExpectCallToSendLocal <TMessage>(
                    delegate(object[] msgs)
            {
                foreach (TMessage msg in msgs)
                {
                    if (!check(msg))
                    {
                        return(false);
                    }
                }

                return(true);
            }
                    )
                );

            delegates.Add(d);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Check that the object publishes a message of the given type complying with the given predicate.
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="check"></param>
        /// <returns></returns>
        public void ExpectPublish <TMessage>(PublishPredicate <TMessage> check)
        {
            MessagingBestPractices.AssertIsValidForPubSub(typeof(TMessage));

            Delegate d = new HandleMessageDelegate(
                () => ExpectCallToPublish(
                    delegate(TMessage[] msgs)
            {
                foreach (TMessage msg in msgs)
                {
                    if (!check(msg))
                    {
                        return(false);
                    }
                }

                return(true);
            }
                    )
                );

            delegates.Add(d);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Check that the object replies to the originator with the given message type.
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="check"></param>
        /// <returns></returns>
        public void ExpectReplyToOrginator <TMessage>(SendPredicate <TMessage> check)
        {
            MessagingBestPractices.AssertIsValidForReply(typeof(TMessage));

            Delegate d = new HandleMessageDelegate(
                () => ExpectCallToSend <TMessage>(
                    delegate(string destination, string correlationId, object[] msgs)
            {
                foreach (TMessage msg in msgs)
                {
                    if (!check(msg))
                    {
                        return(false);
                    }
                }

                return(true);
            }
                    )
                );

            delegates.Add(d);
        }
Exemplo n.º 7
0
 public void Should_not_throw_for_message()
 {
     MessagingBestPractices.AssertIsValidForPubSub(typeof(MyMessage), new Conventions());
 }
Exemplo n.º 8
0
 public void Should_not_throw_for_event()
 {
     MessagingBestPractices.AssertIsValidForPubSub(typeof(MyEvent), new Conventions());
     //TODO: verify log
 }
Exemplo n.º 9
0
            public void Should_throw_for_command()
            {
                var invalidOperationException = Assert.Throws <InvalidOperationException>(() => MessagingBestPractices.AssertIsValidForPubSub(typeof(MyCommand), new Conventions()));

                Assert.AreEqual("Pub/Sub is not supported for Commands. They should be be sent direct to their logical owner.", invalidOperationException.Message);
            }
Exemplo n.º 10
0
            public void Should_throw_for_event()
            {
                var invalidOperationException = Assert.Throws <InvalidOperationException>(() => MessagingBestPractices.AssertIsValidForReply(typeof(MyEvent), new Conventions()));

                Assert.AreEqual("Reply is neither supported for Commands nor Events. Commands should be sent to their logical owner using bus.Send and bus. Events should be Published with bus.Publish.", invalidOperationException.Message);
            }
Exemplo n.º 11
0
 public void Should_not_throw_for_message()
 {
     MessagingBestPractices.AssertIsValidForReply(new[] { new MyMessage() });
 }
Exemplo n.º 12
0
            public void Should_throw_for_event()
            {
                var invalidOperationException = Assert.Throws <InvalidOperationException>(() => MessagingBestPractices.AssertIsValidForReply(new[] { new MyEvent() }));

                Assert.AreEqual("Reply is not supported for Events. Events should be Published with bus.Publish.", invalidOperationException.Message);
            }
Exemplo n.º 13
0
            public void Should_throw_for_command()
            {
                var invalidOperationException = Assert.Throws <InvalidOperationException>(() => MessagingBestPractices.AssertIsValidForReply(new[] { new MyCommand() }));

                Assert.AreEqual("Reply is not supported for Commands. Commands should be sent to their logical owner using bus.Send and bus.", invalidOperationException.Message);
            }