forked from microsoft/vstest
/
ProxyDiscoveryManager.cs
216 lines (186 loc) · 9.61 KB
/
ProxyDiscoveryManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestPlatform.Common;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
using Microsoft.VisualStudio.TestPlatform.Common.Telemetry;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Host;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
/// <summary>
/// Orchestrates discovery operations for the engine communicating with the client.
/// </summary>
public class ProxyDiscoveryManager : ProxyOperationManager, IProxyDiscoveryManager, ITestDiscoveryEventsHandler2
{
private readonly ITestRuntimeProvider testHostManager;
private IDataSerializer dataSerializer;
private bool isCommunicationEstablished;
private IRequestData requestData;
private ITestDiscoveryEventsHandler2 baseTestDiscoveryEventsHandler;
private bool skipDefaultAdapters;
private readonly IFileHelper fileHelper;
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ProxyDiscoveryManager"/> class.
/// </summary>
/// <param name="requestData">The Request Data for providing discovery services and data.</param>
/// <param name="testRequestSender">Test request sender instance.</param>
/// <param name="testHostManager">Test host manager instance.</param>
public ProxyDiscoveryManager(IRequestData requestData, ITestRequestSender testRequestSender, ITestRuntimeProvider testHostManager)
: this(requestData, testRequestSender, testHostManager, JsonDataSerializer.Instance, new FileHelper())
{
this.testHostManager = testHostManager;
}
/// <summary>
/// Initializes a new instance of the <see cref="ProxyDiscoveryManager"/> class.
/// Constructor with Dependency injection. Used for unit testing.
/// </summary>
/// <param name="requestData"></param>
/// <param name="requestSender">
/// The request Sender.
/// </param>
/// <param name="testHostManager">
/// Test host Manager instance
/// </param>
/// <param name="dataSerializer"></param>
internal ProxyDiscoveryManager(IRequestData requestData,
ITestRequestSender requestSender,
ITestRuntimeProvider testHostManager,
IDataSerializer dataSerializer,
IFileHelper fileHelper)
: base(requestData, requestSender, testHostManager)
{
this.dataSerializer = dataSerializer;
this.testHostManager = testHostManager;
this.isCommunicationEstablished = false;
this.requestData = requestData;
this.fileHelper = fileHelper;
}
#endregion
#region IProxyDiscoveryManager implementation.
/// <summary>
/// Ensure that the discovery component of engine is ready for discovery usually by loading extensions.
/// <param name="skipDefaultAdapters">Skip default adapters flag.</param>
/// </summary>
public void Initialize(bool skipDefaultAdapters)
{
this.skipDefaultAdapters = skipDefaultAdapters;
}
/// <summary>
/// Discovers tests
/// </summary>
/// <param name="discoveryCriteria">Settings, parameters for the discovery request</param>
/// <param name="eventHandler">EventHandler for handling discovery events from Engine</param>
public void DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)
{
this.baseTestDiscoveryEventsHandler = eventHandler;
try
{
this.isCommunicationEstablished = this.SetupChannel(discoveryCriteria.Sources, discoveryCriteria.RunSettings);
if (this.isCommunicationEstablished)
{
this.InitializeExtensions(discoveryCriteria.Sources);
discoveryCriteria.UpdateDiscoveryCriteria(testHostManager);
this.RequestSender.DiscoverTests(discoveryCriteria, this);
}
}
catch (Exception exception)
{
EqtTrace.Error("ProxyDiscoveryManager.DiscoverTests: Failed to discover tests: {0}", exception);
// Log to vs ide test output
var testMessagePayload = new TestMessagePayload { MessageLevel = TestMessageLevel.Error, Message = exception.ToString() };
var rawMessage = this.dataSerializer.SerializePayload(MessageType.TestMessage, testMessagePayload);
this.HandleRawMessage(rawMessage);
// Log to vstest.console
// Send a discovery complete to caller. Similar logic is also used in ParallelProxyDiscoveryManager.DiscoverTestsOnConcurrentManager
// Aborted is `true`: in case of parallel discovery (or non shared host), an aborted message ensures another discovery manager
// created to replace the current one. This will help if the current discovery manager is aborted due to irreparable error
// and the test host is lost as well.
this.HandleLogMessage(TestMessageLevel.Error, exception.ToString());
var discoveryCompletePayload = new DiscoveryCompletePayload()
{
IsAborted = true,
LastDiscoveredTests = null,
TotalTests = -1
};
this.HandleRawMessage(this.dataSerializer.SerializePayload(MessageType.DiscoveryComplete, discoveryCompletePayload));
var discoveryCompleteEventsArgs = new DiscoveryCompleteEventArgs(-1, true);
this.HandleDiscoveryComplete(discoveryCompleteEventsArgs, new List<ObjectModel.TestCase>());
}
}
/// <inheritdoc/>
public void Abort()
{
// Cancel fast, try to stop testhost deployment/launch
this.CancellationTokenSource.Cancel();
if (this.isCommunicationEstablished)
{
this.RequestSender.SendTestDiscoveryCancel();
}
}
/// <inheritdoc/>
public void HandleDiscoveryComplete(DiscoveryCompleteEventArgs discoveryCompleteEventArgs, IEnumerable<TestCase> lastChunk)
{
this.baseTestDiscoveryEventsHandler.HandleDiscoveryComplete(discoveryCompleteEventArgs, lastChunk);
}
/// <inheritdoc/>
public void HandleDiscoveredTests(IEnumerable<TestCase> discoveredTestCases)
{
this.baseTestDiscoveryEventsHandler.HandleDiscoveredTests(discoveredTestCases);
}
/// <inheritdoc/>
public void HandleRawMessage(string rawMessage)
{
var message = this.dataSerializer.DeserializeMessage(rawMessage);
if(string.Equals(message.MessageType, MessageType.DiscoveryComplete))
{
this.Close();
}
this.baseTestDiscoveryEventsHandler.HandleRawMessage(rawMessage);
}
/// <inheritdoc/>
public void HandleLogMessage(TestMessageLevel level, string message)
{
this.baseTestDiscoveryEventsHandler.HandleLogMessage(level, message);
}
#endregion
private void InitializeExtensions(IEnumerable<string> sources)
{
var extensions = TestPluginCache.Instance.GetExtensionPaths(TestPlatformConstants.TestAdapterEndsWithPattern, this.skipDefaultAdapters);
// Filter out non existing extensions
var nonExistingExtensions = extensions.Where(extension => !this.fileHelper.Exists(extension));
if (nonExistingExtensions.Any())
{
this.LogMessage(TestMessageLevel.Warning, string.Format(Resources.Resources.NonExistingExtensions, string.Join(",", nonExistingExtensions)));
}
var sourceList = sources.ToList();
var platformExtensions = this.testHostManager.GetTestPlatformExtensions(sourceList, extensions.Except(nonExistingExtensions));
// Only send this if needed.
if (platformExtensions.Any())
{
this.RequestSender.InitializeDiscovery(platformExtensions);
}
}
private void LogMessage(TestMessageLevel testMessageLevel, string message)
{
// Log to translation layer.
var testMessagePayload = new TestMessagePayload { MessageLevel = testMessageLevel, Message = message };
var rawMessage = this.dataSerializer.SerializePayload(MessageType.TestMessage, testMessagePayload);
this.HandleRawMessage(rawMessage);
// Log to vstest.console layer.
this.HandleLogMessage(testMessageLevel, message);
}
}
}