-
Notifications
You must be signed in to change notification settings - Fork 325
/
TestRequestSender.cs
735 lines (629 loc) · 32.1 KB
/
TestRequestSender.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
// 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.CommunicationUtilities
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using CoreUtilities.Helpers;
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.Host;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using CommonResources = Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources.Resources;
using ObjectModelConstants = Microsoft.VisualStudio.TestPlatform.ObjectModel.Constants;
/// <summary>
/// Test request sender implementation.
/// </summary>
public class TestRequestSender : ITestRequestSender
{
// Time to wait for test host exit
private const int ClientProcessExitWaitTimeout = 10 * 1000;
private readonly IDataSerializer dataSerializer;
private readonly ManualResetEventSlim connected;
private readonly ManualResetEventSlim clientExited;
private readonly int clientExitedWaitTime;
private ICommunicationEndPoint communicationEndpoint;
private ICommunicationChannel channel;
private EventHandler<MessageReceivedEventArgs> onMessageReceived;
private Action<DisconnectedEventArgs> onDisconnected;
// Set to 1 if Discovery/Execution is complete, i.e. complete handlers have been invoked
private int operationCompleted;
private ITestMessageEventHandler messageEventHandler;
private string clientExitErrorMessage;
// Set default to 1, if protocol version check does not happen
// that implies host is using version 1.
private int protocolVersion = 1;
private int highestSupportedVersion = 3;
private TestHostConnectionInfo connectionInfo;
private ITestRuntimeProvider runtimeProvider;
/// <summary>
/// Initializes a new instance of the <see cref="TestRequestSender"/> class.
/// </summary>
/// <param name="protocolConfig">Protocol configuration.</param>
/// <param name="runtimeProvider">The runtime provider.</param>
public TestRequestSender(ProtocolConfig protocolConfig, ITestRuntimeProvider runtimeProvider)
: this(
runtimeProvider,
communicationEndPoint: null,
runtimeProvider.GetTestHostConnectionInfo(),
JsonDataSerializer.Instance,
protocolConfig,
ClientProcessExitWaitTimeout)
{
this.SetCommunicationEndPoint();
}
internal TestRequestSender(
ITestRuntimeProvider runtimeProvider,
ICommunicationEndPoint communicationEndPoint,
TestHostConnectionInfo connectionInfo,
IDataSerializer serializer,
ProtocolConfig protocolConfig,
int clientExitedWaitTime)
{
this.dataSerializer = serializer;
this.connected = new ManualResetEventSlim(false);
this.clientExited = new ManualResetEventSlim(false);
this.clientExitedWaitTime = clientExitedWaitTime;
this.operationCompleted = 0;
this.highestSupportedVersion = protocolConfig.Version;
// The connectionInfo here is that of RuntimeProvider, so reverse the role of runner.
this.runtimeProvider = runtimeProvider;
this.communicationEndpoint = communicationEndPoint;
this.connectionInfo.Endpoint = connectionInfo.Endpoint;
this.connectionInfo.Role = connectionInfo.Role == ConnectionRole.Host
? ConnectionRole.Client
: ConnectionRole.Host;
}
/// <summary>
/// Initializes a new instance of the <see cref="TestRequestSender"/> class.
/// Used only for testing to inject communication endpoint.
/// </summary>
/// <param name="communicationEndPoint">Communication server implementation.</param>
/// <param name="connectionInfo">ConnectionInfo to set up transport layer</param>
/// <param name="serializer">Serializer implementation.</param>
/// <param name="protocolConfig">Protocol configuration.</param>
/// <param name="clientExitedWaitTime">Time to wait for client process exit.</param>
internal TestRequestSender(
ICommunicationEndPoint communicationEndPoint,
TestHostConnectionInfo connectionInfo,
IDataSerializer serializer,
ProtocolConfig protocolConfig,
int clientExitedWaitTime)
: this(
runtimeProvider: null,
communicationEndPoint,
connectionInfo,
serializer,
protocolConfig,
clientExitedWaitTime)
{
}
/// <inheritdoc />
public int InitializeCommunication()
{
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.InitializeCommunication: initialize communication. ");
}
// this.clientExitCancellationSource = new CancellationTokenSource();
this.clientExitErrorMessage = string.Empty;
this.communicationEndpoint.Connected += (sender, args) =>
{
this.channel = args.Channel;
if (args.Connected && this.channel != null)
{
this.connected.Set();
}
};
this.communicationEndpoint.Disconnected += (sender, args) =>
{
// If there's an disconnected event handler, call it
this.onDisconnected?.Invoke(args);
};
// Server start returns the listener port
// return int.Parse(this.communicationServer.Start());
var endpoint = this.communicationEndpoint.Start(this.connectionInfo.Endpoint);
return endpoint.GetIPEndPoint().Port;
}
/// <inheritdoc />
public bool WaitForRequestHandlerConnection(int connectionTimeout, CancellationToken cancellationToken)
{
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.WaitForRequestHandlerConnection: waiting for connection with timeout: {0}", connectionTimeout);
}
// Wait until either connection is successful, handled by connected.WaitHandle
// or operation is canceled, handled by cancellationToken.WaitHandle
// or testhost exits unexpectedly, handled by clientExited.WaitHandle
var waitIndex = WaitHandle.WaitAny(new WaitHandle[] { this.connected.WaitHandle, cancellationToken.WaitHandle, this.clientExited.WaitHandle }, connectionTimeout);
// Return true if connection was successful.
return waitIndex == 0;
}
/// <inheritdoc />
public void CheckVersionWithTestHost()
{
// Negotiation follows these steps:
// Runner sends highest supported version to Test host
// Test host sends the version it can support (must be less than highest) to runner
// Error case: test host can send a protocol error if it cannot find a supported version
var protocolNegotiated = new ManualResetEvent(false);
this.onMessageReceived = (sender, args) =>
{
var message = this.dataSerializer.DeserializeMessage(args.Data);
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.CheckVersionWithTestHost: onMessageReceived received message: {0}", message);
}
if (message.MessageType == MessageType.VersionCheck)
{
this.protocolVersion = this.dataSerializer.DeserializePayload<int>(message);
}
// TRH can also send TestMessage if tracing is enabled, so log it at runner end
else if (message.MessageType == MessageType.TestMessage)
{
// Ignore test messages. Currently we don't have handler(which sends messages to client/console.) here.
// Above we are logging it to EqtTrace.
}
else if (message.MessageType == MessageType.ProtocolError)
{
throw new TestPlatformException(string.Format(CultureInfo.CurrentUICulture, CommonResources.VersionCheckFailed));
}
else
{
throw new TestPlatformException(string.Format(
CultureInfo.CurrentUICulture,
CommonResources.UnexpectedMessage,
MessageType.VersionCheck,
message.MessageType));
}
protocolNegotiated.Set();
};
this.channel.MessageReceived += this.onMessageReceived;
try
{
// Send the protocol negotiation request. Note that we always serialize this data
// without any versioning in the message itself.
var data = this.dataSerializer.SerializePayload(MessageType.VersionCheck, this.highestSupportedVersion);
this.channel.Send(data);
// Wait for negotiation response
var timeout = EnvironmentHelper.GetConnectionTimeout();
if (!protocolNegotiated.WaitOne(timeout * 1000))
{
throw new TestPlatformException(string.Format(CultureInfo.CurrentUICulture, CommonResources.VersionCheckTimedout, timeout, EnvironmentHelper.VstestConnectionTimeout));
}
}
finally
{
this.channel.MessageReceived -= this.onMessageReceived;
this.onMessageReceived = null;
}
}
#region Discovery Protocol
/// <inheritdoc />
public void InitializeDiscovery(IEnumerable<string> pathToAdditionalExtensions)
{
var message = this.dataSerializer.SerializePayload(
MessageType.DiscoveryInitialize,
pathToAdditionalExtensions,
this.protocolVersion);
this.channel.Send(message);
}
/// <inheritdoc/>
public void DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 discoveryEventsHandler)
{
this.messageEventHandler = discoveryEventsHandler;
this.onDisconnected = (disconnectedEventArgs) =>
{
this.OnDiscoveryAbort(discoveryEventsHandler, disconnectedEventArgs.Error, true);
};
this.onMessageReceived = (sender, args) => this.OnDiscoveryMessageReceived(discoveryEventsHandler, args);
this.channel.MessageReceived += this.onMessageReceived;
var message = this.dataSerializer.SerializePayload(
MessageType.StartDiscovery,
discoveryCriteria,
this.protocolVersion);
this.channel.Send(message);
}
#endregion
#region Execution Protocol
/// <inheritdoc />
public void InitializeExecution(IEnumerable<string> pathToAdditionalExtensions)
{
var message = this.dataSerializer.SerializePayload(
MessageType.ExecutionInitialize,
pathToAdditionalExtensions,
this.protocolVersion);
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.InitializeExecution: Sending initializing execution with message: {0}", message);
}
this.channel.Send(message);
}
/// <inheritdoc />
public void StartTestRun(TestRunCriteriaWithSources runCriteria, ITestRunEventsHandler eventHandler)
{
this.messageEventHandler = eventHandler;
this.onDisconnected = (disconnectedEventArgs) =>
{
this.OnTestRunAbort(eventHandler, disconnectedEventArgs.Error, true);
};
this.onMessageReceived = (sender, args) => this.OnExecutionMessageReceived(sender, args, eventHandler);
this.channel.MessageReceived += this.onMessageReceived;
// This code section is needed because we altered the old testhost launch process for
// the debugging workflow. Now we don't ask VS to launch and attach to the testhost
// process for us as we previously did, instead we launch it as a standalone process
// and rely on the testhost to ask VS to attach the debugger to itself.
//
// In order to avoid breaking compatibility with previous testhost versions because of
// those changes (older testhosts won't know to request VS to attach to themselves
// thinking instead VS launched and attached to them already), we request VS to attach
// to the testhost here before starting the test run.
if (runCriteria.TestExecutionContext != null
&& runCriteria.TestExecutionContext.IsDebug
&& this.runtimeProvider is ITestRuntimeProvider2 convertedRuntimeProvider
&& this.protocolVersion < ObjectModelConstants.MinimumProtocolVersionWithDebugSupport)
{
var handler = (ITestRunEventsHandler2)eventHandler;
if (!convertedRuntimeProvider.AttachDebuggerToTestHost())
{
EqtTrace.Warning(
string.Format(
CultureInfo.CurrentUICulture,
CommonResources.AttachDebuggerToDefaultTestHostFailure));
}
}
var message = this.dataSerializer.SerializePayload(
MessageType.StartTestExecutionWithSources,
runCriteria,
this.protocolVersion);
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.StartTestRun: Sending test run with message: {0}", message);
}
this.channel.Send(message);
}
/// <inheritdoc />
public void StartTestRun(TestRunCriteriaWithTests runCriteria, ITestRunEventsHandler eventHandler)
{
this.messageEventHandler = eventHandler;
this.onDisconnected = (disconnectedEventArgs) =>
{
this.OnTestRunAbort(eventHandler, disconnectedEventArgs.Error, true);
};
this.onMessageReceived = (sender, args) => this.OnExecutionMessageReceived(sender, args, eventHandler);
this.channel.MessageReceived += this.onMessageReceived;
// This code section is needed because we altered the old testhost launch process for
// the debugging workflow. Now we don't ask VS to launch and attach to the testhost
// process for us as we previously did, instead we launch it as a standalone process
// and rely on the testhost to ask VS to attach the debugger to itself.
//
// In order to avoid breaking compatibility with previous testhost versions because of
// those changes (older testhosts won't know to request VS to attach to themselves
// thinking instead VS launched and attached to them already), we request VS to attach
// to the testhost here before starting the test run.
if (runCriteria.TestExecutionContext != null
&& runCriteria.TestExecutionContext.IsDebug
&& this.runtimeProvider is ITestRuntimeProvider2 convertedRuntimeProvider
&& this.protocolVersion < ObjectModelConstants.MinimumProtocolVersionWithDebugSupport)
{
var handler = (ITestRunEventsHandler2)eventHandler;
if (!convertedRuntimeProvider.AttachDebuggerToTestHost())
{
EqtTrace.Warning(
string.Format(
CultureInfo.CurrentUICulture,
CommonResources.AttachDebuggerToDefaultTestHostFailure));
}
}
var message = this.dataSerializer.SerializePayload(
MessageType.StartTestExecutionWithTests,
runCriteria,
this.protocolVersion);
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.StartTestRun: Sending test run with message: {0}", message);
}
this.channel.Send(message);
}
/// <inheritdoc />
public void SendTestRunCancel()
{
if (this.IsOperationComplete())
{
EqtTrace.Verbose("TestRequestSender: SendTestRunCancel: Operation is already complete. Skip error message.");
return;
}
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.SendTestRunCancel: Sending test run cancel.");
}
this.channel?.Send(this.dataSerializer.SerializeMessage(MessageType.CancelTestRun));
}
/// <inheritdoc />
public void SendTestRunAbort()
{
if (this.IsOperationComplete())
{
EqtTrace.Verbose("TestRequestSender: SendTestRunAbort: Operation is already complete. Skip error message.");
return;
}
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.SendTestRunAbort: Sending test run abort.");
}
this.channel?.Send(this.dataSerializer.SerializeMessage(MessageType.AbortTestRun));
}
#endregion
/// <inheritdoc />
public void EndSession()
{
if (!this.IsOperationComplete())
{
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.EndSession: Sending end session.");
}
this.channel?.Send(this.dataSerializer.SerializeMessage(MessageType.SessionEnd));
}
}
/// <inheritdoc />
public void OnClientProcessExit(string stdError)
{
// This method is called on test host exit. If test host has any errors, stdError
// provides the crash call stack.
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info($"TestRequestSender.OnClientProcessExit: Test host process exited. Standard error: {stdError}");
}
this.clientExitErrorMessage = stdError;
this.clientExited.Set();
// Break communication loop. In some cases (E.g: When tests creates child processes to testhost) communication channel won't break if testhost exits.
this.communicationEndpoint.Stop();
}
/// <inheritdoc />
public void Close()
{
this.Dispose();
EqtTrace.Info("Closing the connection");
}
/// <inheritdoc />
public void Dispose()
{
if (this.channel != null)
{
this.channel.MessageReceived -= this.onMessageReceived;
}
this.communicationEndpoint.Stop();
}
private void OnExecutionMessageReceived(object sender, MessageReceivedEventArgs messageReceived, ITestRunEventsHandler testRunEventsHandler)
{
try
{
var rawMessage = messageReceived.Data;
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.OnExecutionMessageReceived: Received message: {0}", rawMessage);
}
// Send raw message first to unblock handlers waiting to send message to IDEs
testRunEventsHandler.HandleRawMessage(rawMessage);
var message = this.dataSerializer.DeserializeMessage(rawMessage);
switch (message.MessageType)
{
case MessageType.TestRunStatsChange:
var testRunChangedArgs = this.dataSerializer.DeserializePayload<TestRunChangedEventArgs>(message);
testRunEventsHandler.HandleTestRunStatsChange(testRunChangedArgs);
break;
case MessageType.ExecutionComplete:
var testRunCompletePayload = this.dataSerializer.DeserializePayload<TestRunCompletePayload>(message);
testRunEventsHandler.HandleTestRunComplete(
testRunCompletePayload.TestRunCompleteArgs,
testRunCompletePayload.LastRunTests,
testRunCompletePayload.RunAttachments,
testRunCompletePayload.ExecutorUris);
this.SetOperationComplete();
break;
case MessageType.TestMessage:
var testMessagePayload = this.dataSerializer.DeserializePayload<TestMessagePayload>(message);
testRunEventsHandler.HandleLogMessage(testMessagePayload.MessageLevel, testMessagePayload.Message);
break;
case MessageType.LaunchAdapterProcessWithDebuggerAttached:
var testProcessStartInfo = this.dataSerializer.DeserializePayload<TestProcessStartInfo>(message);
int processId = testRunEventsHandler.LaunchProcessWithDebuggerAttached(testProcessStartInfo);
var data =
this.dataSerializer.SerializePayload(
MessageType.LaunchAdapterProcessWithDebuggerAttachedCallback,
processId,
this.protocolVersion);
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.OnExecutionMessageReceived: Sending LaunchAdapterProcessWithDebuggerAttachedCallback message: {0}", data);
}
this.channel.Send(data);
break;
case MessageType.AttachDebugger:
var testProcessPid = this.dataSerializer.DeserializePayload<TestProcessAttachDebuggerPayload>(message);
bool result = ((ITestRunEventsHandler2)testRunEventsHandler).AttachDebuggerToProcess(testProcessPid.ProcessID);
var resultMessage = this.dataSerializer.SerializePayload(
MessageType.AttachDebuggerCallback,
result,
this.protocolVersion);
this.channel.Send(resultMessage);
break;
}
}
catch (Exception exception)
{
this.OnTestRunAbort(testRunEventsHandler, exception, false);
}
}
private void OnDiscoveryMessageReceived(ITestDiscoveryEventsHandler2 discoveryEventsHandler, MessageReceivedEventArgs args)
{
try
{
var rawMessage = args.Data;
// Currently each of the operations are not separate tasks since they should not each take much time. This is just a notification.
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.OnDiscoveryMessageReceived: Received message: {0}", rawMessage);
}
// Send raw message first to unblock handlers waiting to send message to IDEs
discoveryEventsHandler.HandleRawMessage(rawMessage);
var data = this.dataSerializer.DeserializeMessage(rawMessage);
switch (data.MessageType)
{
case MessageType.TestCasesFound:
var testCases = this.dataSerializer.DeserializePayload<IEnumerable<TestCase>>(data);
discoveryEventsHandler.HandleDiscoveredTests(testCases);
break;
case MessageType.DiscoveryComplete:
var discoveryCompletePayload =
this.dataSerializer.DeserializePayload<DiscoveryCompletePayload>(data);
var discoveryCompleteEventArgs = new DiscoveryCompleteEventArgs(discoveryCompletePayload.TotalTests, discoveryCompletePayload.IsAborted);
discoveryCompleteEventArgs.Metrics = discoveryCompletePayload.Metrics;
discoveryEventsHandler.HandleDiscoveryComplete(
discoveryCompleteEventArgs,
discoveryCompletePayload.LastDiscoveredTests);
this.SetOperationComplete();
break;
case MessageType.TestMessage:
var testMessagePayload = this.dataSerializer.DeserializePayload<TestMessagePayload>(
data);
discoveryEventsHandler.HandleLogMessage(
testMessagePayload.MessageLevel,
testMessagePayload.Message);
break;
}
}
catch (Exception ex)
{
this.OnDiscoveryAbort(discoveryEventsHandler, ex, false);
}
}
private void OnTestRunAbort(ITestRunEventsHandler testRunEventsHandler, Exception exception, bool getClientError)
{
if (this.IsOperationComplete())
{
EqtTrace.Verbose("TestRequestSender: OnTestRunAbort: Operation is already complete. Skip error message.");
return;
}
EqtTrace.Verbose("TestRequestSender: OnTestRunAbort: Set operation complete.");
this.SetOperationComplete();
var reason = this.GetAbortErrorMessage(exception, getClientError);
EqtTrace.Error("TestRequestSender: Aborting test run because {0}", reason);
this.LogErrorMessage(string.Format(CommonResources.AbortedTestRun, reason));
// notify test run abort to vstest console wrapper.
var completeArgs = new TestRunCompleteEventArgs(null, false, true, exception, null, TimeSpan.Zero);
var payload = new TestRunCompletePayload { TestRunCompleteArgs = completeArgs };
var rawMessage = this.dataSerializer.SerializePayload(MessageType.ExecutionComplete, payload);
testRunEventsHandler.HandleRawMessage(rawMessage);
// notify of a test run complete and bail out.
testRunEventsHandler.HandleTestRunComplete(completeArgs, null, null, null);
}
private void OnDiscoveryAbort(ITestDiscoveryEventsHandler2 eventHandler, Exception exception, bool getClientError)
{
if (this.IsOperationComplete())
{
EqtTrace.Verbose("TestRequestSender: OnDiscoveryAbort: Operation is already complete. Skip error message.");
return;
}
EqtTrace.Verbose("TestRequestSender: OnDiscoveryAbort: Set operation complete.");
this.SetOperationComplete();
var discoveryCompleteEventArgs = new DiscoveryCompleteEventArgs(-1, true);
var reason = this.GetAbortErrorMessage(exception, getClientError);
EqtTrace.Error("TestRequestSender: Aborting test discovery because {0}", reason);
this.LogErrorMessage(string.Format(CommonResources.AbortedTestDiscovery, reason));
// Notify discovery abort to IDE test output
var payload = new DiscoveryCompletePayload()
{
IsAborted = true,
LastDiscoveredTests = null,
TotalTests = -1
};
var rawMessage = this.dataSerializer.SerializePayload(MessageType.DiscoveryComplete, payload);
eventHandler.HandleRawMessage(rawMessage);
// Complete discovery
eventHandler.HandleDiscoveryComplete(discoveryCompleteEventArgs, null);
}
private string GetAbortErrorMessage(Exception exception, bool getClientError)
{
EqtTrace.Verbose("TestRequestSender: GetAbortErrorMessage: Exception: " + exception);
// It is also possible for an operation to abort even if client has not
// disconnected, e.g. if there's an error parsing the response from test host. We
// want the exception to be available in those scenarios.
var reason = exception?.Message;
if (getClientError)
{
EqtTrace.Verbose("TestRequestSender: GetAbortErrorMessage: Client has disconnected. Wait for standard error.");
// Wait for test host to exit for a moment
if (this.clientExited.Wait(this.clientExitedWaitTime))
{
// Set a default message of test host process exited and additionally specify the error if present
EqtTrace.Info("TestRequestSender: GetAbortErrorMessage: Received test host error message.");
reason = CommonResources.TestHostProcessCrashed;
if (!string.IsNullOrWhiteSpace(this.clientExitErrorMessage))
{
reason = $"{reason} : {this.clientExitErrorMessage}";
}
}
else
{
reason = CommonResources.UnableToCommunicateToTestHost;
EqtTrace.Info("TestRequestSender: GetAbortErrorMessage: Timed out waiting for test host error message.");
}
}
return reason;
}
private void LogErrorMessage(string message)
{
if (this.messageEventHandler == null)
{
EqtTrace.Error("TestRequestSender.LogErrorMessage: Message event handler not set. Error: " + message);
return;
}
// Log to vstest console
this.messageEventHandler.HandleLogMessage(TestMessageLevel.Error, message);
// Log to vs ide test output
var testMessagePayload = new TestMessagePayload { MessageLevel = TestMessageLevel.Error, Message = message };
var rawMessage = this.dataSerializer.SerializePayload(MessageType.TestMessage, testMessagePayload);
this.messageEventHandler.HandleRawMessage(rawMessage);
}
private bool IsOperationComplete()
{
return this.operationCompleted == 1;
}
private void SetOperationComplete()
{
// Complete the currently ongoing operation (Discovery/Execution)
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.SetOperationComplete: Setting operation complete.");
}
this.communicationEndpoint.Stop();
Interlocked.CompareExchange(ref this.operationCompleted, 1, 0);
}
private void SetCommunicationEndPoint()
{
// TODO: Use factory to get the communication endpoint. It will abstract out the type of communication endpoint like socket, shared memory or named pipe etc.,
if (this.connectionInfo.Role == ConnectionRole.Client)
{
this.communicationEndpoint = new SocketClient();
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender is acting as client");
}
}
else
{
this.communicationEndpoint = new SocketServer();
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender is acting as server");
}
}
}
}
}