我认为在测试方法中,“results”集合变量必须是BlockingCollection<int>
类型而不是List<int>
。如果我错了,请向我证明。我从https://blog.stephencleary.com/2012/11/async-producerconsumer-queue-using.html
private static async Task Produce(BufferBlock<int> queue, IEnumerable<int> values)
{
foreach (var value in values)
{
await queue.SendAsync(value);
}
}
public async Task ProduceAll(BufferBlock<int> queue)
{
var producer1 = Produce(queue, Enumerable.Range(0, 10));
var producer2 = Produce(queue, Enumerable.Range(10, 10));
var producer3 = Produce(queue, Enumerable.Range(20, 10));
await Task.WhenAll(producer1, producer2, producer3);
queue.Complete();
}
[TestMethod]
public async Task ConsumerReceivesCorrectValues()
{
var results = new List<int>();
// Define the mesh.
var queue = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 5, });
//var consumerOptions = new ExecutionDataflowBlockOptions { BoundedCapacity = 1, };
var consumer = new ActionBlock<int>(x => results.Add(x), consumerOptions);
queue.LinkTo(consumer, new DataflowLinkOptions { PropagateCompletion = true, });
// Start the producers.
var producers = ProduceAll(queue);
// Wait for everything to complete.
await Task.WhenAll(producers, consumer.Completion);
// Ensure the consumer got what the producer sent.
Assert.IsTrue(results.OrderBy(x => x).SequenceEqual(Enumerable.Range(0, 30)));
}
答案 0 :(得分:4)
由于<div class="box-container">
<div class="box">
foo<br>foo<br>foo
</div>
<div class="box">
foo<br>foo
</div>
<div class="box">
foo
</div>
</div>
默认情况下将其委托限制为一次执行一次(ActionBlock<T>
MaxDegreeOfParallelism
),因此无需使用1
代替BlockingCollection<T>
。
正如预期的那样,代码中的测试对我来说很合适。
如果List<T>
通过了ActionBlock<T>
更高的选项,那么您需要保护MaxDegreeOfParallelism
或将其替换为List<T>
。