Skip to content

Commit

Permalink
fix: force GC collect and finalize on appdomain unload. This is an at…
Browse files Browse the repository at this point in the history
…tempt to fix the error seen sometimes in CI: [xUnit.net 00:00:03.37] [FATAL ERROR] System.AppDomainUnloadedException
  • Loading branch information
skwasjer committed Nov 19, 2023
1 parent a264ef6 commit fa19f88
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/MockHttp.Json.Tests/MockHttp.Json.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<RootNamespace>MockHttp.Json</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\MockHttp.Testing\XunitAppDomainUnloadHook.cs" Link="XunitAppDomainUnloadHook.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MockHttp.Testing\MockHttp.Testing.csproj" />
<ProjectReference Include="..\..\src\MockHttp.Json\MockHttp.Json.csproj" />
Expand Down
4 changes: 4 additions & 0 deletions test/MockHttp.Server.Tests/MockHttp.Server.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<SerilogExtensionsLoggingVersion Condition="$(TargetFramework)=='net6.0' Or $(TargetFramework)=='netcoreapp3.1'">3.1.0</SerilogExtensionsLoggingVersion>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\MockHttp.Testing\XunitAppDomainUnloadHook.cs" Link="XunitAppDomainUnloadHook.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="$(SerilogExtensionsLoggingVersion)" />
Expand Down
15 changes: 15 additions & 0 deletions test/MockHttp.Testing/XunitAppDomainUnloadHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MockHttp;

public sealed class XunitAppDomainUnloadHook
{
[Fact]
public void Unload()
{
AppDomain.CurrentDomain.DomainUnload += (_, _) =>
{
GC.Collect();
GC.WaitForPendingFinalizers();
};
Assert.True(true);
}
}
4 changes: 4 additions & 0 deletions test/MockHttp.Tests/MockHttp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<RootNamespace>MockHttp</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\MockHttp.Testing\XunitAppDomainUnloadHook.cs" Link="XunitAppDomainUnloadHook.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MockHttp.Testing\MockHttp.Testing.csproj" />
</ItemGroup>
Expand Down

0 comments on commit fa19f88

Please sign in to comment.