55using System . Linq . Expressions ;
66using System . Threading ;
77using System . Threading . Tasks ;
8+
89using FluentAssertions ;
10+
911using Gofer . NET . Utils ;
12+
1013using Xunit ;
1114
1215namespace Gofer . NET . Tests
@@ -22,8 +25,8 @@ public override string ToString()
2225 return Value ;
2326 }
2427 }
25-
26- private class CustomException : Exception { }
28+
29+ private class CustomException : Exception { }
2730
2831 [ Fact ]
2932 public async Task ItCapturesArgumentsPassedToEnqueuedDelegate ( )
@@ -40,13 +43,16 @@ public async Task ItCapturesArgumentsPassedToEnqueuedDelegate()
4043 Tuple . Create < Expression < Action > , string > (
4144 actionExp ,
4245 str ) ;
43-
46+
4447 // Action to expected result
4548 var delgates = new Tuple < Expression < Action > , string > [ ]
4649 {
4750 // Exception Argument
4851 TC ( ( ) => ExceptionFunc ( new Exception ( ) , semaphoreFile ) , new Exception ( ) . ToString ( ) ) ,
4952 TC ( ( ) => ExceptionFunc ( new CustomException ( ) , semaphoreFile ) , new CustomException ( ) . ToString ( ) ) ,
53+
54+ // Cancelation Argument
55+ TC ( ( ) => CancellationFunc ( default , semaphoreFile ) , new CancellationToken ( ) . ToString ( ) ) ,
5056
5157 // Integer Arguments
5258 TC ( ( ) => IntFunc ( int . MaxValue , semaphoreFile ) , int . MaxValue . ToString ( ) ) ,
@@ -99,29 +105,29 @@ public async Task ItCapturesArgumentsPassedToEnqueuedDelegate()
99105 TC ( ( ) => AsyncFunc ( semaphoreFile ) . T ( ) , "async" ) ,
100106 TC ( ( ) => AsyncFuncThatReturnsString ( semaphoreFile ) . T ( ) , "async" )
101107 } ;
102-
108+
103109
104110 foreach ( var tup in delgates )
105111 {
106112 var actionExpr = tup . Item1 ;
107113 var expectedString = tup . Item2 ;
108-
114+
109115 File . Delete ( semaphoreFile ) ;
110-
111- await testFixture . TaskQueue . Enqueue ( actionExpr ) ;
116+
117+ await testFixture . TaskQueue . Enqueue ( actionExpr ) ;
112118 await testFixture . TaskQueue . ExecuteNext ( ) ;
113119
114120 File . ReadAllText ( semaphoreFile ) . Should ( ) . Be ( expectedString ) ;
115121 }
116-
122+
117123 File . Delete ( semaphoreFile ) ;
118124 }
119-
125+
120126 [ Fact ]
121127 public async Task ItEnqueuesAndReceivesDelegatesThatAreRunnable ( )
122128 {
123129 var testFixture = new TaskQueueTestFixture ( nameof ( ItEnqueuesAndReceivesDelegatesThatAreRunnable ) ) ;
124-
130+
125131 testFixture . EnsureSemaphoreDoesntExist ( ) ;
126132 await testFixture . PushPopExecuteWriteSemaphore ( ) ;
127133 testFixture . EnsureSemaphore ( ) ;
@@ -133,18 +139,18 @@ public async Task ItsTasksAreConsumedOnlyOnceByMultipleConsumers()
133139 // Higher numbers here increase confidence
134140 var numberOfJobs = 16 ;
135141 var numberOfConsumers = 4 ;
136-
142+
137143 var sharedTaskQueueName = nameof ( ItsTasksAreConsumedOnlyOnceByMultipleConsumers ) ;
138144 var consumers = Enumerable . Range ( 0 , numberOfConsumers )
139145 . Select ( _ => new TaskQueueTestFixture ( sharedTaskQueueName ) ) . ToList ( ) ;
140146
141147 var semaphoreFiles = new List < string > ( ) ;
142- for ( int i = 0 ; i < numberOfJobs ; ++ i )
148+ for ( int i = 0 ; i < numberOfJobs ; ++ i )
143149 {
144150 var path = Path . GetTempFileName ( ) ;
145151 File . Delete ( path ) ;
146152 semaphoreFiles . Add ( path ) ;
147-
153+
148154 var sharedTaskQueue = consumers [ 0 ] . TaskQueue ;
149155 await sharedTaskQueue . Enqueue ( ( ) => TaskQueueTestFixture . WriteSemaphore ( path ) ) ;
150156 }
@@ -175,25 +181,25 @@ public async Task AsyncFunc(string semaphoreFile)
175181 {
176182 // Wait to ensure async waiting is happening.
177183 await Task . Delay ( 1000 ) ;
178-
184+
179185 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , "async" ) ;
180186 }
181-
187+
182188 public async Task < string > AsyncFuncThatReturnsString ( string semaphoreFile )
183189 {
184190 // Wait to ensure async waiting is happening.
185191 await Task . Delay ( 1000 ) ;
186-
192+
187193 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , "async" ) ;
188194
189195 return "async" ;
190196 }
191-
197+
192198 public void NullableTypeFunc ( DateTime ? dateTime , string semaphoreFile )
193199 {
194200 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , dateTime ) ;
195201 }
196-
202+
197203 public void DateTimeFunc ( DateTime dateTime , string semaphoreFile )
198204 {
199205 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , dateTime ) ;
@@ -203,37 +209,37 @@ public void IntFunc(int num, string semaphoreFile)
203209 {
204210 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
205211 }
206-
212+
207213 public void NullableIntFunc ( int ? num , string semaphoreFile )
208214 {
209215 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ?? - 1 ) ;
210216 }
211-
217+
212218 public void LongFunc ( long num , string semaphoreFile )
213219 {
214220 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
215221 }
216-
222+
217223 public void FloatFunc ( float num , string semaphoreFile )
218224 {
219225 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
220226 }
221-
227+
222228 public void BoolFunc ( bool num , string semaphoreFile )
223229 {
224230 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
225231 }
226-
232+
227233 public void DoubleFunc ( double num , string semaphoreFile )
228234 {
229235 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
230236 }
231-
237+
232238 public void StringFunc ( string num , string semaphoreFile )
233239 {
234240 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
235241 }
236-
242+
237243 public void ObjectFunc ( object num , string semaphoreFile )
238244 {
239245 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , num ) ;
@@ -249,21 +255,26 @@ public void ExceptionFunc(Exception exc, string semaphoreFile)
249255 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , exc ) ;
250256 }
251257
258+ public void CancellationFunc ( CancellationToken ct , string semaphoreFile )
259+ {
260+ TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , ct ) ;
261+ }
262+
252263 public void TypeFunc ( Type typeArg , string semaphoreFile )
253264 {
254265 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , typeArg ? . ToString ( ) ?? "null" ) ;
255266 }
256-
267+
257268 public void ArrayFunc1 ( string [ ] nums , string semaphoreFile )
258269 {
259270 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , string . Join ( "," , nums ) ) ;
260271 }
261-
272+
262273 public void ArrayFunc2 ( int [ ] nums , string semaphoreFile )
263274 {
264275 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , string . Join ( "," , nums ) ) ;
265276 }
266-
277+
267278 public void ArrayFunc3 ( int ? [ ] nums , string semaphoreFile )
268279 {
269280 var str = "" ;
@@ -274,7 +285,7 @@ public void ArrayFunc3(int?[] nums, string semaphoreFile)
274285 str += num ? . ToString ( ) ?? "null" ;
275286 first = false ;
276287 }
277-
288+
278289 TaskQueueTestFixture . WriteSemaphoreValue ( semaphoreFile , str ) ;
279290 }
280291 }
0 commit comments