Skip to content

Commit

Permalink
Use powershell sleep in unit tests (#6332)
Browse files Browse the repository at this point in the history
Co-authored-by: Rainer Sigwald <raines@microsoft.com>
  • Loading branch information
KirillOsenkov and rainersigwald committed Apr 22, 2021
1 parent b2d404f commit 10d6a76
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand Down Expand Up @@ -944,7 +944,7 @@ public async Task WaitForExitAsync(ILoggingService loggingService)
_process.Id);
CommunicationsUtilities.Trace("Killing node with pid = {0}", _process.Id);

_process.KillTree(timeout: 5000);
_process.KillTree(timeoutMilliseconds: 5000);
}

#if FEATURE_APM
Expand Down
5 changes: 2 additions & 3 deletions src/Shared/ProcessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.Build.Shared;

namespace Microsoft.Build.Shared
{
internal static class ProcessExtensions
{
public static void KillTree(this Process process, int timeout)
public static void KillTree(this Process process, int timeoutMilliseconds)
{
if (NativeMethodsShared.IsWindows)
{
Expand Down Expand Up @@ -41,7 +40,7 @@ public static void KillTree(this Process process, int timeout)
// wait until the process finishes exiting/getting killed.
// We don't want to wait forever here because the task is already supposed to be dieing, we just want to give it long enough
// to try and flush what it can and stop. If it cannot do that in a reasonable time frame then we will just ignore it.
process.WaitForExit(timeout);
process.WaitForExit(timeoutMilliseconds);
}

private static void GetAllChildIdsUnix(int parentId, ISet<int> children)
Expand Down
9 changes: 7 additions & 2 deletions src/Utilities.UnitTests/ProcessExtensions_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ public class ProcessExtensions_Tests
[Fact]
public async Task KillTree()
{
Process p = Process.Start("sleep", "600"); // sleep 10m.
var psi =
NativeMethodsShared.IsWindows ?
new ProcessStartInfo("powershell", "-NoLogo -NoProfile -command \"Start-Sleep -Seconds 600\"") :
new ProcessStartInfo("sleep", "600");

Process p = Process.Start(psi); // sleep 10m.

// Verify the process is running.
await Task.Delay(500);
p.HasExited.ShouldBe(false);

// Kill the process.
p.KillTree(timeout: 5000);
p.KillTree(timeoutMilliseconds: 5000);
p.HasExited.ShouldBe(true);
p.ExitCode.ShouldNotBe(0);
}
Expand Down

0 comments on commit 10d6a76

Please sign in to comment.