Skip to content
This repository was archived by the owner on Nov 9, 2018. It is now read-only.

Commit bf4b495

Browse files
committed
Adding ProtocolVersion support to TestHost
1 parent 6216030 commit bf4b495

File tree

6 files changed

+149
-2
lines changed

6 files changed

+149
-2
lines changed

src/Microsoft.Framework.TestHost.Client.Sources/TestHostWrapper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public TestHostWrapper(string project, string dnx, bool debug)
3434
Output = new List<Message>();
3535
}
3636

37+
public int? ProtocolVersion { get; set; }
38+
3739
public int? DTHPort { get; set; }
3840

3941
private TcpClient Client { get; set; }
@@ -188,6 +190,32 @@ private void StartCommandAndWaitForResponse(string messageType, object payload,
188190
using (var writer = new BinaryWriter(stream))
189191
using (var reader = new BinaryReader(stream))
190192
{
193+
// If we're using a ProtocolVersion then establish that with the TestHost
194+
if (ProtocolVersion.HasValue)
195+
{
196+
writer.Write(JsonConvert.SerializeObject(new Message()
197+
{
198+
MessageType = "ProtocolVersion",
199+
Payload = JToken.FromObject(new ProtocolVersionMessage()
200+
{
201+
Version = ProtocolVersion.Value,
202+
}),
203+
}));
204+
205+
var message = JsonConvert.DeserializeObject<Message>(reader.ReadString());
206+
OnMessageReceived(this, message);
207+
208+
if (message.MessageType == "ProtocolVersion")
209+
{
210+
ProtocolVersion = message.Payload?.ToObject<ProtocolVersionMessage>().Version;
211+
}
212+
else
213+
{
214+
throw new InvalidOperationException(
215+
$"Invalid ProtocolVersion response {message.MessageType}");
216+
}
217+
}
218+
191219
writer.Write(JsonConvert.SerializeObject(new Message
192220
{
193221
MessageType = messageType,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.Framework.TestHost
5+
{
6+
public class ProtocolVersionMessage
7+
{
8+
public int Version { get; set; }
9+
}
10+
}

src/Microsoft.Framework.TestHost/Program.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading.Tasks;
99
using Microsoft.Framework.Runtime;
1010
using Microsoft.Framework.Runtime.Common.CommandLine;
11+
using Newtonsoft.Json.Linq;
1112

1213
namespace Microsoft.Framework.TestHost
1314
{
@@ -22,7 +23,9 @@ public Program(IServiceProvider services)
2223

2324
public int Main(string[] args)
2425
{
25-
var application = new CommandLineApplication();
26+
// We want to allow unexpected args, in case future VS needs to pass anything in that we don't current.
27+
// This will allow us to retain backwards compatibility.
28+
var application = new CommandLineApplication(throwOnUnexpectedArg: false);
2629
application.HelpOption("-?|-h|--help");
2730

2831
var env = (IApplicationEnvironment)_services.GetService(typeof(IApplicationEnvironment));
@@ -76,6 +79,31 @@ public int Main(string[] args)
7679
}
7780

7881
var message = channel.ReadQueue.Take();
82+
83+
// The message might be a request to negotiate protocol version. For now we only know
84+
// about version 1.
85+
if (message.MessageType == "ProtocolVersion")
86+
{
87+
var version = message.Payload?.ToObject<ProtocolVersionMessage>().Version;
88+
var supportedVersion = 1;
89+
Trace.TraceInformation(
90+
"[ReportingChannel]: Requested Version: {0} - Using Version: {1}",
91+
version,
92+
supportedVersion);
93+
94+
channel.Send(new Message()
95+
{
96+
MessageType = "ProtocolVersion",
97+
Payload = JToken.FromObject(new ProtocolVersionMessage()
98+
{
99+
Version = supportedVersion,
100+
}),
101+
});
102+
103+
// Take the next message, which should be the command to execute.
104+
message = channel.ReadQueue.Take();
105+
}
106+
79107
if (message.MessageType == "TestDiscovery.Start")
80108
{
81109
var commandArgs = new List<string>()

test/Microsoft.Framework.TestHost.Tests/TestHostTest.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,81 @@ public async Task RunTest_ByUniqueName()
130130
Assert.Equal("TestExecution.Response", host.Output[host.Output.Count - 1].MessageType);
131131
}
132132

133+
[ConditionalFact]
134+
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
135+
public async Task RunTest_ByUniqueName_ProtocolVersion_MatchingVersion()
136+
{
137+
// Arrange
138+
var host = new TestHostWrapper(_testProject);
139+
host.ProtocolVersion = 1;
140+
141+
await host.StartAsync();
142+
143+
await host.ListTestsAsync();
144+
145+
var test = host.Output
146+
.Where(m => m.MessageType == "TestDiscovery.TestFound")
147+
.First()
148+
.Payload.ToObject<Test>();
149+
150+
host.Output.Clear();
151+
152+
host = new TestHostWrapper(_testProject);
153+
host.ProtocolVersion = 1;
154+
await host.StartAsync();
155+
156+
// Act
157+
var result = await host.RunTestsAsync(_testProject, test.FullyQualifiedName);
158+
159+
// Assert
160+
Assert.Equal(0, result);
161+
162+
Assert.Equal(1, host.ProtocolVersion);
163+
164+
Assert.Equal(4, host.Output.Count);
165+
Assert.Single(host.Output, m => m.MessageType == "ProtocolVersion");
166+
Assert.Single(host.Output, m => TestStarted(m, test.DisplayName));
167+
Assert.Single(host.Output, m => TestPassed(m, test.DisplayName));
168+
Assert.Equal("TestExecution.Response", host.Output[host.Output.Count - 1].MessageType);
169+
}
170+
171+
[ConditionalFact]
172+
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
173+
public async Task RunTest_ByUniqueName_ProtocolVersion_UnknownVersion()
174+
{
175+
// Arrange
176+
var host = new TestHostWrapper(_testProject);
177+
host.ProtocolVersion = 2;
178+
179+
await host.StartAsync();
180+
181+
await host.ListTestsAsync();
182+
183+
var test = host.Output
184+
.Where(m => m.MessageType == "TestDiscovery.TestFound")
185+
.First()
186+
.Payload.ToObject<Test>();
187+
188+
host.Output.Clear();
189+
190+
host = new TestHostWrapper(_testProject);
191+
host.ProtocolVersion = 2;
192+
await host.StartAsync();
193+
194+
// Act
195+
var result = await host.RunTestsAsync(_testProject, test.FullyQualifiedName);
196+
197+
// Assert
198+
Assert.Equal(0, result);
199+
200+
Assert.Equal(1, host.ProtocolVersion);
201+
202+
Assert.Equal(4, host.Output.Count);
203+
Assert.Single(host.Output, m => m.MessageType == "ProtocolVersion");
204+
Assert.Single(host.Output, m => TestStarted(m, test.DisplayName));
205+
Assert.Single(host.Output, m => TestPassed(m, test.DisplayName));
206+
Assert.Equal("TestExecution.Response", host.Output[host.Output.Count - 1].MessageType);
207+
}
133208

134209
private static bool TestFound(Message message, string name)
135210
{

test/Microsoft.Framework.WebEncoders.Testing.Tests/Microsoft.Framework.WebEncoders.Testing.Tests.xproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
<PropertyGroup>
1515
<SchemaVersion>2.0</SchemaVersion>
1616
</PropertyGroup>
17+
<ItemGroup>
18+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
19+
</ItemGroup>
1720
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
18-
</Project>
21+
</Project>

tools/Microsoft.Framework.TestHost.UI/Microsoft.Framework.TestHost.UI.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<Compile Include="..\..\src\Microsoft.Framework.TestHost\Messages\Message.cs">
6868
<Link>Message.cs</Link>
6969
</Compile>
70+
<Compile Include="..\..\src\Microsoft.Framework.TestHost\Messages\ProtocolVersionMessage.cs">
71+
<Link>ProtocolVersionMessage.cs</Link>
72+
</Compile>
7073
<Compile Include="..\..\src\Microsoft.Framework.TestHost\Messages\RunTestsMessage.cs">
7174
<Link>RunTestsMessage.cs</Link>
7275
</Compile>

0 commit comments

Comments
 (0)