Пример #1
0
        public void Returns_same_provider_for_same_type_of_configured_extensions_and_replaced_service_types()
        {
            var loggerFactory = new ListLoggerFactory();

            var config1 = CreateOptions <CoreOptionsExtension>(loggerFactory);

            config1 = config1.WithExtension(
                config1.FindExtension <CoreOptionsExtension>()
                .WithReplacedService(typeof(object), typeof(Random)));

            var config2 = CreateOptions <CoreOptionsExtension>(loggerFactory);

            config2 = config2.WithExtension(
                config2.FindExtension <CoreOptionsExtension>()
                .WithReplacedService(typeof(object), typeof(Random)));

            var cache = new ServiceProviderCache();

            Assert.Same(cache.GetOrAdd(config1, true), cache.GetOrAdd(config2, true));

            Assert.Equal(1, loggerFactory.Log.Count);

            Assert.Equal(
                CoreStrings.LogServiceProviderCreated.GenerateMessage(),
                loggerFactory.Log[0].Message);
        }
        public void Returns_different_provider_for_extensions_configured_in_different_order()
        {
            var loggerFactory = new ListLoggerFactory();

            var config1Log     = new List <string>();
            var config1Builder = new DbContextOptionsBuilder();

            ((IDbContextOptionsBuilderInfrastructure)config1Builder)
            .AddOrUpdateExtension(new FakeDbContextOptionsExtension1(config1Log));
            ((IDbContextOptionsBuilderInfrastructure)config1Builder)
            .AddOrUpdateExtension(new FakeDbContextOptionsExtension2(config1Log));
            config1Builder.UseLoggerFactory(loggerFactory);
            config1Builder.UseInMemoryDatabase(Guid.NewGuid().ToString());

            var config2Log     = new List <string>();
            var config2Builder = new DbContextOptionsBuilder();

            ((IDbContextOptionsBuilderInfrastructure)config2Builder)
            .AddOrUpdateExtension(new FakeDbContextOptionsExtension2(config2Log));
            ((IDbContextOptionsBuilderInfrastructure)config2Builder)
            .AddOrUpdateExtension(new FakeDbContextOptionsExtension1(config2Log));
            config2Builder.UseLoggerFactory(loggerFactory);
            config2Builder.UseInMemoryDatabase(Guid.NewGuid().ToString());

            var cache = new ServiceProviderCache();

            Assert.NotSame(cache.GetOrAdd(config1Builder.Options, true), cache.GetOrAdd(config2Builder.Options, true));

            Assert.Equal(2, loggerFactory.Log.Count);

            Assert.Equal(new[] { nameof(FakeDbContextOptionsExtension1), nameof(FakeDbContextOptionsExtension2) }, config1Log);
            Assert.Equal(new[] { nameof(FakeDbContextOptionsExtension2), nameof(FakeDbContextOptionsExtension1) }, config2Log);
        }
Пример #3
0
        public void Returns_different_provider_for_different_replaced_service_types()
        {
            var loggerFactory = new ListLoggerFactory();

            var config1 = CreateOptions <CoreOptionsExtension>(loggerFactory);

            config1 = config1.WithExtension(
                config1.FindExtension <CoreOptionsExtension>()
                .WithReplacedService(typeof(object), typeof(Random)));

            var config2 = CreateOptions <CoreOptionsExtension>(loggerFactory);

            config2 = config2.WithExtension(
                config2.FindExtension <CoreOptionsExtension>()
                .WithReplacedService(typeof(object), typeof(string)));

            var cache = new ServiceProviderCache();

            var first  = cache.GetOrAdd(config1, true);
            var second = cache.GetOrAdd(config2, true);

            Assert.NotSame(first, second);

            Assert.Equal(2, loggerFactory.Log.Count);

            Assert.Equal(
                CoreStrings.LogServiceProviderCreated.GenerateMessage(),
                loggerFactory.Log[0].Message);

            Assert.Equal(
                CoreStrings.LogServiceProviderDebugInfo.GenerateMessage(
                    CoreStrings.ServiceProviderConfigChanged("Core:ReplaceService:" + typeof(object).DisplayName())),
                loggerFactory.Log[1].Message);
        }
