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

Only push output device messages to Test Explorer, don't push logs #4178

Merged
merged 32 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2d96954
Only push output device messages to Test Explorer, don't push logs
Youssef1313 Nov 27, 2024
1231ea9
Remove unused usings
Youssef1313 Nov 27, 2024
a3429b0
Adjust test expectation for the new behavior
Youssef1313 Nov 27, 2024
3b0d18d
Review comments
Youssef1313 Nov 28, 2024
0a7df37
Cleanup possibly redundant check
Youssef1313 Nov 28, 2024
f7d05a5
Always log something in before session
Youssef1313 Nov 28, 2024
e3299b8
Adjust
Youssef1313 Nov 29, 2024
13766b5
Fix assert back and TODO
Youssef1313 Nov 29, 2024
c714c07
Add test
Youssef1313 Nov 29, 2024
968c79b
Localize
Youssef1313 Nov 29, 2024
6767387
Fix test
Youssef1313 Nov 30, 2024
74da123
Address review comment
Youssef1313 Dec 2, 2024
5560e40
Refactor
Youssef1313 Dec 2, 2024
7874d42
Further cleanup
Youssef1313 Dec 2, 2024
a823ac0
Fix build errors
Youssef1313 Dec 2, 2024
f6d0c1b
Fix missing initialization
Youssef1313 Dec 2, 2024
7e7fb18
Merge branch 'main' into logging-improvements
Youssef1313 Dec 3, 2024
edf7751
Follow-up per recent changes
Youssef1313 Dec 3, 2024
b4bf543
Remove unused using
Youssef1313 Dec 3, 2024
1d355c2
Temporary restore `FormattedTextOutputDeviceDataBuilder`
Youssef1313 Dec 3, 2024
25dbaaf
Adjust
Youssef1313 Dec 3, 2024
adb40be
Progress
Youssef1313 Dec 3, 2024
7b778db
Merge branch 'main' into logging-improvements
Youssef1313 Dec 3, 2024
d7f4353
Merge branch 'logging-improvements' of https://github.com/Youssef1313…
Youssef1313 Dec 3, 2024
074227f
Forward some messages to output device (hence, to TE)
Youssef1313 Dec 3, 2024
870fb58
Cleanup
Youssef1313 Dec 3, 2024
09aad35
Remove unused using
Youssef1313 Dec 3, 2024
c2edd6a
Decouple IOutputDevice from IPlatformOutputDevice
Youssef1313 Dec 3, 2024
59fd67a
Avoid async void
Youssef1313 Dec 3, 2024
26964af
Fix race
Youssef1313 Dec 4, 2024
39971bd
Address review comments
Youssef1313 Dec 4, 2024
e3e2907
Move comment to server mode output device
Youssef1313 Dec 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,6 @@ protected override async Task<int> InternalRunAsync()
await _logger.LogDebugAsync("Starting server mode");
_messageHandler = await _messageHandlerFactory.CreateMessageHandlerAsync(_testApplicationCancellationTokenSource.CancellationToken);

// Initialize the ServerLoggerForwarderProvider, it can be null if diagnostic is disabled.
ServerLoggerForwarderProvider? serviceLoggerForwarder = ServiceProvider.GetService<ServerLoggerForwarderProvider>();
if (serviceLoggerForwarder is not null)
{
await serviceLoggerForwarder.InitializeAsync(this);
}

await HandleMessagesAsync();

