Ejemplo n.º 1
0
        public async Task Should_start_and_log_activity()
        {
            var startCalled = false;

            using var listener = new ActivityListener
                  {
                      ShouldListenTo  = source => source.Name == "NServiceBus.Extensions.Diagnostics",
                      Sample          = (ref ActivityCreationOptions <ActivityContext> _) => ActivitySamplingResult.AllDataAndRecorded,
                      ActivityStarted = activity =>
                      {
                          startCalled = true;
                          activity.ShouldNotBeNull();
                          activity.OperationName.ShouldBe(ActivityNames.IncomingPhysicalMessage);
                      },
                  };
            ActivitySource.AddActivityListener(listener);

            var context = new TestableIncomingPhysicalMessageContext();

            var behavior = new IncomingPhysicalMessageDiagnostics(new FakeActivityEnricher());

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

            startCalled.ShouldBeTrue();
        }
        public async Task Should_add_headers_to_tags()
        {
            var diagnosticListener = new DiagnosticListener("DummySource");
            var startCalled        = false;

            diagnosticListener.Subscribe(new CallbackDiagnosticListener(pair =>
            {
                if (pair.Key == $"{ActivityNames.IncomingPhysicalMessage}.Start")
                {
                    startCalled = true;

                    var started = Activity.Current;
                    started.ShouldNotBeNull();
                    started.Tags.ShouldNotContain(kvp => kvp.Key == "foo");
                    started.Tags.ShouldContain(kvp => kvp.Key == "SomeHeader1" && kvp.Value == "SomeValue1");
                    started.Tags.ShouldContain(kvp => kvp.Key == "SomeHeader2" && kvp.Value == "SomeValue2");
                }
            }));

            var context = new TestableIncomingPhysicalMessageContext
            {
                MessageHeaders =
                {
                    { "foo",                     "bar"        },
                    { "NServiceBus.SomeHeader1", "SomeValue1" },
                    { "NServiceBus.SomeHeader2", "SomeValue2" },
                }
            };

            var behavior = new IncomingPhysicalMessageDiagnostics(diagnosticListener);

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

            startCalled.ShouldBeTrue();
        }
        public async Task Should_fire_activity_start_stop_when_listener_attached()
        {
            var diagnosticListener = new DiagnosticListener("DummySource");
            var context            = new TestableIncomingPhysicalMessageContext();
            var stopFired          = false;
            var startFired         = false;

            diagnosticListener.Subscribe(new CallbackDiagnosticListener(pair =>
            {
                if (pair.Key == $"{ActivityNames.IncomingPhysicalMessage}.Start")
                {
                    startFired = true;
                }

                if (pair.Key == $"{ActivityNames.IncomingPhysicalMessage}.Stop")
                {
                    stopFired = true;
                }
            }));

            var behavior = new IncomingPhysicalMessageDiagnostics(diagnosticListener);

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

            startFired.ShouldBeTrue();
            stopFired.ShouldBeTrue();
        }
        public async Task Should_start_and_log_activity()
        {
            var diagnosticListener = new DiagnosticListener("DummySource");
            var startCalled        = false;

            diagnosticListener.Subscribe(new CallbackDiagnosticListener(pair =>
            {
                if (pair.Key == $"{ActivityNames.IncomingPhysicalMessage}.Start")
                {
                    startCalled = true;
                    pair.Value.ShouldNotBeNull();
                    Activity.Current.ShouldNotBeNull();
                    Activity.Current.OperationName.ShouldBe(ActivityNames.IncomingPhysicalMessage);
                    pair.Value.ShouldBeAssignableTo <IIncomingPhysicalMessageContext>();
                }
            }));

            var context = new TestableIncomingPhysicalMessageContext();

            var behavior = new IncomingPhysicalMessageDiagnostics(diagnosticListener);

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

            startCalled.ShouldBeTrue();
        }
