Repository navigation
Expand file tree
/
Copy pathPluginUnloadCleanupTests.cs
More file actions
147 lines (124 loc) · 5.98 KB
/
Copy pathPluginUnloadCleanupTests.cs
File metadata and controls
147 lines (124 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Reflection;
using System.Runtime.Loader;
using FolderDiffIL4DotNet.Runner;
using Xunit;
namespace FolderDiffIL4DotNet.Tests.Runner
{
/// <summary>
/// Tests for plugin AssemblyLoadContext unloading and cleanup behavior.
/// Verifies that collectible contexts can be unloaded, that GC reclaims memory,
/// and that failure scenarios do not leave leaked resources.
/// プラグイン AssemblyLoadContext のアンロードおよびクリーンアップ動作のテスト。
/// コレクティブルコンテキストがアンロード可能であること、GC がメモリを回収すること、
/// 失敗シナリオでリソースリークが発生しないことを検証します。
/// </summary>
[Trait("Category", "Unit")]
public sealed class PluginUnloadCleanupTests
{
private static string TestAssemblyPath => typeof(PluginUnloadCleanupTests).Assembly.Location;
[Fact]
public void Unload_CollectibleContext_DoesNotThrow()
{
// A collectible context should unload cleanly
// コレクティブルコンテキストはクリーンにアンロードできること
var ctx = new PluginAssemblyLoadContext(TestAssemblyPath);
Assert.True(ctx.IsCollectible);
var ex = Record.Exception(() => ctx.Unload());
Assert.Null(ex);
}
[Fact]
public void Unload_AfterLoadingAssembly_DoesNotThrow()
{
// Even after loading an assembly, unload should succeed
// アセンブリ読み込み後でもアンロードが成功すること
var ctx = new PluginAssemblyLoadContext(TestAssemblyPath);
var asm = ctx.LoadFromAssemblyName(
new AssemblyName("FolderDiffIL4DotNet.Plugin.Abstractions"));
Assert.NotNull(asm);
var ex = Record.Exception(() => ctx.Unload());
Assert.Null(ex);
}
[Fact]
public void MultipleContexts_Coexist_IndependentUnload()
{
// Multiple contexts can be created and unloaded independently
// 複数コンテキストを独立して作成・アンロードできること
var ctx1 = new PluginAssemblyLoadContext(TestAssemblyPath);
var ctx2 = new PluginAssemblyLoadContext(TestAssemblyPath);
Assert.True(ctx1.IsCollectible);
Assert.True(ctx2.IsCollectible);
// Unload first, second should still work
// 1つ目をアンロードしても2つ目は動作すること
ctx1.Unload();
var asm = ctx2.LoadFromAssemblyName(
new AssemblyName("FolderDiffIL4DotNet.Plugin.Abstractions"));
Assert.NotNull(asm);
ctx2.Unload();
}
[Fact]
public void WeakReference_BecomesNull_AfterUnloadAndGC()
{
// After unloading a collectible context, GC should reclaim it
// コレクティブルコンテキストのアンロード後、GC が回収すること
var weakRef = CreateAndUnloadContext();
// Force GC to collect the unloaded context
// アンロードされたコンテキストを GC に回収させる
for (int i = 0; i < 10; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (!weakRef.IsAlive) break;
}
Assert.False(weakRef.IsAlive);
}
[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static WeakReference CreateAndUnloadContext()
{
var ctx = new PluginAssemblyLoadContext(TestAssemblyPath);
var weakRef = new WeakReference(ctx);
ctx.Unload();
return weakRef;
}
[Fact]
public void DoubleUnload_DoesNotThrow()
{
// .NET allows calling Unload() multiple times on a collectible context
// without throwing an exception — the second call is silently ignored.
// .NET はコレクティブルコンテキストに対する Unload() の複数回呼び出しを許容し、
// 2回目の呼び出しは黙殺される(例外は発生しない)。
var ctx = new PluginAssemblyLoadContext(TestAssemblyPath);
ctx.Unload();
var ex = Record.Exception(() => ctx.Unload());
Assert.Null(ex);
}
[Fact]
public void UnloadingEvent_Fires_OnUnload()
{
// The Unloading event should fire when context is unloaded
// コンテキストのアンロード時に Unloading イベントが発火すること
var ctx = new PluginAssemblyLoadContext(TestAssemblyPath);
bool eventFired = false;
ctx.Unloading += _ => eventFired = true;
ctx.Unload();
Assert.True(eventFired);
}
[Fact]
public void SharedAssembly_PluginAbstractions_NotAffectedByUnload()
{
// Plugin.Abstractions loaded from default context should survive plugin unload
// デフォルトコンテキストからロードされた Plugin.Abstractions はプラグインアンロード後も存続すること
var ctx = new PluginAssemblyLoadContext(TestAssemblyPath);
var asmBefore = ctx.LoadFromAssemblyName(
new AssemblyName("FolderDiffIL4DotNet.Plugin.Abstractions"));
ctx.Unload();
// The assembly from default context should still be accessible
// デフォルトコンテキストのアセンブリは引き続きアクセス可能であること
var asmAfter = AssemblyLoadContext.Default.LoadFromAssemblyName(
new AssemblyName("FolderDiffIL4DotNet.Plugin.Abstractions"));
Assert.NotNull(asmAfter);
Assert.Equal(asmBefore.FullName, asmAfter.FullName);
}
}
}