Skip to content

Commit

Permalink
Added tests to prove unload works ok with modules and onactivated in …
Browse files Browse the repository at this point in the history
…the loop.
  • Loading branch information
alistairjevans committed Feb 26, 2023
1 parent 7a3aaee commit 430e28b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
70 changes: 69 additions & 1 deletion test/Autofac.Specification.Test/LoadContextScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Collections;
using System.Diagnostics.SymbolStore;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;
using Autofac.Core;
using Autofac.Features.ResolveAnything;

namespace Autofac.Specification.Test;
Expand Down Expand Up @@ -34,7 +36,7 @@ public void CanLoadInstanceOfScanAssemblyAndUnloadIt()
}

[Fact]
public void CanLoadInstanceOfScanAssemblyAndUnloadItAfterActnars()
public void CanLoadInstanceOfAssemblyAndUnloadItAfterActnars()
{
var builder = new ContainerBuilder();

Expand All @@ -56,6 +58,72 @@ public void CanLoadInstanceOfScanAssemblyAndUnloadItAfterActnars()
WaitForUnload(loadContextRef);
}

[Fact]
public void CanLoadInstanceOfAssemblyAndUnloadItAfterOnActivatedInModule()
{
var builder = new ContainerBuilder();

using var rootContainer = builder.Build();

LoadAssemblyAndTest(
rootContainer,
out var loadContextRef,
(builder, assembly) =>
{
var module = (IModule)Activator.CreateInstance(assembly.GetType("A.OnActivatedModule"), 100);

builder.RegisterModule(module);
},
(scope, loadContext, assembly) =>
{
var serviceType = assembly.GetType("A.Service1");

var instance = scope.Resolve(serviceType);

Assert.Contains(instance.GetType().Assembly, loadContext.Assemblies);

var valueProp = serviceType.GetProperty("Value");

Assert.Equal(100, valueProp.GetValue(instance));
});

WaitForUnload(loadContextRef);
}

[Fact]
public void CanLoadInstanceOfAssemblyAndUnloadItAfterLifetimeScopeEndingInModule()
{
var builder = new ContainerBuilder();

using var rootContainer = builder.Build();

bool callbackInvoked = false;

LoadAssemblyAndTest(
rootContainer,
out var loadContextRef,
(builder, assembly) =>
{
Action invoke = () => { callbackInvoked = true; };

var module = (IModule)Activator.CreateInstance(assembly.GetType("A.LifetimeScopeEndingModule"), invoke);

builder.RegisterModule(module);
},
(scope, loadContext, assembly) =>
{
var serviceType = assembly.GetType("A.Service1");

var instance = scope.Resolve(serviceType);

Assert.Contains(instance.GetType().Assembly, loadContext.Assemblies);
});

WaitForUnload(loadContextRef);

Assert.True(callbackInvoked);
}

[Fact]
public void CanLoadInstanceOfScanAssemblyAndUnloadItAfterEnumerable()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@
<AdditionalFiles Include="..\..\build\stylecop.json" Link="stylecop.json" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Autofac\Autofac.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Autofac;

namespace A;

public class LifetimeScopeEndingModule : Module
{
private readonly Action _invokeOnEndCallback;

public LifetimeScopeEndingModule(Action invokeOnEndCallback)
{
_invokeOnEndCallback = invokeOnEndCallback;
}

protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<Service1>();

builder.RegisterBuildCallback(scope => scope.CurrentScopeEnding += (sender, ev) =>
{
_invokeOnEndCallback();
});
}
}
21 changes: 21 additions & 0 deletions test/Autofac.Test.Scenarios.LoadContext/OnActivatedModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Autofac;

namespace A;

public class OnActivatedModule : Module
{
private readonly int _value;

public OnActivatedModule(int value)
{
_value = value;
}

protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<Service1>().OnActivated(x => x.Instance.Value = _value);
}
}
1 change: 1 addition & 0 deletions test/Autofac.Test.Scenarios.LoadContext/Service1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace A;

public class Service1
{
public int Value { get; set; }
}

0 comments on commit 430e28b

Please sign in to comment.