diff --git a/libs/CheatEngine.Client.Extensions.DependencyInjection/LuaModuleLifecycle.cs b/libs/CheatEngine.Client.Extensions.DependencyInjection/LuaModuleLifecycle.cs index 662d865..d443bb7 100644 --- a/libs/CheatEngine.Client.Extensions.DependencyInjection/LuaModuleLifecycle.cs +++ b/libs/CheatEngine.Client.Extensions.DependencyInjection/LuaModuleLifecycle.cs @@ -26,7 +26,7 @@ public void OnEnabled(ICheatEngineClient client) "The Lua module lifecycle has already been enabled for this activation."); } - _lease = _lua.RegisterModule(_module); + _lease = _lua.RegisterModule(_module, client.Stopping); } public void OnDisabling(ICheatEngineClient client) diff --git a/tests/CheatEngine.Client.Benchmarks/Program.cs b/tests/CheatEngine.Client.Benchmarks/Program.cs index 6aea1bd..7facd17 100644 --- a/tests/CheatEngine.Client.Benchmarks/Program.cs +++ b/tests/CheatEngine.Client.Benchmarks/Program.cs @@ -7,6 +7,8 @@ return BenchmarkEntryPoint.Run(args); +namespace CheatEngine.Client.Benchmarks +{ internal static class BenchmarkEntryPoint { public static int Run(string[] args) @@ -17,7 +19,7 @@ public static int Run(string[] args) .AddExporter(JsonExporter.Full); Summary[] summaries = BenchmarkSwitcher - .FromAssembly(typeof(Program).Assembly) + .FromAssembly(typeof(BenchmarkEntryPoint).Assembly) .Run(args, config) .ToArray(); @@ -124,3 +126,4 @@ public static bool HasError(this IEnumerable summaries) summary.Reports.Any(static report => !report.Success)); } } +} diff --git a/tests/CheatEngine.Client.Extensions.DependencyInjection.Tests/CheatEngineClientServiceCollectionExtensionsTests.cs b/tests/CheatEngine.Client.Extensions.DependencyInjection.Tests/CheatEngineClientServiceCollectionExtensionsTests.cs index 675b331..be4a3ba 100644 --- a/tests/CheatEngine.Client.Extensions.DependencyInjection.Tests/CheatEngineClientServiceCollectionExtensionsTests.cs +++ b/tests/CheatEngine.Client.Extensions.DependencyInjection.Tests/CheatEngineClientServiceCollectionExtensionsTests.cs @@ -224,6 +224,19 @@ public void LuaModuleLifecycleRegistersAndReleasesItsModuleExactlyOnce() Assert.Equal(1, lease.DisposeCount); } + [Fact] + public void LuaModuleLifecycleForwardsTheClientStoppingTokenToRegistration() + { + RecordingLuaClient lua = new(); + LuaModuleLifecycle lifecycle = new(lua, new FirstLuaModule()); + using CancellationTokenSource stopping = new(); + TestClient client = new() { Stopping = stopping.Token }; + + lifecycle.OnEnabled(client); + + Assert.Equal(client.Stopping, lua.RegistrationCancellationToken); + } + private readonly record struct CustomValue(int Value); private sealed class FirstCustomCodec : IMemoryCodec @@ -346,6 +359,12 @@ internal ILuaModule? RegisteredModule private set; } + internal CancellationToken RegistrationCancellationToken + { + get; + private set; + } + public bool TryRegisterModule( ILuaModule luaModule, [NotNullWhen(true)] out ILuaModuleLease? lease, @@ -353,6 +372,7 @@ public bool TryRegisterModule( CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(luaModule); + RegistrationCancellationToken = cancellationToken; RegisteredModule = luaModule; Lease = new RecordingLease(); lease = Lease; @@ -417,7 +437,11 @@ private sealed class TestClient : ICheatEngineClient { public long Epoch => 1; - public CancellationToken Stopping => CancellationToken.None; + public CancellationToken Stopping + { + get; + init; + } = CancellationToken.None; public ICheatEngineRuntime Runtime => null!;