5
5
using System . Linq . Expressions ;
6
6
using System . Threading ;
7
7
using System . Threading . Tasks ;
8
+
8
9
using FluentAssertions ;
10
+
9
11
using Gofer . NET . Utils ;
12
+
10
13
using Xunit ;
11
14
12
15
namespace Gofer . NET . Tests
@@ -22,8 +25,8 @@ public override string ToString()
22
25
return Value ;
23
26
}
24
27
}
25
-
26
- private class CustomException : Exception { }
28
+
29
+ private class CustomException : Exception { }
27
30
28
31
[ Fact ]
29
32
public async Task ItCapturesArgumentsPassedToEnqueuedDelegate ( )
@@ -40,13 +43,16 @@ public async Task ItCapturesArgumentsPassedToEnqueuedDelegate()
40
43
Tuple . Create < Expression < Action > , string > (
41
44
actionExp ,
42
45
str ) ;
43
-
46
+
44
47
// Action to expected result
45
48
var delgates = new Tuple < Expression < Action > , string > [ ]
46
49
{
47
50
// Exception Argument
48
51
TC ( ( ) => ExceptionFunc ( new Exception ( ) , semaphoreFile ) , new Exception ( ) . ToString ( ) ) ,
49
52
TC ( ( ) => ExceptionFunc ( new CustomException ( ) , semaphoreFile ) , new CustomException ( ) . ToString ( ) ) ,
53
+
54
+ // Cancelation Argument
55
+ TC ( ( ) => CancellationFunc ( default , semaphoreFile ) , new CancellationToken ( ) . ToString ( ) ) ,
50
56
51
57
// Integer Arguments
52
58
TC ( ( ) => IntFunc ( int . MaxValue , semaphoreFile ) , int . MaxValue . ToString ( ) ) ,
@@ -99,29 +105,29 @@ public async Task ItCapturesArgumentsPassedToEnqueuedDelegate()
99
105
TC ( ( ) => AsyncFunc ( semaphoreFile ) . T ( ) , "async" ) ,
100
106
TC ( ( ) => AsyncFuncThatReturnsString ( semaphoreFile ) . T ( ) , "async" )
101
107
} ;
102
-
108
+
103
109
104
110
foreach ( var tup in delgates )
105
111
{
106
112
var actionExpr = tup . Item1 ;
107
113
var expectedString = tup . Item2 ;
108
-
114
+
109
115
File . Delete ( semaphoreFile ) ;
110
-
111
- await testFixture . TaskQueue . Enqueue ( actionExpr ) ;
116
+
117
+ await testFixture . TaskQueue . Enqueue ( actionExpr ) ;
112
118
await testFixture . TaskQueue . ExecuteNext ( ) ;
113
119
114
120
File . ReadAllText ( semaphoreFile ) . Should ( ) . Be ( expectedString ) ;
115
121
}
116
-
122
+
117
123
File . Delete ( semaphoreFile ) ;
118
124
}
119
-
125
+
120
126
[ Fact ]
121
127
public async Task ItEnqueuesAndReceivesDelegatesThatAreRunnable ( )
122
128
{
123
129
var testFixture = new TaskQueueTestFixture ( nameof ( ItEnqueuesAndReceivesDelegatesThatAreRunnable ) ) ;
124
-
130
+
125
131
testFixture . EnsureSemaphoreDoesntExist ( ) ;
126
132
await testFixture . PushPopExecuteWriteSemaphore ( ) ;
127
133
testFixture . EnsureSemaphore ( ) ;
@@ -133,18 +139,18 @@ public async Task ItsTasksAreConsumedOnlyOnceByMultipleConsumers()
133
139
// Higher numbers here increase confidence
134
140
var numberOfJobs = 16 ;
135
141
var numberOfConsumers = 4 ;
136
-
142
+
137
143
var sharedTaskQueueName = nameof ( ItsTasksAreConsumedOnlyOnceByMultipleConsumers ) ;
138
144
var consumers = Enumerable . Range ( 0 , numberOfConsumers )
139
145
. Select ( _ => new TaskQueueTestFixture ( sharedTaskQueueName ) ) . ToList ( ) ;
140
146
141
147
var semaphoreFiles = new List < string > ( ) ;
142
- for ( int i = 0 ; i < numberOfJobs ; ++ i )
148
+ for ( int i = 0 ; i < numberOfJobs ; ++ i )
143
149
{
144
150
var path = Path . GetTempFileName ( ) ;
145
151
File . Delete ( path ) ;
146
152
semaphoreFiles . Add ( path ) ;
147
-
153
+
148
154
var sharedTaskQueue = consumers [ 0 ] . TaskQueue ;
149
155
await sharedTaskQueue . Enqueue ( ( ) => TaskQueueTestFixture . WriteSemaphore ( path ) ) ;
150
156
}
@@ -175,25 +181,25 @@ public async Task AsyncFunc(string semaphoreFile)
175
181
{
176
182
// Wait to ensure async waiting is happening.
177
183
await Task . Delay ( 1000 ) ;
178
-
184
+
179
185
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , "async" ) ;
180
186
}
181
-
187
+
182
188
public async Task < string > AsyncFuncThatReturnsString ( string semaphoreFile )
183
189
{
184
190
// Wait to ensure async waiting is happening.
185
191
await Task . Delay ( 1000 ) ;
186
-
192
+
187
193
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , "async" ) ;
188
194
189
195
return "async" ;
190
196
}
191
-
197
+
192
198
public void NullableTypeFunc ( DateTime ? dateTime , string semaphoreFile )
193
199
{
194
200
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , dateTime ) ;
195
201
}
196
-
202
+
197
203
public void DateTimeFunc ( DateTime dateTime , string semaphoreFile )
198
204
{
199
205
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , dateTime ) ;
@@ -203,37 +209,37 @@ public void IntFunc(int num, string semaphoreFile)
203
209
{
204
210
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
205
211
}
206
-
212
+
207
213
public void NullableIntFunc ( int ? num , string semaphoreFile )
208
214
{
209
215
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ?? - 1 ) ;
210
216
}
211
-
217
+
212
218
public void LongFunc ( long num , string semaphoreFile )
213
219
{
214
220
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
215
221
}
216
-
222
+
217
223
public void FloatFunc ( float num , string semaphoreFile )
218
224
{
219
225
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
220
226
}
221
-
227
+
222
228
public void BoolFunc ( bool num , string semaphoreFile )
223
229
{
224
230
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
225
231
}
226
-
232
+
227
233
public void DoubleFunc ( double num , string semaphoreFile )
228
234
{
229
235
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
230
236
}
231
-
237
+
232
238
public void StringFunc ( string num , string semaphoreFile )
233
239
{
234
240
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
235
241
}
236
-
242
+
237
243
public void ObjectFunc ( object num , string semaphoreFile )
238
244
{
239
245
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
@@ -249,21 +255,26 @@ public void ExceptionFunc(Exception exc, string semaphoreFile)
249
255
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , exc ) ;
250
256
}
251
257
258
+ public void CancellationFunc ( CancellationToken ct , string semaphoreFile )
259
+ {
260
+ TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , ct ) ;
261
+ }
262
+
252
263
public void TypeFunc ( Type typeArg , string semaphoreFile )
253
264
{
254
265
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , typeArg ? . ToString ( ) ?? "null" ) ;
255
266
}
256
-
267
+
257
268
public void ArrayFunc1 ( string [ ] nums , string semaphoreFile )
258
269
{
259
270
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , string . Join ( "," , nums ) ) ;
260
271
}
261
-
272
+
262
273
public void ArrayFunc2 ( int [ ] nums , string semaphoreFile )
263
274
{
264
275
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , string . Join ( "," , nums ) ) ;
265
276
}
266
-
277
+
267
278
public void ArrayFunc3 ( int ? [ ] nums , string semaphoreFile )
268
279
{
269
280
var str = "" ;
@@ -274,7 +285,7 @@ public void ArrayFunc3(int?[] nums, string semaphoreFile)
274
285
str += num ? . ToString ( ) ?? "null" ;
275
286
first = false ;
276
287
}
277
-
288
+
278
289
TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , str ) ;
279
290
}
280
291
}
0 commit comments