未验证 提交 137cb02f 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #25896 from sharwell/token-diagnostics

Improve diagnostics when tests fail due to an asynchronous operation timeout
......@@ -226,7 +226,7 @@ private bool UpToDate
}
public void RegisterNotification(Action action, int delay, CancellationToken cancellationToken)
=> _notificationService.RegisterNotification(action, delay, _asyncListener.BeginAsyncOperation("TagSource"), cancellationToken);
=> _notificationService.RegisterNotification(action, delay, _asyncListener.BeginAsyncOperation(typeof(TTag).Name), cancellationToken);
private void Connect()
{
......
......@@ -61,11 +61,20 @@ internal sealed class AsynchronousOperationListenerProvider : IAsynchronousOpera
/// </summary>
private bool? _enableDiagnosticTokens;
/// <summary>
/// Provides a default value for <see cref="_enableDiagnosticTokens"/>.
/// </summary>
private static bool? s_enableDiagnosticTokens;
public static void Enable(bool enable)
=> Enable(enable, diagnostics: null);
public static void Enable(bool enable, bool? diagnostics)
{
// right now, made it static so that one can enable it through reflection easy
// but we can think of some other way
s_enabled = enable;
s_enableDiagnosticTokens = diagnostics;
}
[Obsolete("This exported object must be obtained through the MEF export provider.", error: true)]
......@@ -192,9 +201,16 @@ private bool DiagnosticTokensEnabled
{
if (!_enableDiagnosticTokens.HasValue)
{
// if _trackingBehavior has never been set, check environment variable to see whether it should be enabled.
var enabled = Environment.GetEnvironmentVariable("RoslynWaiterDiagnosticTokenEnabled");
_enableDiagnosticTokens = string.Equals(enabled, "1", StringComparison.OrdinalIgnoreCase) || string.Equals(enabled, "True", StringComparison.OrdinalIgnoreCase);
if (s_enableDiagnosticTokens.HasValue)
{
_enableDiagnosticTokens = s_enableDiagnosticTokens;
}
else
{
// if _enableDiagnosticTokens has never been set, check environment variable to see whether it should be enabled.
var enabled = Environment.GetEnvironmentVariable("RoslynWaiterDiagnosticTokenEnabled");
_enableDiagnosticTokens = string.Equals(enabled, "1", StringComparison.OrdinalIgnoreCase) || string.Equals(enabled, "True", StringComparison.OrdinalIgnoreCase);
}
}
return _enableDiagnosticTokens.Value;
......
......@@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
......@@ -70,7 +71,7 @@ public override void Before(MethodInfo methodUnderTest)
DesktopMefHostServices.ResetHostServicesTestOnly();
// make sure we enable this for all unit tests
AsynchronousOperationListenerProvider.Enable(true);
AsynchronousOperationListenerProvider.Enable(enable: true, diagnostics: true);
ExportProviderCache.SetEnabled_OnlyUseExportProviderAttributeCanCall(true);
}
......@@ -112,7 +113,13 @@ public override void After(MethodInfo methodUnderTest)
}
catch (OperationCanceledException ex) when (timeoutTokenSource.IsCancellationRequested)
{
throw new TimeoutException("Failed to clean up listeners in a timely manner.", ex);
var messageBuilder = new StringBuilder("Failed to clean up listeners in a timely manner.");
foreach (var token in ((AsynchronousOperationListenerProvider)listenerProvider).GetTokens())
{
messageBuilder.AppendLine().Append($" {token}");
}
throw new TimeoutException(messageBuilder.ToString(), ex);
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册