-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlockAsyncBasicTests.cs
More file actions
749 lines (643 loc) · 26.9 KB
/
BlockAsyncBasicTests.cs
File metadata and controls
749 lines (643 loc) · 26.9 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
using System.Linq.Expressions;
using System.Reflection;
using Hyperbee.Expressions.Tests.TestSupport;
using static System.Linq.Expressions.Expression;
using static Hyperbee.Expressions.ExpressionExtensions;
namespace Hyperbee.Expressions.Tests;
[TestClass]
public class BlockAsyncBasicTests
{
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAwaitSuccessfully_WithCompletedTask( CompleterType completer, CompilerType compiler )
{
// Arrange
var block = BlockAsync(
Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 1 )
) )
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
//Assert
Assert.AreEqual( 1, result );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAwaitMultipleSuccessfully_WithCompletedTask( CompleterType completer, CompilerType compiler )
{
// Arrange
var block = BlockAsync(
Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 1 )
) ),
Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 2 )
) )
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
//Assert
Assert.AreEqual( 2, result );
}
[TestMethod]
[DataRow( CompilerType.Fast )]
[DataRow( CompilerType.System )]
[DataRow( CompilerType.Interpret )]
public async Task BlockAsync_ShouldAwaitSuccessfully_WithDelayedTask( CompilerType compiler )
{
// Arrange
var delayedTask = Task.Delay( 100 ).ContinueWith( _ => 5 );
var block = BlockAsync( Await( Constant( delayedTask ) ) );
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 5, result );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAwaitMultipleTasks_WithDifferentResults( CompleterType completer, CompilerType compiler )
{
// Arrange
var block = BlockAsync(
Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 1 )
) ),
Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 2 )
) )
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 2, result ); // Last awaited result should be returned
}
[TestMethod]
[DataRow( CompilerType.Fast )]
[DataRow( CompilerType.System )]
[DataRow( CompilerType.Interpret )]
public async Task BlockAsync_ShouldThrowException_WithFaultedTask( CompilerType compiler )
{
// Arrange
var block = BlockAsync(
Await( Constant( Task.FromException<int>( new InvalidOperationException( "Test Exception" ) ) ) )
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act & Assert
await Assert.ThrowsExactlyAsync<InvalidOperationException>( async () => await compiledLambda() );
}
[TestMethod]
[DataRow( CompilerType.Fast )]
[DataRow( CompilerType.System )]
[DataRow( CompilerType.Interpret )]
public async Task BlockAsync_ShouldHandleCanceledTask_WithCancellation( CompilerType compiler )
{
// Arrange
var cancellationTokenSource = new CancellationTokenSource();
await cancellationTokenSource.CancelAsync();
var canceledTask = Task.FromCanceled<int>( cancellationTokenSource.Token );
var block = BlockAsync( Await( Constant( canceledTask ) ) );
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act & Assert
await Assert.ThrowsExactlyAsync<TaskCanceledException>( async () => await compiledLambda() );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAwaitNestedBlockAsync_WithNestedTasks( CompleterType completer, CompilerType compiler )
{
// Arrange
var innerBlock = BlockAsync(
Await( AsyncHelper.Completer( Constant( completer ), Constant( 2 ) ) )
);
var block = BlockAsync(
Await( AsyncHelper.Completer( Constant( completer ), Constant( 1 ) ) ),
Await( innerBlock )
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 2, result );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAwaitNestedBlockAsync_WithNestedLambdas( CompleterType completer, CompilerType compiler )
{
// Arrange
var innerBlock = Lambda<Func<Task<int>>>(
BlockAsync(
Await( AsyncHelper.Completer( Constant( completer ), Constant( 2 ) ) )
)
);
var block = BlockAsync(
Await( AsyncHelper.Completer( Constant( completer ), Constant( 1 ) ) ),
Await( Invoke( innerBlock ) )
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 2, result );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldShareVariablesBetweenBlocks_WithNestedAwaits( CompleterType completer, CompilerType compiler )
{
// Arrange
var var1 = Variable( typeof( int ), "var1" );
var var2 = Variable( typeof( int ), "var2" );
// uses variables from both outer blocks
var mostInnerBlock = Lambda<Func<Task>>(
BlockAsync(
Assign( var1, Add( var1, var2 ) ),
Await( AsyncHelper.Completer(
Constant( completer )
) )
) );
var innerBlock = Lambda<Func<Task<int>>>(
BlockAsync(
[var2], // in scoped here and most inner block
Assign( var2, Add( var1, Constant( 1 ) ) ),
Await( Invoke( mostInnerBlock ) ),
var1
) );
var block = BlockAsync(
[var1], // in scope for all blocks
Assign( var1, Constant( 3 ) ),
Await( Invoke( innerBlock ) )
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 7, result );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldHandleSyncAndAsync_WithMixedOperations( CompleterType completer, CompilerType compiler )
{
// Arrange
var block = BlockAsync(
Constant( 10 ),
Await( AsyncHelper.Completer( Constant( completer ), Constant( 20 ) ) )
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 20, result ); // The last operation is asynchronous and returns 20
}
[TestMethod]
[DataRow( CompilerType.Fast )]
[DataRow( CompilerType.System )]
[DataRow( CompilerType.Interpret )]
public async Task BlockAsync_ShouldAwaitSuccessfully_WithTask( CompilerType compiler )
{
// Arrange
var block = BlockAsync( Await( Constant( Task.CompletedTask, typeof( Task ) ) ) );
var lambda = Lambda<Func<Task>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
await compiledLambda(); // Should complete without exception
// Assert
// If no exception, the test is successful
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAwaitSuccessfully_WithReturnLabel( CompleterType completer, CompilerType compiler )
{
// Arrange
var returnLabel = Label( typeof( int ) );
var block = BlockAsync(
IfThenElse(
Constant( true ),
Return( returnLabel, Await( AsyncHelper.Completer( Constant( completer ), Constant( 10 ) ) ) ),
Return( returnLabel, Await( AsyncHelper.Completer( Constant( completer ), Constant( 20 ) ) ) )
),
Label( returnLabel, Constant( 30 ) )
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda(); // Should complete without exception
// Assert
Assert.AreEqual( 10, result ); // The last operation is asynchronous and returns 10
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldPreserveVariableAssignment_BeforeAndAfterAwait( CompleterType completer, CompilerType compiler )
{
// Arrange
var resultValue = Parameter( typeof( int ), "result" );
var block = BlockAsync(
[resultValue],
Assign( resultValue, Constant( 5 ) ),
Await( AsyncHelper.Completer(
Constant( completer )
) ),
Assign( resultValue, Add( resultValue, Constant( 10 ) ) ),
resultValue // Return the result
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 15, result );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldPreserveVariablesInNestedBlock_WithAwaits( CompleterType completer, CompilerType compiler )
{
// Arrange
// NOTE: this test is also verifying the hoisting visitor and reuse of names
var outerValue = Parameter( typeof( int ), "value" );
var innerValue = Parameter( typeof( string ), "value" );
var otherValue1 = Variable( typeof( int ), "value" );
var otherValue2 = Variable( typeof( string ), "value" );
var otherValue3 = Variable( typeof( string ), "value" );
Expression<Func<string, int>> test = s => int.Parse( s );
var innerBlock = Lambda<Func<string, Task<string>>>(
BlockAsync(
[otherValue1, otherValue2],
Block(
Assign( otherValue1, Condition( Constant( false ), Constant( 100 ), Block( Constant( 150 ) ) ) ),
Assign( otherValue2, Constant( "200" ) ),
Await( AsyncHelper.Completer( Constant( completer ) ) ),
innerValue ),
Condition( Constant( false ), Assign( innerValue, Constant( "200" ) ), Assign( otherValue2, Constant( "250" ) ) )
),
parameters: [innerValue]
);
var block = BlockAsync(
[otherValue3],
Assign( otherValue3, Constant( "300" ) ),
Assign( outerValue,
Add( outerValue,
Invoke( test, Await( Invoke( innerBlock, otherValue3 ) ) ) ) ),
outerValue
);
var lambda = Lambda<Func<int, Task<int>>>( block, outerValue );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda( 5 );
// Assert
Assert.AreEqual( 255, result );
}
[TestMethod]
[DataRow( CompilerType.Fast )]
[DataRow( CompilerType.System )]
[DataRow( CompilerType.Interpret )]
public async Task BlockAsync_ShouldPreserveVariables_WithNestedAwaits( CompilerType compiler )
{
Expression<Func<Task<int>>> initVariableAsync = () => Task.FromResult( 5 );
Expression<Func<Task<bool>>> isTrueAsync = () => Task.FromResult( true );
Expression<Func<int, int, Task<int>>> addAsync = ( a, b ) => Task.FromResult( a + b );
var variable = Variable( typeof( int ), "variable" );
var asyncBlock = BlockAsync(
[variable],
Assign(
variable,
Await( Invoke( initVariableAsync ) )
),
IfThen(
Await( Invoke( isTrueAsync ) ),
Assign(
variable,
Await( Invoke( addAsync, variable, variable ) )
)
),
variable
);
var lambda = (Lambda<Func<Task<int>>>( asyncBlock ).Reduce() as Expression<Func<Task<int>>>)!;
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 10, result );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAllowLambdaParameters( CompleterType completer, CompilerType compiler )
{
var var1 = Variable( typeof( int ), "var1" );
var param1 = Parameter( typeof( Func<Task<int>> ), "param1" );
var asyncBlock = BlockAsync(
Await(
Invoke(
Lambda<Func<Func<Task<int>>, Task<int>>>(
BlockAsync(
[var1],
Assign( var1, Await( Invoke( param1 ) ) ),
var1
),
parameters: [param1]
),
Lambda<Func<Task<int>>>(
BlockAsync(
Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 15 ) )
)
)
)
)
)
);
var lambda = Lambda<Func<Task<int>>>( asyncBlock );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 15, result );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAllowParallelBlocks_WithTaskWhenAll( CompleterType completer, CompilerType compiler )
{
// Arrange
var tracker = Variable( typeof( int[] ), "tracker" );
var temp = Variable( typeof( int ), "temp" );
var tasks = Variable( typeof( List<Task> ), "tasks" );
var i = Variable( typeof( int ), "i" );
var taskRun = Call(
typeof( Task ).GetMethod( nameof( Task.Run ), [typeof( Func<Task> )] )!,
Lambda<Func<Task>>(
BlockAsync(
Await( AsyncHelper.Completer( Constant( completer ) ) ),
Assign( ArrayAccess( tracker, temp ), temp )
)
)
);
const int threadCount = 5;
var initIncrement = Assign( i, Constant( 0 ) );
var condition = LessThan( i, Constant( threadCount ) );
var iteration = PostIncrementAssign( i );
var block = BlockAsync(
[tracker, tasks],
Assign( tracker, NewArrayBounds( typeof( int ), Constant( threadCount ) ) ),
Assign( tasks, New( typeof( List<Task> ).GetConstructors()[0] ) ),
For( [i], initIncrement, condition, iteration,
Block(
[temp],
Assign( temp, i ),
Call( tasks, typeof( List<Task> ).GetMethod( nameof( List<Task>.Add ) )!, taskRun )
)
),
Await( Call( typeof( Task ).GetMethod( nameof( Task.WhenAll ), [typeof( IEnumerable<Task> )] )!, tasks ) ),
tracker
);
var lambda = Lambda<Func<Task<int[]>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.HasCount( threadCount, result );
for ( var tC = 0; tC < threadCount; tC++ )
{
Assert.AreEqual( tC, result[tC] );
}
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAllowNestedBlocks_WithoutAsync( CompleterType completer, CompilerType compiler )
{
// Arrange
var innerVar = Variable( typeof( int ), "innerVar" );
var middleVar = Variable( typeof( int ), "middleVar" );
var outerVar = Variable( typeof( int ), "outerVar" );
var blockAsync = BlockAsync(
[outerVar],
Block(
[middleVar],
Await(
BlockAsync(
[innerVar],
Assign( outerVar, Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 3 )
) ) ),
Assign( innerVar, Constant( 1 ) ),
Assign( middleVar, Constant( 2 ) )
)
),
Assign( middleVar, Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 4 )
) ) ),
Assign( outerVar, Add( outerVar, middleVar ) )
),
Await(
AsyncHelper.Completer( Constant( completer ) )
),
outerVar
);
// Act
var lambda = Lambda<Func<Task<int>>>( blockAsync );
var compiledLambda = lambda.Compile( compiler );
var result = await compiledLambda();
// Assert
Assert.AreEqual( 7, result );
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Immediate, CompilerType.Interpret )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Interpret )]
public async Task BlockAsync_ShouldAwaitSuccessfully_WithBlockConditional( CompleterType completer, CompilerType compiler )
{
// Arrange
var block = BlockAsync(
Block(
Constant( 5 ),
Condition( Constant( true ),
Block(
Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 1 )
) ),
Constant( 2 )
),
Constant( 0 )
)
)
);
var lambda = Lambda<Func<Task<int>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.AreEqual( 2, result );
}
[TestMethod]
[DataRow( CompilerType.Fast )]
[DataRow( CompilerType.System )]
[DataRow( CompilerType.Interpret )]
public async Task BlockAsync_ShouldHandleValueTypeToObjectAssignment_WithBoxing( CompilerType compiler )
{
// Arrange: assign value-type (int) to variable before await
// Reproduces: https://github.com/Stillpoint-Software/hyperbee.expressions/issues/126
var variable = Variable( typeof( int ) );
var block = BlockAsync(
[variable],
Assign( variable, Constant( 0 ) ),
Await( Constant( Task.FromResult( new object() ) ) )
);
var lambda = Lambda<Func<Task<object>>>( block );
var compiledLambda = lambda.Compile( compiler );
// Act
var result = await compiledLambda();
// Assert
Assert.IsNotNull( result );
}
public class TestContext
{
public int Id { get; set; }
}
private sealed class Disposable( Action dispose ) : IDisposable
{
public static readonly ConstructorInfo ConstructorInfo = typeof( Disposable ).GetConstructors()[0];
private int _disposed;
private Action Disposer { get; } = dispose;
public void Dispose()
{
if ( Interlocked.CompareExchange( ref _disposed, 1, 0 ) == 0 )
Disposer.Invoke();
}
}
[TestMethod]
[DataRow( CompleterType.Immediate, CompilerType.Fast )]
[DataRow( CompleterType.Immediate, CompilerType.System )]
[DataRow( CompleterType.Deferred, CompilerType.Fast )]
[DataRow( CompleterType.Deferred, CompilerType.System )]
public async Task BlockAsync_ShouldAwaitSuccessfully_WithNestedLambdaArgument( CompleterType completer, CompilerType compiler )
{
// Arrange
var control = Constant( new TestContext() );
var idVariable = Variable( typeof( int ), "originalId" );
var idProperty = Property( control, "Id" );
var exception = Variable( typeof( Exception ), "exception" );
var disposableBlock = Block(
[idVariable],
Assign( idVariable, idProperty ),
TryCatch(
Block(
Assign( idProperty, Constant( 10 ) ),
New( Disposable.ConstructorInfo,
Lambda<Action>(
Block(
Assign( idProperty, idVariable )
)
) )
),
Catch(
exception,
Block(
[exception],
Assign( idProperty, idVariable ),
Throw( exception, typeof( Disposable ) )
)
)
)
);
// Act
var lambda = Lambda<Func<Task<int>>>( BlockAsync(
Using( disposableBlock,
Await( AsyncHelper.Completer(
Constant( completer ),
Constant( 3 )
) ) )
) );
var compiledLambda = lambda.Compile( compiler );
var result = await compiledLambda();
// Assert
Assert.AreEqual( 3, result );
}
}