forked from microsoft/vstest
/
TestRequestHandlerTests.cs
531 lines (431 loc) · 22.9 KB
/
TestRequestHandlerTests.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Net.Sockets;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.ClientProtocol;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities;
namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities
{
using System.IO;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.TesthostProtocol;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
[TestClass]
public class TestRequestHandlerTests
{
private readonly Mock<ICommunicationEndPoint> mockCommunicationClient;
private readonly Mock<ICommunicationEndpointFactory> mockCommunicationEndpointFactory;
private readonly Mock<ICommunicationChannel> mockChannel;
private readonly Mock<ITestHostManagerFactory> mockTestHostManagerFactory;
private readonly Mock<IDiscoveryManager> mockDiscoveryManager;
private readonly Mock<IExecutionManager> mockExecutionManager;
private readonly JsonDataSerializer dataSerializer;
private ITestRequestHandler requestHandler;
private readonly TestHostConnectionInfo testHostConnectionInfo;
private readonly JobQueue<Action> jobQueue;
public TestRequestHandlerTests()
{
this.mockCommunicationClient = new Mock<ICommunicationEndPoint>();
this.mockCommunicationEndpointFactory = new Mock<ICommunicationEndpointFactory>();
this.mockChannel = new Mock<ICommunicationChannel>();
this.dataSerializer = JsonDataSerializer.Instance;
this.testHostConnectionInfo = new TestHostConnectionInfo
{
Endpoint = IPAddress.Loopback + ":123",
Role = ConnectionRole.Client
};
this.jobQueue = new JobQueue<Action>(
action => { action(); },
"TestHostOperationQueue",
500,
25000000,
true,
message => EqtTrace.Error(message));
// Setup mock discovery and execution managers
this.mockTestHostManagerFactory = new Mock<ITestHostManagerFactory>();
this.mockDiscoveryManager = new Mock<IDiscoveryManager>();
this.mockExecutionManager = new Mock<IExecutionManager>();
this.mockTestHostManagerFactory.Setup(mf => mf.GetDiscoveryManager()).Returns(this.mockDiscoveryManager.Object);
this.mockTestHostManagerFactory.Setup(mf => mf.GetExecutionManager()).Returns(this.mockExecutionManager.Object);
this.mockCommunicationEndpointFactory.Setup(f => f.Create(ConnectionRole.Client))
.Returns(this.mockCommunicationClient.Object);
this.requestHandler = new TestableTestRequestHandler(
this.testHostConnectionInfo,
this.mockCommunicationEndpointFactory.Object,
JsonDataSerializer.Instance,
jobQueue);
this.requestHandler.InitializeCommunication();
this.mockCommunicationClient.Raise(e => e.Connected += null, new ConnectedEventArgs(this.mockChannel.Object));
}
[TestMethod]
public void InitializeCommunicationShouldConnectToServerAsynchronously()
{
this.mockCommunicationClient.Verify(c => c.Start(this.testHostConnectionInfo.Endpoint), Times.Once);
}
[TestMethod]
public void InitializeCommunicationShouldThrowIfServerIsNotAccessible()
{
var connectionInfo = new TestHostConnectionInfo
{
Endpoint = IPAddress.Loopback + ":123",
Role = ConnectionRole.Client
};
var socketClient = new SocketClient();
socketClient.Connected += (sender, connectedEventArgs) =>
{
Assert.IsFalse(connectedEventArgs.Connected);
Assert.AreEqual(typeof(SocketException), connectedEventArgs.Fault.InnerException.GetType());
};
this.mockCommunicationEndpointFactory.Setup(f => f.Create(ConnectionRole.Client))
.Returns(socketClient);
var rh = new TestableTestRequestHandler(connectionInfo, this.mockCommunicationEndpointFactory.Object, this.dataSerializer, this.jobQueue);
rh.InitializeCommunication();
this.requestHandler.WaitForRequestSenderConnection(1000);
}
[TestMethod]
public void WaitForRequestSenderConnectionShouldWaitUntilConnectionIsSetup()
{
Assert.IsTrue(this.requestHandler.WaitForRequestSenderConnection(1000));
}
[TestMethod]
public void WaitForRequestSenderConnectionShouldReturnFalseIfConnectionSetupTimesout()
{
this.requestHandler = new TestableTestRequestHandler(
this.testHostConnectionInfo,
this.mockCommunicationEndpointFactory.Object,
JsonDataSerializer.Instance,
jobQueue);
this.requestHandler.InitializeCommunication();
Assert.IsFalse(this.requestHandler.WaitForRequestSenderConnection(1));
}
[TestMethod]
public void ProcessRequestsShouldProcessMessagesUntilSessionCompleted()
{
var task = this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendSessionEnd();
Assert.IsTrue(task.Wait(2000));
}
#region Version Check Protocol
[TestMethod]
public void ProcessRequestsVersionCheckShouldAckMinimumOfGivenAndHighestSupportedVersion()
{
var message = new Message { MessageType = MessageType.VersionCheck, Payload = 1 };
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
this.VerifyResponseMessageEquals(this.Serialize(message));
this.SendSessionEnd();
}
[TestMethod]
public void ProcessRequestsVersionCheckShouldLogErrorIfDiagnosticsEnableFails()
{
if (!string.IsNullOrEmpty(EqtTrace.LogFile))
{
// This test is no-op if diagnostics session is enabled
return;
}
EqtTrace.ErrorOnInitialization = "non-existent-error";
var message = new Message { MessageType = MessageType.VersionCheck, Payload = 1 };
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
this.VerifyResponseMessageContains(EqtTrace.ErrorOnInitialization);
this.SendSessionEnd();
}
#endregion
#region Discovery Protocol
[TestMethod]
public void ProcessRequestsDiscoveryInitializeShouldSetExtensionPaths()
{
var message = this.dataSerializer.SerializePayload(MessageType.DiscoveryInitialize, new[] { "testadapter.dll" });
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
this.jobQueue.Flush();
this.mockDiscoveryManager.Verify(d => d.Initialize(It.Is<IEnumerable<string>>(paths => paths.Any(p => p.Equals("testadapter.dll"))), It.IsAny<ITestDiscoveryEventsHandler2>()));
this.SendSessionEnd();
}
[TestMethod]
public void ProcessRequestsDiscoveryStartShouldStartDiscoveryWithGivenCriteria()
{
var message = this.dataSerializer.SerializePayload(MessageType.StartDiscovery, new DiscoveryCriteria(new[] { "test.dll" }, 1, string.Empty));
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
this.jobQueue.Flush();
this.mockDiscoveryManager.Verify(d => d.DiscoverTests(It.Is<DiscoveryCriteria>(dc => dc.Sources.Contains("test.dll")), It.IsAny<ITestDiscoveryEventsHandler2>()));
this.SendSessionEnd();
}
[TestMethod]
public void DiscoveryCompleteShouldSendDiscoveryCompletePayloadOnChannel()
{
var discoveryComplete = new DiscoveryCompletePayload { TotalTests = 1, LastDiscoveredTests = Enumerable.Empty<TestCase>(), IsAborted = false };
var message = this.dataSerializer.SerializePayload(MessageType.DiscoveryComplete, discoveryComplete);
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.requestHandler.DiscoveryComplete(new DiscoveryCompleteEventArgs(discoveryComplete.TotalTests, discoveryComplete.IsAborted), discoveryComplete.LastDiscoveredTests);
this.VerifyResponseMessageEquals(message);
this.SendSessionEnd();
}
#endregion
#region Execution Protocol
[TestMethod]
public void ProcessRequestsExecutionInitializeShouldSetExtensionPaths()
{
var message = this.dataSerializer.SerializePayload(MessageType.ExecutionInitialize, new[] { "testadapter.dll" });
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
this.jobQueue.Flush();
this.mockExecutionManager.Verify(e => e.Initialize(It.Is<IEnumerable<string>>(paths => paths.Any(p => p.Equals("testadapter.dll"))), It.IsAny<ITestMessageEventHandler>()));
this.SendSessionEnd();
}
[TestMethod]
public void ProcessRequestsExecutionStartShouldStartExecutionWithGivenSources()
{
var asm = new Dictionary<string, IEnumerable<string>>();
asm["mstestv2"] = new[] {"test1.dll", "test2.dll"};
var testRunCriteriaWithSources = new TestRunCriteriaWithSources(asm, "runsettings", null, null);
var message = this.dataSerializer.SerializePayload(MessageType.StartTestExecutionWithSources, testRunCriteriaWithSources);
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
this.jobQueue.Flush();
mockExecutionManager.Verify(e =>
e.StartTestRun(
It.Is<Dictionary<string, IEnumerable<string>>>(d => d.ContainsKey("mstestv2")), It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<TestExecutionContext>(), It.IsAny<ITestCaseEventsHandler>(),
It.IsAny<ITestRunEventsHandler>()));
this.SendSessionEnd();
}
[TestMethod]
public void ProcessRequestsExecutionStartShouldStartExecutionWithGivenTests()
{
var t1 = new TestCase("N.C.M1", new Uri("executor://mstest/v2"), "test1.dll");
var t2 = new TestCase("N.C.M2", new Uri("executor://mstest/v2"), "test1.dll");
var testCases = new [] { t1, t2 };
var testRunCriteriaWithTests = new TestRunCriteriaWithTests(testCases, "runsettings", null, null);
var message = this.dataSerializer.SerializePayload(MessageType.StartTestExecutionWithTests, testRunCriteriaWithTests);
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
this.jobQueue.Flush();
mockExecutionManager.Verify(e =>
e.StartTestRun(
It.Is<IEnumerable<TestCase>>(tcs =>
tcs.Any(t => t.FullyQualifiedName.Equals("N.C.M1")) &&
tcs.Any(t => t.FullyQualifiedName.Equals("N.C.M2"))), It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<TestExecutionContext>(), It.IsAny<ITestCaseEventsHandler>(),
It.IsAny<ITestRunEventsHandler>()));
this.SendSessionEnd();
}
[TestMethod]
public void ProcessRequestsExecutionCancelShouldCancelTestRun()
{
var message = this.dataSerializer.SerializePayload(MessageType.CancelTestRun, string.Empty);
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
mockExecutionManager.Verify(e => e.Cancel(It.IsAny<ITestRunEventsHandler>()));
this.SendSessionEnd();
}
// ProcessRequestsExecutionCancelShouldStopRequestProcessing
[TestMethod]
public void ProcessRequestsExecutionLaunchAdapterProcessWithDebuggerShouldSendAckMessage()
{
var message = this.dataSerializer.SerializePayload(MessageType.LaunchAdapterProcessWithDebuggerAttachedCallback, string.Empty);
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
this.jobQueue.Flush();
this.SendSessionEnd();
}
[TestMethod]
public void ProcessRequestsExecutionAbortShouldStopTestRun()
{
var message = this.dataSerializer.SerializePayload(MessageType.AbortTestRun, string.Empty);
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
mockExecutionManager.Verify(e => e.Abort(It.IsAny<ITestRunEventsHandler>()));
this.SendSessionEnd();
}
// ProcessRequestsExecutionAbortShouldStopRequestProcessing
[TestMethod]
public void SendExecutionCompleteShouldSendTestRunCompletePayloadOnChannel()
{
var t1 = new TestCase("N.C.M1", new Uri("executor://mstest/v2"), "test1.dll");
var t2 = new TestCase("N.C.M2", new Uri("executor://mstest/v2"), "test1.dll");
var testCases = new[] { t1, t2 };
var testRunCriteriaWithTests = new TestRunCriteriaWithTests(testCases, "runsettings", null, null);
var message = this.dataSerializer.SerializePayload(MessageType.StartTestExecutionWithTests, testRunCriteriaWithTests);
this.mockExecutionManager.Setup(em => em.StartTestRun(It.IsAny<IEnumerable<TestCase>>(), It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<TestExecutionContext>(), It.IsAny<ITestCaseEventsHandler>(),
It.IsAny<ITestRunEventsHandler>())).Callback(() =>
{
this.requestHandler.SendExecutionComplete(It.IsAny<TestRunCompleteEventArgs>(), It.IsAny<TestRunChangedEventArgs>(), It.IsAny<ICollection<AttachmentSet>>(), It.IsAny<ICollection<string>>());
});
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.mockChannel.Setup(mc => mc.Send(It.Is<string>(d => d.Contains(MessageType.ExecutionComplete))))
.Callback<string>(
(d) =>
{
var msg = this.dataSerializer.DeserializeMessage(d);
var payload = this.dataSerializer.DeserializePayload<TestRunCompletePayload>(msg);
Assert.IsNotNull(payload);
});
this.SendMessageOnChannel(message);
this.jobQueue.Flush();
mockExecutionManager.Verify(e =>
e.StartTestRun(
It.Is<IEnumerable<TestCase>>(tcs =>
tcs.Any(t => t.FullyQualifiedName.Equals("N.C.M1")) &&
tcs.Any(t => t.FullyQualifiedName.Equals("N.C.M2"))), It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<TestExecutionContext>(), It.IsAny<ITestCaseEventsHandler>(),
It.IsAny<ITestRunEventsHandler>()));
this.SendSessionEnd();
}
[TestMethod]
public void LaunchProcessWithDebuggerAttachedShouldSendProcessInformationOnChannel()
{
var message = this.dataSerializer.SerializePayload(MessageType.LaunchAdapterProcessWithDebuggerAttachedCallback, "123");
this.mockChannel.Setup(mc => mc.Send(It.Is<string>(d => d.Contains(MessageType.LaunchAdapterProcessWithDebuggerAttached))))
.Callback<string>(
(d) =>
{
var msg = this.dataSerializer.DeserializeMessage(d);
var payload = this.dataSerializer.DeserializePayload<TestProcessStartInfo>(msg);
Assert.IsNotNull(payload);
this.SendMessageOnChannel(message);
this.SendSessionEnd();
});
var task = Task.Run(() => this.requestHandler.LaunchProcessWithDebuggerAttached(new TestProcessStartInfo()));
}
[TestMethod]
public void LaunchProcessWithDebuggerAttachedShouldWaitForProcessIdFromRunner()
{
var message = dataSerializer.SerializePayload(MessageType.LaunchAdapterProcessWithDebuggerAttachedCallback, "123");
this.mockChannel.Setup(mc => mc.Send(It.Is<string>(d => d.Contains(MessageType.LaunchAdapterProcessWithDebuggerAttached))))
.Callback<string>(
(d) =>
{
var msg = this.dataSerializer.DeserializeMessage(d);
var payload = this.dataSerializer.DeserializePayload<TestProcessStartInfo>(msg);
Assert.IsNotNull(payload);
this.SendMessageOnChannel(message);
this.SendSessionEnd();
});
var task = Task.Run(() => this.requestHandler.LaunchProcessWithDebuggerAttached(new TestProcessStartInfo()));
Assert.AreEqual(123, task.Result);
}
#endregion
#region Logging Protocol
[TestMethod]
public void SendLogShouldSendTestMessageWithLevelOnChannel()
{
var logMsg = "Testing log message on channel";
this.mockChannel.Setup(mc => mc.Send(It.Is<string>(d => d.Contains(MessageType.TestMessage))))
.Callback<string>(
(d) =>
{
var msg = this.dataSerializer.DeserializeMessage(d);
var payload = this.dataSerializer.DeserializePayload<TestMessagePayload>(msg);
Assert.IsNotNull(payload);
Assert.AreEqual(payload.Message, logMsg);
});
this.requestHandler.SendLog(TestMessageLevel.Informational, "Testing log message on channel");
this.SendSessionEnd();
}
#endregion
[TestMethod]
public void ProcessRequestsEndSessionShouldCloseRequestHandler()
{
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendSessionEnd();
this.mockCommunicationClient.Verify(mc=>mc.Stop(), Times.Once);
}
[TestMethod]
public void ProcessRequestsAbortSessionShouldBeNoOp()
{
var message = dataSerializer.SerializePayload(MessageType.SessionAbort, string.Empty);
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
// Session abort should not close the client
this.mockCommunicationClient.Verify(mc => mc.Stop(), Times.Never);
}
[TestMethod]
public void ProcessRequestsInvalidMessageTypeShouldNotThrow()
{
var message = dataSerializer.SerializePayload("DummyMessage", string.Empty);
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
this.SendSessionEnd();
}
[TestMethod]
public void ProcessRequestsInvalidMessageTypeShouldProcessFutureMessages()
{
var message = dataSerializer.SerializePayload("DummyMessage", string.Empty);
this.ProcessRequestsAsync(this.mockTestHostManagerFactory.Object);
this.SendMessageOnChannel(message);
// Should process this message, after the invalid message
this.SendSessionEnd();
this.mockCommunicationClient.Verify(mc => mc.Stop(), Times.Once);
}
[TestMethod]
public void DisposeShouldStopCommunicationChannel()
{
var testRequestHandler = this.requestHandler as TestRequestHandler;
Assert.IsNotNull(testRequestHandler);
testRequestHandler.Dispose();
this.mockCommunicationClient.Verify(mc => mc.Stop(), Times.Once);
}
private void SendMessageOnChannel(Message message)
{
// Setup message to be returned on deserialization of data
var data = this.Serialize(message);
this.SendMessageOnChannel(data);
}
private void SendMessageOnChannel(string data)
{
this.mockChannel.Raise(c => c.MessageReceived += null, new MessageReceivedEventArgs { Data = data });
}
private void SendSessionEnd()
{
this.SendMessageOnChannel(new Message { MessageType = MessageType.SessionEnd, Payload = string.Empty });
}
private Task ProcessRequestsAsync()
{
return Task.Run(() => this.requestHandler.ProcessRequests(new Mock<ITestHostManagerFactory>().Object));
}
private Task ProcessRequestsAsync(ITestHostManagerFactory testHostManagerFactory)
{
return Task.Run(() => this.requestHandler.ProcessRequests(testHostManagerFactory));
}
private string Serialize(Message message)
{
return this.dataSerializer.SerializePayload(message.MessageType, message.Payload);
}
private void VerifyResponseMessageEquals(string message)
{
this.mockChannel.Verify(mc => mc.Send(It.Is<string>(s => s.Equals(message))));
}
private void VerifyResponseMessageContains(string message)
{
this.mockChannel.Verify(mc => mc.Send(It.Is<string>(s => s.Contains(message))));
}
}
public class TestableTestRequestHandler : TestRequestHandler
{
public TestableTestRequestHandler(TestHostConnectionInfo testHostConnectionInfo,ICommunicationEndpointFactory communicationEndpointFactory, IDataSerializer dataSerializer, JobQueue<Action> jobQueue)
: base(testHostConnectionInfo, communicationEndpointFactory, dataSerializer, jobQueue, OnAckMessageReceived)
{
}
private static void OnAckMessageReceived(Message message)
{
Assert.AreEqual(message.MessageType, MessageType.LaunchAdapterProcessWithDebuggerAttachedCallback);
}
}
}