コード例 #1
0
        public async Task When_message_is_non_durable_should_set_non_durable_header()
        {
            var behavior = new DetermineMessageDurabilityBehavior(t => true);
            var context  = new TestableOutgoingLogicalMessageContext();

            await behavior.Invoke(context, _ => TaskEx.CompletedTask);

            Assert.IsTrue(bool.Parse(context.Headers[Headers.NonDurableMessage]));
        }
コード例 #2
0
        public async Task When_message_is_durable_should_not_set_non_durable_header()
        {
            var behavior = new DetermineMessageDurabilityBehavior(t => false);
            var context  = new TestableOutgoingLogicalMessageContext();

            await behavior.Invoke(context, _ => TaskEx.CompletedTask);

            Assert.IsFalse(context.Headers.ContainsKey(Headers.NonDurableMessage));
        }
コード例 #3
0
        static IOutgoingLogicalMessageContext CreateContext(ExtendableOptions options)
        {
            var context = new TestableOutgoingLogicalMessageContext
            {
                Extensions = options.Context
            };

            return context;
        }
コード例 #4
0
    public Task OutgoingLogicalMessageContext()
    {
        var context = new TestableOutgoingLogicalMessageContext
        {
            Message = BuildOutgoingLogicalMessage()
        };

        return(Verify(context));
    }
コード例 #5
0
        static IOutgoingLogicalMessageContext CreateContext(ExtendableOptions options)
        {
            var context = new TestableOutgoingLogicalMessageContext
            {
                Extensions = options.Context
            };

            return(context);
        }
コード例 #6
0
        public async Task Should_generate_new_conversation_id_when_sending_outside_of_handlers()
        {
            var generatedId = "some generated conversation id";
            var behavior    = new AttachCausationHeadersBehavior(_ => generatedId);
            var context     = new TestableOutgoingLogicalMessageContext();

            await behavior.Invoke(context, ctx => TaskEx.CompletedTask);

            Assert.AreEqual(generatedId, context.Headers[Headers.ConversationId]);
        }
コード例 #7
0
        public async Task When_message_is_durable_should_not_add_non_durable_constraint()
        {
            var behavior = new DetermineMessageDurabilityBehavior(t => false);
            var context  = new TestableOutgoingLogicalMessageContext();

            await behavior.Invoke(context, _ => TaskEx.CompletedTask);

            Assert.IsFalse(context.Extensions.TryGetDeliveryConstraint(out NonDurableDelivery nonDurableDeliveryConstraint));
            Assert.IsNull(nonDurableDeliveryConstraint);
        }
コード例 #8
0
        public async Task Should_set_the_related_to_header_with_the_id_of_the_current_message()
        {
            var behavior = new AttachCausationHeadersBehavior(ReturnDefaultConversationId);
            var context  = new TestableOutgoingLogicalMessageContext();

            context.Extensions.Set(new IncomingMessage("the message id", new Dictionary <string, string>(), new byte[0]));

            await behavior.Invoke(context, ctx => TaskEx.CompletedTask);

            Assert.AreEqual("the message id", context.Headers[Headers.RelatedTo]);
        }
コード例 #9
0
        public void Should_throw_friendly_exception_when_IMutateOutgoingMessages_MutateOutgoing_returns_null()
        {
            var behavior = new MutateOutgoingMessageBehavior(hasOutgoingMessageMutators: true);

            var context = new TestableOutgoingLogicalMessageContext();
            context.Extensions.Set(new IncomingMessage("messageId", new Dictionary<string, string>(), new byte[0]));
            context.Extensions.Set(new LogicalMessage(null, null));
            context.Builder.Register<IMutateOutgoingMessages>(() => new MutateOutgoingMessagesReturnsNull());

            Assert.That(async () => await behavior.Invoke(context, ctx => TaskEx.CompletedTask), Throws.Exception.With.Message.EqualTo("Return a Task or mark the method as async."));
        }
コード例 #10
0
        public void Should_throw_friendly_exception_when_IMutateOutgoingMessages_MutateOutgoing_returns_null()
        {
            var behavior = new MutateOutgoingMessageBehavior();

            var context = new TestableOutgoingLogicalMessageContext();

            context.Extensions.Set(new IncomingMessage("messageId", new Dictionary <string, string>(), new byte[0]));
            context.Extensions.Set(new LogicalMessage(null, null));
            context.Builder.Register <IMutateOutgoingMessages>(() => new MutateOutgoingMessagesReturnsNull());

            Assert.That(async() => await behavior.Invoke(context, ctx => TaskEx.CompletedTask), Throws.Exception.With.Message.EqualTo("Return a Task or mark the method as async."));
        }