Ejemplo n.º 5
0
        public async Task Should_add_headers_to_tags()
        {
            var startCalled = false;

            using var listener = new ActivityListener
                  {
                      ShouldListenTo  = source => source.Name == "NServiceBus.Extensions.Diagnostics",
                      Sample          = (ref ActivityCreationOptions <ActivityContext> _) => ActivitySamplingResult.AllDataAndRecorded,
                      ActivityStarted = activity =>
                      {
                          startCalled = true;

                          activity.ShouldNotBeNull();
                          activity.Tags.ShouldNotContain(kvp => kvp.Key == "foo");
                      },
                  };
            ActivitySource.AddActivityListener(listener);

            var context = new TestableIncomingPhysicalMessageContext
            {
                MessageHeaders =
                {
                    { "foo", "bar" },
                }
            };

            var behavior = new IncomingPhysicalMessageDiagnostics(new FakeActivityEnricher());

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

            startCalled.ShouldBeTrue();
        }
Ejemplo n.º 6
0
        public IncomingPhysicalMessageBehaviorTestsFixture()
        {
            Behavior          = new IncomingPhysicalMessageBehavior();
            Builder           = new FakeBuilder();
            UnitOfWorkContext = new Mock <IUnitOfWorkContext>();

            Context = new TestableIncomingPhysicalMessageContext
            {
                Builder = Builder
            };

            UnitOfWorkContext.Setup(c => c.Set <DbConnection>(null)).Callback <DbConnection>(t =>
            {
                if (NextTaskInvoked)
                {
                    throw new Exception("Set called too late");
                }
            });

            UnitOfWorkContext.Setup(c => c.Set <DbTransaction>(null)).Callback <DbTransaction>(t =>
            {
                if (NextTaskInvoked)
                {
                    throw new Exception("Set called too late");
                }
            });

            Builder.Register(UnitOfWorkContext.Object);
        }
Ejemplo n.º 7
0
    public Task IncomingPhysicalMessageContext()
    {
        var context = new TestableIncomingPhysicalMessageContext
        {
            Message = BuildIncomingMessage(),
        };

        return(Verify(context));
    }
    public async Task Empty()
    {
        var logBuilder = new LogBuilder(new FakeLogger(), "endpoint");
        var behavior   = new InjectIncomingPhysicalBehavior(logBuilder, "endpoint");
        var context    = new TestableIncomingPhysicalMessageContext();
        await behavior.Invoke(context, () => Task.CompletedTask);

        await Verify(context);
    }
    public Task IncomingPhysicalMessageContext()
    {
        var context = new TestableIncomingPhysicalMessageContext
        {
            Message = BuildIncomingMessage(),
        };

        context.Extensions.Set("key", "value");
        return(Verify(context));
    }
Ejemplo n.º 10
0
        public void Should_throw_friendly_exception_when_IMutateIncomingTransportMessages_MutateIncoming_returns_null()
        {
            var behavior = new MutateIncomingTransportMessageBehavior(new HashSet <IMutateIncomingTransportMessages>());

            var context = new TestableIncomingPhysicalMessageContext();

            context.Builder.Register <IMutateIncomingTransportMessages>(() => new MutateIncomingTransportMessagesReturnsNull());

            Assert.That(async() => await behavior.Invoke(context, ctx => TaskEx.CompletedTask), Throws.Exception.With.Message.EqualTo("Return a Task or mark the method as async."));
        }
    public async Task WithMultipleMessageTypesAssemblyQualifiedName()
    {
        var logBuilder = new LogBuilder(new FakeLogger(), "endpoint");
        var behavior   = new InjectIncomingPhysicalBehavior(logBuilder, "endpoint");
        var context    = new TestableIncomingPhysicalMessageContext();

        context.MessageHeaders.Add(Headers.EnclosedMessageTypes, $"{typeof(Message1).AssemblyQualifiedName};{typeof(Message2).AssemblyQualifiedName}");
        await behavior.Invoke(context, () => Task.CompletedTask);

        await Verify(context);
    }
Ejemplo n.º 12
0
        public async Task Should_not_call_MutateIncoming_when_hasIncomingTransportMessageMutators_is_false()
        {
            var behavior = new MutateIncomingTransportMessageBehavior(hasIncomingTransportMessageMutators: false);

            var context = new TestableIncomingPhysicalMessageContext();

            var mutator = new MutatorThatIndicatesIfItWasCalled();

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

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

            Assert.IsFalse(mutator.MutateIncomingCalled);
        }
