forked from microsoft/vstest
/
ExecutionManager.cs
250 lines (220 loc) · 10.8 KB
/
ExecutionManager.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// 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.Execution
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
using Microsoft.VisualStudio.TestPlatform.Common.Logging;
using Microsoft.VisualStudio.TestPlatform.Common.SettingsProvider;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Tracing;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Tracing.Interfaces;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.Interfaces;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.ClientProtocol;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.TesthostProtocol;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
/// <summary>
/// Orchestrates test execution related functionality for the engine communicating with the test host process.
/// </summary>
public class ExecutionManager : IExecutionManager
{
private readonly ITestPlatformEventSource testPlatformEventSource;
private BaseRunTests activeTestRun;
private IRequestData requestData;
private readonly TestSessionMessageLogger sessionMessageLogger;
private ITestMessageEventHandler testMessageEventsHandler;
/// <summary>
/// Initializes a new instance of the <see cref="ExecutionManager"/> class.
/// </summary>
public ExecutionManager(IRequestData requestData) : this(TestPlatformEventSource.Instance, requestData)
{
this.sessionMessageLogger = TestSessionMessageLogger.Instance;
this.sessionMessageLogger.TestRunMessage += this.TestSessionMessageHandler;
}
private void TestSessionMessageHandler(object sender, TestRunMessageEventArgs e)
{
if (this.testMessageEventsHandler != null)
{
this.testMessageEventsHandler.HandleLogMessage(e.Level, e.Message);
}
else
{
if (EqtTrace.IsWarningEnabled)
{
EqtTrace.Warning(
"ExecutionManager: Could not pass the log message '{0}' as the callback is null.",
e.Message);
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ExecutionManager"/> class.
/// </summary>
/// <param name="testPlatformEventSource">Test platform event source.</param>
protected ExecutionManager(ITestPlatformEventSource testPlatformEventSource, IRequestData requestData)
{
this.testPlatformEventSource = testPlatformEventSource;
this.requestData = requestData;
}
#region IExecutionManager Implementation
/// <summary>
/// Initializes the execution manager.
/// </summary>
/// <param name="pathToAdditionalExtensions"> The path to additional extensions. </param>
public void Initialize(IEnumerable<string> pathToAdditionalExtensions, ITestMessageEventHandler testMessageEventsHandler)
{
this.testMessageEventsHandler = testMessageEventsHandler;
this.testPlatformEventSource.AdapterSearchStart();
if (pathToAdditionalExtensions != null && pathToAdditionalExtensions.Any())
{
// Start using these additional extensions
TestPluginCache.Instance.DefaultExtensionPaths = pathToAdditionalExtensions;
}
this.LoadExtensions();
this.testPlatformEventSource.AdapterSearchStop();
}
/// <summary>
/// Starts the test run
/// </summary>
/// <param name="adapterSourceMap"> The adapter Source Map. </param>
/// <param name="package">The user input test source(package) if it differ from actual test source otherwise null.</param>
/// <param name="runSettings"> The run Settings. </param>
/// <param name="testExecutionContext"> The test Execution Context. </param>
/// <param name="testCaseEventsHandler"> EventHandler for handling test cases level events from Engine. </param>
/// <param name="runEventsHandler"> EventHandler for handling execution events from Engine. </param>
public void StartTestRun(
Dictionary<string, IEnumerable<string>> adapterSourceMap,
string package,
string runSettings,
TestExecutionContext testExecutionContext,
ITestCaseEventsHandler testCaseEventsHandler,
ITestRunEventsHandler runEventsHandler)
{
try
{
this.InitializeDataCollectors(runSettings, testCaseEventsHandler as ITestEventsPublisher, TestSourcesUtility.GetDefaultCodebasePath(adapterSourceMap));
this.activeTestRun = new RunTestsWithSources(this.requestData, adapterSourceMap, package, runSettings, testExecutionContext, testCaseEventsHandler, runEventsHandler);
this.activeTestRun.RunTests();
}
catch(Exception e)
{
runEventsHandler.HandleLogMessage(ObjectModel.Logging.TestMessageLevel.Error, e.ToString());
this.Abort(runEventsHandler);
}
finally
{
this.activeTestRun = null;
}
}
/// <summary>
/// Starts the test run with tests.
/// </summary>
/// <param name="tests"> The test list. </param>
/// <param name="package">The user input test source(package) if it differ from actual test source otherwise null.</param>
/// <param name="runSettings"> The run Settings. </param>
/// <param name="testExecutionContext"> The test Execution Context. </param>
/// <param name="testCaseEventsHandler"> EventHandler for handling test cases level events from Engine. </param>
/// <param name="runEventsHandler"> EventHandler for handling execution events from Engine. </param>
public void StartTestRun(
IEnumerable<TestCase> tests,
string package,
string runSettings,
TestExecutionContext testExecutionContext,
ITestCaseEventsHandler testCaseEventsHandler,
ITestRunEventsHandler runEventsHandler)
{
try
{
this.InitializeDataCollectors(runSettings, testCaseEventsHandler as ITestEventsPublisher, TestSourcesUtility.GetDefaultCodebasePath(tests));
this.activeTestRun = new RunTestsWithTests(this.requestData, tests, package, runSettings, testExecutionContext, testCaseEventsHandler, runEventsHandler);
this.activeTestRun.RunTests();
}
catch(Exception e)
{
runEventsHandler.HandleLogMessage(ObjectModel.Logging.TestMessageLevel.Error, e.ToString());
this.Abort(runEventsHandler);
}
finally
{
this.activeTestRun = null;
}
}
/// <summary>
/// Cancel the test execution.
/// </summary>
public void Cancel(ITestRunEventsHandler testRunEventsHandler)
{
if (this.activeTestRun == null)
{
var testRunCompleteEventArgs = new TestRunCompleteEventArgs(null, true, false, null, null, TimeSpan.Zero);
testRunEventsHandler.HandleTestRunComplete(testRunCompleteEventArgs, null, null, null);
}
else
{
this.activeTestRun.Cancel();
}
}
/// <summary>
/// Aborts the test execution.
/// </summary>
public void Abort(ITestRunEventsHandler testRunEventsHandler)
{
if (this.activeTestRun == null)
{
var testRunCompleteEventArgs = new TestRunCompleteEventArgs(null, false, true, null, null, TimeSpan.Zero);
testRunEventsHandler.HandleTestRunComplete(testRunCompleteEventArgs, null, null, null);
}
else
{
this.activeTestRun.Abort();
}
}
#endregion
#region private methods
private void LoadExtensions()
{
try
{
// Load the extensions on creation so that we dont have to spend time during first execution.
EqtTrace.Verbose("TestExecutorService: Loading the extensions");
TestExecutorExtensionManager.LoadAndInitializeAllExtensions(false);
EqtTrace.Verbose("TestExecutorService: Loaded the executors");
SettingsProviderExtensionManager.LoadAndInitializeAllExtensions(false);
EqtTrace.Verbose("TestExecutorService: Loaded the settings providers");
EqtTrace.Info("TestExecutorService: Loaded the extensions");
}
catch (Exception ex)
{
if (EqtTrace.IsWarningEnabled)
{
EqtTrace.Warning("TestExecutorWebService: Exception occured while calling test connection. {0}", ex);
}
}
}
/// <summary>
/// Initializes outproc and inproc data collectors.
/// </summary>
private void InitializeDataCollectors(string runSettings, ITestEventsPublisher testEventsPublisher, string defaultCodeBase)
{
// Initialize outproc data collectors if declared in run settings.
if (DataCollectionTestCaseEventSender.Instance != null && XmlRunSettingsUtilities.IsDataCollectionEnabled(runSettings))
{
var outOfProcDataCollectionManager = new ProxyOutOfProcDataCollectionManager(DataCollectionTestCaseEventSender.Instance, testEventsPublisher);
}
// Initialize inproc data collectors if declared in run settings.
if (XmlRunSettingsUtilities.IsInProcDataCollectionEnabled(runSettings))
{
var inProcDataCollectionExtensionManager = new InProcDataCollectionExtensionManager(runSettings, testEventsPublisher, defaultCodeBase, TestPluginCache.Instance);
}
}
#endregion
}
}