コード例 #11
0
    public async Task ShouldNotAddCustomHeaderToOtherMessageTypes(Type messageType)
    {
        var behavior = new CustomBehavior();
        var context  = new TestableOutgoingLogicalMessageContext
        {
            Message = new OutgoingLogicalMessage(messageType, Activator.CreateInstance(messageType))
        };

        await behavior.Invoke(context, () => Task.CompletedTask);

        Assert.IsFalse(context.Headers.ContainsKey("custom-header"));
    }
コード例 #12
0
    public async Task ShouldAddCustomHeaderToMyResponse()
    {
        var behavior = new CustomBehavior();
        var context  = new TestableOutgoingLogicalMessageContext
        {
            Message = new OutgoingLogicalMessage(typeof(MyResponse), new MyResponse())
        };

        await behavior.Invoke(context, () => Task.CompletedTask);

        Assert.AreEqual("custom header value", context.Headers["custom-header"]);
    }
コード例 #13
0
        public async Task Should_not_call_MutateOutgoing_when_hasOutgoingMessageMutators_is_false()
        {
            var behavior = new MutateOutgoingMessageBehavior(hasOutgoingMessageMutators: false);

            var context = new TestableOutgoingLogicalMessageContext();

            var mutator = new MutatorThatIndicatesIfItWasCalled();
            context.Builder.Register<IMutateOutgoingMessages>(() => mutator);

            await behavior.Invoke(context, ctx => TaskEx.CompletedTask);

            Assert.IsFalse(mutator.MutateOutgoingCalled);
        }
コード例 #14
0
        public async Task Should_not_call_MutateOutgoing_when_hasOutgoingMessageMutators_is_false()
        {
            var behavior = new MutateOutgoingMessageBehavior(hasOutgoingMessageMutators: false);

            var context = new TestableOutgoingLogicalMessageContext();

            var mutator = new MutatorThatIndicatesIfItWasCalled();

            context.Builder.Register <IMutateOutgoingMessages>(() => mutator);

            await behavior.Invoke(context, ctx => TaskEx.CompletedTask);

            Assert.IsFalse(mutator.MutateOutgoingCalled);
        }
        public async Task Should_not_blow_up()
        {
            var context = new TestableOutgoingLogicalMessageContext();
            context.Message = new OutgoingLogicalMessage(typeof(MessageWithNullDataBusProperty), new MessageWithNullDataBusProperty());

            var sendBehavior = new DataBusSendBehavior(null, new DefaultDataBusSerializer(), new Conventions());

            using (var stream = new MemoryStream())
            {
                new BinaryFormatter().Serialize(stream, "test");
                stream.Position = 0;

                await sendBehavior.Invoke(context, ctx => TaskEx.CompletedTask);
            }
        }
        public async Task Should_not_blow_up()
        {
            var context = new TestableOutgoingLogicalMessageContext();

            context.Message = new OutgoingLogicalMessage(typeof(MessageWithNullDataBusProperty), new MessageWithNullDataBusProperty());

            var sendBehavior = new DataBusSendBehavior(null, new DefaultDataBusSerializer(), new Conventions());

            using (var stream = new MemoryStream())
            {
                new BinaryFormatter().Serialize(stream, "test");
                stream.Position = 0;

                await sendBehavior.Invoke(context, ctx => Task.CompletedTask);
            }
        }
        public async Task Outgoing_databus_properties_should_be_dehydrated()
        {
            var context = new TestableOutgoingLogicalMessageContext();
            context.Message = new OutgoingLogicalMessage(typeof(MessageWithDataBusProperty), new MessageWithDataBusProperty
            {
                DataBusProperty = new DataBusProperty<string>("test")
            });

            var fakeDatabus = new FakeDataBus();

            var sendBehavior = new DataBusSendBehavior(fakeDatabus, new DefaultDataBusSerializer(), new Conventions());

            await sendBehavior.Invoke(context, ctx => TaskEx.CompletedTask);

            Assert.AreEqual(TimeSpan.MaxValue, fakeDatabus.TTBRUsed);
        }
コード例 #18
0
        public async Task Should_not_call_MutateOutgoing_when_hasOutgoingMessageMutators_is_false()
        {
            var behavior = new MutateOutgoingMessageBehavior(new HashSet <IMutateOutgoingMessages>());

            var context = new TestableOutgoingLogicalMessageContext();

            await behavior.Invoke(context, ctx => Task.CompletedTask);

            var mutator = new MutatorThatIndicatesIfItWasCalled();

            context.Services.AddTransient <IMutateOutgoingMessages>(sp => mutator);

            await behavior.Invoke(context, ctx => Task.CompletedTask);

            Assert.IsFalse(mutator.MutateOutgoingCalled);
        }
