From a66cdc9b8d20c9e5754083debd47b7a7029bb3de Mon Sep 17 00:00:00 2001 From: "m.volkau" Date: Wed, 25 Mar 2020 16:43:20 +0300 Subject: [PATCH] fixed the test to be run sequentially --- .../PrismContainerExtension.cs | 3 +- .../PrismContainerExtension.cs | 1 + ...ontainer.Extensions.Shared.Tests.projitems | 1 + .../Tests/CommonAspNetServiceTests.cs | 8 +- .../Tests/SharedTestCollection.cs | 14 + .../ContainerTests.cs | 501 ++++++++---------- .../MicrosoftExtensionsTests.cs | 215 ++++---- 7 files changed, 323 insertions(+), 420 deletions(-) create mode 100644 tests/Prism.Container.Extensions.Shared.Tests/Tests/SharedTestCollection.cs diff --git a/src/Prism.DryIoc.Extensions/PrismContainerExtension.cs b/src/Prism.DryIoc.Extensions/PrismContainerExtension.cs index 0b9a53b..fa9a72f 100644 --- a/src/Prism.DryIoc.Extensions/PrismContainerExtension.cs +++ b/src/Prism.DryIoc.Extensions/PrismContainerExtension.cs @@ -35,7 +35,8 @@ public static IContainerExtension Current internal static void Reset() { _current = null; - GC.Collect(); + GC.Collect(Int32.MaxValue, GCCollectionMode.Forced); + GC.WaitForFullGCComplete(); } public static IContainerExtension Create() => diff --git a/src/Prism.Unity.Extensions/PrismContainerExtension.cs b/src/Prism.Unity.Extensions/PrismContainerExtension.cs index 6684c20..ad4d3e2 100644 --- a/src/Prism.Unity.Extensions/PrismContainerExtension.cs +++ b/src/Prism.Unity.Extensions/PrismContainerExtension.cs @@ -49,6 +49,7 @@ public static IContainerExtension Create(IUnityContainer container) return new PrismContainerExtension(container); } + private PrismContainerExtension() : this(new UnityContainer()) { diff --git a/tests/Prism.Container.Extensions.Shared.Tests/Prism.Container.Extensions.Shared.Tests.projitems b/tests/Prism.Container.Extensions.Shared.Tests/Prism.Container.Extensions.Shared.Tests.projitems index f12f06b..28521af 100644 --- a/tests/Prism.Container.Extensions.Shared.Tests/Prism.Container.Extensions.Shared.Tests.projitems +++ b/tests/Prism.Container.Extensions.Shared.Tests/Prism.Container.Extensions.Shared.Tests.projitems @@ -11,6 +11,7 @@ + \ No newline at end of file diff --git a/tests/Prism.Container.Extensions.Shared.Tests/Tests/CommonAspNetServiceTests.cs b/tests/Prism.Container.Extensions.Shared.Tests/Tests/CommonAspNetServiceTests.cs index 9f36742..bf45a87 100644 --- a/tests/Prism.Container.Extensions.Shared.Tests/Tests/CommonAspNetServiceTests.cs +++ b/tests/Prism.Container.Extensions.Shared.Tests/Tests/CommonAspNetServiceTests.cs @@ -17,8 +17,14 @@ namespace Prism.Container.Extensions.Shared.Tests { + [Collection(nameof(SharedTests))] public class CommonAspNetServiceTests { + public CommonAspNetServiceTests() + { + PrismContainerExtension.Reset(); + } + [Fact] public void CreatingServicesDoesNotThrowException() { @@ -91,7 +97,6 @@ public void ScopingProvidesNewInstance() [Fact] public void RegisterServicesExtensionsAddsServicesToContainer() { - PrismContainerExtension.Reset(); PrismContainerExtension.Current.RegisterServices(s => { s.AddHttpClient(); @@ -102,7 +107,6 @@ public void RegisterServicesExtensionsAddsServicesToContainer() private void ConfigureServices() { - PrismContainerExtension.Reset(); var services = new ServiceCollection(); services.AddDbContext(o => o.UseInMemoryDatabase("test")); services.AddHttpClient(); diff --git a/tests/Prism.Container.Extensions.Shared.Tests/Tests/SharedTestCollection.cs b/tests/Prism.Container.Extensions.Shared.Tests/Tests/SharedTestCollection.cs new file mode 100644 index 0000000..073ce73 --- /dev/null +++ b/tests/Prism.Container.Extensions.Shared.Tests/Tests/SharedTestCollection.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace Prism.Container.Extensions.Shared.Tests +{ + public class SharedTests { } + + [Collection(nameof(SharedTests))] + public class SharedTestCollection : ICollectionFixture + { + } +} diff --git a/tests/Prism.DryIoc.Extensions.Tests/ContainerTests.cs b/tests/Prism.DryIoc.Extensions.Tests/ContainerTests.cs index b9f0481..475a830 100644 --- a/tests/Prism.DryIoc.Extensions.Tests/ContainerTests.cs +++ b/tests/Prism.DryIoc.Extensions.Tests/ContainerTests.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using DryIoc; using Prism.Container.Extensions.Shared.Mocks; +using Prism.Container.Extensions.Shared.Tests; using Prism.Container.Extensions.Tests.Mocks; using Prism.Ioc; using Xunit; @@ -10,322 +10,283 @@ namespace Prism.DryIoc.Extensions.Tests { + [Collection(nameof(SharedTests))] public class ContainerTests { - private readonly object testLock = new object(); - private ITestOutputHelper _testOutputHelper; + private readonly ITestOutputHelper _testOutputHelper; - public ContainerTests(ITestOutputHelper testOutputHelper) => + public ContainerTests(ITestOutputHelper testOutputHelper) + { _testOutputHelper = testOutputHelper; + PrismContainerExtension.Reset(); + } [Fact] public void StaticInstanceSameAsNewInstance() { - lock(testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - var newInstance = PrismContainerExtension.Create(); - Assert.Same(newInstance, PrismContainerExtension.Current); - } + var newInstance = PrismContainerExtension.Create(); + Assert.Same(newInstance, PrismContainerExtension.Current); } [Fact] public void StaticInstanceSameAsCreateInstance() { - lock(testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - var created = PrismContainerExtension.Create(new global::DryIoc.Container()); - Assert.Same(created, PrismContainerExtension.Current); - } + var created = PrismContainerExtension.Create(new global::DryIoc.Container()); + Assert.Same(created, PrismContainerExtension.Current); } [Fact] public void CreateCanOnlyBeCalledOnce() { - lock(testLock) - { - var newInstance1 = CreateContainer(); - Assert.Same(newInstance1, PrismContainerExtension.Current); + var newInstance1 = CreateContainer(); + Assert.Same(newInstance1, PrismContainerExtension.Current); - var ex = Record.Exception(() => PrismContainerExtension.Create()); - Assert.NotNull(ex); - Assert.IsType(ex); - } + var ex = Record.Exception(() => PrismContainerExtension.Create()); + Assert.NotNull(ex); + Assert.IsType(ex); } [Fact] public void IServiceProviderIsRegistered() { - lock(testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - Assert.True(PrismContainerExtension.Current.IsRegistered()); - } + Assert.True(PrismContainerExtension.Current.IsRegistered()); } [Fact] public void IContainerProviderIsRegistered() { - lock(testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - Assert.True(PrismContainerExtension.Current.IsRegistered()); - } + Assert.True(PrismContainerExtension.Current.IsRegistered()); } [Fact] public void RegisterManyHasSameTypeAcrossServices() { - lock(testLock) - { - var c = CreateContainer(); - c.RegisterMany(); + var c = CreateContainer(); + c.RegisterMany(); - IFoo foo = null; - IBar bar = null; - var ex = Record.Exception(() => foo = c.Resolve()); + IFoo foo = null; + IBar bar = null; + var ex = Record.Exception(() => foo = c.Resolve()); - Assert.Null(ex); - Assert.NotNull(foo); - Assert.IsType(foo); + Assert.Null(ex); + Assert.NotNull(foo); + Assert.IsType(foo); - ex = Record.Exception(() => bar = c.Resolve()); + ex = Record.Exception(() => bar = c.Resolve()); - Assert.Null(ex); - Assert.NotNull(bar); - Assert.IsType(bar); + Assert.Null(ex); + Assert.NotNull(bar); + Assert.IsType(bar); - Assert.NotSame(foo, bar); - } + Assert.NotSame(foo, bar); } [Fact] public void RegisterManyHasSameInstanceAcrossServices() { - lock(testLock) - { - var c = CreateContainer(); - c.RegisterManySingleton(); + var c = CreateContainer(); + c.RegisterManySingleton(); - IFoo foo = null; - IBar bar = null; - var ex = Record.Exception(() => foo = c.Resolve()); + IFoo foo = null; + IBar bar = null; + var ex = Record.Exception(() => foo = c.Resolve()); - Assert.Null(ex); - Assert.NotNull(foo); - Assert.IsType(foo); + Assert.Null(ex); + Assert.NotNull(foo); + Assert.IsType(foo); - ex = Record.Exception(() => bar = c.Resolve()); + ex = Record.Exception(() => bar = c.Resolve()); - Assert.Null(ex); - Assert.NotNull(bar); - Assert.IsType(bar); + Assert.Null(ex); + Assert.NotNull(bar); + Assert.IsType(bar); - Assert.Same(foo, bar); - } + Assert.Same(foo, bar); } [Fact] public void RegisterTransientService() { - lock(testLock) - { - var c = CreateContainer(); - c.Register(); - IFoo foo = null; - var ex = Record.Exception(() => foo = c.Resolve()); - - Assert.Null(ex); - Assert.NotNull(foo); - Assert.IsType(foo); - } + var c = CreateContainer(); + c.Register(); + IFoo foo = null; + var ex = Record.Exception(() => foo = c.Resolve()); + + Assert.Null(ex); + Assert.NotNull(foo); + Assert.IsType(foo); } [Fact] public void RegisterTransientNamedService() { - lock (testLock) - { - var c = CreateContainer(); - c.Register("fooBar"); - IFoo foo = null; - var ex = Record.Exception(() => foo = c.Resolve()); + var c = CreateContainer(); + c.Register("fooBar"); + IFoo foo = null; + var ex = Record.Exception(() => foo = c.Resolve()); - Assert.NotNull(ex); + Assert.NotNull(ex); - ex = null; - ex = Record.Exception(() => foo = c.Resolve("fooBar")); + ex = Record.Exception(() => foo = c.Resolve("fooBar")); - Assert.Null(ex); - Assert.NotNull(foo); - Assert.IsType(foo); - } + Assert.Null(ex); + Assert.NotNull(foo); + Assert.IsType(foo); } [Fact] public void RegisterSingletonService() { - lock(testLock) - { - var c = CreateContainer(); - c.RegisterSingleton(); - IFoo foo = null; - var ex = Record.Exception(() => foo = c.Resolve()); + var c = CreateContainer(); + c.RegisterSingleton(); + IFoo foo = null; + var ex = Record.Exception(() => foo = c.Resolve()); - Assert.Null(ex); - Assert.NotNull(foo); - Assert.IsType(foo); + Assert.Null(ex); + Assert.NotNull(foo); + Assert.IsType(foo); - Assert.Same(foo, c.Resolve()); - } + Assert.Same(foo, c.Resolve()); } [Fact] public void RegisterInstanceResolveSameInstance() { - lock(testLock) - { - var c = CreateContainer(); - var foo = new Foo(); + var c = CreateContainer(); + var foo = new Foo(); - c.RegisterInstance(foo); + c.RegisterInstance(foo); - Assert.True(c.IsRegistered()); - Assert.Same(foo, c.Resolve()); - } + Assert.True(c.IsRegistered()); + Assert.Same(foo, c.Resolve()); } [Fact] public void RegisterInstanceResolveSameNamedInstance() { - lock(testLock) - { - var c = CreateContainer(); - var foo = new Foo(); + var c = CreateContainer(); + var foo = new Foo(); - c.RegisterInstance(foo, "test"); + c.RegisterInstance(foo, "test"); - Assert.True(c.IsRegistered("test")); - Assert.Same(foo, c.Resolve("test")); - } + Assert.True(c.IsRegistered("test")); + Assert.Same(foo, c.Resolve("test")); } [Fact] public void RegisterSingletonNamedService() { - lock(testLock) - { - var c = CreateContainer(); - c.RegisterSingleton("fooBar"); - IFoo foo = null; - var ex = Record.Exception(() => foo = c.Resolve()); + var c = CreateContainer(); + c.RegisterSingleton("fooBar"); + IFoo foo = null; + var ex = Record.Exception(() => foo = c.Resolve()); - Assert.NotNull(ex); + Assert.NotNull(ex); - ex = null; - ex = Record.Exception(() => foo = c.Resolve("fooBar")); + ex = Record.Exception(() => foo = c.Resolve("fooBar")); - Assert.Null(ex); - Assert.NotNull(foo); - Assert.IsType(foo); + Assert.Null(ex); + Assert.NotNull(foo); + Assert.IsType(foo); - Assert.Same(foo, c.Resolve("fooBar")); - } + Assert.Same(foo, c.Resolve("fooBar")); } [Fact] public void FactoryCreatesTransientTypeWithoutContainerProvider() { - lock(testLock) - { - var c = CreateContainer(); - var message = "expected"; - c.RegisterDelegate(FooFactory); + var c = CreateContainer(); + var message = "expected"; + c.RegisterDelegate(FooFactory); - IFoo foo = null; - var ex = Record.Exception(() => foo = c.Resolve()); + IFoo foo = null; + var ex = Record.Exception(() => foo = c.Resolve()); - Assert.Null(ex); - Assert.Equal(message, foo.Message); + Assert.Null(ex); + Assert.Equal(message, foo.Message); - Assert.NotSame(foo, c.Resolve()); - } + Assert.NotSame(foo, c.Resolve()); } [Fact] public void FactoryCreatesSingletonTypeWithoutContainerProvider() { - lock(testLock) - { - var c = CreateContainer(); - var message = "expected"; - c.RegisterSingletonFromDelegate(FooFactory); + var c = CreateContainer(); + var message = "expected"; + c.RegisterSingletonFromDelegate(FooFactory); - IFoo foo = null; - var ex = Record.Exception(() => foo = c.Resolve()); + IFoo foo = null; + var ex = Record.Exception(() => foo = c.Resolve()); - Assert.Null(ex); - Assert.Equal(message, foo.Message); + Assert.Null(ex); + Assert.Equal(message, foo.Message); - Assert.Same(foo, c.Resolve()); - } + Assert.Same(foo, c.Resolve()); } [Fact] public void FactoryCreatesTransientTypeWithServiceProvider() { - lock(testLock) - { - var c = CreateContainer(); - var expectedMessage = "constructed with IServiceProvider"; - c.RegisterDelegate(typeof(IBar), BarFactoryWithIServiceProvider); - c.RegisterInstance(new Foo { Message = expectedMessage }); + var c = CreateContainer(); + var expectedMessage = "constructed with IServiceProvider"; + c.RegisterDelegate(typeof(IBar), BarFactoryWithIServiceProvider); + c.RegisterInstance(new Foo { Message = expectedMessage }); - IBar bar = null; - var ex = Record.Exception(() => bar = c.Resolve()); + IBar bar = null; + var ex = Record.Exception(() => bar = c.Resolve()); - Assert.Null(ex); - Assert.False(string.IsNullOrWhiteSpace(bar.Foo.Message)); - Assert.Equal(expectedMessage, bar.Foo.Message); + Assert.Null(ex); + Assert.False(string.IsNullOrWhiteSpace(bar.Foo.Message)); + Assert.Equal(expectedMessage, bar.Foo.Message); - Assert.NotSame(bar, c.Resolve()); - } + Assert.NotSame(bar, c.Resolve()); } [Fact] public void FactoryCreatesTransientTypeWithServiceProviderFromGeneric() { - lock(testLock) - { - var c = CreateContainer(); - var expectedMessage = "constructed with IServiceProvider"; - c.RegisterDelegate(BarFactoryWithIServiceProvider); - c.RegisterInstance(new Foo { Message = expectedMessage }); + var c = CreateContainer(); + var expectedMessage = "constructed with IServiceProvider"; + c.RegisterDelegate(BarFactoryWithIServiceProvider); + c.RegisterInstance(new Foo { Message = expectedMessage }); - IBar bar = null; - var ex = Record.Exception(() => bar = c.Resolve()); + IBar bar = null; + var ex = Record.Exception(() => bar = c.Resolve()); - Assert.Null(ex); - Assert.False(string.IsNullOrWhiteSpace(bar.Foo.Message)); - Assert.Equal(expectedMessage, bar.Foo.Message); + Assert.Null(ex); + Assert.False(string.IsNullOrWhiteSpace(bar.Foo.Message)); + Assert.Equal(expectedMessage, bar.Foo.Message); - Assert.NotSame(bar, c.Resolve()); - } + Assert.NotSame(bar, c.Resolve()); } [Fact] public void FactoryCreatesSingletonTypeWithServiceProvider() { - lock(testLock) + var c = CreateContainer(); + var expectedMessage = "constructed with IServiceProvider"; + c.RegisterSingletonFromDelegate(typeof(IBar), BarFactoryWithIServiceProvider); + c.RegisterInstance(new Foo { Message = expectedMessage }); + + IBar bar = null; + var ex = Record.Exception(() => bar = c.Resolve()); + + Assert.Null(ex); + Assert.False(string.IsNullOrWhiteSpace(bar.Foo.Message)); + Assert.Equal(expectedMessage, bar.Foo.Message); + + Assert.Same(bar, c.Resolve()); + } + + [Fact] + public void FactoryCreatesSingletonTypeWithServiceProviderFromGeneric() + { + try { var c = CreateContainer(); + Assert.NotNull(c); var expectedMessage = "constructed with IServiceProvider"; - c.RegisterSingletonFromDelegate(typeof(IBar), BarFactoryWithIServiceProvider); + c.RegisterSingletonFromDelegate(BarFactoryWithIServiceProvider); c.RegisterInstance(new Foo { Message = expectedMessage }); IBar bar = null; @@ -337,147 +298,107 @@ public void FactoryCreatesSingletonTypeWithServiceProvider() Assert.Same(bar, c.Resolve()); } - } - - [Fact] - public void FactoryCreatesSingletonTypeWithServiceProviderFromGeneric() - { - lock(testLock) + catch (Exception ex) { - try - { - var c = CreateContainer(); - Assert.NotNull(c); - var expectedMessage = "constructed with IServiceProvider"; - c.RegisterSingletonFromDelegate(BarFactoryWithIServiceProvider); - c.RegisterInstance(new Foo { Message = expectedMessage }); - - IBar bar = null; - var ex = Record.Exception(() => bar = c.Resolve()); - - Assert.Null(ex); - Assert.False(string.IsNullOrWhiteSpace(bar.Foo.Message)); - Assert.Equal(expectedMessage, bar.Foo.Message); - - Assert.Same(bar, c.Resolve()); - } - catch (Exception ex) - { - _testOutputHelper.WriteLine(ex.ToString()); - throw; - } + _testOutputHelper.WriteLine(ex.ToString()); + throw; } } [Fact] public void FactoryCreatesTransientTypeWithContainerProvider() { - lock(testLock) - { - var c = CreateContainer(); + var c = CreateContainer(); - var expectedMessage = "constructed with IContainerProvider"; - c.RegisterDelegate(typeof(IBar), BarFactoryWithIContainerProvider); - c.RegisterSingleton(); + var expectedMessage = "constructed with IContainerProvider"; + c.RegisterDelegate(typeof(IBar), BarFactoryWithIContainerProvider); + c.RegisterSingleton(); - IBar bar = null; - var ex = Record.Exception(() => bar = c.Resolve()); + IBar bar = null; + var ex = Record.Exception(() => bar = c.Resolve()); - Assert.Null(ex); - Assert.IsType(bar); - Assert.Equal(expectedMessage, ((Bar)bar).Message); + Assert.Null(ex); + Assert.IsType(bar); + Assert.Equal(expectedMessage, ((Bar)bar).Message); - Assert.Same(c.Resolve(), bar.Foo); - Assert.NotSame(c.Resolve(), bar); - } + Assert.Same(c.Resolve(), bar.Foo); + Assert.NotSame(c.Resolve(), bar); } [Fact] public void FactoryCreatesTransientTypeWithContainerProviderWithGeneric() { - lock(testLock) - { - var c = CreateContainer(); + var c = CreateContainer(); - var expectedMessage = "constructed with IContainerProvider"; - c.RegisterDelegate(BarFactoryWithIContainerProvider); - c.RegisterSingleton(); + var expectedMessage = "constructed with IContainerProvider"; + c.RegisterDelegate(BarFactoryWithIContainerProvider); + c.RegisterSingleton(); - IBar bar = null; - var ex = Record.Exception(() => bar = c.Resolve()); + IBar bar = null; + var ex = Record.Exception(() => bar = c.Resolve()); - Assert.Null(ex); - Assert.IsType(bar); - Assert.Equal(expectedMessage, ((Bar)bar).Message); + Assert.Null(ex); + Assert.IsType(bar); + Assert.Equal(expectedMessage, ((Bar)bar).Message); - Assert.Same(c.Resolve(), bar.Foo); - Assert.NotSame(c.Resolve(), bar); - } + Assert.Same(c.Resolve(), bar.Foo); + Assert.NotSame(c.Resolve(), bar); } [Fact] public void FactoryCreatesSingletonTypeWithContainerProvider() { - lock(testLock) - { - var c = CreateContainer(); + var c = CreateContainer(); - var expectedMessage = "constructed with IContainerProvider"; - c.RegisterSingletonFromDelegate(typeof(IBar), BarFactoryWithIContainerProvider); - c.RegisterSingleton(); + var expectedMessage = "constructed with IContainerProvider"; + c.RegisterSingletonFromDelegate(typeof(IBar), BarFactoryWithIContainerProvider); + c.RegisterSingleton(); - IBar bar = null; - var ex = Record.Exception(() => bar = c.Resolve()); + IBar bar = null; + var ex = Record.Exception(() => bar = c.Resolve()); - Assert.Null(ex); - Assert.IsType(bar); - Assert.Equal(expectedMessage, ((Bar)bar).Message); + Assert.Null(ex); + Assert.IsType(bar); + Assert.Equal(expectedMessage, ((Bar)bar).Message); - Assert.Same(c.Resolve(), bar.Foo); - Assert.Same(c.Resolve(), bar); - } + Assert.Same(c.Resolve(), bar.Foo); + Assert.Same(c.Resolve(), bar); } [Fact] public void FactoryCreatesSingletonTypeWithContainerProviderWithGeneric() { - lock(testLock) - { - var c = CreateContainer(); + var c = CreateContainer(); - var expectedMessage = "constructed with IContainerProvider"; - c.RegisterSingletonFromDelegate(BarFactoryWithIContainerProvider); - c.RegisterSingleton(); + var expectedMessage = "constructed with IContainerProvider"; + c.RegisterSingletonFromDelegate(BarFactoryWithIContainerProvider); + c.RegisterSingleton(); - IBar bar = null; - var ex = Record.Exception(() => bar = c.Resolve()); + IBar bar = null; + var ex = Record.Exception(() => bar = c.Resolve()); - Assert.Null(ex); - Assert.IsType(bar); - Assert.Equal(expectedMessage, ((Bar)bar).Message); + Assert.Null(ex); + Assert.IsType(bar); + Assert.Equal(expectedMessage, ((Bar)bar).Message); - Assert.Same(c.Resolve(), bar.Foo); - Assert.Same(c.Resolve(), bar); - } + Assert.Same(c.Resolve(), bar.Foo); + Assert.Same(c.Resolve(), bar); } [Fact] public void ResolveWithSpecifiedTypeOverridesRegistration() { - lock(testLock) - { - var c = CreateContainer(); - c.Register(); - var foo = new Foo { Message = "This shouldn't be resolved" }; - c.RegisterInstance(foo); + var c = CreateContainer(); + c.Register(); + var foo = new Foo { Message = "This shouldn't be resolved" }; + c.RegisterInstance(foo); - var overrideFoo = new Foo { Message = "We expect this one" }; + var overrideFoo = new Foo { Message = "We expect this one" }; - Assert.Same(foo, c.Resolve()); + Assert.Same(foo, c.Resolve()); - var bar = c.Resolve((typeof(IFoo), overrideFoo)); - Assert.Same(overrideFoo, bar.Foo); - } + var bar = c.Resolve((typeof(IFoo), overrideFoo)); + Assert.Same(overrideFoo, bar.Foo); } [Fact] @@ -485,15 +406,12 @@ public void ResolveNamedInstance() { var genA = new GenericService { Name = "genA" }; var genB = new GenericService { Name = "genB" }; - lock(testLock) - { - var c = CreateContainer(); - c.RegisterInstance(genA, "genA"); - c.RegisterInstance(genB, "genB"); + var c = CreateContainer(); + c.RegisterInstance(genA, "genA"); + c.RegisterInstance(genB, "genB"); - Assert.Same(genA, c.Resolve("genA")); - Assert.Same(genB, c.Resolve("genB")); - } + Assert.Same(genA, c.Resolve("genA")); + Assert.Same(genB, c.Resolve("genB")); } public static IFoo FooFactory() => new Foo { Message = "expected" }; @@ -507,12 +425,7 @@ public static IBar BarFactoryWithIContainerProvider(IContainerProvider container public static IBar BarFactoryWithIServiceProvider(IServiceProvider serviceProvider) => new Bar((IFoo)serviceProvider.GetService(typeof(IFoo))); - private static IContainerExtension CreateContainer() - { - PrismContainerExtension.Reset(); - GC.Collect(); - return PrismContainerExtension.Current; - } + private static IContainerExtension CreateContainer() => PrismContainerExtension.Current; } internal class MockListener : TraceListener diff --git a/tests/Prism.DryIoc.Extensions.Tests/MicrosoftExtensionsTests.cs b/tests/Prism.DryIoc.Extensions.Tests/MicrosoftExtensionsTests.cs index 45dd7c8..5e8152f 100644 --- a/tests/Prism.DryIoc.Extensions.Tests/MicrosoftExtensionsTests.cs +++ b/tests/Prism.DryIoc.Extensions.Tests/MicrosoftExtensionsTests.cs @@ -1,169 +1,138 @@ using System; using Microsoft.Extensions.DependencyInjection; +using Prism.Container.Extensions.Shared.Tests; using Prism.Container.Extensions.Tests.Mocks; using Prism.Ioc; using Xunit; namespace Prism.DryIoc.Extensions.Tests { + [Collection(nameof(SharedTests))] public class MicrosoftExtensionsTests { - private readonly object testLock = new object(); + public MicrosoftExtensionsTests() + { + PrismContainerExtension.Reset(); + } [Fact] public void TransientServiceIsRegistered() { - lock (testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - - var services = new ServiceCollection(); - services.AddTransient(); - var container = PrismContainerExtension.Create(); - var serviceProvider = container.CreateServiceProvider(services); - - object service = null; - var ex = Record.Exception(() => service = serviceProvider.GetService(typeof(IFoo))); - - Assert.Null(ex); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - Assert.IsType(service); - - Assert.NotSame(service, serviceProvider.GetService(typeof(IFoo))); - } + var services = new ServiceCollection(); + services.AddTransient(); + var container = PrismContainerExtension.Create(); + var serviceProvider = container.CreateServiceProvider(services); + + object service = null; + var ex = Record.Exception(() => service = serviceProvider.GetService(typeof(IFoo))); + + Assert.Null(ex); + Assert.NotNull(service); + Assert.IsAssignableFrom(service); + Assert.IsType(service); + + Assert.NotSame(service, serviceProvider.GetService(typeof(IFoo))); } [Fact] public void SingletonServiceIsRegistered() { - lock(testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - - var services = new ServiceCollection(); - services.AddSingleton(); - var container = PrismContainerExtension.Create(); - var serviceProvider = container.CreateServiceProvider(services); - - object service = null; - var ex = Record.Exception(() => service = serviceProvider.GetService(typeof(IFoo))); - - Assert.Null(ex); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - Assert.IsType(service); - - Assert.Same(service, serviceProvider.GetService(typeof(IFoo))); - } + var services = new ServiceCollection(); + services.AddSingleton(); + var container = PrismContainerExtension.Create(); + var serviceProvider = container.CreateServiceProvider(services); + + object service = null; + var ex = Record.Exception(() => service = serviceProvider.GetService(typeof(IFoo))); + + Assert.Null(ex); + Assert.NotNull(service); + Assert.IsAssignableFrom(service); + Assert.IsType(service); + + Assert.Same(service, serviceProvider.GetService(typeof(IFoo))); } [Fact] public void SingletonInstanceIsResolved() { - lock(testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - - var foo = new Foo(); - var services = new ServiceCollection(); - services.AddSingleton(foo); - var container = PrismContainerExtension.Create(); - var serviceProvider = container.CreateServiceProvider(services); - - object service = null; - var ex = Record.Exception(() => service = serviceProvider.GetService(typeof(IFoo))); - - Assert.Null(ex); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - Assert.IsType(service); - - Assert.Same(foo, service); - } + var foo = new Foo(); + var services = new ServiceCollection(); + services.AddSingleton(foo); + var container = PrismContainerExtension.Create(); + var serviceProvider = container.CreateServiceProvider(services); + + object service = null; + var ex = Record.Exception(() => service = serviceProvider.GetService(typeof(IFoo))); + + Assert.Null(ex); + Assert.NotNull(service); + Assert.IsAssignableFrom(service); + Assert.IsType(service); + + Assert.Same(foo, service); } [Fact] public void SingletonFactoryIsResolved() { - lock(testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - - int counter = 0; - var services = new ServiceCollection(); - services.AddSingleton(sp => new Foo { Message = $"Foo has been resolved {++counter} time(s)." }); - PrismContainerExtension.Current.CreateServiceProvider(services); - - IFoo foo = null; - var ex = Record.Exception(() => foo = PrismContainerExtension.Current.Resolve()); - - Assert.Null(ex); - Assert.NotNull(foo); - Assert.Equal(1, counter); - Assert.Equal("Foo has been resolved 1 time(s).", foo.Message); - - var foo2 = PrismContainerExtension.Current.Resolve(); - - Assert.Equal(1, counter); - Assert.Same(foo, foo2); - Assert.Equal("Foo has been resolved 1 time(s).", foo2.Message); - } + int counter = 0; + var services = new ServiceCollection(); + services.AddSingleton(sp => new Foo { Message = $"Foo has been resolved {++counter} time(s)." }); + PrismContainerExtension.Current.CreateServiceProvider(services); + + IFoo foo = null; + var ex = Record.Exception(() => foo = PrismContainerExtension.Current.Resolve()); + + Assert.Null(ex); + Assert.NotNull(foo); + Assert.Equal(1, counter); + Assert.Equal("Foo has been resolved 1 time(s).", foo.Message); + + var foo2 = PrismContainerExtension.Current.Resolve(); + + Assert.Equal(1, counter); + Assert.Same(foo, foo2); + Assert.Equal("Foo has been resolved 1 time(s).", foo2.Message); } [Fact] public void TransientFactoryIsResolved() { - lock (testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - - int counter = 0; - var services = new ServiceCollection(); - services.AddTransient(sp => new Foo { Message = $"Foo has been resolved {++counter} time(s)." }); - PrismContainerExtension.Current.CreateServiceProvider(services); - - IFoo foo = null; - var ex = Record.Exception(() => foo = PrismContainerExtension.Current.Resolve()); - - Assert.Null(ex); - Assert.NotNull(foo); - Assert.Equal(1, counter); - Assert.Equal("Foo has been resolved 1 time(s).", foo.Message); - - var foo2 = PrismContainerExtension.Current.Resolve(); - - Assert.Equal(2, counter); - Assert.NotSame(foo, foo2); - Assert.Equal("Foo has been resolved 2 time(s).", foo2.Message); - } + int counter = 0; + var services = new ServiceCollection(); + services.AddTransient(sp => new Foo { Message = $"Foo has been resolved {++counter} time(s)." }); + PrismContainerExtension.Current.CreateServiceProvider(services); + + IFoo foo = null; + var ex = Record.Exception(() => foo = PrismContainerExtension.Current.Resolve()); + + Assert.Null(ex); + Assert.NotNull(foo); + Assert.Equal(1, counter); + Assert.Equal("Foo has been resolved 1 time(s).", foo.Message); + + var foo2 = PrismContainerExtension.Current.Resolve(); + + Assert.Equal(2, counter); + Assert.NotSame(foo, foo2); + Assert.Equal("Foo has been resolved 2 time(s).", foo2.Message); } [Fact] public void ScopedServiceIsSupported() { - lock(testLock) - { - PrismContainerExtension.Reset(); - GC.Collect(); - - var services = new ServiceCollection(); - services.AddScoped(); + var services = new ServiceCollection(); + services.AddScoped(); - var container = PrismContainerExtension.Current; - IServiceProvider serviceProvider = null; + var container = PrismContainerExtension.Current; + IServiceProvider serviceProvider = null; - var ex = Record.Exception(() => serviceProvider = container.CreateServiceProvider(services)); + var ex = Record.Exception(() => serviceProvider = container.CreateServiceProvider(services)); - Assert.Null(ex); + Assert.Null(ex); - Assert.True(container.IsRegistered()); - } + Assert.True(container.IsRegistered()); } } }