Skip to content

Commit

Permalink
Test that templating AppDomains are unloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutch committed Oct 3, 2022
1 parent 6c9e070 commit 640e7eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 26 additions & 3 deletions Mono.TextTemplating.Tests/AppDomainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@ namespace Mono.TextTemplating.Tests;
public class AppDomainTests : AssemblyLoadTests<SnapshotSet<string>>
{
protected override TemplateGenerator CreateGenerator ([CallerMemberName] string testName = null) => CreateGeneratorWithAppDomain (testName: testName);

protected override void CleanupGenerator (TemplateGenerator generator)
{
// verify that the AppDomain is collected
var weakRef = ((TestTemplateGeneratorWithAppDomain)generator).ReleaseDomain ();
int count = 0;
while (weakRef.IsAlive) {
GC.Collect ();
Assert.InRange (count++, 0, 5);
}
}

protected override SnapshotSet<string> GetInitialState () => Snapshot.LoadedAssemblies ();

protected override void VerifyFinalState (SnapshotSet<string> state)
{
(var added, var removed) = state.GetChanges ();
Expand All @@ -40,6 +53,7 @@ public async Task BadAppDomain ()
var templateText = await testDir["LoadOpenApiDll.tt"].ReadAllTextNormalizedAsync ();

await Assert.ThrowsAnyAsync<TemplatingEngineException> (() => badGen.ProcessTemplateAsync ("LoadOpenApiDll.tt", templateText, null));
CleanupGenerator (badGen);
}

[Fact]
Expand All @@ -56,6 +70,7 @@ public async Task RunsInAppDomain ()
Assert.Null (gen.Errors.OfType<CompilerError> ().FirstOrDefault ());
Assert.Equal (expectedOutputText, result.content);

CleanupGenerator (gen);
VerifyFinalState (state);
}

Expand All @@ -82,12 +97,20 @@ static TestTemplateGeneratorWithAppDomain CreateGeneratorWithAppDomain (
}

static string GetAppDomainNameForCurrentTest ([CallerMemberName] string testName = null) => $"Template Test - {testName ?? "(unknown)"}";

class TestTemplateGeneratorWithAppDomain : TemplateGenerator
{
public TestTemplateGeneratorWithAppDomain (AppDomain appDomain) => AppDomain = appDomain;
AppDomain appDomain;
public TestTemplateGeneratorWithAppDomain (AppDomain appDomain) => this.appDomain = appDomain;

public AppDomain AppDomain { get; private set; }
public override AppDomain ProvideTemplatingAppDomain (string content) => AppDomain;
public override AppDomain ProvideTemplatingAppDomain (string content) => appDomain;

public WeakReference ReleaseDomain ()
{
var weakRef = new WeakReference (appDomain);
appDomain = null;
return weakRef;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions Mono.TextTemplating.Tests/AssemblyLoadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Mono.TextTemplating.Tests;
public abstract class AssemblyLoadTests<T> : StatefulTest<T>
{
protected virtual TemplateGenerator CreateGenerator ([CallerMemberName] string testName = null) => new ();
protected virtual void CleanupGenerator (TemplateGenerator generator) { }

[Fact]
public async Task LoadOpenApiDll ()
Expand All @@ -34,6 +35,7 @@ public async Task LoadOpenApiDll ()
Assert.Equal (expectedOutputText, result.content);
Assert.Equal (expectedOutputPath, result.fileName);

CleanupGenerator (gen);
VerifyFinalState (state);
}

Expand All @@ -56,6 +58,7 @@ public async Task LoadOpenApiReadersDll ()
Assert.Null (gen.Errors.OfType<CompilerError> ().FirstOrDefault ());
Assert.Equal ("Example", result.content);

CleanupGenerator (gen);
VerifyFinalState (state);
}

Expand All @@ -74,5 +77,7 @@ public async Task MissingTransitiveReference ()

var firstError = gen.Errors.OfType<CompilerError> ().FirstOrDefault ()?.ErrorText;
Assert.Contains ("FileNotFoundException: Could not load file or assembly 'SharpYaml, Version=1.6.5.0", firstError);

CleanupGenerator (gen);
}
}

0 comments on commit 640e7eb

Please sign in to comment.