From 90284ef20dcd30619376f6405b57a8789dda5a0f Mon Sep 17 00:00:00 2001 From: Sergei Rogovtcev Date: Sun, 8 Jan 2023 15:31:58 -0500 Subject: [PATCH] autofac/autofac#1361: adding (FAILING) test for OnActivated behavior when using decorator --- .../Lifetime/LifetimeEventTests.cs | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs b/test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs index 9c2caa0c2..ccb44bff6 100644 --- a/test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs +++ b/test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs @@ -593,17 +593,31 @@ public void OnReleaseForSingletonAsInterfaceStillFiresIfNotResolved() Assert.True(instance.Released); } - [Fact] - public void ActivatedAllowsBypassingCircularDependencies() + private static readonly Action[] ConfigurationsForCircularDependenciesImpl = + { + builder => { }, + builder => builder.RegisterDecorator(), + builder => builder.RegisterDecorator((_, __, toDecorate) => new InnerServiceDecorator(toDecorate)), + }; + + public static readonly object[][] ConfigurationsForCircularDependencies = + ConfigurationsForCircularDependenciesImpl.Select(c => new object[] {c}).ToArray(); + + [Theory] + [MemberData(nameof(ConfigurationsForCircularDependencies))] + public void ActivatedAllowsBypassingCircularDependencies(Action additionalContainerConfiguration) { var builder = new ContainerBuilder(); builder.RegisterType() - .SingleInstance(); - builder.RegisterType(); + .SingleInstance(); + builder.RegisterType() + .As(); builder.RegisterType() - .SingleInstance() - .OnActivated(args => args.Instance.Outer = args.Context.Resolve()); + .SingleInstance() + .OnActivated(args => args.Instance.Outer = args.Context.Resolve()); + + additionalContainerConfiguration(builder); using var container = builder.Build(); var outer = container.Resolve(); @@ -654,18 +668,29 @@ public void Method(int param) private class OuterService { - public OuterService(InnerService service) + public OuterService(IInnerService service) { } } - private class InnerService + private interface IInnerService + { + } + + private class InnerService : IInnerService { public InnerService(InnermostService service) { } } + private class InnerServiceDecorator : IInnerService + { + public InnerServiceDecorator(IInnerService toDecorate) + { + } + } + private class InnermostService { public OuterService Outer { get; set; }