Skip to content
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

Improve killing of processes #338

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion GoogleTestAdapter/Common/ProcessUtils.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace GoogleTestAdapter.Common
{
public static class ProcessUtils
{
public static void KillProcess(int processId, ILogger logger)
{
logger.DebugInfo($"Scheduling to kill process with id {processId}...");
Task.Run(() => DoKillProcess(processId, logger));
}

private static void DoKillProcess(int processId, ILogger logger)
{
try
{
Process process = Process.GetProcessById(processId);
DateTime startTime = process.StartTime;
try
{
logger.DebugInfo($"Trying to kill process {process} with startTime={startTime.ToShortTimeString()}");
process.Kill();
logger.DebugInfo($"Killed process {process} with startTime={startTime.ToShortTimeString()}");
logger.DebugInfo($"Invoked Kill() on process {process} with startTime={startTime.ToShortTimeString()}, waiting for it to exit...");
process.WaitForExit();
if (process.HasExited)
{
logger.DebugInfo($"Successfully killed process {process} with startTime={startTime.ToShortTimeString()}");
}
else
{
logger.DebugWarning($"Wasn't able to kill process {process} with startTime={startTime.ToShortTimeString()}...");
}
}
catch (Exception e)
{
Expand All @@ -24,6 +41,7 @@ public static void KillProcess(int processId, ILogger logger)
catch (Exception)
{
// process was not running - nothing to do
logger.DebugInfo($"Process with id {processId} should be killed, but apparently was not running");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void CreateTestCases_DiscoveryTimeoutIsExceeded_DiscoveryIsCanceledAndCan

reportedTestCases.Should().BeEmpty();
returnedTestCases.Should().BeEmpty();
stopWatch.Elapsed.Should().BeGreaterOrEqualTo(TimeSpan.FromSeconds(1));
stopWatch.Elapsed.Should().BeGreaterOrEqualTo(TimeSpan.FromSeconds(0.9));
stopWatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(2));
MockLogger.Verify(o => o.LogError(It.Is<string>(s => s.Contains(TestResources.TenSecondsWaiter))), Times.Once);
MockLogger.Verify(o => o.DebugError(It.Is<string>(s => s.Contains(Path.GetFileName(TestResources.TenSecondsWaiter)))), Times.AtLeastOnce);
Expand Down
17 changes: 9 additions & 8 deletions GoogleTestAdapter/Tests.Common/Helpers/TestProcessLauncher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using GoogleTestAdapter.Common;
Expand All @@ -19,18 +20,18 @@ public int GetOutputStreams(string workingDirectory, string command, string para
{
Process process = CreateProcess(workingDirectory, command, param);

var localStandardOut = new List<string>();
var localStandardErr = new List<string>();
var localAllOutput = new List<string>();
var localStandardOut = new ConcurrentQueue<string>();
var localStandardErr = new ConcurrentQueue<string>();
var localAllOutput = new ConcurrentQueue<string>();
process.OutputDataReceived += (sender, e) =>
{
localStandardOut.Add(e.Data);
localAllOutput.Add(e.Data);
localStandardOut.Enqueue(e.Data);
localAllOutput.Enqueue(e.Data);
};
process.ErrorDataReceived += (sender, e) =>
{
localStandardErr.Add(e.Data);
localAllOutput.Add(e.Data);
localStandardErr.Enqueue(e.Data);
localAllOutput.Enqueue(e.Data);
};

try
Expand Down