@@ -5,12 +5,10 @@ public class TaskExecutorTests
55 [ Fact ]
66 public async Task EnqueueTask_ShouldExecuteTasksConcurrently ( )
77 {
8- // Arrange
98 var cts = new CancellationTokenSource ( ) ;
109 var executor = new TaskExecutor ( 3 , cts . Token ) ;
1110 int completedTasks = 0 ;
1211
13- // Act
1412 for ( int i = 0 ; i < 10 ; i ++ )
1513 {
1614 executor . EnqueueTask ( i , async ( ) =>
@@ -20,27 +18,24 @@ public async Task EnqueueTask_ShouldExecuteTasksConcurrently()
2018 } ) ;
2119 }
2220
23- await Task . Delay ( 500 ) ; // Allow time for tasks to start
21+ await Task . Delay ( 500 ) ;
2422
2523 while ( executor . HasRunningTasks )
2624 {
2725 await Task . Delay ( 100 ) ;
2826 }
2927 await executor . StopAsync ( ) ;
3028
31- // Assert
3229 Assert . Equal ( 10 , completedTasks ) ;
3330 }
3431
3532 [ Fact ]
3633 public async Task ChangeConcurrency_ShouldAdjustConcurrencyLevel ( )
3734 {
38- // Arrange
3935 var cts = new CancellationTokenSource ( ) ;
4036 var executor = new TaskExecutor ( 1 , cts . Token ) ;
4137 int runningTasks = 0 ;
4238
43- // Act
4439 for ( int i = 0 ; i < 5 ; i ++ )
4540 {
4641 executor . EnqueueTask ( i , async ( ) =>
@@ -51,13 +46,12 @@ public async Task ChangeConcurrency_ShouldAdjustConcurrencyLevel()
5146 } ) ;
5247 }
5348
54- await Task . Delay ( 100 ) ; // Allow initial tasks to start
49+ await Task . Delay ( 100 ) ;
5550 Assert . Equal ( 1 , runningTasks ) ;
5651
5752 executor . ChangeConcurrency ( 3 ) ;
58- await Task . Delay ( 100 ) ; // Allow new concurrency level to take effect
53+ await Task . Delay ( 100 ) ;
5954
60- // Assert
6155 Assert . True ( runningTasks >= 2 ) ;
6256
6357 await executor . StopAsync ( ) ;
@@ -66,27 +60,23 @@ public async Task ChangeConcurrency_ShouldAdjustConcurrencyLevel()
6660 [ Fact ]
6761 public async Task OnTaskError_ShouldCaptureTaskExceptions ( )
6862 {
69- // Arrange
7063 var cts = new CancellationTokenSource ( ) ;
7164 var executor = new TaskExecutor ( 3 , cts . Token ) ;
7265 Exception ? capturedException = null ;
7366
7467 executor . OnTaskError += ( id , ex ) => capturedException = ex ;
7568
76- // Act
7769 executor . EnqueueTask ( 1 , ( ) => throw new InvalidOperationException ( "Test exception" ) ) ;
78- await Task . Delay ( 200 ) ; // Allow time for the task to fail
70+ await Task . Delay ( 200 ) ;
7971 await executor . StopAsync ( ) ;
8072
81- // Assert
8273 Assert . NotNull ( capturedException ) ;
8374 Assert . IsType < InvalidOperationException > ( capturedException ) ;
8475 }
8576
8677 [ Fact ]
8778 public async Task StopAsync_ShouldWaitForRunningTasksToComplete ( )
8879 {
89- // Arrange
9080 var cts = new CancellationTokenSource ( ) ;
9181 var executor = new TaskExecutor ( 3 , cts . Token ) ;
9282 bool taskCompleted = false ;
@@ -97,11 +87,37 @@ public async Task StopAsync_ShouldWaitForRunningTasksToComplete()
9787 taskCompleted = true ;
9888 } ) ;
9989
100- // Act
101- await Task . Delay ( 100 ) ; // Allow time for the task to start
90+ await Task . Delay ( 100 ) ;
10291 await executor . StopAsync ( ) ;
10392
104- // Assert
10593 Assert . True ( taskCompleted ) ;
10694 }
95+
96+ [ Fact ]
97+ public void QueuePollingDelayMilliseconds_Setter_ShouldThrowOnNegative ( )
98+ {
99+ var executor = new TaskExecutor ( 1 , CancellationToken . None ) ;
100+ Assert . Throws < ArgumentException > ( ( ) => executor . QueuePollingDelayMilliseconds = - 1 ) ;
101+ }
102+
103+ [ Fact ]
104+ public void Constructor_ShouldThrowOnNegativeQueuePollingDelay ( )
105+ {
106+ Assert . Throws < ArgumentException > ( ( ) => new TaskExecutor ( 1 , - 10 ) ) ;
107+ }
108+
109+ [ Fact ]
110+ public void Constructor_ShouldThrowOnZeroOrNegativeConcurrency ( )
111+ {
112+ Assert . Throws < ArgumentException > ( ( ) => new TaskExecutor ( 0 , CancellationToken . None ) ) ;
113+ Assert . Throws < ArgumentException > ( ( ) => new TaskExecutor ( - 1 , CancellationToken . None ) ) ;
114+ }
115+
116+ [ Fact ]
117+ public void ChangeConcurrency_ShouldThrowOnZeroOrNegative ( )
118+ {
119+ var executor = new TaskExecutor ( 1 , CancellationToken . None ) ;
120+ Assert . Throws < ArgumentException > ( ( ) => executor . ChangeConcurrency ( 0 ) ) ;
121+ Assert . Throws < ArgumentException > ( ( ) => executor . ChangeConcurrency ( - 5 ) ) ;
122+ }
107123}
0 commit comments