-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestTaskResult.cs
More file actions
96 lines (85 loc) · 2.59 KB
/
Copy pathTestTaskResult.cs
File metadata and controls
96 lines (85 loc) · 2.59 KB
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
using System;
using System.Threading.Tasks;
namespace LoLo.Analyzers.Reliability.Concurrency;
/// <summary>
/// Test class to verify the TaskResultNotObservedAnalyzer works correctly.
/// This file should trigger NN_R002 errors for unobserved Task<T> results.
/// </summary>
public class TestTaskResult
{
/// <summary>
/// This should trigger NN_R002 analyzer error: await result should be observed
/// </summary>
public async Task TestFireAndForgetTask()
{
// This should trigger NN_R002 analyzer error: await result should be observed
await DoSomethingAsync();
}
/// <summary>
/// This should NOT trigger any error: result is properly observed
/// </summary>
public async Task<bool> TestProperlyObservedTask()
{
// This should NOT trigger error: result is properly observed
var result = await DoSomethingAsync();
return result;
}
/// <summary>
/// This should NOT trigger any error: result is returned directly
/// </summary>
public async Task<bool> TestDirectReturnTask()
{
// This should NOT trigger error: result is returned directly
return await DoSomethingAsync();
}
/// <summary>
/// This should NOT trigger any error: result is used in condition
/// </summary>
public async Task TestConditionalUseTask()
{
// This should NOT trigger error: result is used in condition
if (await DoSomethingAsync())
{
var tmp = "Task returned true";
}
}
/// <summary>
/// This should NOT trigger any error: void Task (not Task<T>)
/// </summary>
public async Task TestVoidTask()
{
// This should NOT trigger error: void Task (not Task<T>)
await DoVoidAsync();
}
/// <summary>
/// This should trigger NN_R002 analyzer error: ValueTask<T> result not observed
/// </summary>
public async Task TestValueTaskNotObserved()
{
// This should trigger NN_R002 analyzer error: ValueTask<T> result not observed
await DoValueTaskAsync();
}
/// <summary>
/// Returns a Task<bool> for testing
/// </summary>
private async Task<bool> DoSomethingAsync()
{
await Task.Delay(100);
return true;
}
/// <summary>
/// Returns a void Task for testing
/// </summary>
private async Task DoVoidAsync()
{
await Task.Delay(100);
}
/// <summary>
/// Returns a ValueTask<int> for testing
/// </summary>
private async ValueTask<int> DoValueTaskAsync()
{
await Task.Delay(100);
return 42;
}
}