我有一个奇怪的死锁问题,简单的数据流 - 转换块和动作块。变换块应等待第一个项目,直到第二个项目进入操作块。基本上我想测试一下,如果一个项目阻塞某些外部资源,那么其他项目应继续流动。但它似乎无法正常工作。
[Fact]
public void ProcessAction_FirstWaitsForTheSecond_DataflowEndedOnlyAfterBothProcessed ()
{
// arrange
var second_item_finished = new SemaphoreSlim (0, 1);
var completed_items = new ConcurrentBag<string> ();
var starting_block = new TransformBlock<string, string>
(x =>
{
if (x == "1")
second_item_finished.Wait ();
return x;
},
new ExecutionDataflowBlockOptions
{
BoundedCapacity = 100,
MaxDegreeOfParallelism = 4
});
var ending_block = new ActionBlock<string>
(x =>
{
completed_items.Add (x);
if (x == "2")
second_item_finished.Release ();
},
new ExecutionDataflowBlockOptions
{
BoundedCapacity = 100,
MaxDegreeOfParallelism = 4
});
starting_block.LinkTo (ending_block, new DataflowLinkOptions { PropagateCompletion = true });
// act
foreach (var data in new[] { "1", "2" })
{
if (!starting_block.SendAsync (data).Wait (1000))
throw new TimeoutException ();
}
starting_block.Complete ();
// timeout here
if (!Task.WaitAll (new[] { starting_block.Completion, ending_block.Completion }, 1000))
throw new TimeoutException ();
// assert
completed_items.Should ().Equal ("2", "1");
}
我在&#34; WaitAll&#34;线。