コード例 #19
0
        public async Task Should_invoke_all_explicit_mutators()
        {
            var mutator      = new MutatorThatIndicatesIfItWasCalled();
            var otherMutator = new MutatorThatIndicatesIfItWasCalled();

            var behavior = new MutateOutgoingMessageBehavior(new HashSet <IMutateOutgoingMessages> {
                mutator, otherMutator
            });

            var context = new TestableOutgoingLogicalMessageContext();

            await behavior.Invoke(context, ctx => Task.CompletedTask);

            Assert.True(mutator.MutateOutgoingCalled);
            Assert.True(otherMutator.MutateOutgoingCalled);
        }
        public async Task Outgoing_databus_properties_should_be_dehydrated()
        {
            var context = new TestableOutgoingLogicalMessageContext();

            context.Message = new OutgoingLogicalMessage(typeof(MessageWithDataBusProperty), new MessageWithDataBusProperty
            {
                DataBusProperty = new DataBusProperty <string>("test")
            });

            var fakeDatabus = new FakeDataBus();

            var sendBehavior = new DataBusSendBehavior(fakeDatabus, new DefaultDataBusSerializer(), new Conventions());

            await sendBehavior.Invoke(context, ctx => Task.CompletedTask);

            Assert.AreEqual(TimeSpan.MaxValue, fakeDatabus.TTBRUsed);
        }
コード例 #21
0
        public async Task When_no_incoming_message_should_not_override_a_conversation_id_specified_by_the_user()
        {
            var userConversationId = Guid.NewGuid().ToString();

            var behavior = new AttachCausationHeadersBehavior(ReturnDefaultConversationId);
            var context  = new TestableOutgoingLogicalMessageContext
            {
                Headers =
                {
                    [Headers.ConversationId] = userConversationId
                }
            };

            await behavior.Invoke(context, ctx => TaskEx.CompletedTask);

            Assert.AreEqual(userConversationId, context.Headers[Headers.ConversationId]);
        }
コード例 #22
0
        public async Task ShouldMutateMessage()
        {
            var mutator = new FakeMutator();

            A.CallTo(() => Configuration.Settings.Container.Resolve(A <Type> .Ignored)).Returns(mutator);
            MutationManager.RegisterMutator("test", typeof(FakeMutator));

            var next    = A.Fake <Func <Task> >();
            var context = new TestableOutgoingLogicalMessageContext();

            context.UpdateMessage(Fake <Messages.IEvent>());

            await Sut.Invoke(context, next).ConfigureAwait(false);

            A.CallTo(() => next()).MustHaveHappened();
            mutator.MutatedOutgoing.Should().BeTrue();
        }
コード例 #23
0
        public async Task Should_set_content_type_header()
        {
            var registry = new MessageMetadataRegistry(new Conventions());

            registry.RegisterMessageTypesFoundIn(new List<Type>
            {
                typeof(MyMessage)
            });

            var context = new TestableOutgoingLogicalMessageContext();
            context.Message = new OutgoingLogicalMessage(typeof(MyMessage), new MyMessage());

            var behavior = new SerializeMessageConnector(new FakeSerializer("myContentType"), registry);

            await behavior.Invoke(context, c => TaskEx.CompletedTask);

            Assert.AreEqual("myContentType", context.Headers[Headers.ContentType]);
        }
        public async Task Time_to_live_should_be_passed_on_the_databus()
        {
            var context = new TestableOutgoingLogicalMessageContext();
            context.Message = new OutgoingLogicalMessage(typeof(MessageWithExplicitTimeToLive), new MessageWithExplicitTimeToLive
            {
                DataBusProperty = new DataBusProperty<string>("test")
            });

            context.Extensions.AddDeliveryConstraint(new DiscardIfNotReceivedBefore(TimeSpan.FromMinutes(1)));

            var fakeDatabus = new FakeDataBus();

            var sendBehavior = new DataBusSendBehavior(fakeDatabus, new DefaultDataBusSerializer(), new Conventions());

            await sendBehavior.Invoke(context, ctx => TaskEx.CompletedTask);

            Assert.AreEqual(TimeSpan.FromMinutes(1), fakeDatabus.TTBRUsed);
        }
コード例 #25
0
        public async Task Should_not_blow_up()
        {
            var context = new TestableOutgoingLogicalMessageContext
            {
                Message = new OutgoingLogicalMessage(typeof(MessageWithNullDataBusProperty), new MessageWithNullDataBusProperty())
            };

            var sendBehavior = new DataBusSendBehavior(null, new XmlDataBusSerializer <string>(), new Conventions());

            using (var stream = new MemoryStream())
            {
                var serializer = new System.Xml.Serialization.XmlSerializer(typeof(string));
                serializer.Serialize(stream, "test");
                stream.Position = 0;

                await sendBehavior.Invoke(context, ctx => Task.CompletedTask);
            }
        }
