55using System . Collections . Concurrent ;
66using System . Collections . Generic ;
77using System . Threading . Tasks ;
8+ using FluentAssertions ;
89using WouterVanRanst . Utils . Collections ;
910
1011public class TaskCompletionBufferCoyoteTests
@@ -70,7 +71,7 @@ public static async Task TestConcurrentAddAndCompleteAdding()
7071 /// Verifies that tasks added after CompleteAdding is called throw exceptions
7172 /// and that existing tasks are processed correctly.
7273 /// </summary>
73- [ Test ]
74+ [ Fact ]
7475 public static async Task TestAddAfterCompleteAddingThrows ( )
7576 {
7677 await RunCoyoteTest ( async ( buffer ) =>
@@ -87,48 +88,48 @@ await RunCoyoteTest(async (buffer) =>
8788 exceptionThrown = true ;
8889 }
8990
90- Assert ( exceptionThrown , "Add after CompleteAdding should throw" ) ;
91+ Assert . True ( exceptionThrown , "Add after CompleteAdding should throw" ) ;
9192 } ) ;
9293 }
9394
94- ///// <summary>
95- ///// Ensures all tasks are processed exactly once even with multiple consumers.
96- ///// </summary>
97- //[Test ]
98- // public static async Task TestMultipleConsumersProcessAllTasks()
99- // {
100- // await RunCoyoteTest(async (buffer) =>
101- // {
102- // int numTasks = 20;
103- // var pendingTasks = new List<TaskCompletionSource<int>>();
95+ /// <summary>
96+ /// Ensures all tasks are processed exactly once even with multiple consumers.
97+ /// </summary>
98+ [ Fact ]
99+ public static async Task TestMultipleConsumersProcessAllTasks ( )
100+ {
101+ await RunCoyoteTest ( async ( buffer ) =>
102+ {
103+ int numTasks = 20 ;
104+ var pendingTasks = new List < TaskCompletionSource < int > > ( ) ;
104105
105- // for (int i = 0; i < numTasks; i++)
106- // {
107- // var tcs = new TaskCompletionSource<int>();
108- // buffer.Add(tcs.Task);
109- // pendingTasks.Add(tcs);
110- // }
106+ for ( int i = 0 ; i < numTasks ; i ++ )
107+ {
108+ var tcs = new TaskCompletionSource < int > ( ) ;
109+ buffer . Add ( tcs . Task ) ;
110+ pendingTasks . Add ( tcs ) ;
111+ }
111112
112- // buffer.CompleteAdding();
113+ buffer . CompleteAdding ( ) ;
113114
114- // var results = new ConcurrentBag<int>();
115- // var consumer1 = ConsumeAsync(buffer, results);
116- // var consumer2 = ConsumeAsync(buffer, results);
115+ var results = new ConcurrentBag < int > ( ) ;
116+ var consumer1 = ConsumeAsync ( buffer , results ) ;
117+ var consumer2 = ConsumeAsync ( buffer , results ) ;
117118
118- // // Complete tasks in random order
119- // var random = new Random();
120- // while (pendingTasks.Count > 0)
121- // {
122- // int index = random.Next(pendingTasks.Count);
123- // pendingTasks[index].SetResult(pendingTasks.Count);
124- // pendingTasks.RemoveAt(index);
125- // }
119+ // Complete tasks in random order
120+ var random = new Random ( ) ;
121+ while ( pendingTasks . Count > 0 )
122+ {
123+ int index = random . Next ( pendingTasks . Count ) ;
124+ pendingTasks [ index ] . SetResult ( pendingTasks . Count ) ;
125+ pendingTasks . RemoveAt ( index ) ;
126+ }
126127
127- // await Task.WhenAll(consumer1, consumer2);
128+ await Task . WhenAll ( consumer1 , consumer2 ) ;
128129
129- // Assert( results.Count == numTasks , $"Expected {numTasks} results, got {results.Count}");
130- // });
131- // }
130+ numTasks . Should ( ) . Be ( results . Count , $ "Expected { numTasks } results, got { results . Count } ") ;
131+ } ) ;
132+ }
132133
133134 ///// <summary>
134135 ///// Validates that the buffer handles the completion order correctly when
@@ -177,13 +178,13 @@ private static async Task RunCoyoteTest(Func<TaskCompletionBuffer<int>, Task> te
177178 Assert . Equal ( 0 , testResult . NumOfFoundBugs ) ;
178179 }
179180
180- // private static async Task ConsumeAsync(TaskCompletionBuffer<int> buffer, ConcurrentBag<int> results)
181- // {
182- // await foreach (var task in buffer.GetConsumingEnumerable())
183- // {
184- // results.Add(await task);
185- // }
186- // }
181+ private static async Task ConsumeAsync ( TaskCompletionBuffer < int > buffer , ConcurrentBag < int > results )
182+ {
183+ await foreach ( var task in buffer . GetConsumingEnumerable ( ) )
184+ {
185+ results . Add ( await task ) ;
186+ }
187+ }
187188}
188189
189190
0 commit comments