public void Should_consume_with_respect_slots_order_results_completed_by_different_threads() { var expectedResults = Enumerable.Range(1, 10).ToArray(); var producer = Task.Run( () => { var random = new Random(); for (var i = 0; i < 10; i++) { var slot = boundedResultsQueue.AcquireFreeResultOrWait(); output.WriteLine($"{DateTime.Now.TimeOfDay}. producer thrd {Thread.CurrentThread.ManagedThreadId}. acquired slot #{i}"); var result = i; Task.Delay(random.Next(100, 301)) .ContinueWith( _ => { output.WriteLine($"{DateTime.Now.TimeOfDay}. worker thrd {Thread.CurrentThread.ManagedThreadId}. complete result #{result} as {result + 1}"); slot.SetResult(result + 1); }); } output.WriteLine($"{DateTime.Now.TimeOfDay}. producer thrd {Thread.CurrentThread.ManagedThreadId} stopped."); }); List <int> actual = null; var consumer = Task.Factory.StartNew( () => { output.WriteLine($"{DateTime.Now.TimeOfDay}. consumer thrd {Thread.CurrentThread.ManagedThreadId} started."); var results = new List <int>(); foreach (var result in boundedResultsQueue.DequeResultsOnCompletion().Take(10)) { output.WriteLine($"{DateTime.Now.TimeOfDay}. consumer thrd {Thread.CurrentThread.ManagedThreadId}. consume result {result}"); results.Add(result); } actual = results; output.WriteLine($"{DateTime.Now.TimeOfDay}. consumer thrd {Thread.CurrentThread.ManagedThreadId} stopped."); }, TaskCreationOptions.LongRunning); var task = Task.WhenAll(producer, consumer); task.Wait(5.Seconds()); task.IsCompleted.Should().BeTrue(); actual.Should().BeEquivalentTo(expectedResults); }
public void Should_return_free_slot_when_queue_is_empty() { var slot = boundedResultsQueue.AcquireFreeResultOrWait(); slot.Should().NotBeNull(); }