Ejemplo n.º 13
0
            public async Task Invoke(ITransportReceiveContext context, Func <IIncomingPhysicalMessageContext, Task> next)
            {
                context.PrintInstanceWithRunSpecificIfPossible(instance, writer);

                var physicalMessageContext = new TestableIncomingPhysicalMessageContext();

                physicalMessageContext.Extensions.Merge(context.Extensions);

                await next(physicalMessageContext).ConfigureAwait(false);

                var dispatchContext = new TestableBatchDispatchContext();

                dispatchContext.Extensions.Merge(context.Extensions);

                await this.Fork(dispatchContext).ConfigureAwait(false);
            }
Ejemplo n.º 14
0
        public async Task Should_invoke_all_explicit_mutators()
        {
            var mutator      = new MutatorThatIndicatesIfItWasCalled();
            var otherMutator = new MutatorThatIndicatesIfItWasCalled();

            var behavior = new MutateIncomingTransportMessageBehavior(new HashSet <IMutateIncomingTransportMessages> {
                mutator, otherMutator
            });

            var context = new TestableIncomingPhysicalMessageContext();

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

            Assert.True(mutator.MutateIncomingCalled);
            Assert.True(otherMutator.MutateIncomingCalled);
        }
Ejemplo n.º 15
0
        public async Task Should_not_call_MutateIncoming_when_hasIncomingTransportMessageMutators_is_false()
        {
            var behavior = new MutateIncomingTransportMessageBehavior(new HashSet <IMutateIncomingTransportMessages>());

            var context = new TestableIncomingPhysicalMessageContext();

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

            var mutator = new MutatorThatIndicatesIfItWasCalled();

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

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

            Assert.IsFalse(mutator.MutateIncomingCalled);
        }
Ejemplo n.º 16
0
        static Task InvokeBehavior(FakeBuilder builder, Exception toThrow = null)
        {
            var runner = new UnitOfWorkBehavior();

            var context = new TestableIncomingPhysicalMessageContext();

            context.Builder = builder;

            return(runner.Invoke(context, ctx =>
            {
                if (toThrow != null)
                {
                    throw toThrow;
                }
                return TaskEx.CompletedTask;
            }));
        }
Ejemplo n.º 17
0
        public async Task Should_invoke_both_explicit_and_container_provided_mutators()
        {
            var explicitMutator  = new MutatorThatIndicatesIfItWasCalled();
            var containerMutator = new MutatorThatIndicatesIfItWasCalled();

            var behavior = new MutateIncomingTransportMessageBehavior(new HashSet <IMutateIncomingTransportMessages> {
                explicitMutator
            });

            var context = new TestableIncomingPhysicalMessageContext();

            context.Builder.Register <IMutateIncomingTransportMessages>(() => containerMutator);

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

            Assert.True(explicitMutator.MutateIncomingCalled);
            Assert.True(containerMutator.MutateIncomingCalled);
        }
Ejemplo n.º 18
0
        static Task InvokeBehavior(IServiceCollection services, Exception toThrow = null, UnitOfWorkBehavior behavior = null)
        {
            var runner = behavior ?? new UnitOfWorkBehavior();

            var context = new TestableIncomingPhysicalMessageContext();

            context.Services = services;

            return(runner.Invoke(context, ctx =>
            {
                if (toThrow != null)
                {
                    throw toThrow;
                }

                return Task.CompletedTask;
            }));
        }
        public async Task Should_start_activity_and_set_appropriate_headers()
        {
            // Generate an id we can use for the request id header (in the correct format)
            var activity = new Activity("IncomingRequest");

            activity.Start();
            var id = activity.Id;

            activity.Stop();

            var      diagnosticListener = new DiagnosticListener("DummySource");
            Activity started            = null;

            diagnosticListener.Subscribe(new CallbackDiagnosticListener(pair =>
            {
                if (pair.Key == $"{ActivityNames.IncomingPhysicalMessage}.Start")
                {
                    started = Activity.Current;
                }
            }));

            var tracestate = "TraceState";

            var context = new TestableIncomingPhysicalMessageContext
            {
                MessageHeaders =
                {
                    { "traceparent",         id                         },
                    { "tracestate",          tracestate                 },
                    { "Correlation-Context", "Key1=value1, Key2=value2" }
                }
            };

            var behavior = new IncomingPhysicalMessageDiagnostics(diagnosticListener);

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

            started.ShouldNotBeNull();
            started.ParentId.ShouldBe(id);
            started.TraceStateString.ShouldBe(tracestate);
            started.Baggage.ShouldContain(kvp => kvp.Key == "Key1" && kvp.Value == "value1");
            started.Baggage.ShouldContain(kvp => kvp.Key == "Key2" && kvp.Value == "value2");
        }
