Beispiel #1
0
        public async Task Invoke_SuccessfullyRestoresDistributedProperties()
        {
            FlowingContext.Configuration.RegisterDistributedGlobal("global", ContextSerializers.String);
            FlowingContext.Configuration.RegisterDistributedProperty("property", ContextSerializers.String);

            var middleware  = new DistributedContextMiddleware();
            var httpContext = new TestingHttpContext();

            using (FlowingContext.Globals.Use("globalValue"))
                using (FlowingContext.Properties.Use("property", "propertyValue"))
                {
                    httpContext.Request.Headers.Add(HeaderNames.ContextProperties, FlowingContext.SerializeDistributedProperties());
                    httpContext.Request.Headers.Add(HeaderNames.ContextGlobals, FlowingContext.SerializeDistributedGlobals());
                }

            var nextInvoked = false;
            await middleware.InvokeAsync(httpContext, _ =>
            {
                nextInvoked = true;
                FlowingContext.Globals.Get <string>().Should().Be("globalValue");
                FlowingContext.Properties.Get <string>("property").Should().Be("propertyValue");
                return(Task.CompletedTask);
            });

            nextInvoked.Should().BeTrue();
        }
Beispiel #2
0
        public void Should_correctly_serialize_and_restore_distributed_properties_according_to_whitelist()
        {
            FlowingContext.Configuration.RegisterDistributedProperty(name1, ContextSerializers.String);
            FlowingContext.Configuration.RegisterDistributedProperty(name2, ContextSerializers.String);
            FlowingContext.Configuration.RegisterDistributedProperty(name4, ContextSerializers.String);
            FlowingContext.Configuration.RegisterDistributedProperty(name5, ContextSerializers.String);

            FlowingContext.Properties.Set(name1, "value1");
            FlowingContext.Properties.Set(name2, "value2");
            FlowingContext.Properties.Set(name3, "value3");
            FlowingContext.Properties.Set(name4, null);

            var serialized = FlowingContext.SerializeDistributedProperties();

            // (iloktionov): Now spoil all the properties:
            FlowingContext.Properties.Set(name1, null);
            FlowingContext.Properties.Set(name2, null);
            FlowingContext.Properties.Set(name3, null);
            FlowingContext.Properties.Set(name4, "value4");
            FlowingContext.Properties.Set(name5, "value5");

            FlowingContext.RestoreDistributedProperties(serialized);

            FlowingContext.Properties.Get <string>(name1).Should().Be("value1");
            FlowingContext.Properties.Get <string>(name2).Should().Be("value2");
            FlowingContext.Properties.Get <string>(name3).Should().BeNull();     // should not get restored due to whitelist
            FlowingContext.Properties.Get <string>(name4).Should().Be("value4"); // should not get restored due to null value
            FlowingContext.Properties.Get <string>(name5).Should().Be("value5"); // should not get restored due absence in original properties
        }
Beispiel #3
0
        public void SerializeDistributedProperties_should_return_null_when_all_registered_distributed_properties_have_null_values()
        {
            FlowingContext.Configuration.RegisterDistributedProperty(name1, ContextSerializers.Uri);

            FlowingContext.Properties.Set(name1, null);

            FlowingContext.SerializeDistributedProperties().Should().BeNull();
        }
Beispiel #4
0
        public Task <Response> SendAsync(Request request, TimeSpan?connectionTimeout, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var globals = FlowingContext.SerializeDistributedGlobals();

            if (globals != null)
            {
                request = request.WithHeader(HeaderNames.ContextGlobals, globals);
            }

            var properties = FlowingContext.SerializeDistributedProperties();

            if (properties != null)
            {
                request = request.WithHeader(HeaderNames.ContextProperties, properties);
            }

            return(transport.SendAsync(request, connectionTimeout, timeout, cancellationToken));
        }
Beispiel #5
0
        public void Should_skip_values_with_failing_serializers_during_properties_serialization_and_report_errors_to_listener()
        {
            FlowingContext.Configuration.RegisterDistributedProperty(name1, new FailingSerializer <string>());
            FlowingContext.Configuration.RegisterDistributedProperty(name2, ContextSerializers.String);

            FlowingContext.Properties.Set(name1, "value1");
            FlowingContext.Properties.Set(name2, "value2");

            var serialized = FlowingContext.SerializeDistributedProperties();

            // (iloktionov): Now spoil all the properties:
            FlowingContext.Properties.Set(name1, null);
            FlowingContext.Properties.Set(name2, null);

            FlowingContext.RestoreDistributedProperties(serialized);

            FlowingContext.Properties.Get <string>(name1).Should().BeNull();
            FlowingContext.Properties.Get <string>(name2).Should().Be("value2");

            errorCallback.Received(1).Invoke(Arg.Any <string>(), Arg.Any <Exception>());
        }
Beispiel #6
0
 public void SerializeDistributedProperties_should_return_null_when_no_distributed_properties_are_registered()
 {
     FlowingContext.SerializeDistributedProperties().Should().BeNull();
 }