Skip to content

Commit

Permalink
Use distinct signal to wait to cancel the submission (#9326)
Browse files Browse the repository at this point in the history
* Use distinct signal to wait to cancel the submission

* Registering log event handler to set the signal instead of iterating all log events

* Add timeout to the wait
  • Loading branch information
GangWang01 authored Oct 23, 2023
1 parent 221fd2e commit 08494c7
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Build.UnitTests/BackEnd/TaskBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,24 @@ public void CanceledTasksDoNotLogMSB4181()
BuildManager manager = new BuildManager();
ProjectCollection collection = new ProjectCollection();

string sleepCommand = Helpers.GetSleepCommand(TimeSpan.FromSeconds(10));
string contents = @"
<Project ToolsVersion ='Current'>
<Target Name='test'>
<Exec Command='" + Helpers.GetSleepCommand(TimeSpan.FromSeconds(10)) + @"'/>
<Exec Command='" + sleepCommand + @"'/>
</Target>
</Project>";

MockLogger logger = new MockLogger(_testOutput);
ManualResetEvent waitCommandExecuted = new ManualResetEvent(false);
string unescapedSleepCommand = sleepCommand.Replace("&quot;", "\"").Replace("&gt;", ">");
logger.AdditionalHandlers.Add((sender, args) =>
{
if (unescapedSleepCommand.Equals(args.Message))
{
waitCommandExecuted.Set();
}
});

var project = new Project(XmlReader.Create(new StringReader(contents)), null, MSBuildConstants.CurrentToolsVersion, collection)
{
Expand All @@ -169,11 +179,14 @@ public void CanceledTasksDoNotLogMSB4181()
manager.BeginBuild(_parameters);
BuildSubmission asyncResult = manager.PendBuildRequest(data);
asyncResult.ExecuteAsync(null, null);
Thread.Sleep(500);
int timeoutMilliseconds = 2000;
bool isCommandExecuted = waitCommandExecuted.WaitOne(timeoutMilliseconds);
manager.CancelAllSubmissions();
asyncResult.WaitHandle.WaitOne();
bool isSubmissionComplated = asyncResult.WaitHandle.WaitOne(timeoutMilliseconds);
BuildResult result = asyncResult.BuildResult;
manager.EndBuild();
isCommandExecuted.ShouldBeTrue($"Waiting for that the sleep command is executed failed in the timeout period {timeoutMilliseconds} ms.");
isSubmissionComplated.ShouldBeTrue($"Waiting for that the build submission is completed failed in the timeout period {timeoutMilliseconds} ms.");

// No errors from cancelling a build.
logger.ErrorCount.ShouldBe(0);
Expand Down

0 comments on commit 08494c7

Please sign in to comment.