-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathTaskRecyclerTests.cs
More file actions
167 lines (143 loc) · 6.83 KB
/
TaskRecyclerTests.cs
File metadata and controls
167 lines (143 loc) · 6.83 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Diagnostics.CodeAnalysis;
namespace UniGetUI.Core.Classes.Tests;
public class TaskRecyclerTests
{
private int MySlowMethod1()
{
Thread.Sleep(1000);
return new Random().Next();
}
private sealed class TestClass
{
public TestClass() {}
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Instance methods are required to validate TaskRecycler instance-bound delegate behavior.")]
public string SlowMethod2()
{
Thread.Sleep(1000);
return new Random().Next().ToString();
}
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Instance methods are required to validate TaskRecycler instance-bound delegate behavior.")]
public string SlowMethod3()
{
Thread.Sleep(1000);
return new Random().Next().ToString();
}
}
private int MySlowMethod4(int argument)
{
Thread.Sleep(1000);
return new Random().Next() + (argument - argument);
}
[Fact]
public async Task TestTaskRecycler_Static_Int()
{
// The same static method should be cached, and therefore the return value should be the same
var task1 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1);
var task2 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1);
int result1 = await task1;
int result2 = await task2;
Assert.Equal(result1, result2);
// The same static method should be cached, and therefore the return value should be the same, but different from previous runs
var task3 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1);
var task4 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1);
int result4 = await task4;
int result3 = await task3;
Assert.Equal(result3, result4);
// Ensure the last call was not permanently cached
Assert.NotEqual(result1, result3);
}
[Fact]
public async Task TestTaskRecycler_Static_Int_WithCache()
{
// The same static method should be cached, and therefore the return value should be the same
var task1 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
var task2 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
int result1 = await task1;
int result2 = await task2;
Assert.Equal(result1, result2);
// The same static method should be cached, and therefore the return value should be the same,
// and equal to previous runs due to 3 seconds cache
var task3 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
var task4 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
int result4 = await task4;
int result3 = await task3;
Assert.Equal(result3, result4);
Assert.Equal(result1, result3);
// Wait for caches to clear
Thread.Sleep(3000);
// The same static method should be cached, but cached runs should have been removed. This results should differ from previous ones
var task5 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
var task6 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
int result5 = await task6;
int result6 = await task5;
Assert.Equal(result5, result6);
Assert.NotEqual(result4, result5);
// Clear cache
TaskRecycler<int>.RemoveFromCache(MySlowMethod1);
// The same static method should be cached, but cached runs should have been cleared manually. This results should differ from previous ones
var task7 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
var task8 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
int result7 = await task7;
int result8 = await task8;
Assert.Equal(result7, result8);
Assert.NotEqual(result6, result7);
}
[Fact]
public async Task TestTaskRecycler_StaticWithArgument_Int()
{
// The same static method should be cached, and therefore the return value should be the same
var task1 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 2);
var task2 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 2);
var task3 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 3);
int result1 = await task1;
int result2 = await task2;
int result3 = await task3;
Assert.Equal(result1, result2);
Assert.NotEqual(result1, result3);
// The same static method should be cached, and therefore the return value should be the same, but different from previous runs
var task4 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 2);
var task5 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 3);
int result4 = await task4;
int result5 = await task5;
Assert.NotEqual(result4, result5);
Assert.NotEqual(result1, result4);
Assert.NotEqual(result3, result5);
}
[Fact]
public async Task TestTaskRecycler_Class_String()
{
var class1 = new TestClass();
var class2 = new TestClass();
// The SAME method from the SAME instance should be cached,
// and therefore the return value should be the same
var task1 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod2);
var task2 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod2);
string result1 = await task1;
string result2 = await task2;
Assert.Equal(result1, result2);
var class1_copy = class1;
// The SAME method from the SAME instance, even when called
// from different variable names should be cached, and therefore the return value should be the same
var task5 = TaskRecycler<string>.RunOrAttachAsync(class1_copy.SlowMethod2);
var task6 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod2);
string result5 = await task5;
string result6 = await task6;
Assert.Equal(result5, result6);
// The SAME method from two DIFFERENT instances should NOT be
// cached, so the results should differ
var task3 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod2);
var task4 = TaskRecycler<string>.RunOrAttachAsync(class2.SlowMethod2);
string result4 = await task4;
string result3 = await task3;
Assert.NotEqual(result3, result4);
// Ensure the last call was not permanently cached
Assert.NotEqual(result1, result3);
// The SAME method from two DIFFERENT instances should NOT be
// cached, so the results should differ
var task7 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod3);
var task8 = TaskRecycler<string>.RunOrAttachAsync(class2.SlowMethod2);
string result7 = await task7;
string result8 = await task8;
Assert.NotEqual(result7, result8);
}
}