Пример #4
0
        public void Returns_different_provider_for_different_type_of_configured_extensions()
        {
            var loggerFactory = new ListLoggerFactory();

            var config1 = CreateOptions <FakeDbContextOptionsExtension1>(loggerFactory);
            var config2 = CreateOptions <FakeDbContextOptionsExtension2>(loggerFactory);

            var cache = new ServiceProviderCache();

            var first  = cache.GetOrAdd(config1, true);
            var second = cache.GetOrAdd(config2, true);

            Assert.NotSame(first, second);

            Assert.Equal(2, loggerFactory.Log.Count);

            Assert.Equal(
                CoreStrings.LogServiceProviderCreated.GenerateMessage(),
                loggerFactory.Log[0].Message);

            Assert.Equal(
                CoreStrings.LogServiceProviderDebugInfo.GenerateMessage(
                    string.Join(
                        ", ",
                        CoreStrings.ServiceProviderConfigRemoved(typeof(FakeDbContextOptionsExtension1).DisplayName()),
                        CoreStrings.ServiceProviderConfigAdded(typeof(FakeDbContextOptionsExtension2).DisplayName()))),
                loggerFactory.Log[1].Message);
        }
Пример #5
0
        public void Returns_same_provider_for_same_type_of_configured_extensions()
        {
            var config1 = CreateOptions <FakeDbContextOptionsExtension1>();
            var config2 = CreateOptions <FakeDbContextOptionsExtension1>();

            var cache = new ServiceProviderCache();

            Assert.Same(cache.GetOrAdd(config1), cache.GetOrAdd(config2));
        }
Пример #6
0
        public void Returns_different_provider_for_different_type_of_configured_extensions()
        {
            var config1 = CreateOptions <FakeDbContextOptionsExtension1>();
            var config2 = CreateOptions <FakeDbContextOptionsExtension2>();

            var cache = new ServiceProviderCache();

            Assert.NotSame(cache.GetOrAdd(config1, true), cache.GetOrAdd(config2, true));
        }
Пример #7
0
        public void Returns_different_provider_for_different_replaced_service_types()
        {
            var config1 = CreateOptions <CoreOptionsExtension>();

            config1.FindExtension <CoreOptionsExtension>().ReplaceService(typeof(object), typeof(Random));

            var config2 = CreateOptions <CoreOptionsExtension>();

            config2.FindExtension <CoreOptionsExtension>().ReplaceService(typeof(object), typeof(string));

            var cache = new ServiceProviderCache();

            Assert.NotSame(cache.GetOrAdd(config1), cache.GetOrAdd(config2));
        }
Пример #8
0
        public void Returns_same_provider_for_same_type_of_configured_extensions_and_replaced_service_types()
        {
            var config1 = CreateOptions <CoreOptionsExtension>();

            config1.FindExtension <CoreOptionsExtension>().ReplaceService(typeof(object), typeof(Random));

            var config2 = CreateOptions <CoreOptionsExtension>();

            config2.FindExtension <CoreOptionsExtension>().ReplaceService(typeof(object), typeof(Random));

            var cache = new ServiceProviderCache();

            Assert.Same(cache.GetOrAdd(config1), cache.GetOrAdd(config2));
        }
Пример #9
0
        public void Different_ILoggerFactory_instances_does_not_trigger_new_internal_provider()
        {
            var config1 = CreateOptions <CoreOptionsExtension>(new ListLoggerFactory());

            var loggerFactory = new ListLoggerFactory();

            var config2 = CreateOptions <CoreOptionsExtension>(loggerFactory);

            var cache = new ServiceProviderCache();

            var first  = cache.GetOrAdd(config1, true);
            var second = cache.GetOrAdd(config2, true);

            Assert.Same(first, second);
        }
        public void Returns_same_provider_for_same_type_of_configured_extensions()
        {
            var loggerFactory = new ListLoggerFactory();

            var config1 = CreateOptions <FakeDbContextOptionsExtension1>(loggerFactory);
            var config2 = CreateOptions <FakeDbContextOptionsExtension1>(loggerFactory);

            var cache = new ServiceProviderCache();

            Assert.Same(cache.GetOrAdd(config1, true), cache.GetOrAdd(config2, true));

            Assert.Equal(1, loggerFactory.Log.Count);

            Assert.Equal(
                CoreStrings.LogServiceProviderCreated.GenerateMessage(),
                loggerFactory.Log[0].Message);
        }
Пример #11
0
        public void Returns_same_provider_for_same_type_of_configured_extensions()
        {
            var loggerFactory = new ListLoggerFactory();

            var config1 = CreateOptions <FakeDbContextOptionsExtension1>(loggerFactory);
            var config2 = CreateOptions <FakeDbContextOptionsExtension1>(loggerFactory);

            var cache = new ServiceProviderCache();

            Assert.Same(cache.GetOrAdd(config1, true), cache.GetOrAdd(config2, true));

            Assert.Single(loggerFactory.Log);

            Assert.Equal(
                CoreResources.LogServiceProviderCreated(new TestLogger <TestLoggingDefinitions>()).GenerateMessage(),
                loggerFactory.Log[0].Message);
        }
