From 1927ea55c28225bb5660df3cb56e53a68473cee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E6=B0=B4?= <1123993881@qq.com> Date: Fri, 5 Dec 2025 16:51:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8Add=20tests=20for=20`TedToolkit.Sc?= =?UTF-8?q?opes`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TedToolkit.slnx | 1 + Tests/TedToolkit.Scopes.Tests/ScopesTests.cs | 13 +++++++++++++ .../TedToolkit.Scopes.Tests.csproj | 5 +++++ Tests/TedToolkit.Scopes.Tests/TestScope.cs | 6 ++++++ 4 files changed, 25 insertions(+) create mode 100644 Tests/TedToolkit.Scopes.Tests/ScopesTests.cs create mode 100644 Tests/TedToolkit.Scopes.Tests/TedToolkit.Scopes.Tests.csproj create mode 100644 Tests/TedToolkit.Scopes.Tests/TestScope.cs diff --git a/TedToolkit.slnx b/TedToolkit.slnx index 59c3970..fceeb9a 100644 --- a/TedToolkit.slnx +++ b/TedToolkit.slnx @@ -74,6 +74,7 @@ + diff --git a/Tests/TedToolkit.Scopes.Tests/ScopesTests.cs b/Tests/TedToolkit.Scopes.Tests/ScopesTests.cs new file mode 100644 index 0000000..4139828 --- /dev/null +++ b/Tests/TedToolkit.Scopes.Tests/ScopesTests.cs @@ -0,0 +1,13 @@ +namespace TedToolkit.Scopes.Tests; + +public class ScopesTests +{ + [Test] + [MatrixDataSource] + public async Task ScopeCreateTest([MatrixRange(-1000, 1000)]int value) + { + using var scope = new TestScope(value); + await Task.Delay(1000); + await Assert.That(TestScope.Current?.Value).IsEqualTo(value); + } +} \ No newline at end of file diff --git a/Tests/TedToolkit.Scopes.Tests/TedToolkit.Scopes.Tests.csproj b/Tests/TedToolkit.Scopes.Tests/TedToolkit.Scopes.Tests.csproj new file mode 100644 index 0000000..e2700a8 --- /dev/null +++ b/Tests/TedToolkit.Scopes.Tests/TedToolkit.Scopes.Tests.csproj @@ -0,0 +1,5 @@ + + + + + diff --git a/Tests/TedToolkit.Scopes.Tests/TestScope.cs b/Tests/TedToolkit.Scopes.Tests/TestScope.cs new file mode 100644 index 0000000..5053328 --- /dev/null +++ b/Tests/TedToolkit.Scopes.Tests/TestScope.cs @@ -0,0 +1,6 @@ +namespace TedToolkit.Scopes.Tests; + +public class TestScope(int value) : ScopeBase +{ + public int Value => value; +} \ No newline at end of file From c54105df469f8454c4a1182a77d37d0b14b408ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E6=B0=B4?= <1123993881@qq.com> Date: Tue, 9 Dec 2025 23:23:46 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=90=9BRefactor=20`NativeFunctionLoade?= =?UTF-8?q?r`=20to=20remove=20lazy=20initialization=20and=20improve=20reso?= =?UTF-8?q?urce=20management.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NativeFunctionLoader.cs | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/src/libraries/TedToolkit.CppInteropGen/NativeFunctionLoader.cs b/src/libraries/TedToolkit.CppInteropGen/NativeFunctionLoader.cs index f8212fc..61b3bbd 100644 --- a/src/libraries/TedToolkit.CppInteropGen/NativeFunctionLoader.cs +++ b/src/libraries/TedToolkit.CppInteropGen/NativeFunctionLoader.cs @@ -8,42 +8,41 @@ namespace TedToolkit.CppInteropGen; public sealed class NativeFunctionLoader : IDisposable { - private static readonly ConcurrentDictionary Loaders = []; - private readonly ConcurrentDictionary _exportCache = new(StringComparer.Ordinal); - private readonly Lazy _libHandle; - private bool _disposed; + private static readonly ConcurrentDictionary Loaders = []; + private readonly ConcurrentDictionary _exportCache = new(StringComparer.Ordinal); + private readonly IntPtr _libHandle; + private bool _disposed; - private NativeFunctionLoader(string libraryPath) - { - _libHandle = new Lazy(() => - { - var handle = NativeLibrary.Load(libraryPath); - if (handle != IntPtr.Zero) return handle; - throw new InvalidOperationException($"Failed to load native library: {libraryPath}"); - }); - } + private NativeFunctionLoader(string libraryPath) + { + _libHandle = NativeLibrary.Load(libraryPath); + if (_libHandle != IntPtr.Zero) return; + throw new InvalidOperationException($"Failed to load native library: {libraryPath}"); + } - public void Dispose() - { - if (_disposed) return; - _disposed = true; - if (_libHandle.IsValueCreated) NativeLibrary.Free(_libHandle.Value); - } + public void Dispose() + { + if (_disposed) return; + _disposed = true; + NativeLibrary.Free(_libHandle); + } - internal static NativeFunctionLoader GetLoader(string libName) - { - if (string.IsNullOrEmpty(libName)) - throw new ArgumentException("The libName is null or empty.", nameof(libName)); - var loader = Loaders.GetOrAdd(libName, n => new NativeFunctionLoader(n)); - if (!loader._disposed) return loader; - return Loaders[libName] = new NativeFunctionLoader(libName); - } + internal static NativeFunctionLoader GetLoader(string libName) + { + if (string.IsNullOrEmpty(libName)) + throw new ArgumentException("The libName is null or empty.", nameof(libName)); - public IntPtr GetFunctionPointer(string name) - { - if (_disposed) throw new ObjectDisposedException(nameof(NativeFunctionLoader)); - return _exportCache.GetOrAdd(name, n => NativeLibrary.GetExport(_libHandle.Value, n)); - } + return Loaders.AddOrUpdate( + key: libName, + addValueFactory: n => new(n), + updateValueFactory: (n, existingLoader) => existingLoader._disposed ? new(n) : existingLoader); + } + + public IntPtr GetFunctionPointer(string name) + { + if (_disposed) throw new ObjectDisposedException(nameof(NativeFunctionLoader)); + return _exportCache.GetOrAdd(name, n => NativeLibrary.GetExport(_libHandle, n)); + } } #if NETSTANDARD || NETFRAMEWORK From beace2ae5d10dfd3d84f7568ab40c356c4747f78 Mon Sep 17 00:00:00 2001 From: nuke-bot Date: Tue, 9 Dec 2025 15:26:02 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=94=96=202025.12.9.0=20Released!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 883eb2d..3da0ef4 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@  - 2025.11.25.0 + 2025.12.9.0 enable enable preview