-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First Draft for the Protocol tool. (#306)
* First Draft for the Protocol tool. Emits the json sent and recieved for Discovery, RunAll, RunSelected scenarios. * Fixing the Testplatform.sln build. * Changes as per discussion with Arun. - Removed the runnerlocation, adapter and testhost configs.
- Loading branch information
1 parent
34a8665
commit 230c966
Showing
14 changed files
with
1,085 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
samples/Microsoft.TestPlatform.Protocol/Communication/JsonDataSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// 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.TestPlatform.Protocol | ||
{ | ||
using System.IO; | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
/// <summary> | ||
/// JsonDataSerializes serializes and deserializes data using Json format | ||
/// </summary> | ||
public class JsonDataSerializer | ||
{ | ||
private static JsonDataSerializer instance; | ||
|
||
private static JsonSerializer serializer; | ||
|
||
/// <summary> | ||
/// Prevents a default instance of the <see cref="JsonDataSerializer"/> class from being created. | ||
/// </summary> | ||
private JsonDataSerializer() | ||
{ | ||
serializer = JsonSerializer.Create( | ||
new JsonSerializerSettings | ||
{ | ||
DateFormatHandling = DateFormatHandling.IsoDateFormat, | ||
DateParseHandling = DateParseHandling.DateTimeOffset, | ||
DateTimeZoneHandling = DateTimeZoneHandling.Utc, | ||
TypeNameHandling = TypeNameHandling.None | ||
}); | ||
#if DEBUG | ||
// MemoryTraceWriter can help diagnose serialization issues. Enable it for | ||
// debug builds only. | ||
serializer.TraceWriter = new MemoryTraceWriter(); | ||
#endif | ||
} | ||
|
||
/// <summary> | ||
/// Gets the JSON Serializer instance. | ||
/// </summary> | ||
public static JsonDataSerializer Instance | ||
{ | ||
get | ||
{ | ||
return instance ?? (instance = new JsonDataSerializer()); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Deserialize a <see cref="Message"/> from raw JSON text. | ||
/// </summary> | ||
/// <param name="rawMessage">JSON string.</param> | ||
/// <returns>A <see cref="Message"/> instance.</returns> | ||
public Message DeserializeMessage(string rawMessage) | ||
{ | ||
return JsonConvert.DeserializeObject<Message>(rawMessage); | ||
} | ||
|
||
/// <summary> | ||
/// Deserialize the <see cref="Message.Payload"/> for a message. | ||
/// </summary> | ||
/// <param name="message">A <see cref="Message"/> object.</param> | ||
/// <typeparam name="T">Payload type.</typeparam> | ||
/// <returns>The deserialized payload.</returns> | ||
public T DeserializePayload<T>(Message message) | ||
{ | ||
T retValue = default(T); | ||
|
||
// TODO: Currently we use json serializer auto only for non-testmessage types | ||
// CHECK: Can't we just use auto for everything | ||
if (Microsoft.TestPlatform.Protocol.MessageType.TestMessage.Equals(message.MessageType)) | ||
{ | ||
retValue = message.Payload.ToObject<T>(); | ||
} | ||
else | ||
{ | ||
retValue = message.Payload.ToObject<T>(serializer); | ||
} | ||
|
||
return retValue; | ||
} | ||
|
||
/// <summary> | ||
/// Deserialize raw JSON to an object using the default serializer. | ||
/// </summary> | ||
/// <param name="json">JSON string.</param> | ||
/// <typeparam name="T">Target type to deserialize.</typeparam> | ||
/// <returns>An instance of <see cref="T"/>.</returns> | ||
public T Deserialize<T>(string json) | ||
{ | ||
using (var stringReader = new StringReader(json)) | ||
using (var jsonReader = new JsonTextReader(stringReader)) | ||
{ | ||
return serializer.Deserialize<T>(jsonReader); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Serialize an empty message. | ||
/// </summary> | ||
/// <param name="messageType">Type of the message.</param> | ||
/// <returns>Serialized message.</returns> | ||
public string SerializeMessage(string messageType) | ||
{ | ||
return JsonConvert.SerializeObject(new Message { MessageType = messageType }); | ||
} | ||
|
||
/// <summary> | ||
/// Serialize a message with payload. | ||
/// </summary> | ||
/// <param name="messageType">Type of the message.</param> | ||
/// <param name="payload">Payload for the message.</param> | ||
/// <returns>Serialized message.</returns> | ||
public string SerializePayload(string messageType, object payload) | ||
{ | ||
JToken serializedPayload = null; | ||
|
||
// TODO: Currently we use json serializer auto only for non-testmessage types | ||
// CHECK: Can't we just use auto for everything | ||
if (MessageType.TestMessage.Equals(messageType)) | ||
{ | ||
serializedPayload = JToken.FromObject(payload); | ||
} | ||
else | ||
{ | ||
serializedPayload = JToken.FromObject(payload, serializer); | ||
} | ||
|
||
return JsonConvert.SerializeObject(new Message { MessageType = messageType, Payload = serializedPayload }); | ||
} | ||
|
||
/// <summary> | ||
/// Serialize an object to JSON using default serialization settings. | ||
/// </summary> | ||
/// <typeparam name="T">Type of object to serialize.</typeparam> | ||
/// <param name="data">Instance of the object to serialize.</param> | ||
/// <returns>JSON string.</returns> | ||
public string Serialize<T>(T data) | ||
{ | ||
using (var stringWriter = new StringWriter()) | ||
using (var jsonWriter = new JsonTextWriter(stringWriter)) | ||
{ | ||
serializer.Serialize(jsonWriter, data); | ||
|
||
return stringWriter.ToString(); | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
samples/Microsoft.TestPlatform.Protocol/Communication/Message.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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.TestPlatform.Protocol | ||
{ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
public class Message | ||
{ | ||
/// <summary> | ||
/// Gets or sets the message type. | ||
/// </summary> | ||
public string MessageType { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the payload. | ||
/// </summary> | ||
public JToken Payload { get; set; } | ||
|
||
/// <summary> | ||
/// To string implementation. | ||
/// </summary> | ||
/// <returns> The <see cref="string"/>. </returns> | ||
public override string ToString() | ||
{ | ||
return "(" + MessageType + ") -> " + (Payload == null ? "null" : Payload.ToString(Formatting.Indented)); | ||
} | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
samples/Microsoft.TestPlatform.Protocol/Communication/MessageType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// 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.TestPlatform.Protocol | ||
{ | ||
/// <summary> | ||
/// The message type. | ||
/// </summary> | ||
public static class MessageType | ||
{ | ||
/// <summary> | ||
/// The session start. | ||
/// </summary> | ||
public const string SessionStart = "TestSession.Start"; | ||
|
||
/// <summary> | ||
/// The session end. | ||
/// </summary> | ||
public const string SessionEnd = "TestSession.Terminate"; | ||
|
||
/// <summary> | ||
/// The is aborted. | ||
/// </summary> | ||
public const string SessionAbort = "TestSession.Abort"; | ||
|
||
/// <summary> | ||
/// The session connected. | ||
/// </summary> | ||
public const string SessionConnected = "TestSession.Connected"; | ||
|
||
/// <summary> | ||
/// Test Message | ||
/// </summary> | ||
public const string TestMessage = "TestSession.Message"; | ||
|
||
/// <summary> | ||
/// Protocol Version | ||
/// </summary> | ||
public const string VersionCheck = "ProtocolVersion"; | ||
|
||
/// <summary> | ||
/// The session start. | ||
/// </summary> | ||
public const string DiscoveryInitialize = "TestDiscovery.Initialize"; | ||
|
||
/// <summary> | ||
/// The discovery started. | ||
/// </summary> | ||
public const string StartDiscovery = "TestDiscovery.Start"; | ||
|
||
/// <summary> | ||
/// The test cases found. | ||
/// </summary> | ||
public const string TestCasesFound = "TestDiscovery.TestFound"; | ||
|
||
/// <summary> | ||
/// The discovery complete. | ||
/// </summary> | ||
public const string DiscoveryComplete = "TestDiscovery.Completed"; | ||
|
||
/// <summary> | ||
/// The session start. | ||
/// </summary> | ||
public const string ExecutionInitialize = "TestExecution.Initialize"; | ||
|
||
/// <summary> | ||
/// Cancel the current test run | ||
/// </summary> | ||
public const string CancelTestRun = "TestExecution.Cancel"; | ||
|
||
/// <summary> | ||
/// Cancel the current test run | ||
/// </summary> | ||
public const string AbortTestRun = "TestExecution.Abort"; | ||
|
||
/// <summary> | ||
/// Start test execution. | ||
/// </summary> | ||
public const string StartTestExecutionWithSources = "TestExecution.StartWithSources"; | ||
|
||
/// <summary> | ||
/// Start test execution. | ||
/// </summary> | ||
public const string StartTestExecutionWithTests = "TestExecution.StartWithTests"; | ||
|
||
/// <summary> | ||
/// The test run stats change. | ||
/// </summary> | ||
public const string TestRunStatsChange = "TestExecution.StatsChange"; | ||
|
||
/// <summary> | ||
/// The execution complete. | ||
/// </summary> | ||
public const string ExecutionComplete = "TestExecution.Completed"; | ||
|
||
/// <summary> | ||
/// The message to get runner process startInfo for run all tests in given sources | ||
/// </summary> | ||
public const string GetTestRunnerProcessStartInfoForRunAll = "TestExecution.GetTestRunnerProcessStartInfoForRunAll"; | ||
|
||
/// <summary> | ||
/// The message to get runner process startInfo for run selected tests | ||
/// </summary> | ||
public const string GetTestRunnerProcessStartInfoForRunSelected = "TestExecution.GetTestRunnerProcessStartInfoForRunSelected"; | ||
|
||
/// <summary> | ||
/// CustomTestHostLaunch | ||
/// </summary> | ||
public const string CustomTestHostLaunch = "TestExecution.CustomTestHostLaunch"; | ||
|
||
/// <summary> | ||
/// Custom Test Host launch callback | ||
/// </summary> | ||
public const string CustomTestHostLaunchCallback = "TestExecution.CustomTestHostLaunchCallback"; | ||
|
||
/// <summary> | ||
/// Extensions Initialization | ||
/// </summary> | ||
public const string ExtensionsInitialize = "Extensions.Initialize"; | ||
|
||
/// <summary> | ||
/// Start Test Run All Sources | ||
/// </summary> | ||
public const string TestRunAllSourcesWithDefaultHost = "TestExecution.RunAllWithDefaultHost"; | ||
|
||
/// <summary> | ||
/// Start Test Run - Testcases | ||
/// </summary> | ||
public const string TestRunSelectedTestCasesDefaultHost = "TestExecution.RunSelectedWithDefaultHost"; | ||
|
||
/// <summary> | ||
/// Launch Adapter Process With DebuggerAttached | ||
/// </summary> | ||
public const string LaunchAdapterProcessWithDebuggerAttached = "TestExecution.LaunchAdapterProcessWithDebuggerAttached"; | ||
|
||
/// <summary> | ||
/// Launch Adapter Process With DebuggerAttached | ||
/// </summary> | ||
public const string LaunchAdapterProcessWithDebuggerAttachedCallback = "TestExecution.LaunchAdapterProcessWithDebuggerAttachedCallback"; | ||
|
||
} | ||
} |
Oops, something went wrong.