Пример #12
0
        public void Returns_different_provider_for_configured_services_differing_by_instance()
        {
            var config1 = CreateOptions(b =>
            {
                b.GetService().AddSingleton <IFakeServiceA, FakeService1>();
                b.GetService().AddSingleton <FakeService3>();
                b.GetService().AddInstance(new FakeService4());
            });

            var config2 = CreateOptions(b =>
            {
                b.GetService().AddSingleton <IFakeServiceA, FakeService1>();
                b.GetService().AddSingleton <FakeService3>();
                b.GetService().AddInstance(new FakeService4());
            });

            var cache = new ServiceProviderCache();

            Assert.NotSame(cache.GetOrAdd(config1), cache.GetOrAdd(config2));
        }
        public void Returns_different_provider_for_configured_services_differing_by_lifetime()
        {
            var serviceInstance = new FakeService4();

            var config1 = BuildConfiguration(b =>
            {
                b.ServiceCollection.AddSingleton <IFakeServiceA, FakeService1>();
                b.ServiceCollection.AddSingleton <FakeService3>();
                b.ServiceCollection.AddInstance(serviceInstance);
            });

            var config2 = BuildConfiguration(b =>
            {
                b.ServiceCollection.AddSingleton <IFakeServiceA, FakeService1>();
                b.ServiceCollection.AddScoped <FakeService3>();
                b.ServiceCollection.AddInstance(serviceInstance);
            });

            var cache = new ServiceProviderCache();

            Assert.NotSame(cache.GetOrAdd(config1), cache.GetOrAdd(config2));
        }
Пример #14
0
        public void Reports_debug_info_for_different_ILoggerFactory_instances()
        {
            var config1 = CreateOptions <CoreOptionsExtension>(new ListLoggerFactory());

            var loggerFactory = new ListLoggerFactory();

            var config2 = CreateOptions <CoreOptionsExtension>(loggerFactory);

            var cache = new ServiceProviderCache();

            var first  = cache.GetOrAdd(config1, true);
            var second = cache.GetOrAdd(config2, true);

            Assert.NotSame(first, second);

            Assert.Equal(1, loggerFactory.Log.Count);

            Assert.Equal(
                CoreStrings.LogServiceProviderDebugInfo.GenerateMessage(
                    CoreStrings.ServiceProviderConfigChanged("Core:UseLoggerFactory")),
                loggerFactory.Log[0].Message);
        }
Пример #15
0
        public void Returns_same_provider_for_same_set_of_configured_services()
        {
            var serviceInstance = new FakeService4();

            var config1 = CreateOptions(b =>
            {
                b.GetService().AddSingleton <IFakeServiceA, FakeService1>();
                b.GetService().AddSingleton <FakeService3>();
                b.GetService().AddInstance(serviceInstance);
            });

            var config2 = CreateOptions(b =>
            {
                b.GetService().AddSingleton <IFakeServiceA, FakeService1>();
                b.GetService().AddSingleton <FakeService3>();
                b.GetService().AddInstance(serviceInstance);
            });

            var cache = new ServiceProviderCache();

            Assert.Same(cache.GetOrAdd(config1), cache.GetOrAdd(config2));
        }
