-
Notifications
You must be signed in to change notification settings - Fork 1k
Load plugins with isolated AssemblyLoadContext #4397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jim8y
wants to merge
11
commits into
master
Choose a base branch
from
feature/plugin-alc-loader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f1fa8be
Load plugins with isolated AssemblyLoadContext
Jim8y 4b5d0ce
Add plugin load context fixture tests
Jim8y a9da440
Keep MSTest assembly attributes for fixture projects
Jim8y e42841b
Stabilize plugin ALC tests on Windows
Jim8y 70dc801
Merge branch 'master' into feature/plugin-alc-loader
Wi1l-B0t 7642e4d
Merge branch 'master' into feature/plugin-alc-loader
ajara87 7b8008b
Merge branch 'master' into feature/plugin-alc-loader
Wi1l-B0t dfeaf3a
Merge branch 'master' into feature/plugin-alc-loader
ajara87 aaac32a
Merge branch 'master' into feature/plugin-alc-loader
cschuchardt88 4596311
Merge branch 'master' into feature/plugin-alc-loader
ajara87 818da1c
Merge branch 'master' into feature/plugin-alc-loader
ajara87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // FixtureDependency.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| namespace Neo.PluginFixture.Dependency; | ||
|
|
||
| public static class FixtureDependency | ||
| { | ||
| public static string Value => "fixture-dependency"; | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
tests/Neo.PluginFixture.Dependency/Neo.PluginFixture.Dependency.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <AssemblyName>Neo.PluginFixture.Dependency</AssemblyName> | ||
| <OutputType>Library</OutputType> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // FixturePlugin.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.Plugins; | ||
|
|
||
| namespace Neo.PluginFixture; | ||
|
|
||
| public sealed class FixturePlugin : Plugin | ||
| { | ||
| public string GetDependencyAssemblyLocation() => | ||
| typeof(Dependency.FixtureDependency).Assembly.Location; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <AssemblyName>Neo.PluginFixture</AssemblyName> | ||
| <OutputType>Library</OutputType> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Neo\Neo.csproj" /> | ||
| <ProjectReference Include="..\Neo.PluginFixture.Dependency\Neo.PluginFixture.Dependency.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // UT_PluginLoadContext.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.Plugins; | ||
| using System.Runtime.Loader; | ||
|
|
||
| namespace Neo.UnitTests.Plugins; | ||
|
|
||
| [TestClass] | ||
| public class UT_PluginLoadContext | ||
| { | ||
| private const string FixtureAssemblyName = "Neo.PluginFixture"; | ||
| private const string DependencyAssemblyName = "Neo.PluginFixture.Dependency"; | ||
| private static readonly Lock s_locker = new(); | ||
|
|
||
| [TestMethod] | ||
| public void TestLoadPluginsUsesIsolatedContext() | ||
| { | ||
| lock (s_locker) | ||
| { | ||
| try | ||
| { | ||
| var pluginRoot = PrepareFixturePluginDirectory(out var pluginAssemblyPath); | ||
| Plugin.Plugins.Clear(); | ||
|
|
||
| Plugin.LoadPlugins(); | ||
|
|
||
| var plugin = Plugin.Plugins.SingleOrDefault(p => | ||
| string.Equals(p.GetType().Assembly.Location, pluginAssemblyPath, StringComparison.OrdinalIgnoreCase)); | ||
| Assert.IsNotNull(plugin); | ||
|
|
||
| var loadContext = AssemblyLoadContext.GetLoadContext(plugin.GetType().Assembly); | ||
| Assert.IsNotNull(loadContext); | ||
| Assert.AreNotSame(AssemblyLoadContext.Default, loadContext); | ||
| Assert.AreEqual(pluginAssemblyPath, plugin.GetType().Assembly.Location); | ||
| } | ||
| finally | ||
| { | ||
| Plugin.Plugins.Clear(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestLoadPluginsResolvesDependencyFromPluginDirectory() | ||
| { | ||
| lock (s_locker) | ||
| { | ||
| try | ||
| { | ||
| var pluginRoot = PrepareFixturePluginDirectory(out _); | ||
| Plugin.Plugins.Clear(); | ||
|
|
||
| Plugin.LoadPlugins(); | ||
|
|
||
| var plugin = Plugin.Plugins.SingleOrDefault(p => | ||
| p.GetType().Assembly.Location.StartsWith(pluginRoot, StringComparison.OrdinalIgnoreCase)); | ||
| Assert.IsNotNull(plugin); | ||
|
|
||
| var method = plugin.GetType().GetMethod("GetDependencyAssemblyLocation"); | ||
| Assert.IsNotNull(method); | ||
|
|
||
| var location = method.Invoke(plugin, null) as string; | ||
| Assert.IsFalse(string.IsNullOrEmpty(location)); | ||
| Assert.AreEqual(Path.Combine(pluginRoot, $"{DependencyAssemblyName}.dll"), location); | ||
| } | ||
| finally | ||
| { | ||
| Plugin.Plugins.Clear(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static string PrepareFixturePluginDirectory(out string pluginAssemblyPath) | ||
| { | ||
| var pluginsDir = Plugin.PluginsDirectory; | ||
| Directory.CreateDirectory(pluginsDir); | ||
| var pluginDirectoryName = $"{FixtureAssemblyName}.{Guid.NewGuid():N}"; | ||
| var pluginRoot = Path.Combine(pluginsDir, pluginDirectoryName); | ||
| Directory.CreateDirectory(pluginRoot); | ||
|
|
||
| var sourcePluginPath = Path.Combine(AppContext.BaseDirectory, $"{FixtureAssemblyName}.dll"); | ||
| var sourceDependencyPath = Path.Combine(AppContext.BaseDirectory, $"{DependencyAssemblyName}.dll"); | ||
|
|
||
| Assert.IsTrue(File.Exists(sourcePluginPath), $"Missing fixture plugin assembly: {sourcePluginPath}"); | ||
| Assert.IsTrue(File.Exists(sourceDependencyPath), $"Missing fixture dependency assembly: {sourceDependencyPath}"); | ||
|
|
||
| pluginAssemblyPath = Path.Combine(pluginRoot, $"{pluginDirectoryName}.dll"); | ||
| File.Copy(sourcePluginPath, pluginAssemblyPath, true); | ||
| File.Copy(sourceDependencyPath, Path.Combine(pluginRoot, $"{DependencyAssemblyName}.dll"), true); | ||
|
Comment on lines
+97
to
+98
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ut is not cleaned, we should delete the folder after execute it |
||
|
|
||
| return pluginRoot; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move it to a separate file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping @Jim8y