Esempio n. 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();
        }
Esempio n. 2
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
        }
Esempio n. 3
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();
        }
Esempio n. 4
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);
        }
Esempio n. 5
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));
        }
Esempio n. 6
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>());
        }
Esempio n. 7
0
        public void SerializeDistributedGlobals_should_return_null_when_no_distributed_globals_are_registered()
        {
            FlowingContext.Globals.Set("whatever");

            FlowingContext.SerializeDistributedGlobals().Should().BeNull();
        }