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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace System.Threading.Tasks
/// and may be used from multiple threads concurrently.
/// </para>
/// </remarks>
[DebuggerTypeProxy(typeof(SystemThreadingTasks_TaskDebugView<>))]
[DebuggerTypeProxy(typeof(SystemThreadingTasks_TaskOfTResultDebugView<>))]
[DebuggerDisplay("Id = {Id}, Status = {Status}, Method = {DebuggerDisplayMethodDescription}, Result = {DebuggerDisplayResultDescription}")]
public class Task<TResult> : Task
{
Expand Down Expand Up @@ -1411,17 +1411,17 @@ internal Task<TNewResult> ContinueWith<TNewResult>(Func<Task<TResult>, object?,
}

// Proxy class for better debugging experience
internal sealed class SystemThreadingTasks_TaskDebugView<TResult>
internal sealed class SystemThreadingTasks_TaskOfTResultDebugView<TResult>
{
private readonly Task<TResult> m_task;

public SystemThreadingTasks_TaskDebugView(Task<TResult> task)
public SystemThreadingTasks_TaskOfTResultDebugView(Task<TResult> task)
{
Debug.Assert(task != null);
m_task = task;
}

public TResult? Result => m_task.Status == TaskStatus.RanToCompletion ? m_task.Result : default;
public TResult Result => m_task.IsCompleted ? m_task.Result : default!;
public object? AsyncState => m_task.AsyncState;
public TaskCreationOptions CreationOptions => m_task.CreationOptions;
public Exception? Exception => m_task.Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@
<Compile Include="$(CommonTestPath)System\Threading\ThreadPoolHelpers.cs" Link="CommonTest\System\Threading\ThreadPoolHelpers.cs" />
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs" Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
<Compile Include="Task\TaskDisposeTests.cs" />
<Compile Include="Task\TaskDebugViewTests.cs" />
<Compile Include="$(CommonTestPath)System\Diagnostics\DebuggerAttributes.cs" Link="Common\System\Diagnostics\DebuggerAttributes.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Reflection;
using Xunit;

namespace System.Threading.Tasks.Tests
{
public static class TaskDebugViewTests
{
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public static void TaskOfTResult_DebugProxy_CompletedSuccessfully_ReturnsResult()
{
Task<string> task = Task.FromResult("expected");

object proxy = DebuggerAttributes.GetProxyObject(task);
PropertyInfo resultProperty = proxy.GetType().GetProperty("Result");
object result = resultProperty.GetValue(proxy);

Assert.Equal("expected", result);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public static void TaskOfTResult_DebugProxy_Faulted_ResultThrows()
{
var tcs = new TaskCompletionSource<string>();
tcs.SetException(new InvalidOperationException("task faulted"));
Task<string> task = tcs.Task;

object proxy = DebuggerAttributes.GetProxyObject(task);
PropertyInfo resultProperty = proxy.GetType().GetProperty("Result");

TargetInvocationException tie = Assert.Throws<TargetInvocationException>(() => resultProperty.GetValue(proxy));
AggregateException ae = Assert.IsType<AggregateException>(tie.InnerException);
Assert.IsType<InvalidOperationException>(ae.InnerException);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public static void TaskOfTResult_DebugProxy_Canceled_ResultThrows()
{
var tcs = new TaskCompletionSource<string>();
tcs.SetCanceled();
Task<string> task = tcs.Task;

object proxy = DebuggerAttributes.GetProxyObject(task);
PropertyInfo resultProperty = proxy.GetType().GetProperty("Result");

TargetInvocationException tie = Assert.Throws<TargetInvocationException>(() => resultProperty.GetValue(proxy));
Assert.IsType<AggregateException>(tie.InnerException);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public static void TaskOfTResult_DebugProxy_Incomplete_ResultReturnsDefault()
{
var tcs = new TaskCompletionSource<string>();
Task<string> task = tcs.Task;

object proxy = DebuggerAttributes.GetProxyObject(task);
PropertyInfo resultProperty = proxy.GetType().GetProperty("Result");
object result = resultProperty.GetValue(proxy);

Assert.Null(result);

tcs.SetResult("done");
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public static void TaskOfTResult_DebugProxy_Incomplete_ValueType_ResultReturnsDefault()
{
var tcs = new TaskCompletionSource<int>();
Task<int> task = tcs.Task;

object proxy = DebuggerAttributes.GetProxyObject(task);
PropertyInfo resultProperty = proxy.GetType().GetProperty("Result");
object result = resultProperty.GetValue(proxy);

Assert.Equal(0, result);

tcs.SetResult(42);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public static void TaskOfTResult_DebugProxy_ShowsCorrectStatus()
{
var tcs = new TaskCompletionSource<string>();
tcs.SetException(new InvalidOperationException());
Task<string> task = tcs.Task;

object proxy = DebuggerAttributes.GetProxyObject(task);

PropertyInfo statusProperty = proxy.GetType().GetProperty("Status");
Assert.Equal(TaskStatus.Faulted, statusProperty.GetValue(proxy));

PropertyInfo exceptionProperty = proxy.GetType().GetProperty("Exception");
Assert.NotNull(exceptionProperty.GetValue(proxy));
}
}
}
Loading