Пример #16
0
        public void Reports_debug_info_for_most_similar_existing_service_provider()
        {
            // Do this a bunch of times since in the past this exposed issues with cache collisions
            for (var i = 0; i < 1000; i++)
            {
                var loggerFactory = new ListLoggerFactory();

                var config1 = new DbContextOptionsBuilder(CreateOptions <CoreOptionsExtension>(loggerFactory))
                              .EnableDetailedErrors()
                              .EnableSensitiveDataLogging()
                              .ConfigureWarnings(w => w.Throw(CoreEventId.CascadeDelete))
                              .Options;

                var config2 = new DbContextOptionsBuilder(CreateOptions <CoreOptionsExtension>(loggerFactory))
                              .EnableDetailedErrors()
                              .EnableSensitiveDataLogging()
                              .ConfigureWarnings(w => w.Throw(CoreEventId.CascadeDeleteOrphan))
                              .Options;

                var config3 = new DbContextOptionsBuilder(CreateOptions <CoreOptionsExtension>(loggerFactory))
                              .EnableDetailedErrors()
                              .ConfigureWarnings(w => w.Throw(CoreEventId.CascadeDelete))
                              .Options;

                var config4 = new DbContextOptionsBuilder(CreateOptions <CoreOptionsExtension>(loggerFactory))
                              .EnableSensitiveDataLogging()
                              .ConfigureWarnings(w => w.Throw(CoreEventId.ContextDisposed))
                              .Options;

                var cache = new ServiceProviderCache();

                var first  = cache.GetOrAdd(config1, true);
                var second = cache.GetOrAdd(config2, true);
                var third  = cache.GetOrAdd(config3, true);
                var forth  = cache.GetOrAdd(config4, true);

                Assert.NotSame(first, second);
                Assert.NotSame(first, third);
                Assert.NotSame(first, forth);
                Assert.NotSame(second, third);
                Assert.NotSame(second, forth);
                Assert.NotSame(third, forth);

                Assert.Equal(4, loggerFactory.Log.Count);

                Assert.Equal(
                    CoreResources.LogServiceProviderCreated(new TestLogger <TestLoggingDefinitions>()).GenerateMessage(),
                    loggerFactory.Log[0].Message);

                Assert.Equal(
                    CoreResources.LogServiceProviderDebugInfo(new TestLogger <TestLoggingDefinitions>()).GenerateMessage(
                        CoreStrings.ServiceProviderConfigChanged("Core:ConfigureWarnings")),
                    loggerFactory.Log[1].Message);

                Assert.Equal(
                    CoreResources.LogServiceProviderDebugInfo(new TestLogger <TestLoggingDefinitions>()).GenerateMessage(
                        CoreStrings.ServiceProviderConfigChanged("Core:EnableSensitiveDataLogging")),
                    loggerFactory.Log[2].Message);

                Assert.Equal(
                    CoreResources.LogServiceProviderDebugInfo(new TestLogger <TestLoggingDefinitions>()).GenerateMessage(
                        string.Join(
                            ", ",
                            CoreStrings.ServiceProviderConfigChanged("Core:EnableDetailedErrors"),
                            CoreStrings.ServiceProviderConfigChanged("Core:ConfigureWarnings"))),
                    loggerFactory.Log[3].Message);
            }
        }
Пример #17
0
        public void Reports_debug_info_for_most_similar_existing_service_provider()
        {
            var loggerFactory = new ListLoggerFactory();

            var config1 = new DbContextOptionsBuilder(CreateOptions <CoreOptionsExtension>(loggerFactory))
                          .EnableRichDataErrorHandling()
                          .EnableSensitiveDataLogging()
                          .ConfigureWarnings(w => w.Throw(CoreEventId.CascadeDelete))
                          .Options;

            var config2 = new DbContextOptionsBuilder(CreateOptions <CoreOptionsExtension>(loggerFactory))
                          .EnableRichDataErrorHandling()
                          .EnableSensitiveDataLogging()
                          .ConfigureWarnings(w => w.Throw(CoreEventId.CascadeDeleteOrphan))
                          .Options;

            var config3 = new DbContextOptionsBuilder(CreateOptions <CoreOptionsExtension>(loggerFactory))
                          .EnableRichDataErrorHandling()
                          .ConfigureWarnings(w => w.Throw(CoreEventId.CascadeDelete))
                          .Options;

            var config4 = new DbContextOptionsBuilder(CreateOptions <CoreOptionsExtension>(loggerFactory))
                          .EnableSensitiveDataLogging()
                          .ConfigureWarnings(w => w.Throw(CoreEventId.ContextDisposed))
                          .Options;

            var cache = new ServiceProviderCache();

            var first  = cache.GetOrAdd(config1, true);
            var second = cache.GetOrAdd(config2, true);
            var third  = cache.GetOrAdd(config3, true);
            var forth  = cache.GetOrAdd(config4, true);

            Assert.NotSame(first, second);
            Assert.NotSame(second, third);
            Assert.NotSame(third, forth);

            Assert.Equal(4, loggerFactory.Log.Count);

            Assert.Equal(
                CoreStrings.LogServiceProviderCreated.GenerateMessage(),
                loggerFactory.Log[0].Message);

            Assert.Equal(
                CoreStrings.LogServiceProviderDebugInfo.GenerateMessage(
                    CoreStrings.ServiceProviderConfigChanged("Core:ConfigureWarnings")),
                loggerFactory.Log[1].Message);

            Assert.Equal(
                CoreStrings.LogServiceProviderDebugInfo.GenerateMessage(
                    CoreStrings.ServiceProviderConfigChanged("Core:EnableSensitiveDataLogging")),
                loggerFactory.Log[2].Message);

            Assert.Equal(
                CoreStrings.LogServiceProviderDebugInfo.GenerateMessage(
                    string.Join(
                        ", ",
                        CoreStrings.ServiceProviderConfigChanged("Core:EnableRichDataErrorHandling"),
                        CoreStrings.ServiceProviderConfigChanged("Core:ConfigureWarnings"))),
                loggerFactory.Log[3].Message);
        }