-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathTest_TaskExtensions.cs
150 lines (100 loc) · 4.34 KB
/
Test_TaskExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommunityToolkit.Common.UnitTests.Extensions;
[TestClass]
public class Test_TaskExtensions
{
[TestMethod]
public void Test_TaskExtensions_NonGeneric_CompletedTask()
{
_ = Task.CompletedTask.GetResultOrDefault();
}
[TestMethod]
public void Test_TaskExtensions_Generic_ValueType()
{
TaskCompletionSource<int> tcs = new();
Assert.AreEqual(0, tcs.Task.GetResultOrDefault());
tcs.SetResult(42);
Assert.AreEqual(42, tcs.Task.GetResultOrDefault());
}
[TestMethod]
public void Test_TaskExtensions_Generic_ReferenceType()
{
TaskCompletionSource<string?> tcs = new();
Assert.AreEqual(null, tcs.Task.GetResultOrDefault());
tcs.SetResult(nameof(Test_TaskExtensions_Generic_ReferenceType));
Assert.AreEqual(nameof(Test_TaskExtensions_Generic_ReferenceType), tcs.Task.GetResultOrDefault());
}
[TestMethod]
public void Test_TaskExtensions_ResultOrDefault()
{
TaskCompletionSource<int> tcs = new();
Assert.AreEqual(null, ((Task)tcs.Task).GetResultOrDefault());
tcs.SetCanceled();
Assert.AreEqual(null, ((Task)tcs.Task).GetResultOrDefault());
tcs = new TaskCompletionSource<int>();
tcs.SetException(new InvalidOperationException("Test"));
Assert.AreEqual(null, ((Task)tcs.Task).GetResultOrDefault());
tcs = new TaskCompletionSource<int>();
tcs.SetResult(42);
Assert.AreEqual(42, ((Task)tcs.Task).GetResultOrDefault());
}
[TestMethod]
public void Test_TaskExtensions_ResultOrDefault_FromTaskCompleted()
{
Assert.AreEqual(null, Task.CompletedTask.GetResultOrDefault());
}
[TestMethod]
public async Task Test_TaskExtensions_ResultOrDefault_FromAsyncTaskMethodBuilder()
{
TaskCompletionSource<object?>? tcs = new();
Task<string?> taskFromBuilder = GetTaskFromAsyncMethodBuilder("Test", tcs);
Assert.IsNull(((Task)taskFromBuilder).GetResultOrDefault());
Assert.IsNull(taskFromBuilder.GetResultOrDefault());
tcs.SetResult(null);
_ = await taskFromBuilder;
Assert.AreEqual("Test", ((Task)taskFromBuilder).GetResultOrDefault());
Assert.AreEqual("Test", taskFromBuilder.GetResultOrDefault());
}
[TestMethod]
public void Test_TaskExtensions_ResultOrDefault_OfT_Int32()
{
TaskCompletionSource<int> tcs = new();
Assert.AreEqual(0, tcs.Task.GetResultOrDefault());
tcs.SetCanceled();
Assert.AreEqual(0, tcs.Task.GetResultOrDefault());
tcs = new TaskCompletionSource<int>();
tcs.SetException(new InvalidOperationException("Test"));
Assert.AreEqual(0, tcs.Task.GetResultOrDefault());
tcs = new TaskCompletionSource<int>();
tcs.SetResult(42);
Assert.AreEqual(42, tcs.Task.GetResultOrDefault());
}
[TestMethod]
public void Test_TaskExtensions_ResultOrDefault_OfT_String()
{
TaskCompletionSource<string?> tcs = new();
Assert.AreEqual(null, tcs.Task.GetResultOrDefault());
tcs.SetCanceled();
Assert.AreEqual(null, tcs.Task.GetResultOrDefault());
tcs = new TaskCompletionSource<string?>();
tcs.SetException(new InvalidOperationException("Test"));
Assert.AreEqual(null, tcs.Task.GetResultOrDefault());
tcs = new TaskCompletionSource<string?>();
tcs.SetResult("Hello world");
Assert.AreEqual("Hello world", tcs.Task.GetResultOrDefault());
}
// Creates a Task<T> of a given type which is actually an instance of
// System.Runtime.CompilerServices.AsyncTaskMethodBuilder<TResult>.AsyncStateMachineBox<TStateMachine>.
// See https://source.dot.net/#System.Private.CoreLib/AsyncTaskMethodBuilderT.cs,f8f35fd356112b30.
// This is needed to verify that the extension also works when the input Task<T> is of a derived type.
private static async Task<T?> GetTaskFromAsyncMethodBuilder<T>(T? result, TaskCompletionSource<object?> tcs)
{
_ = await tcs.Task;
return result;
}
}