Skip to content

Commit 9b8fac9

Browse files
authored
Aborting discovery (#2896)
1 parent 02b06df commit 9b8fac9

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/Microsoft.TestPlatform.CrossPlatEngine/Client/Parallel/ParallelDiscoveryEventsHandler.cs

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.Parallel
1414
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;
1515
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
1616

17+
using CommonResources = Common.Resources.Resources;
18+
1719
/// <summary>
1820
/// ParallelDiscoveryEventsHandler for handling the discovery events in case of parallel discovery
1921
/// </summary>
@@ -122,6 +124,13 @@ public void HandleRawMessage(string rawMessage)
122124
// Always aggregate data, deserialize and raw for complete events
123125
var message = this.dataSerializer.DeserializeMessage(rawMessage);
124126

127+
// Do not send CancellationRequested message to Output window in IDE, as it is not useful for user
128+
if (string.Equals(message.MessageType, MessageType.TestMessage)
129+
&& rawMessage.IndexOf(CommonResources.CancellationRequested) >= 0)
130+
{
131+
return;
132+
}
133+
125134
// Do not deserialize further
126135
if (!string.Equals(MessageType.DiscoveryComplete, message.MessageType))
127136
{

src/Microsoft.TestPlatform.CrossPlatEngine/Client/Parallel/ParallelProxyDiscoveryManager.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ internal class ParallelProxyDiscoveryManager : ParallelOperationManager<IProxyDi
3838

3939
private IRequestData requestData;
4040

41+
// This field indicates if abort was requested by testplatform (user)
42+
private bool discoveryAbortRequested = false;
43+
4144
#endregion
4245

4346
#region Concurrency Keeper Objects
@@ -89,6 +92,7 @@ public void DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEve
8992
/// <inheritdoc/>
9093
public void Abort()
9194
{
95+
this.discoveryAbortRequested = true;
9296
this.DoActionOnAllManagers((proxyManager) => proxyManager.Abort(), doActionsInParallel: true);
9397
}
9498

@@ -121,8 +125,13 @@ public bool HandlePartialDiscoveryComplete(IProxyDiscoveryManager proxyDiscovery
121125
}
122126
}
123127

124-
// Discovery is completed. Schedule the clean up for managers and handlers.
125-
if (allDiscoverersCompleted)
128+
/*
129+
If discovery is complete or discovery aborting was requsted by testPlatfrom(user)
130+
we need to stop all ongoing discoveries, because we want to separate aborting request
131+
when testhost crashed by itself and when user requested it (f.e. through TW)
132+
Schedule the clean up for managers and handlers.
133+
*/
134+
if (allDiscoverersCompleted || discoveryAbortRequested)
126135
{
127136
// Reset enumerators
128137
this.sourceEnumerator = null;

test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/Parallel/ParallelProxyDiscoveryManagerTests.cs

+22
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@ public void DiscoveryTestsShouldProcessAllSourcesOnDiscoveryAbortsForAnySource()
122122
Assert.AreEqual(2, processedSources.Count, "All Sources must be processed.");
123123
}
124124

125+
/// <summary>
126+
/// Create ParallelProxyDiscoveryManager with parallel level 1 and two sources,
127+
/// Overall discovery should stop, if aborting was requested
128+
/// </summary>
129+
[TestMethod]
130+
public void DiscoveryTestsShouldStopDiscoveryIfAbortionWasRequested()
131+
{
132+
// Since the hosts are aborted, total aggregated tests sent across will be -1
133+
var discoveryManagerMock = new Mock<IProxyDiscoveryManager>();
134+
this.createdMockManagers.Add(discoveryManagerMock);
135+
var parallelDiscoveryManager = this.SetupDiscoveryManager(() => discoveryManagerMock.Object, 1, true, totalTests: -1);
136+
137+
Task.Run(() =>
138+
{
139+
parallelDiscoveryManager.DiscoverTests(this.testDiscoveryCriteria, this.mockHandler.Object);
140+
parallelDiscoveryManager.Abort();
141+
});
142+
143+
Assert.IsTrue(this.discoveryCompleted.Wait(taskTimeout), "Test discovery not completed.");
144+
Assert.AreEqual(1, processedSources.Count, "One source should be processed.");
145+
}
146+
125147
[TestMethod]
126148
public void DiscoveryTestsShouldProcessAllSourceIfOneDiscoveryManagerIsStarved()
127149
{

0 commit comments

Comments
 (0)