Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions WindowsDevicePortalWrapper/MockDataGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ public class Program
new Endpoint(HttpMethods.Get, DevicePortal.IpConfigApi),
new Endpoint(HttpMethods.Get, DevicePortal.SystemPerfApi),
new Endpoint(HttpMethods.Get, DevicePortal.RunningProcessApi),
new Endpoint(HttpMethods.Get, DevicePortal.CustomEtwProvidersApi),
new Endpoint(HttpMethods.Get, DevicePortal.EtwProvidersApi),
new Endpoint(HttpMethods.WebSocket, DevicePortal.SystemPerfApi),
new Endpoint(HttpMethods.WebSocket, DevicePortal.RunningProcessApi),
new Endpoint(HttpMethods.WebSocket, DevicePortal.RealtimeEtwSessionApi),

// HoloLens specific endpoints
new Endpoint(HttpMethods.Get, DevicePortal.HolographicIpdApi),
Expand Down
112 changes: 112 additions & 0 deletions WindowsDevicePortalWrapper/UnitTestProject/Core/EtwTests.cs
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();
Copy link
Copy Markdown
Member

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();

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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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"}]}
10 changes: 10 additions & 0 deletions WindowsDevicePortalWrapper/UnitTestProject/UnitTestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<ItemGroup>
<Compile Include="BaseTests.cs" />
<Compile Include="Core\AppFileExplorerTests.cs" />
<Compile Include="Core\EtwTests.cs" />
<Compile Include="Core\PerformanceDataTests.cs" />
<Compile Include="Device-VersionTests\HoloLens\HoloLensHelpers.cs" />
<Compile Include="Device-VersionTests\HoloLens\HoloLens_rs1_release.cs" />
Expand All @@ -85,9 +86,18 @@
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
<None Include="MockData\Defaults\WebSocket_api_etw_session_realtime_Default.dat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="MockData\Defaults\api_etw_providers_Default.dat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="MockData\Defaults\api_os_devicefamily_Default.dat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="MockData\Defaults\api_etw_customproviders_Default.dat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="MockData\Defaults\api_os_info_Default.dat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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>
Expand All @@ -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();

Expand Down Expand Up @@ -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);
}
}
}
}
}
Loading