Skip to content
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

[wasm][testing] hosting webSocket echo server in xharness process #52546

Closed
Closed
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
5 changes: 4 additions & 1 deletion docs/workflow/testing/libraries/testing-wasm.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ The following shows how to run tests for a specific library

- `$(WasmXHarnessArgs)` - xharness command arguments

Example: `WasmXHarnessArgs="--xyz"` -> becomes `dotnet xharness wasm test --xyz`
Example: `WasmXHarnessArgs="--set-web-server-env"` -> becomes `dotnet xharness wasm test --set-web-server-env`

- `$(WasmXHarnessMonoArgs)` - arguments to mono

Example: `WasmXHarnessMonoArgs="--runtime-arg=--trace=E"`

- `$(WasmXHarnessMonoArgs)` - env variables to mono
Example: `WasmXHarnessMonoArgs="--setenv=MONO_LOG_LEVEL=debug"`

- `$(WasmTestAppArgs)` - arguments for the test app itself

### Running outer loop tests using Browser instance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;

namespace MonoNetTestServer
{
public class GenericHandler
{
RequestDelegate next;
public GenericHandler(RequestDelegate next)
{
this.next = next;
}

public async Task Invoke(HttpContext context)
{
PathString path = context.Request.Path;
if (path.Equals(new PathString("/remoteLoop")))
{
await RemoteLoopHandler.InvokeAsync(context);
return;
}

await next(context);
}
}

public static class GenericHandlerExtensions
{
public static IApplicationBuilder UseGenericHandler(this IApplicationBuilder builder)
{
return builder.UseMiddleware<GenericHandler>();
}

public static void SetStatusDescription(this HttpResponse response, string description)
{
response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = description;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace MonoNetTestServer
{
public class RemoteLoopHandler
{
private const int MaxBufferSize = 128 * 1024;

public static async Task InvokeAsync(HttpContext context)
{
try
{
if (!context.WebSockets.IsWebSocketRequest)
{
context.Response.StatusCode = 400;
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Not a websocket request");

return;
}

using (WebSocket socket = await context.WebSockets.AcceptWebSocketAsync())
{
await ProcessWebSocketRequest(context, socket);
}

}
catch (Exception)
{
// We might want to log these exceptions. But for now we ignore them.
}
}

enum CommandType
{
Listen,
Open,
Send,
Receive,
Close
}

class Command
{
public CommandType Type { get; set; }
public byte[] Data { get; set; }
public int Port { get; set; }
public int ListenBacklog { get; set; }
public IPAddress Address { get; set; }
}

private static async Task ProcessWebSocketRequest(HttpContext context, WebSocket webSocket)
{
Socket listenSocket = null;
Socket current = null;
Memory<byte> ms = new Memory<byte>();

ValueWebSocketReceiveResult result = await webSocket.ReceiveAsync(ms, CancellationToken.None);
while (result.MessageType != WebSocketMessageType.Close)
{
Command command = null; // get bytes=> json=>command
switch (command.Type)
{
case CommandType.Listen:
listenSocket = new Socket(command.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listenSocket.Bind(new IPEndPoint(command.Address, 0));
listenSocket.Listen(command.ListenBacklog);
break;
case CommandType.Open:
current = await listenSocket.AcceptAsync().ConfigureAwait(false);
break;
case CommandType.Close:
current.Close();
break;
}

result = await webSocket.ReceiveAsync(ms, CancellationToken.None);
}
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "closing remoteLoop", CancellationToken.None);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be $(NetCoreAppToolCurrent)

<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<OutputType>Exe</OutputType>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

<ItemGroup>
<Compile Include="Handlers\RemoteLoopHandler.cs" />
<Compile Include="GenericHandler.cs" />
<Compile Include="Program.cs" />
<Compile Include="Startup.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace MonoNetTestServer
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace MonoNetTestServer
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseWebSockets();
app.UseGenericHandler();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class GenericHandler
// Must have constructor with this signature, otherwise exception at run time.
public GenericHandler(RequestDelegate next)
{
// This is an HTTP Handler, so no need to store next.
// This catch all HTTP Handler, so no need to store next.
}

public async Task Invoke(HttpContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<OutputType>Exe</OutputType>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31229.75
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonoNetTestServer", "MonoNetTestServer\MonoNetTestServer.csproj", "{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreServer", "NetCoreServer\NetCoreServer.csproj", "{2BB687CC-3F0C-43A9-8F38-140E91892EB0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}.Release|Any CPU.Build.0 = Release|Any CPU
{2BB687CC-3F0C-43A9-8F38-140E91892EB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2BB687CC-3F0C-43A9-8F38-140E91892EB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BB687CC-3F0C-43A9-8F38-140E91892EB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BB687CC-3F0C-43A9-8F38-140E91892EB0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2F9A0637-452E-4FB9-9403-CB52944982DA}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,26 @@
<StringResourcesPath>../src/Resources/Strings.resx</StringResourcesPath>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppCurrent)-Browser</TargetFrameworks>
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
<Scenario>WasmTestOnBrowser</Scenario>
<WasmXHarnessArgs>$(WasmXHarnessArgs) --set-web-server-env --web-server-middleware=$(ArtifactsDir)bin/MonoNetTestServer/net5.0-$(Configuration)/MonoNetTestServer.dll --web-server-middleware=$(ArtifactsDir)bin/NetCoreServer/net5.0-$(Configuration)/NetCoreServer.dll</WasmXHarnessArgs>
</PropertyGroup>
<!-- Do not reference these assemblies from the TargetingPack since we are building part of the source code for tests. -->
<ItemGroup>
<DefaultReferenceExclusion Include="System.Configuration" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)System\Net\Capability.Security.cs"
Link="Common\System\Net\Capability.Security.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.cs"
Link="Common\System\Net\Configuration.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.Certificates.cs"
Link="Common\System\Net\Configuration.Certificates.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.Http.cs"
Link="Common\System\Net\Configuration.Http.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.Security.cs"
Link="Common\System\Net\Configuration.Security.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.WebSockets.cs"
Link="Common\System\Net\Configuration.WebSockets.cs" />
<Compile Include="$(CommonTestPath)System\Net\EventSourceTestLogging.cs"
Link="Common\System\Net\EventSourceTestLogging.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\LoopbackProxyServer.cs"
Link="Common\System\Net\Http\LoopbackProxyServer.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\LoopbackServer.cs"
Link="Common\System\Net\Http\LoopbackServer.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\GenericLoopbackServer.cs"
Link="Common\System\Net\Http\GenericLoopbackServer.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\PlatformSupport.cs"
Link="CommonTest\System\Security\Cryptography\PlatformSupport.cs" />
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs"
Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
<Compile Include="$(CommonTestPath)System\Net\Capability.Security.cs" Link="Common\System\Net\Capability.Security.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.cs" Link="Common\System\Net\Configuration.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.Certificates.cs" Link="Common\System\Net\Configuration.Certificates.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.Http.cs" Link="Common\System\Net\Configuration.Http.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.Security.cs" Link="Common\System\Net\Configuration.Security.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.WebSockets.cs" Link="Common\System\Net\Configuration.WebSockets.cs" />
<Compile Include="$(CommonTestPath)System\Net\EventSourceTestLogging.cs" Link="Common\System\Net\EventSourceTestLogging.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\LoopbackProxyServer.cs" Link="Common\System\Net\Http\LoopbackProxyServer.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\LoopbackServer.cs" Link="Common\System\Net\Http\LoopbackServer.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\GenericLoopbackServer.cs" Link="Common\System\Net\Http\GenericLoopbackServer.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\PlatformSupport.cs" Link="CommonTest\System\Security\Cryptography\PlatformSupport.cs" />
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs" Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
<Compile Include="AbortTest.cs" />
<Compile Include="CancelTest.cs" />
<Compile Include="ClientWebSocketOptionsTests.cs" />
Expand All @@ -51,4 +41,10 @@
<ItemGroup>
<PackageReference Include="System.Net.TestData" Version="$(SystemNetTestDataVersion)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetOS)' == 'Browser'">
<!-- TODO
<ProjectReference Include="$(CommonTestPath)System/Net/Prerequisites/MonoNetTestServer/MonoNetTestServer.csproj" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I think you want <ProjectReference ... ReferenceOutputAssembly="false"/> because you just want to establish a build-order, not to actually link it in to the tests
  2. You actually should reference a .proj that uses the MSBuild task (or maybe an Exec. not sure) to build the server for the build machine RID, not for browser-wasm.

<ProjectReference Include="$(CommonTestPath)System/Net/Prerequisites/NetCoreServer/NetCoreServer.csproj" />
-->
</ItemGroup>
</Project>