(_messageHandler as IDisposable)?.Dispose();
Expand Down Expand Up @@ -456,7 +449,7 @@ private async Task<ResponseArgsBase> ExecuteRequestAsync(RequestArgsBase args, s
requestFactory,
invoker,
filterFactory,
new ServerModePerCallOutputDevice(),
new ServerModePerCallOutputDevice(this, _messageMonitor),
[testNodeUpdateProcessor],
_testFrameworkManager,
_testSessionManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,6 @@ public async Task<ITestHost> BuildAsync(
// Get the command line options
ICommandLineOptions commandLineOptions = serviceProvider.GetCommandLineOptions();

// Register the server mode log forwarder if needed. We follow the console --diagnostic behavior.
bool hasServerFlag = commandLineHandler.TryGetOptionArgumentList(PlatformCommandLineProvider.ServerOptionKey, out string[]? protocolName);
bool isJsonRpcProtocol = protocolName is null || protocolName.Length == 0 || protocolName[0].Equals(PlatformCommandLineProvider.JsonRpcProtocolName, StringComparison.OrdinalIgnoreCase);
if (hasServerFlag && isJsonRpcProtocol)
{
ServerLoggerForwarderProvider serverLoggerProxy = new(loggingState.LogLevel, serviceProvider);
serviceProvider.AddService(serverLoggerProxy);
Logging.AddProvider((logLevel, services) => serverLoggerProxy);
}

// Build the logger factory.
ILoggerFactory loggerFactory = await ((LoggingManager)Logging).BuildAsync(serviceProvider, loggingState.LogLevel, systemMonitor);

Expand Down Expand Up @@ -393,6 +383,9 @@ await LogTestHostCreatedAsync(
return new TestHostOrchestratorHost(testHostOrchestratorConfiguration, serviceProvider);
}

bool hasServerFlag = commandLineHandler.TryGetOptionArgumentList(PlatformCommandLineProvider.ServerOptionKey, out string[]? protocolName);
bool isJsonRpcProtocol = protocolName is null || protocolName.Length == 0 || protocolName[0].Equals(PlatformCommandLineProvider.JsonRpcProtocolName, StringComparison.OrdinalIgnoreCase);

// ======= TEST HOST CONTROLLER MODE ======== //
// Check if we're in the test host or we should check test controllers extensions
// Environment variable check should not be needed but in case we will rollback to use only env var we will need it.
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text;

using Microsoft.Testing.Platform.Extensions.OutputDevice;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Hosts;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.OutputDevice;

namespace Microsoft.Testing.Platform.ServerMode;

internal class ServerModePerCallOutputDevice : IPlatformOutputDevice
{
private readonly IServerTestHost _serverTestHost;
private readonly IAsyncMonitor _asyncMonitor;

private static readonly string[] NewLineStrings = { "\r\n", "\n" };

public ServerModePerCallOutputDevice(IServerTestHost serverTestHost, IAsyncMonitor asyncMonitor)
{
_serverTestHost = serverTestHost;
_asyncMonitor = asyncMonitor;
}

public string Uid => nameof(ServerModePerCallOutputDevice);

public string Version => AppVersion.DefaultSemVer;
Expand All @@ -19,11 +34,72 @@ internal class ServerModePerCallOutputDevice : IPlatformOutputDevice

public Task DisplayAfterSessionEndRunAsync() => Task.CompletedTask;

public Task DisplayAsync(IOutputDeviceDataProducer producer, IOutputDeviceData data) => Task.CompletedTask;
public async Task DisplayAsync(IOutputDeviceDataProducer producer, IOutputDeviceData data)
{
using (await _asyncMonitor.LockAsync(TimeoutHelper.DefaultHangTimeSpanTimeout))
{
switch (data)
{
case FormattedTextOutputDeviceData formattedTextOutputDeviceData:
LogLevel logLevel = formattedTextOutputDeviceData.ForegroundColor is SystemConsoleColor color
? color.ConsoleColor switch
{
ConsoleColor.Red => LogLevel.Error,
ConsoleColor.Yellow => LogLevel.Warning,
_ => LogLevel.Information,
}
: LogLevel.Information;

await LogAsync(logLevel, formattedTextOutputDeviceData.Text, formattedTextOutputDeviceData.Padding);
break;

case TextOutputDeviceData textOutputDeviceData:
await LogAsync(LogLevel.Information, textOutputDeviceData.Text, padding: null);
break;

case ExceptionOutputDeviceData exceptionOutputDeviceData:
await LogAsync(LogLevel.Error, exceptionOutputDeviceData.Exception.ToString(), padding: null);
break;
}
}
}

public Task DisplayBannerAsync(string? bannerMessage) => Task.CompletedTask;

public Task DisplayBeforeSessionStartAsync() => Task.CompletedTask;

public Task<bool> IsEnabledAsync() => Task.FromResult(false);
public Task<bool> IsEnabledAsync() => Task.FromResult(true);

private async Task LogAsync(LogLevel logLevel, string message, int? padding)
{
message = GetIndentedMessage(message, padding);
ServerLogMessage logMessage = new(logLevel, message);
await _serverTestHost.PushDataAsync(logMessage);
}

private static string GetIndentedMessage(string message, int? padding)
{
int paddingValue = padding.GetValueOrDefault();
if (paddingValue == 0)
{
return message;
}

string indent = new(' ', paddingValue);

if (!message.Contains('\n'))
{
return indent + message;
}

string[] lines = message.Split(NewLineStrings, StringSplitOptions.None);
StringBuilder builder = new();
foreach (string line in lines)
{
builder.Append(indent);
builder.AppendLine(line);
}

return builder.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task DiscoverAndRun(string tfm)
await Task.WhenAll(discoveryListener.WaitCompletion(), runListener.WaitCompletion());
Assert.AreEqual(1, discoveryCollector.TestNodeUpdates.Count(x => x.Node.NodeType == "action"), $"Wrong number of discovery");
Assert.AreEqual(2, runCollector.TestNodeUpdates.Count, $"Wrong number of updates");
Assert.IsFalse(logs.IsEmpty, $"Logs are empty");
Assert.IsTrue(logs.IsEmpty, $"Logs are not empty");
Assert.IsFalse(telemetry.IsEmpty, $"telemetry is empty");
await jsonClient.Exit();
Assert.AreEqual(0, await jsonClient.WaitServerProcessExit());
Expand Down
Loading
Loading