Ejemplo n.º 20
0
        public async Task Should_start_activity_and_parse_appropriate_headers()
        {
            // Generate an id we can use for the request id header (in the correct format)
            using var dummy = new Activity("IncomingRequest");
            dummy.Start();
            var id = dummy.Id;

            dummy.Stop();

            Activity started = null;

            using var listener = new ActivityListener
                  {
                      ShouldListenTo  = source => source.Name == "NServiceBus.Extensions.Diagnostics",
                      Sample          = (ref ActivityCreationOptions <ActivityContext> _) => ActivitySamplingResult.AllDataAndRecorded,
                      ActivityStarted = activity => started = activity,
                  };
            ActivitySource.AddActivityListener(listener);

            var tracestate = "TraceState";

            var context = new TestableIncomingPhysicalMessageContext
            {
                MessageHeaders =
                {
                    { "traceparent", id                         },
                    { "tracestate",  tracestate                 },
                    { "baggage",     "Key1=value1, Key2=value2" }
                }
            };

            var behavior = new IncomingPhysicalMessageDiagnostics(new FakeActivityEnricher());

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

            started.ShouldNotBeNull();
            started.ParentId.ShouldBe(id);
            started.TraceStateString.ShouldBe(tracestate);
            started.Baggage.ShouldContain(kvp => kvp.Key == "Key1" && kvp.Value == "value1");
            started.Baggage.ShouldContain(kvp => kvp.Key == "Key2" && kvp.Value == "value2");
        }
Ejemplo n.º 21
0
        public async Task Should_not_fire_activity_start_stop_when_no_listener_attached()
        {
            var context    = new TestableIncomingPhysicalMessageContext();
            var stopFired  = false;
            var startFired = false;

            using var listener = new ActivityListener
                  {
                      ShouldListenTo  = source => source.Name == "Nonsense",
                      ActivityStarted = _ => startFired = true,
                      ActivityStopped = _ => stopFired = true
                  };
            ActivitySource.AddActivityListener(listener);

            var behavior = new IncomingPhysicalMessageDiagnostics(new FakeActivityEnricher());

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

            startFired.ShouldBeFalse();
            stopFired.ShouldBeFalse();
        }
Ejemplo n.º 22
0
        public async Task Should_fire_activity_start_stop_when_listener_attached()
        {
            var context    = new TestableIncomingPhysicalMessageContext();
            var stopFired  = false;
            var startFired = false;

            using var listener = new ActivityListener
                  {
                      ShouldListenTo  = source => source.Name == "NServiceBus.Extensions.Diagnostics",
                      Sample          = (ref ActivityCreationOptions <ActivityContext> _) => ActivitySamplingResult.AllDataAndRecorded,
                      ActivityStarted = _ => startFired = true,
                      ActivityStopped = _ => stopFired = true
                  };
            ActivitySource.AddActivityListener(listener);

            var behavior = new IncomingPhysicalMessageDiagnostics(new FakeActivityEnricher());

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

            startFired.ShouldBeTrue();
            stopFired.ShouldBeTrue();
        }
        static Task InvokeBehavior(FakeBuilder builder, Exception toThrow = null, bool hasUnitsOfWork = true)
        {
            var runner = new UnitOfWorkBehavior(hasUnitsOfWork);

            var context = new TestableIncomingPhysicalMessageContext();
            context.Builder = builder;

            return runner.Invoke(context, ctx =>
            {
                if (toThrow != null)
                {
                    throw toThrow;
                }
                return TaskEx.CompletedTask;
            });
        }