Skip to content

Commit

Permalink
Unload AssemblyLoadContext after test completion
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Jan 31, 2023
1 parent ec1b59b commit 437d4cc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void Exec(ITestOutputHelper testOutputHelper, AssemblyLoadContext compile
}
finally
{
loader.UnloadAll();
testOutputHelper.WriteLine($"Test fixture root: {fixture.TempDirectory.Path}");

foreach (var context in loader.GetDirectoryLoadContextsSnapshot())
Expand Down Expand Up @@ -195,11 +196,18 @@ private void Run(
Action<AnalyzerAssemblyLoader, AssemblyLoadTestFixture> testAction,
[CallerMemberName] string? memberName = null)
{
var alc = new AssemblyLoadContext($"Test {memberName}");
using var fixture = new AssemblyLoadTestFixture();
prepLoadContextAction(alc, fixture);
var util = new InvokeUtil();
util.Exec(TestOutputHelper, alc, fixture, shadowLoad, testAction.Method.DeclaringType!.FullName!, testAction.Method.Name);
var alc = new AssemblyLoadContext($"Test {memberName}", isCollectible: true);
try
{
using var fixture = new AssemblyLoadTestFixture();
prepLoadContextAction(alc, fixture);
var util = new InvokeUtil();
util.Exec(TestOutputHelper, alc, fixture, shadowLoad, testAction.Method.DeclaringType!.FullName!, testAction.Method.Name);
}
finally
{
alc.Unload();
}
}

#else
Expand Down Expand Up @@ -1228,14 +1236,15 @@ public void AssemblyLoading_DeleteAfterLoad1(bool shadowLoad)
{
Run(shadowLoad, static (AnalyzerAssemblyLoader loader, AssemblyLoadTestFixture testFixture) =>
{
StringBuilder sb = new StringBuilder();

loader.AddDependencyLocation(testFixture.Delta1.Path);
_ = loader.LoadFromPath(testFixture.Delta1.Path);
using var temp = new TempRoot();
var tempDir = temp.CreateDirectory();
var deltaCopy = tempDir.CreateFile("Delta.dll").CopyContentFrom(testFixture.Delta1.Path);
loader.AddDependencyLocation(deltaCopy.Path);
_ = loader.LoadFromPath(deltaCopy.Path);

if (loader is ShadowCopyAnalyzerAssemblyLoader || !ExecutionConditionUtil.IsWindows)
{
File.Delete(testFixture.Delta1.Path);
File.Delete(deltaCopy.Path);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,29 @@ internal DirectoryLoadContext[] GetDirectoryLoadContextsSnapshot()
}
}

internal void UnloadAll()
{
List<DirectoryLoadContext> contexts;
lock (_guard)
{
contexts = _loadContextByDirectory.Values.ToList();
_loadContextByDirectory.Clear();
}

foreach (var context in contexts)
{
context.Unload();
}
}

internal sealed class DirectoryLoadContext : AssemblyLoadContext
{
internal string Directory { get; }
private readonly AnalyzerAssemblyLoader _loader;
private readonly AssemblyLoadContext _compilerLoadContext;

public DirectoryLoadContext(string directory, AnalyzerAssemblyLoader loader, AssemblyLoadContext compilerLoadContext)
: base(isCollectible: true)
{
Directory = directory;
_loader = loader;
Expand Down

0 comments on commit 437d4cc

Please sign in to comment.