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); } }
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(); } }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
public void Should_not_throw_for_message() { MessagingBestPractices.AssertIsValidForPubSub(typeof(MyMessage), new Conventions()); }
public void Should_not_throw_for_event() { MessagingBestPractices.AssertIsValidForPubSub(typeof(MyEvent), new Conventions()); //TODO: verify log }
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); }
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); }
public void Should_not_throw_for_message() { MessagingBestPractices.AssertIsValidForReply(new[] { new MyMessage() }); }
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); }
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); }