-
Notifications
You must be signed in to change notification settings - Fork 82
Issue #31: ETW APIs are unimplemented #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b4d29f0
5861be2
834b52d
74b3137
42c7ed8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| //---------------------------------------------------------------------------------------------- | ||
| // <copyright file="EtwTests.cs" company="Microsoft Corporation"> | ||
| // Licensed under the MIT License. See LICENSE.TXT in the project root license information. | ||
| // </copyright> | ||
| //---------------------------------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using static Microsoft.Tools.WindowsDevicePortal.DevicePortal; | ||
|
|
||
| namespace Microsoft.Tools.WindowsDevicePortal.Tests.Core | ||
| { | ||
| /// <summary> | ||
| /// Test class for ETW APIs. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class EtwTests : BaseTests | ||
| { | ||
| /// <summary> | ||
| /// Basic test of GET method for getting a list of custom registered ETW providers. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void GetCustomEtwProvidersTest() | ||
| { | ||
| TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.CustomEtwProvidersApi, HttpMethods.Get); | ||
|
|
||
| Task<EtwProviders> getCustomEtwProvidersTask = TestHelpers.Portal.GetCustomEtwProvidersAsync(); | ||
| getCustomEtwProvidersTask.Wait(); | ||
|
|
||
| Assert.AreEqual(TaskStatus.RanToCompletion, getCustomEtwProvidersTask.Status); | ||
|
|
||
| ValidateEtwProviders(getCustomEtwProvidersTask.Result); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Basic test of GET method for getting a list of registered ETW providers. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void GetEtwProvidersTest() | ||
| { | ||
| TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.EtwProvidersApi, HttpMethods.Get); | ||
|
|
||
| Task<EtwProviders> getEtwProvidersTask = TestHelpers.Portal.GetEtwProvidersAsync(); | ||
| getEtwProvidersTask.Wait(); | ||
|
|
||
| Assert.AreEqual(TaskStatus.RanToCompletion, getEtwProvidersTask.Status); | ||
|
|
||
| ValidateEtwProviders(getEtwProvidersTask.Result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetEtwEventsTest() | ||
| { | ||
| TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.RealtimeEtwSessionApi, HttpMethods.WebSocket); | ||
|
|
||
| ManualResetEvent etwEventsReceived = new ManualResetEvent(false); | ||
| EtwEvents etwEvents = null; | ||
|
|
||
| WindowsDevicePortal.WebSocketMessageReceivedEventHandler<EtwEvents> etwEventsReceivedHandler = | ||
| delegate (DevicePortal sender, WebSocketMessageReceivedEventArgs<EtwEvents> args) | ||
| { | ||
| if (args.Message != null) | ||
| { | ||
| etwEvents = args.Message; | ||
| etwEventsReceived.Set(); | ||
| } | ||
| }; | ||
|
|
||
| TestHelpers.Portal.RealtimeEventsMessageReceived += etwEventsReceivedHandler; | ||
|
|
||
| Task startListeningForEtwEventsTask = TestHelpers.Portal.StartListeningForEtwEventsAsync(); | ||
| startListeningForEtwEventsTask.Wait(); | ||
| Assert.AreEqual(TaskStatus.RanToCompletion, startListeningForEtwEventsTask.Status); | ||
|
|
||
| etwEventsReceived.WaitOne(); | ||
|
|
||
| Task stopListeningForEtwEventsTask = TestHelpers.Portal.StopListeningForEtwEventsAsync(); | ||
| stopListeningForEtwEventsTask.Wait(); | ||
| Assert.AreEqual(TaskStatus.RanToCompletion, stopListeningForEtwEventsTask.Status); | ||
|
|
||
| TestHelpers.Portal.RealtimeEventsMessageReceived -= etwEventsReceivedHandler; | ||
|
|
||
| ValidateEtwEvents(etwEvents); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validate the <see cref="EtwEvents"/> returned from the tests. | ||
| /// </summary> | ||
| /// <param name="etwEvents">The <see cref="EtwEvents"/> to validate.</param> | ||
| private static void ValidateEtwEvents(EtwEvents etwEvents) | ||
| { | ||
| Assert.IsNotNull(etwEvents); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need to check event list elements? |
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validate the <see cref="EtwProviders"/> returned from the tests. | ||
| /// </summary> | ||
| /// <param name="etw">The <see cref="EtwProviders"/> to validate.</param> | ||
| private static void ValidateEtwProviders(EtwProviders etw) | ||
| { | ||
| Guid result; | ||
| Assert.IsTrue(etw.Providers.Count > 0); | ||
| Assert.IsTrue(etw.Providers.All(etwProvider => | ||
| Guid.TryParse(etwProvider.GUID, out result) && | ||
| !string.IsNullOrEmpty(etwProvider.Name))); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"Providers" : [{"GUID" : "3A43D90E-530E-41E5-A897-B555516070E2", "Name" : "Provider.One"},{"GUID" : "52B1715B-5AB6-4B48-A61A-A93EE8D4B5CD", "Name" : "Provider-Two"},{"GUID" : "698F02FF-2B63-4CBB-AB06-FEB88011347E", "Name" : "Provider.Three"}]} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"Providers" : [{"GUID" : "3A43D90E-530E-41E5-A897-B555516070E2", "Name" : "Provider.One"},{"GUID" : "52B1715B-5AB6-4B48-A61A-A93EE8D4B5CD", "Name" : "Provider-Two"},{"GUID" : "698F02FF-2B63-4CBB-AB06-FEB88011347E", "Name" : "Provider.Three"}]} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,11 @@ internal partial class WebSocket<T> | |
| /// </summary> | ||
| private Func<object, X509Certificate, X509Chain, SslPolicyErrors, bool> serverCertificateValidationHandler; | ||
|
|
||
| /// <summary> | ||
| /// The connection to the websocket. | ||
| /// </summary> | ||
| private Task<HttpResponseMessage> webSocketTask; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're adding an IDisposable member - shouldn't you add an IDisposable implementation to this class? That being said, there was already an IDisposable member, so perhaps it's unnecessary? |
||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="WebSocket{T}" /> class. | ||
| /// </summary> | ||
|
|
@@ -50,42 +55,66 @@ public WebSocket(IDevicePortalConnection connection, Func<object, X509Certificat | |
| this.serverCertificateValidationHandler = serverCertificateValidationHandler; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initialize a connection to the websocket. | ||
| /// </summary> | ||
| /// <param name="endpoint">The uri that the weboscket should connect to.</param> | ||
| /// <returns>The task of opening a connection to the websocket.</returns> | ||
| private async Task ConnectInternalAsync(Uri endpoint) | ||
| { | ||
| await Task.Run(() => | ||
| { | ||
| webSocketTask = TestHelpers.MockHttpResponder.WebSocketAsync(endpoint); | ||
| this.IsConnected = true; | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Closes the connection to the websocket. | ||
| /// </summary> | ||
| /// <returns>The task of closing the websocket connection.</returns> | ||
| private async Task CloseInternalAsync() | ||
| { | ||
| await Task.Run(() => | ||
| { | ||
| webSocketTask.Dispose(); | ||
| webSocketTask = null; | ||
| this.IsConnected = false; | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Stops listneing for messages from the websocket and closes the connection to the websocket. | ||
| /// </summary> | ||
| /// <returns>The task of closing the websocket connection.</returns> | ||
| #pragma warning disable 1998 | ||
| private async Task StopListeningForMessagesInternalAsync() | ||
| { | ||
| this.keepListeningForMessages = false; | ||
|
|
||
| // Wait for web socket to no longer be receiving messages. | ||
| if (this.IsListeningForMessages) | ||
| { | ||
| this.keepListeningForMessages = false; | ||
|
|
||
| // Wait for web socket to no longer be receiving messages. | ||
| if (this.IsListeningForMessages) | ||
| { | ||
| this.stoppedReceivingMessages.WaitOne(); | ||
| this.stoppedReceivingMessages.Reset(); | ||
| } | ||
| this.stoppedReceivingMessages.WaitOne(); | ||
| this.stoppedReceivingMessages.Reset(); | ||
| } | ||
| } | ||
| #pragma warning restore 1998 | ||
|
|
||
| /// <summary> | ||
| /// Connects to the websocket and starts listening for messages from the websocket. | ||
| /// Starts listening for messages from the websocket. | ||
| /// Once they are received they are parsed and the WebSocketMessageReceived event is raised. | ||
| /// </summary> | ||
| /// <param name="endpoint">The uri that the weboscket should connect to</param> | ||
| /// <returns>The task of listening for messages from the websocket.</returns> | ||
| private async Task StartListeningForMessagesInternalAsync(Uri endpoint) | ||
| private async Task StartListeningForMessagesInternalAsync() | ||
| { | ||
| this.IsListeningForMessages = true; | ||
| this.keepListeningForMessages = true; | ||
|
|
||
| try | ||
| { | ||
| while (this.keepListeningForMessages) | ||
| { | ||
| Task<HttpResponseMessage> webSocketTask = TestHelpers.MockHttpResponder.WebSocketAsync(endpoint); | ||
| await webSocketTask.ConfigureAwait(false); | ||
| webSocketTask.Wait(); | ||
|
|
||
|
|
@@ -121,5 +150,24 @@ private async Task StartListeningForMessagesInternalAsync(Uri endpoint) | |
| this.IsListeningForMessages = false; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sends a message to the server. | ||
| /// </summary> | ||
| /// <param name="message">The message to send.</param> | ||
| /// <returns>The task of sending the message to the websocket</returns> | ||
| private async Task SendMessageInternalAsync(string message) | ||
| { | ||
| await webSocketTask.ConfigureAwait(false); | ||
| webSocketTask.Wait(); | ||
|
|
||
| using (HttpResponseMessage response = webSocketTask.Result) | ||
| { | ||
| if (!response.IsSuccessStatusCode) | ||
| { | ||
| throw new DevicePortalException(response); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that the VS test infrastructure supports making methods 'public async Task' so you can 'await' on the task to make things a little cleaner and more representative; i.e.
EtwProviders etwProviders = await TestHelpers.Portal.GetEtwProvidersAsync();