fengling-gateway/tests/YarpGateway.Tests/Unit/Plugins/PluginHostTests.cs
movingsam 52eba07097 feat: add MigrationTool for gateway config migration (IMPL-7)
- Create MigrationTool console app for exporting DB config to K8s YAML
- Support dry-run mode and validation
- Add Npgsql and YamlDotNet dependencies
2026-03-08 00:35:04 +08:00

139 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Fengling.Gateway.Plugin.Abstractions;
using Xunit;
using YarpGateway.Plugins;
namespace YarpGateway.Tests.Unit.Plugins;
public class PluginHostTests : IDisposable
{
private readonly string _testDir;
public PluginHostTests()
{
_testDir = Path.Combine(Path.GetTempPath(), "plugin-host-test-" + Guid.NewGuid());
Directory.CreateDirectory(_testDir);
}
public void Dispose()
{
try
{
Directory.Delete(_testDir, true);
}
catch { }
}
[Fact]
public void Constructor_ShouldInitialize()
{
// Act
var host = new PluginHost(_testDir);
// Assert
Assert.NotNull(host);
}
[Fact]
public void GetPlugins_Empty_ShouldReturnEmpty()
{
// Arrange
var host = new PluginHost(_testDir);
// Act
var plugins = host.GetPlugins().ToList();
// Assert
Assert.Empty(plugins);
}
[Fact]
public void LoadAllAsync_EmptyDirectory_ShouldReturnZero()
{
// Arrange
var host = new PluginHost(_testDir);
// Act
var count = host.LoadAllAsync().Result;
// Assert
Assert.Equal(0, count);
}
[Fact]
public async Task GetPluginInfo_AfterLoad_ShouldReturnPlugins()
{
// Arrange
var host = new PluginHost(_testDir);
// 创建测试插件目录
var pluginDir = Path.Combine(_testDir, "test-plugin");
Directory.CreateDirectory(pluginDir);
// 创建 plugin.json
var manifest = new
{
id = "test-plugin",
name = "Test Plugin",
version = "1.0.0",
entryPoint = "TestPlugin.TestPlugin"
};
File.WriteAllText(Path.Combine(pluginDir, "plugin.json"), System.Text.Json.JsonSerializer.Serialize(manifest));
// 创建 DLL占位符不会真正加载
File.WriteAllText(Path.Combine(pluginDir, "test-plugin.dll"), "dummy");
// Act
var count = await host.LoadAllAsync();
// Assert - 加载会失败因为 DLL 是无效的,但应该返回 0
Assert.Equal(0, count);
}
[Fact]
public async Task UnloadAsync_NotLoaded_ShouldNotThrow()
{
// Arrange
var host = new PluginHost(_testDir);
// Act & Assert - 不应抛出异常
await host.UnloadAsync("non-existent");
}
[Fact]
public async Task UnloadAllAsync_Empty_ShouldNotThrow()
{
// Arrange
var host = new PluginHost(_testDir);
// Act & Assert - 不应抛出异常
await host.UnloadAllAsync();
}
[Fact]
public void VerifyUnload_AliveObject_ShouldReturnFalse()
{
// Arrange
var obj = new object();
var weakRef = new WeakReference(obj);
// Act
var result = PluginHost.VerifyUnload(weakRef, maxAttempts: 1);
// Assert
Assert.False(result);
}
}
/// <summary>
/// 测试用插件实现
/// </summary>
public class TestPlugin : IGatewayPlugin
{
public string Name => "TestPlugin";
public string Version => "1.0.0";
public string? Description => "A test plugin";
public Task OnLoadAsync() => Task.CompletedTask;
public Task OnUnloadAsync() => Task.CompletedTask;
}