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][debugger] Improving logging control for tests #42175

Merged
merged 1 commit into from
Sep 17, 2020
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
5 changes: 5 additions & 0 deletions src/mono/wasm/debugger/DebuggerTestSuite/TestHarnessProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Microsoft.WebAssembly.Diagnostics
{
Expand All @@ -35,6 +36,10 @@ public static Task Start(string chromePath, string appPath, string pagePath)
{
config.AddEnvironmentVariables(prefix: "WASM_TESTS_");
})
.ConfigureLogging(logging =>
{
logging.AddConsole();
})
.ConfigureServices((ctx, services) =>
{
services.Configure<TestHarnessOptions>(ctx.Configuration);
Expand Down
47 changes: 25 additions & 22 deletions src/mono/wasm/debugger/DebuggerTestSuite/TestHarnessStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public TestHarnessStartup(IConfiguration configuration)
}

public IConfiguration Configuration { get; set; }
public ILogger<TestHarnessProxy> Logger { get; private set; }

private ILoggerFactory _loggerFactory;

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
Expand All @@ -43,7 +46,7 @@ public void ConfigureServices(IServiceCollection services)

async Task SendNodeVersion(HttpContext context)
{
Console.WriteLine("hello chrome! json/version");
Logger.LogTrace("hello chrome! json/version");
var resp_obj = new JObject();
resp_obj["Browser"] = "node.js/v9.11.1";
resp_obj["Protocol-Version"] = "1.1";
Expand All @@ -54,7 +57,7 @@ async Task SendNodeVersion(HttpContext context)

async Task SendNodeList(HttpContext context)
{
Console.WriteLine("hello chrome! json/list");
Logger.LogTrace("webserver: hello chrome! json/list");
try
{
var response = new JArray(JObject.FromObject(new
Expand All @@ -68,10 +71,10 @@ async Task SendNodeList(HttpContext context)
webSocketDebuggerUrl = "ws://localhost:9300/91d87807-8a81-4f49-878c-a5604103b0a4"
})).ToString();

Console.WriteLine($"sending: {response}");
Logger.LogTrace($"webserver: sending: {response}");
await context.Response.WriteAsync(response, new CancellationTokenSource().Token);
}
catch (Exception e) { Console.WriteLine(e); }
catch (Exception e) { Logger.LogError(e, "webserver: SendNodeList failed"); }
}

public async Task LaunchAndServe(ProcessStartInfo psi, HttpContext context, Func<string, Task<string>> extract_conn_url)
Expand All @@ -91,7 +94,7 @@ public async Task LaunchAndServe(ProcessStartInfo psi, HttpContext context, Func
proc.ErrorDataReceived += (sender, e) =>
{
var str = e.Data;
Console.WriteLine($"stderr: {str}");
Logger.LogTrace($"browser-stderr: {str}");

if (tcs.Task.IsCompleted)
return;
Expand All @@ -105,35 +108,32 @@ public async Task LaunchAndServe(ProcessStartInfo psi, HttpContext context, Func

proc.OutputDataReceived += (sender, e) =>
{
Console.WriteLine($"stdout: {e.Data}");
Logger.LogTrace($"browser-stdout: {e.Data}");
};

proc.BeginErrorReadLine();
proc.BeginOutputReadLine();

if (await Task.WhenAny(tcs.Task, Task.Delay(5000)) != tcs.Task)
{
Console.WriteLine("Didnt get the con string after 5s.");
Logger.LogError("Didnt get the con string after 5s.");
throw new Exception("node.js timedout");
}
var line = await tcs.Task;
var con_str = extract_conn_url != null ? await extract_conn_url(line) : line;

Console.WriteLine($"launching proxy for {con_str}");

using var loggerFactory = LoggerFactory.Create(
builder => builder.AddConsole().AddFilter(null, LogLevel.Information));
Logger.LogInformation($"launching proxy for {con_str}");

var proxy = new DebuggerProxy(loggerFactory, null);
var proxy = new DebuggerProxy(_loggerFactory, null);
var browserUri = new Uri(con_str);
var ideSocket = await context.WebSockets.AcceptWebSocketAsync();

await proxy.Run(browserUri, ideSocket);
Console.WriteLine("Proxy done");
Logger.LogInformation("Proxy done");
}
catch (Exception e)
{
Console.WriteLine("got exception {0}", e);
Logger.LogError("got exception {0}", e);
}
finally
{
Expand All @@ -146,8 +146,11 @@ public async Task LaunchAndServe(ProcessStartInfo psi, HttpContext context, Func
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IOptionsMonitor<TestHarnessOptions> optionsAccessor, IWebHostEnvironment env)
public void Configure(IApplicationBuilder app, IOptionsMonitor<TestHarnessOptions> optionsAccessor, IWebHostEnvironment env, ILogger<TestHarnessProxy> logger, ILoggerFactory loggerFactory)
{
this.Logger = logger;
this._loggerFactory = loggerFactory;

app.UseWebSockets();
app.UseStaticFiles();

Expand All @@ -169,7 +172,7 @@ public void Configure(IApplicationBuilder app, IOptionsMonitor<TestHarnessOption
{
router.MapGet("launch-chrome-and-connect", async context =>
{
Console.WriteLine("New test request");
Logger.LogInformation("New test request");
try
{
var client = new HttpClient();
Expand All @@ -195,7 +198,7 @@ await LaunchAndServe(psi, context, async (str) =>
await Task.Delay(100);

var res = await client.GetStringAsync(new Uri(new Uri(str), "/json/list"));
Console.WriteLine("res is {0}", res);
Logger.LogTrace("res is {0}", res);

if (!String.IsNullOrEmpty(res))
{
Expand All @@ -208,29 +211,29 @@ await LaunchAndServe(psi, context, async (str) =>
var elapsed = DateTime.Now - start;
if (elapsed.Milliseconds > 5000)
{
Console.WriteLine($"Unable to get DevTools /json/list response in {elapsed.Seconds} seconds, stopping");
Logger.LogError($"Unable to get DevTools /json/list response in {elapsed.Seconds} seconds, stopping");
return null;
}
}

var wsURl = obj[0]?["webSocketDebuggerUrl"]?.Value<string>();
Console.WriteLine(">>> {0}", wsURl);
Logger.LogTrace(">>> {0}", wsURl);

return wsURl;
});
}
catch (Exception ex)
{
Console.WriteLine($"launch-chrome-and-connect failed with {ex.ToString()}");
Logger.LogError($"launch-chrome-and-connect failed with {ex.ToString()}");
}
});
});

if (options.NodeApp != null)
{
Console.WriteLine($"Doing the nodejs: {options.NodeApp}");
Logger.LogTrace($"Doing the nodejs: {options.NodeApp}");
var nodeFullPath = Path.GetFullPath(options.NodeApp);
Console.WriteLine(nodeFullPath);
Logger.LogTrace(nodeFullPath);
var psi = new ProcessStartInfo();

psi.UseShellExecute = false;
Expand Down
5 changes: 4 additions & 1 deletion src/mono/wasm/debugger/DebuggerTestSuite/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"LogLevel": {
"Default": "Error",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.WebAssembly.Diagnostics.TestHarnessProxy": "Information",
"Microsoft.WebAssembly.Diagnostics.DevToolsProxy": "Information",
"DebuggerTests.Inspector": "Information"
}
}
}