2016-05-30 58 views
0

以下單元測試從未完成。爲什麼?TPL Dataflow not PropagateCompletion not working with predicated linkTos

[TestMethod] 
    public async Task TestFlow() 
    { 

     var transform1 = new TransformBlock<string, string>(async (s) => 
     { 
      await Task.Delay(1000); 

      return s; 
     }); 

     var transform2 = new TransformBlock<string, string>(async (s) => 
     { 
      await Task.Delay(1000); 
      Console.WriteLine(s); 
      return s; 
     },new ExecutionDataflowBlockOptions 
     { 

     }); 

     transform1.LinkTo(DataflowBlock.NullTarget<string>(), new DataflowLinkOptions { PropagateCompletion = true }, s => s == null); 
     transform1.LinkTo(transform2, new DataflowLinkOptions { PropagateCompletion = true },s => s != null); 


     transform1.Post("hello"); 
     transform1.Post(null); 
     transform1.Post("world"); 

     transform1.Complete(); 
     // await transform2.Completion; 

     while (!transform2.Completion.IsCompleted) 
     { 
      await Task.Delay(5000); 
     } 

    } 

回答

0

第二流需要清空它完成:

[TestMethod] 
    public async Task TestFlow() 
    { 

     var transform1 = new TransformBlock<string, string>(async (s) => 
     { 
      await Task.Delay(1000); 

      return s; 
     }); 

     var transform2 = new TransformBlock<string, string>(async (s) => 
     { 
      await Task.Delay(1000); 
      Console.WriteLine(s); 
      return s; 
     },new ExecutionDataflowBlockOptions 
     { 

     }); 

     transform1.LinkTo(DataflowBlock.NullTarget<string>(), new DataflowLinkOptions { PropagateCompletion = true }, s => s == null); 
     transform1.LinkTo(transform2, new DataflowLinkOptions { PropagateCompletion = true },s => s != null); 
     transform2.LinkTo(DataflowBlock.NullTarget<string>()); 

     transform1.Post("hello"); 
     transform1.Post(null); 
     transform1.Post("world"); 

     transform1.Complete(); 
     // await transform2.Completion; 

     while (!transform2.Completion.IsCompleted) 
     { 
      await Task.Delay(5000); 
     }