/* * ThreadPilot - process monitor manager unit tests. */ namespace ThreadPilot.Core.Tests { using System.Collections.ObjectModel; using System.Threading; using Microsoft.Extensions.Logging.Abstractions; using Moq; using ThreadPilot.Models; using ThreadPilot.Services; /// /// Unit tests for orchestration logic in . /// public sealed class ProcessMonitorManagerServiceTests { [Fact] public async Task StartAsync_LoadsConfiguration_AndStartsMonitoring() { var processMonitor = new FakeProcessMonitorService(); var configuration = new ProcessMonitorConfiguration(); var associationService = CreateAssociationService(configuration); var powerPlanService = CreatePowerPlanService(); var notificationService = CreateNotificationService(); var processService = CreateProcessService(); var coreMaskService = CreateCoreMaskService(); var affinityApplyService = CreateAffinityApplyService(); var manager = CreateService( processMonitor, associationService, powerPlanService, notificationService, processService, coreMaskService, affinityApplyService); await manager.StartAsync(); Assert.True(manager.IsRunning); Assert.Equal("Running", manager.Status); Assert.Equal(1, processMonitor.StartCalls); associationService.Verify(x => x.LoadConfigurationAsync(), Times.Once); } [Fact] public async Task StartAsync_SelectsHighestPriorityAssociation() { var processMonitor = new FakeProcessMonitorService { RunningProcesses = { new ProcessModel { ProcessId = 1, Name = "game-low" }, new ProcessModel { ProcessId = 2, Name = "game-high" }, }, }; var configuration = new ProcessMonitorConfiguration { Associations = { new ProcessPowerPlanAssociation("game-low", "plan-low", "Low") { Priority = 1 }, new ProcessPowerPlanAssociation("game-high", "plan-high", "High") { Priority = 10 }, }, PowerPlanChangeDelayMs = 0, }; var associationService = CreateAssociationService(configuration); var powerPlanService = CreatePowerPlanService(); var notificationService = CreateNotificationService(); var processService = CreateProcessService(); var coreMaskService = CreateCoreMaskService(); var affinityApplyService = CreateAffinityApplyService(); var manager = CreateService( processMonitor, associationService, powerPlanService, notificationService, processService, coreMaskService, affinityApplyService); await manager.StartAsync(); powerPlanService.Verify( x => x.SetActivePowerPlanByGuidAsync("plan-high", true), Times.Once); notificationService.Verify( x => x.ShowPowerPlanChangeNotificationAsync("Balanced", "plan-high-name", "game-high"), Times.Once); } [Fact] public async Task ProcessStarted_WithDelay_TriggersSingleReevaluation() { var processMonitor = new FakeProcessMonitorService(); var configuration = new ProcessMonitorConfiguration { PowerPlanChangeDelayMs = 25, Associations = { new ProcessPowerPlanAssociation("game", "plan-game", "Game") { Priority = 5 }, }, }; var associationService = CreateAssociationService(configuration); var powerPlanService = CreatePowerPlanService(); var notificationService = CreateNotificationService(); var processService = CreateProcessService(); var coreMaskService = CreateCoreMaskService(); var affinityApplyService = CreateAffinityApplyService(); var manager = CreateService( processMonitor, associationService, powerPlanService, notificationService, processService, coreMaskService, affinityApplyService); await manager.StartAsync(); processMonitor.RaiseProcessStarted(new ProcessModel { ProcessId = 10, Name = "game" }); processMonitor.RaiseProcessStarted(new ProcessModel { ProcessId = 11, Name = "game" }); await Task.Delay(150); powerPlanService.Verify( x => x.SetActivePowerPlanByGuidAsync("plan-game", true), Times.Once); } [Fact] public async Task ProcessStarted_SamePlanRequest_IsSuppressedWithinDuplicateWindow() { var processMonitor = new FakeProcessMonitorService(); var configuration = new ProcessMonitorConfiguration { PowerPlanChangeDelayMs = 0, Associations = { new ProcessPowerPlanAssociation("game", "plan-game", "Game") { Priority = 5 }, }, }; var associationService = CreateAssociationService(configuration); var powerPlanService = CreatePowerPlanService(); var notificationService = CreateNotificationService(); var processService = CreateProcessService(); var coreMaskService = CreateCoreMaskService(); var affinityApplyService = CreateAffinityApplyService(); var manager = CreateService( processMonitor, associationService, powerPlanService, notificationService, processService, coreMaskService, affinityApplyService); await manager.StartAsync(); processMonitor.RaiseProcessStarted(new ProcessModel { ProcessId = 10, Name = "game" }); processMonitor.RaiseProcessStarted(new ProcessModel { ProcessId = 11, Name = "game" }); await Task.Delay(100); powerPlanService.Verify( x => x.SetActivePowerPlanByGuidAsync("plan-game", true), Times.Once); } [Fact] public async Task ProcessStarted_WhenPowerPlanChangeFails_DoesNotRetrySamePlanImmediately() { var processMonitor = new FakeProcessMonitorService(); var configuration = new ProcessMonitorConfiguration { PowerPlanChangeDelayMs = 0, Associations = { new ProcessPowerPlanAssociation("game", "plan-game", "Game") { Priority = 5 }, }, }; var associationService = CreateAssociationService(configuration); var powerPlanService = CreatePowerPlanService(); powerPlanService .Setup(x => x.SetActivePowerPlanByGuidAsync("plan-game", true)) .ReturnsAsync(false); var notificationService = CreateNotificationService(); var processService = CreateProcessService(); var coreMaskService = CreateCoreMaskService(); var affinityApplyService = CreateAffinityApplyService(); var manager = CreateService( processMonitor, associationService, powerPlanService, notificationService, processService, coreMaskService, affinityApplyService); await manager.StartAsync(); processMonitor.RaiseProcessStarted(new ProcessModel { ProcessId = 10, Name = "game" }); processMonitor.RaiseProcessStarted(new ProcessModel { ProcessId = 11, Name = "game" }); await Task.Delay(100); powerPlanService.Verify( x => x.SetActivePowerPlanByGuidAsync("plan-game", true), Times.Once); notificationService.Verify( x => x.ShowPowerPlanChangeNotificationAsync(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Fact] public async Task StopAsync_RestoresDefaultPowerPlan_WhenConfigured() { var processMonitor = new FakeProcessMonitorService { RunningProcesses = { new ProcessModel { ProcessId = 21, Name = "game" }, }, }; var configuration = new ProcessMonitorConfiguration { DefaultPowerPlanGuid = "plan-default", DefaultPowerPlanName = "Balanced", PowerPlanChangeDelayMs = 0, Associations = { new ProcessPowerPlanAssociation("game", "plan-game", "Game") { Priority = 1 }, }, }; var associationService = CreateAssociationService(configuration); var powerPlanService = CreatePowerPlanService(); var notificationService = CreateNotificationService(); var processService = CreateProcessService(); var coreMaskService = CreateCoreMaskService(); var affinityApplyService = CreateAffinityApplyService(); var manager = CreateService( processMonitor, associationService, powerPlanService, notificationService, processService, coreMaskService, affinityApplyService); await manager.StartAsync(); await manager.StopAsync(); powerPlanService.Verify( x => x.SetActivePowerPlanByGuidAsync("plan-default", true), Times.AtLeastOnce); processService.Verify(x => x.UntrackProcess(21), Times.Once); coreMaskService.Verify(x => x.UnregisterMaskApplication(21), Times.Once); } [Fact] public async Task ProcessStarted_AppliesConfiguredCoreMaskForMatchingProcess() { var process = new ProcessModel { ProcessId = 31, Name = "game" }; var processMonitor = new FakeProcessMonitorService(); var configuration = new ProcessMonitorConfiguration { PowerPlanChangeDelayMs = 0, Associations = { new ProcessPowerPlanAssociation("game", "plan-game", "Game") { CoreMaskId = "mask-game", Priority = 5, }, }, }; var associationService = CreateAssociationService(configuration); var powerPlanService = CreatePowerPlanService(); var notificationService = CreateNotificationService(); var processService = CreateProcessService(); processService.Setup(x => x.TrackAppliedMask(31, "mask-game")); var coreMaskService = CreateCoreMaskService(); coreMaskService.SetupGet(x => x.AvailableMasks).Returns(new ObservableCollection { new() { Id = "mask-game", Name = "Game Mask", BoolMask = new ObservableCollection { true, false }, }, }); coreMaskService.Setup(x => x.RegisterMaskApplication(31, "mask-game")); var affinityApplyService = CreateAffinityApplyService(); var manager = CreateService( processMonitor, associationService, powerPlanService, notificationService, processService, coreMaskService, affinityApplyService); await manager.StartAsync(); processMonitor.RaiseProcessStarted(process); await Task.Delay(100); affinityApplyService.Verify(x => x.ApplyAsync(process, 1), Times.Once); processService.Verify(x => x.TrackAppliedMask(31, "mask-game"), Times.Once); coreMaskService.Verify(x => x.RegisterMaskApplication(31, "mask-game"), Times.Once); } [Fact] public async Task Dispose_CompletesOnBlockingSynchronizationContext() { var processMonitor = new FakeProcessMonitorService { StopMonitoringAsyncImpl = async () => { await Task.Yield(); }, }; var configuration = new ProcessMonitorConfiguration(); var associationService = CreateAssociationService(configuration); var powerPlanService = CreatePowerPlanService(); var notificationService = CreateNotificationService(); var processService = CreateProcessService(); var coreMaskService = CreateCoreMaskService(); var affinityApplyService = CreateAffinityApplyService(); var manager = CreateService( processMonitor, associationService, powerPlanService, notificationService, processService, coreMaskService, affinityApplyService); await manager.StartAsync(); Exception? disposeException = null; using var completed = new ManualResetEventSlim(false); var disposeThread = new Thread(() => { SynchronizationContext.SetSynchronizationContext(new BlockingSynchronizationContext()); try { manager.Dispose(); } catch (Exception ex) { disposeException = ex; } finally { completed.Set(); } }); disposeThread.Start(); Assert.True(completed.Wait(TimeSpan.FromSeconds(1)), "Dispose did not complete promptly."); Assert.Null(disposeException); Assert.Equal(1, processMonitor.StopCalls); Assert.Equal(1, processMonitor.DisposeCalls); } private static ProcessMonitorManagerService CreateService( FakeProcessMonitorService processMonitorService, Mock associationService, Mock powerPlanService, Mock notificationService, Mock processService, Mock coreMaskService, Mock affinityApplyService) { var enhancedLogger = new Mock(MockBehavior.Loose); enhancedLogger .Setup(x => x.LogSystemEventAsync(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); enhancedLogger .Setup(x => x.LogProcessMonitoringEventAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); enhancedLogger .Setup(x => x.LogErrorAsync(It.IsAny(), It.IsAny(), It.IsAny?>())) .Returns(Task.CompletedTask); var settingsService = new Mock(MockBehavior.Loose); settingsService.SetupGet(x => x.Settings).Returns(new ApplicationSettingsModel()); return new ProcessMonitorManagerService( processMonitorService, associationService.Object, powerPlanService.Object, notificationService.Object, settingsService.Object, processService.Object, coreMaskService.Object, affinityApplyService.Object, new PowerPlanTransitionGate(TimeSpan.FromSeconds(2), () => DateTimeOffset.UtcNow), NullLogger.Instance, enhancedLogger.Object); } private static Mock CreateAssociationService(ProcessMonitorConfiguration configuration) { var associationService = new Mock(MockBehavior.Strict); associationService.SetupGet(x => x.Configuration).Returns(configuration); associationService.Setup(x => x.LoadConfigurationAsync()).ReturnsAsync(true); return associationService; } private static Mock CreatePowerPlanService() { var powerPlanService = new Mock(MockBehavior.Strict); powerPlanService.Setup(x => x.GetActivePowerPlan()).ReturnsAsync(new PowerPlanModel { Guid = "balanced", Name = "Balanced" }); powerPlanService.Setup(x => x.SetActivePowerPlanByGuidAsync(It.IsAny(), It.IsAny())).ReturnsAsync(true); powerPlanService.Setup(x => x.GetPowerPlanByGuidAsync(It.IsAny())) .ReturnsAsync((string guid) => new PowerPlanModel { Guid = guid, Name = $"{guid}-name" }); return powerPlanService; } private static Mock CreateNotificationService() { var notificationService = new Mock(MockBehavior.Strict); notificationService.SetupGet(x => x.NotificationHistory).Returns(Array.Empty()); notificationService.Setup(x => x.ShowPowerPlanChangeNotificationAsync(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); notificationService.Setup(x => x.ShowErrorNotificationAsync(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); notificationService.Setup(x => x.ShowNotificationAsync(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); return notificationService; } private static Mock CreateProcessService() { var processService = new Mock(MockBehavior.Strict); processService.Setup(x => x.UntrackProcess(It.IsAny())); return processService; } private static Mock CreateCoreMaskService() { var coreMaskService = new Mock(MockBehavior.Strict); coreMaskService.SetupGet(x => x.AvailableMasks).Returns(new ObservableCollection()); coreMaskService.Setup(x => x.UnregisterMaskApplication(It.IsAny())); return coreMaskService; } private static Mock CreateAffinityApplyService() { var affinityApplyService = new Mock(MockBehavior.Strict); affinityApplyService .Setup(x => x.ApplyAsync(It.IsAny(), It.IsAny())) .ReturnsAsync((ProcessModel process, long affinity) => AffinityApplyResult.Succeeded(affinity, affinity)); return affinityApplyService; } private sealed class FakeProcessMonitorService : IProcessMonitorService { public event EventHandler? ProcessStarted; public event EventHandler? ProcessStopped; public event EventHandler? MonitoringStatusChanged; public List RunningProcesses { get; } = new(); public int StartCalls { get; private set; } public int StopCalls { get; private set; } public int DisposeCalls { get; private set; } public bool IsMonitoring { get; private set; } public bool IsWmiAvailable => false; public bool IsFallbackPollingActive => false; public Func? StopMonitoringAsyncImpl { get; init; } public Task StartMonitoringAsync() { this.StartCalls++; this.IsMonitoring = true; return Task.CompletedTask; } public Task StopMonitoringAsync() { this.StopCalls++; this.IsMonitoring = false; return this.StopMonitoringAsyncImpl?.Invoke() ?? Task.CompletedTask; } public Task> GetRunningProcessesAsync() => Task.FromResult>(this.RunningProcesses.ToList()); public Task IsProcessRunningAsync(string executableName) => Task.FromResult(this.RunningProcesses.Any(x => string.Equals(x.Name, executableName, StringComparison.OrdinalIgnoreCase))); public void UpdateSettings() { } public void RaiseProcessStarted(ProcessModel process) => this.ProcessStarted?.Invoke(this, new ProcessEventArgs(process)); public void Dispose() { this.DisposeCalls++; } } private sealed class BlockingSynchronizationContext : SynchronizationContext { public override void Post(SendOrPostCallback d, object? state) { // Intentionally do not pump posted work to emulate a blocked UI thread. } } } }