コード例 #26
0
        public async Task Should_set_the_conversation_id_to_conversation_id_of_incoming_message()
        {
            var incomingConversationId = Guid.NewGuid().ToString();

            var behavior = new AttachCausationHeadersBehavior(ReturnDefaultConversationId);
            var context  = new TestableOutgoingLogicalMessageContext();

            var transportMessage = new IncomingMessage("xyz", new Dictionary <string, string>
            {
                { Headers.ConversationId, incomingConversationId }
            }, new byte[0]);

            context.Extensions.Set(transportMessage);

            await behavior.Invoke(context, ctx => TaskEx.CompletedTask);

            Assert.AreEqual(incomingConversationId, context.Headers[Headers.ConversationId]);
        }
コード例 #27
0
        public async Task Should_invoke_both_explicit_and_container_provided_mutators()
        {
            var explicitMutator  = new MutatorThatIndicatesIfItWasCalled();
            var containerMutator = new MutatorThatIndicatesIfItWasCalled();

            var behavior = new MutateOutgoingMessageBehavior(new HashSet <IMutateOutgoingMessages> {
                explicitMutator
            });

            var context = new TestableOutgoingLogicalMessageContext();

            context.Services.AddTransient <IMutateOutgoingMessages>(sp => containerMutator);

            await behavior.Invoke(context, ctx => Task.CompletedTask);

            Assert.True(explicitMutator.MutateOutgoingCalled);
            Assert.True(containerMutator.MutateOutgoingCalled);
        }
        public async Task Time_to_live_should_be_passed_on_the_databus()
        {
            var context = new TestableOutgoingLogicalMessageContext();

            context.Message = new OutgoingLogicalMessage(typeof(MessageWithExplicitTimeToLive), new MessageWithExplicitTimeToLive
            {
                DataBusProperty = new DataBusProperty <string>("test")
            });

            context.Extensions.AddDeliveryConstraint(new DiscardIfNotReceivedBefore(TimeSpan.FromMinutes(1)));

            var fakeDatabus = new FakeDataBus();

            var sendBehavior = new DataBusSendBehavior(fakeDatabus, new DefaultDataBusSerializer(), new Conventions());

            await sendBehavior.Invoke(context, ctx => Task.CompletedTask);

            Assert.AreEqual(TimeSpan.FromMinutes(1), fakeDatabus.TTBRUsed);
        }
コード例 #29
0
        public async Task Should_set_content_type_header()
        {
            var registry = new MessageMetadataRegistry(new Conventions().IsMessageType);

            registry.RegisterMessageTypesFoundIn(new List <Type>
            {
                typeof(MyMessage)
            });

            var context = new TestableOutgoingLogicalMessageContext();

            context.Message = new OutgoingLogicalMessage(typeof(MyMessage), new MyMessage());

            var behavior = new SerializeMessageConnector(new FakeSerializer("myContentType"), registry);

            await behavior.Invoke(context, c => Task.CompletedTask);

            Assert.AreEqual("myContentType", context.Headers[Headers.ContentType]);
        }
        public async Task Should_fire_activity_start_stop_when_listener_attached()
        {
            var diagnosticListener = new DiagnosticListener("DummySource");
            var context            = new TestableOutgoingLogicalMessageContext();
            var processedFired     = false;

            diagnosticListener.Subscribe(new CallbackDiagnosticListener(pair =>
            {
                if (pair.Key == $"{ActivityNames.OutgoingLogicalMessage}.Sent")
                {
                    processedFired = true;
                }
            }));

            var behavior = new OutgoingLogicalMessageDiagnostics(diagnosticListener);

            await behavior.Invoke(context, () => Task.CompletedTask);

            processedFired.ShouldBeTrue();
        }
コード例 #31
0
        public void When_user_defined_conversation_id_would_overwrite_incoming_conversation_id_should_throw()
        {
            var incomingConversationId    = Guid.NewGuid().ToString();
            var userDefinedConversationId = Guid.NewGuid().ToString();

            var behavior = new AttachCausationHeadersBehavior(ReturnDefaultConversationId);
            var context  = new TestableOutgoingLogicalMessageContext
            {
                Headers =
                {
                    [Headers.ConversationId] = userDefinedConversationId
                }
            };
            var transportMessage = new IncomingMessage("xyz", new Dictionary <string, string>
            {
                { Headers.ConversationId, incomingConversationId }
            }, new byte[0]);

            context.Extensions.Set(transportMessage);

            var exception = Assert.ThrowsAsync <Exception>(() => behavior.Invoke(context, ctx => TaskEx.CompletedTask));

            Assert.AreEqual($"Cannot set the {Headers.ConversationId} header to '{userDefinedConversationId}' as it cannot override the incoming header value ('{incomingConversationId}'). To start a new conversation use sendOptions.StartNewConversation().", exception.Message);
        }