File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
test/AsyncWorkerCollection.Tests/Reentrancy Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
3
+ using System . Threading . Tasks ;
4
+ using dotnetCampus . Threading . Reentrancy ;
5
+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
6
+ using MSTest . Extensions . Contracts ;
7
+
8
+ namespace AsyncWorkerCollection . Tests . Reentrancy
9
+ {
10
+ [ TestClass ]
11
+ public class QueueReentrancyTaskTests
12
+ {
13
+ [ ContractTestCase ]
14
+ public void InvokeAsync ( )
15
+ {
16
+ "并发执行大量任务,最终返回值按顺序获取到。" . Test ( async ( ) =>
17
+ {
18
+ // Arrange
19
+ var concurrentCount = 100 ;
20
+ var resultList = new List < int > ( ) ;
21
+ var reentrancy = new QueueReentrancyTask < int , int > ( async i =>
22
+ {
23
+ await Task . Delay ( 10 ) . ConfigureAwait ( false ) ;
24
+ resultList . Add ( i ) ;
25
+ return i ;
26
+ } ) ;
27
+
28
+ // Action
29
+ await Task . WhenAll ( Enumerable . Range ( 0 , concurrentCount ) . Select ( i => reentrancy . InvokeAsync ( i ) ) ) ;
30
+
31
+ // Assert
32
+ for ( var i = 0 ; i < concurrentCount ; i ++ )
33
+ {
34
+ Assert . AreEqual ( i , resultList [ i ] ) ;
35
+ }
36
+ } ) ;
37
+ }
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments