Skip to content

Commit

Permalink
Renaming variables, ProcessTerminated method usage fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lilla28 committed Apr 20, 2023
1 parent c2196af commit 93f5e8f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,67 @@

namespace ProcessExplorer.Abstractions.Processes;

//TODO(Lilla): Add description, should be async
//TODO(Lilla): should be async?
public interface IProcessInfoMonitor : IDisposable
{
/// <summary>
/// Clears the currently initilialzed processes.
/// </summary>
void ClearProcessIds();

/// <summary>
/// Returns the CPU usage of the given process.
/// </summary>
/// <param name="processId"></param>
/// <param name="processName"></param>
/// <returns></returns>
float GetCpuUsage(int processId, string processName);

/// <summary>
/// Returns the memory usage of the given process.
/// </summary>
/// <param name="processId"></param>
/// <param name="processName"></param>
/// <returns></returns>
float GetMemoryUsage(int processId, string processName);

/// <summary>
/// Returns the parent id of the given process.
/// </summary>
/// <param name="processId"></param>
/// <param name="processName"></param>
/// <returns></returns>
int? GetParentId(int processId, string processName);

/// <summary>
/// Returns the initialized/watched process ids.
/// </summary>
/// <returns></returns>
ReadOnlySpan<int> GetProcessIds();

/// <summary>
/// Sets the behaviors of the process changed events.
/// </summary>
/// <param name="processModifiedHandler"></param>
/// <param name="processTerminatedHandler"></param>
/// <param name="processCreatedHandler"></param>
void SetHandlers(
ProcessModifiedHandler processModifiedHandler,
ProcessTerminatedHandler processTerminatedHandler,
ProcessCreatedHandler processCreatedHandler,
ProcessesModifiedHandler processesModifiedHandler,
ProcessStatusChangedHandler processStatusChangedHandler);

/// <summary>
/// Sets the watchable process list.
/// </summary>
/// <param name="mainProcessId"></param>
/// <param name="processIds"></param>
void SetProcessIds(int mainProcessId, ReadOnlySpan<int> processIds);

/// <summary>
/// Enables watching processes.
/// </summary>
/// <param name="mainProcessId"></param>
void WatchProcesses(int mainProcessId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void ProcessIdsChanged(object? sender, NotifyCollectionChangedEventArgs
{
if (e.OldItems != null)
foreach (int pid in e.OldItems)
_processTerminatedHandler?.Invoke(pid);
ProcessTerminated(pid);

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace ProcessExplorer.Core.Factories;
public static class ProcessAggregatorFactory
{
public static IProcessInfoAggregator CreateProcessInfoAggregator(
ProcessInfoMonitor processInfoManager,
ProcessInfoMonitor processInfoMonitor,
IUiHandler handler,
ISubsystemController? subsystemController = null,
ILogger<IProcessInfoAggregator> logger = null)
ILogger<IProcessInfoAggregator>? logger = null)
{
return new ProcessInfoAggregator(processInfoManager, handler, subsystemController, logger);
return new ProcessInfoAggregator(processInfoMonitor, handler, subsystemController, logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace ProcessExplorer.Core.Factories;
public static class ProcessMonitorFactory
{
public static ProcessInfoMonitor CreateProcessInfoGeneratorWindows(ILogger<ProcessInfoMonitor> logger)
public static ProcessInfoMonitor CreateProcessInfoMonitorWindows(ILogger<ProcessInfoMonitor> logger)
{
#pragma warning disable CA1416 // Validate platform compatibility
return new WindowsProcessInfoMonitor(logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class ProcessInfoAggregator : IProcessInfoAggregator
private readonly object _processsInformationLock = new();
//putting subsystem change messages to the queue and remove it if it has been sent ~ FIFO
private readonly ConcurrentQueue<KeyValuePair<Guid, string>> _subsystemStateChanges = new();
private readonly IProcessInfoMonitor _processInfoManager;
private readonly IProcessInfoMonitor _processInfoMonitor;
private readonly IUiHandler _handler;
private bool _disposed;

Expand All @@ -53,7 +53,7 @@ public ProcessInfoAggregator(

_handler = handler;
UiHandler = _handler;
_processInfoManager = processInfoManager;
_processInfoMonitor = processInfoManager;
SubsystemController = subsystemController;

if (SubsystemController != null)
Expand All @@ -64,7 +64,7 @@ public ProcessInfoAggregator(

private void DisposeCore()
{
_processInfoManager.Dispose();
_processInfoMonitor.Dispose();
}

private Task UpdateInfoOnUI(Func<IUiHandler, Task> handlerAction)
Expand All @@ -90,7 +90,7 @@ private void UpdateProcessInfoCollectorData(string assemblyId, ProcessInfoCollec

private void SetUiCommunicatorsToWatchProcessChanges()
{
_processInfoManager.SetHandlers(
_processInfoMonitor.SetHandlers(
ProcessModified,
ProcessTerminated,
ProcessCreated,
Expand All @@ -104,7 +104,7 @@ private IEnumerable<ProcessInfoData> GetProcesses(ReadOnlySpan<int> processIds)

foreach (var id in processIds)
{
var process = ProcessInformation.GetProcessInfoWithCalculatedData(Process.GetProcessById(id), _processInfoManager);
var process = ProcessInformation.GetProcessInfoWithCalculatedData(Process.GetProcessById(id), _processInfoMonitor);
processes.Add(process.ProcessInfo);
}

Expand All @@ -127,7 +127,7 @@ await Task.Run(() =>
Task.Delay(TerminatingProcessDelay);
});

var processes = GetProcesses(_processInfoManager.GetProcessIds());
var processes = GetProcesses(_processInfoMonitor.GetProcessIds());

await _handler.TerminateProcess(processId);
await _handler.AddProcesses(processes);
Expand All @@ -146,7 +146,7 @@ private async void ProcessCreated(int processId)

private ProcessInfoData? GetProcess(int processId)
{
var process = GetProcesses(_processInfoManager.GetProcessIds()).FirstOrDefault(proc => proc.ProcessId == processId);
var process = GetProcesses(_processInfoMonitor.GetProcessIds()).FirstOrDefault(proc => proc.ProcessId == processId);

if (process == null) return null;

Expand Down Expand Up @@ -190,7 +190,7 @@ public async Task RunSubsystemStateQueue(CancellationToken cancellationToken)

public IEnumerable<ProcessInfoData> GetProcesses()
{
var processIds = _processInfoManager.GetProcessIds();
var processIds = _processInfoMonitor.GetProcessIds();
return GetProcesses(processIds);
}

Expand Down Expand Up @@ -331,18 +331,18 @@ public async Task UpdateOrAddModuleInfo(string assemblyId, IEnumerable<ModuleInf

public void EnableWatchingSavedProcesses()
{
_processInfoManager.WatchProcesses(MainProcessId);
_processInfoMonitor.WatchProcesses(MainProcessId);
}

public void DisableWatchingProcesses()
{
_processInfoManager.Dispose();
_processInfoMonitor.Dispose();
}

public void InitProcesses(ReadOnlySpan<int> processIds)
{
_processInfoManager.ClearProcessIds();
_processInfoManager.SetProcessIds(MainProcessId, processIds);
_processInfoMonitor.ClearProcessIds();
_processInfoMonitor.SetProcessIds(MainProcessId, processIds);
}

public void SetSubsystemController(ISubsystemController subsystemController)
Expand Down Expand Up @@ -380,7 +380,7 @@ public void ScheduleSubsystemStateChanged(Guid instanceId, string state)

public Task AddProcesses(ReadOnlySpan<int> processes)
{
_processInfoManager.SetProcessIds(MainProcessId, processes);
_processInfoMonitor.SetProcessIds(MainProcessId, processes);
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public async Task RunSubsystemQueue_will_cancel_after_timeout()
//Creating mocks to handle method
var mockUiHandler = new Mock<IUiHandler>();
var mockSubsystemController = new Mock<ISubsystemController>();
var mockProcessInfoManager = new Mock<ProcessInfoMonitor>(NullLogger.Instance);
var mockProcessInfoMonitor = new Mock<ProcessInfoMonitor>(NullLogger.Instance);
var clientMock = new Mock<IClientConnection<SubsystemLauncherTests.DummyStartType>>();
var processInfoAggregator = new ProcessInfoAggregator(
mockProcessInfoManager.Object,
mockProcessInfoMonitor.Object,
mockUiHandler.Object,
mockSubsystemController.Object,
NullLogger<IProcessInfoAggregator>.Instance);
Expand Down Expand Up @@ -323,16 +323,16 @@ public async Task UpdateModuleInfo_will_update_modules()
public void EnableWatchingSavedProcesses_will_begin_to_watch_processes()
{
var mockSubsystemController = new Mock<ISubsystemController>();
var mockProcessInfoManager = new Mock<IProcessInfoMonitor>();
var mockProcessInfoMonitor = new Mock<IProcessInfoMonitor>();
var mockUiHandler = new Mock<IUiHandler>();
var processInfoAggregator = new ProcessInfoAggregator(
mockProcessInfoManager.Object,
mockProcessInfoMonitor.Object,
mockUiHandler.Object,
mockSubsystemController.Object,
NullLogger<IProcessInfoAggregator>.Instance);

processInfoAggregator.EnableWatchingSavedProcesses();
mockProcessInfoManager.Verify(x => x.WatchProcesses(processInfoAggregator.MainProcessId), Times.Once);
mockProcessInfoMonitor.Verify(x => x.WatchProcesses(processInfoAggregator.MainProcessId), Times.Once);
}


Expand Down Expand Up @@ -361,10 +361,10 @@ public void ScheduleSubsystemStateChanged_will_put_items_to_the_queue()
private IProcessInfoAggregator CreateProcessInfoAggregator()
{
var mockSubsystemController = new Mock<ISubsystemController>();
var mockProcessInfoManager = new Mock<IProcessInfoMonitor>();
var mockProcessInfoMonitor = new Mock<IProcessInfoMonitor>();
var mockUiHandler = new Mock<IUiHandler>();
var processInfoAggregator = new ProcessInfoAggregator(
mockProcessInfoManager.Object,
mockProcessInfoMonitor.Object,
mockUiHandler.Object,
mockSubsystemController.Object,
NullLogger<IProcessInfoAggregator>.Instance);
Expand Down

0 comments on commit 93f5e8f

Please sign in to comment.