-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathGivenARedisTaskQueue.cs
More file actions
286 lines (227 loc) · 11 KB
/
GivenARedisTaskQueue.cs
File metadata and controls
286 lines (227 loc) · 11 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
namespace Gofer.NET.Tests
{
public class GivenARedisTaskQueue
{
private class TestDataHolder
{
public string Value { get; set; }
public override string ToString()
{
return Value;
}
}
private class CustomException : Exception {}
[Fact]
public async Task ItCapturesArgumentsPassedToEnqueuedDelegate()
{
var testFixture = new TaskQueueTestFixture(nameof(ItCapturesArgumentsPassedToEnqueuedDelegate));
string variableToExtract = "extracted";
var semaphoreFile = Path.GetTempFileName();
var now = DateTime.Now;
var utcNow = DateTime.UtcNow;
Func<Expression<Action>, string, Tuple<Expression<Action>, string>> TC = (actionExp, str) =>
Tuple.Create<Expression<Action>, string>(
actionExp,
str);
// Action to expected result
var delgates = new Tuple<Expression<Action>, string>[]
{
// Exception Argument
TC(() => ExceptionFunc(new Exception(), semaphoreFile), new Exception().ToString()),
TC(() => ExceptionFunc(new CustomException(), semaphoreFile), new CustomException().ToString()),
// Integer Arguments
TC(() => IntFunc(int.MaxValue, semaphoreFile), int.MaxValue.ToString()),
TC(() => IntFunc(int.MinValue, semaphoreFile), int.MinValue.ToString()),
TC(() => NullableIntFunc(null, semaphoreFile), "-1"),
TC(() => NullableIntFunc(int.MinValue, semaphoreFile), int.MinValue.ToString()),
TC(() => NullableIntFunc(int.MaxValue, semaphoreFile), int.MaxValue.ToString()),
// Float Arguments
TC(() => FloatFunc(float.MaxValue, semaphoreFile), float.MaxValue.ToString()),
TC(() => FloatFunc(float.MinValue, semaphoreFile), float.MinValue.ToString()),
// Double Arguments
TC(() => DoubleFunc(double.MaxValue, semaphoreFile), double.MaxValue.ToString()),
TC(() => DoubleFunc(double.MinValue, semaphoreFile), double.MinValue.ToString()),
// Long Arguments
TC(() => LongFunc(long.MaxValue, semaphoreFile), long.MaxValue.ToString()),
TC(() => LongFunc(long.MinValue, semaphoreFile), long.MinValue.ToString()),
// Boolean Arguments
TC(() => BoolFunc(true, semaphoreFile), true.ToString()),
TC(() => BoolFunc(false, semaphoreFile), false.ToString()),
// String Arguments
TC(() => StringFunc("astring", semaphoreFile), "astring"),
TC(() => StringFunc(variableToExtract, semaphoreFile), variableToExtract),
// Object Arguments
TC(() => ObjectFunc(new TestDataHolder {Value = "astring"}, semaphoreFile), "astring"),
// DateTime Arguments
TC(() => DateTimeFunc(now, semaphoreFile), now.ToString()),
TC(() => DateTimeFunc(utcNow, semaphoreFile), utcNow.ToString()),
// Nullable Type Arguments
TC(() => NullableTypeFunc(null, semaphoreFile), "null"),
TC(() => NullableTypeFunc(now, semaphoreFile), now.ToString()),
// Array Arguments
TC(() => ArrayFunc1(new[] {"this", "string", "is"}, semaphoreFile), "this,string,is"),
TC(() => ArrayFunc2(new[] {1, 2, 3, 4}, semaphoreFile), "1,2,3,4"),
TC(() => ArrayFunc3(new int?[] {1, 2, 3, null, 5}, semaphoreFile), "1,2,3,null,5"),
// Type 'Type' Arguments
TC(() => TypeFunc(typeof(object), semaphoreFile), typeof(object).ToString()),
TC(() => TypeFunc(typeof(GivenARedisTaskQueue), semaphoreFile), typeof(GivenARedisTaskQueue).ToString()),
TC(() => TypeFunc(null, semaphoreFile), "null"),
// Function Arguments
TC(() => FuncFunc(() => 1, semaphoreFile), "1"),
// Awaiting inside the lambda is unnecessary, as the method is extracted and serialized.
#pragma warning disable 4014
TC(() => AsyncFunc(semaphoreFile), "async"),
TC(() => AsyncFuncThatReturnsString(semaphoreFile), "async")
#pragma warning restore 4014
};
foreach (var tup in delgates)
{
var actionExpr = tup.Item1;
var expectedString = tup.Item2;
File.Delete(semaphoreFile);
await testFixture.TaskQueue.Enqueue(actionExpr);
await testFixture.TaskQueue.ExecuteNext();
File.ReadAllText(semaphoreFile).Should().Be(expectedString);
}
File.Delete(semaphoreFile);
}
[Fact]
public async Task ItEnqueuesAndReceivesDelegatesThatAreRunnable()
{
var testFixture = new TaskQueueTestFixture(nameof(ItEnqueuesAndReceivesDelegatesThatAreRunnable));
testFixture.EnsureSemaphoreDoesntExist();
await testFixture.PushPopExecuteWriteSemaphore();
testFixture.EnsureSemaphore();
}
[Fact]
public async Task ItsTasksAreConsumedOnlyOnceByMultipleConsumers()
{
// Higher numbers here increase confidence
var numberOfJobs = 16;
var numberOfConsumers = 4;
var sharedTaskQueueName = nameof(ItsTasksAreConsumedOnlyOnceByMultipleConsumers);
var consumers = Enumerable.Range(0, numberOfConsumers)
.Select(_ => new TaskQueueTestFixture(sharedTaskQueueName)).ToList();
var semaphoreFiles = new List<string>();
for(int i=0;i < numberOfJobs;++i)
{
var path = Path.GetTempFileName();
File.Delete(path);
semaphoreFiles.Add(path);
var sharedTaskQueue = consumers[0].TaskQueue;
await sharedTaskQueue.Enqueue(() => TaskQueueTestFixture.WriteSemaphore(path));
}
var tasks = new List<Task>();
// Purposely executing more times than the number of tasks we have
// Specifically numberOfJobs * numberOfConsumers times.
for (var i = 0; i < numberOfJobs; i += 1)
{
foreach (var consumer in consumers)
{
var task = Task.Run(() => consumer.TaskQueue.ExecuteNext());
tasks.Add(task);
}
}
await Task.WhenAll(tasks);
foreach (var semaphoreFile in semaphoreFiles)
{
File.ReadAllText(semaphoreFile).Should()
.Be(TaskQueueTestFixture.SemaphoreText);
}
}
public async Task AsyncFunc(string semaphoreFile)
{
// Wait to ensure async waiting is happening.
await Task.Delay(1000);
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, "async");
}
public async Task<string> AsyncFuncThatReturnsString(string semaphoreFile)
{
// Wait to ensure async waiting is happening.
await Task.Delay(1000);
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, "async");
return "async";
}
public void NullableTypeFunc(DateTime? dateTime, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, dateTime);
}
public void DateTimeFunc(DateTime dateTime, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, dateTime);
}
public void IntFunc(int num, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, num);
}
public void NullableIntFunc(int? num, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, num ?? -1);
}
public void LongFunc(long num, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, num);
}
public void FloatFunc(float num, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, num);
}
public void BoolFunc(bool num, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, num);
}
public void DoubleFunc(double num, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, num);
}
public void StringFunc(string num, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, num);
}
public void ObjectFunc(object num, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, num);
}
public void ExceptionFunc(Exception exc, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, exc);
}
public void TypeFunc(Type typeArg, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, typeArg?.ToString() ?? "null");
}
public void ArrayFunc1(string[] nums, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, string.Join(",", nums));
}
public void ArrayFunc2(int[] nums, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, string.Join(",", nums));
}
public void ArrayFunc3(int?[] nums, string semaphoreFile)
{
var str = "";
var first = true;
foreach (var num in nums)
{
if (!first) str += ",";
str += num?.ToString() ?? "null";
first = false;
}
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, str);
}
public void FuncFunc(Func<int> func, string semaphoreFile)
{
TaskQueueTestFixture.WriteSemaphoreValue(semaphoreFile, func().ToString());
}
}
}