Ejemplo n.º 1
0
        public void Should_correctly_serialize_and_restore_distributed_globals_according_to_whitelist()
        {
            FlowingContext.Configuration.RegisterDistributedGlobal(name1, ContextSerializers.Uri);
            FlowingContext.Configuration.RegisterDistributedGlobal(name2, ContextSerializers.TimeSpan);
            FlowingContext.Configuration.RegisterDistributedGlobal(name4, ContextSerializers.IPAddress);

            FlowingContext.Globals.Set(new Uri("https://kontur.ru"));
            FlowingContext.Globals.Set(5.Hours());
            FlowingContext.Globals.Set(123);

            var serialized = FlowingContext.SerializeDistributedGlobals();

            // (iloktionov): Now spoil all the globals:
            FlowingContext.Globals.Set(null as Uri);
            FlowingContext.Globals.Set(default(TimeSpan));
            FlowingContext.Globals.Set(default(int));
            FlowingContext.Globals.Set(IPAddress.Loopback);

            FlowingContext.RestoreDistributedGlobals(serialized);

            FlowingContext.Globals.Get <Uri>().Should().Be(new Uri("https://kontur.ru"));
            FlowingContext.Globals.Get <TimeSpan>().Should().Be(5.Hours());
            FlowingContext.Globals.Get <int>().Should().Be(0);                        // should not get restored due to whitelist
            FlowingContext.Globals.Get <IPAddress>().Should().Be(IPAddress.Loopback); // should not get restored due to null value
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
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
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
0
        public void SerializeDistributedGlobals_should_return_null_when_all_registered_distributed_globals_have_null_values()
        {
            FlowingContext.Configuration.RegisterDistributedGlobal(name1, ContextSerializers.Uri);

            FlowingContext.Globals.Set(null as Uri);

            FlowingContext.SerializeDistributedGlobals().Should().BeNull();
        }
Ejemplo n.º 6
0
        public void Should_register_trace_context_as_a_distributed_global_in_flowing_context_configuration()
        {
            string serialized;

            var context = new TraceContext(Guid.NewGuid(), Guid.NewGuid());

            using (FlowingContext.Globals.Use(context))
            {
                serialized = FlowingContext.SerializeDistributedGlobals();
            }

            FlowingContext.RestoreDistributedGlobals(serialized);

            FlowingContext.Globals.Get <TraceContext>().Should().BeEquivalentTo(context);
        }
Ejemplo n.º 7
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));
        }
Ejemplo n.º 8
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>());
        }
Ejemplo n.º 9
0
        public void Should_skip_values_with_failing_serializers_during_globals_serialization_and_report_errors_to_listener()
        {
            FlowingContext.Configuration.RegisterDistributedGlobal(name1, new FailingSerializer <Uri>());
            FlowingContext.Configuration.RegisterDistributedGlobal(name2, ContextSerializers.String);

            FlowingContext.Globals.Set(new Uri("https://kontur.ru"));
            FlowingContext.Globals.Set("whatever");

            var serialized = FlowingContext.SerializeDistributedGlobals();

            // (iloktionov): Now spoil all the globals:
            FlowingContext.Globals.Set(null as Uri);
            FlowingContext.Globals.Set(null as string);

            FlowingContext.RestoreDistributedGlobals(serialized);

            FlowingContext.Globals.Get <Uri>().Should().BeNull();
            FlowingContext.Globals.Get <string>().Should().Be("whatever");

            errorCallback.Received(1).Invoke(Arg.Any <string>(), Arg.Any <Exception>());
        }
Ejemplo n.º 10
0
 public void RestoreDistributedGlobals_should_not_fail_on_null_input()
 {
     FlowingContext.RestoreDistributedGlobals(null);
 }
Ejemplo n.º 11
0
 public void SerializeDistributedProperties_should_return_null_when_no_distributed_properties_are_registered()
 {
     FlowingContext.SerializeDistributedProperties().Should().BeNull();
 }
Ejemplo n.º 12
0
        public void SerializeDistributedGlobals_should_return_null_when_no_distributed_globals_are_registered()
        {
            FlowingContext.Globals.Set("whatever");

            FlowingContext.SerializeDistributedGlobals().Should().BeNull();
        }
Ejemplo n.º 13
0
        public void RestoreDistributedProperties_should_not_fail_on_incorrect_input()
        {
            FlowingContext.RestoreDistributedProperties("whatever");

            errorCallback.Received(1).Invoke(Arg.Any <string>(), Arg.Any <Exception>());
        }
Ejemplo n.º 14
0
 public void RestoreDistributedProperties_should_not_fail_on_null_input()
 {
     FlowingContext.RestoreDistributedProperties(null);
 }
Ejemplo n.º 15
0
 public async Task InvokeAsync(HttpContext context, RequestDelegate next)
 {
     FlowingContext.RestoreDistributedGlobals(context.Request.Headers["Context-Globals"]);
     await next(context).ConfigureAwait(false);
 }
Ejemplo n.º 16
0
 public async Task InvokeAsync(HttpContext context, RequestDelegate next)
 {
     FlowingContext.RestoreDistributedProperties(context.Request.Headers[HeaderNames.ContextProperties]);
     FlowingContext.RestoreDistributedGlobals(context.Request.Headers[HeaderNames.ContextGlobals]);
     await next(context).ConfigureAwait(false);
 }