本文整理汇总了C#中BufferBlock.PostItems方法的典型用法代码示例。如果您正苦于以下问题:C# BufferBlock.PostItems方法的具体用法?C# BufferBlock.PostItems怎么用?C# BufferBlock.PostItems使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BufferBlock
的用法示例。
在下文中一共展示了BufferBlock.PostItems方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestReceive_NotYetAvailable
public async Task TestReceive_NotYetAvailable()
{
var buffer = new BufferBlock<int>();
// The following test is racy, but just in terms
// of what's being tested. The test should always succeed
// regardless, but we might actually be testing receiving
// an already-available value rather than one not yet available.
var ignored = Task.Run(() => buffer.Post(1));
Assert.Equal(expected: 1, actual: buffer.Receive());
ignored = Task.Run(() => buffer.Post(2));
Assert.Equal(expected: 2, actual: buffer.Receive(new CancellationTokenSource().Token));
ignored = Task.Run(() => buffer.Post(3));
Assert.Equal(expected: 3, actual: buffer.Receive(TimeSpan.FromDays(1)));
ignored = Task.Run(() => buffer.Post(4));
Assert.Equal(expected: 4, actual: buffer.Receive(TimeSpan.FromDays(1), new CancellationTokenSource().Token));
// The following are non-racy.
var t1 = buffer.ReceiveAsync();
var t2 = buffer.ReceiveAsync(new CancellationTokenSource().Token);
var t3 = buffer.ReceiveAsync(TimeSpan.FromDays(1));
var t4 = buffer.ReceiveAsync(TimeSpan.FromDays(1), new CancellationTokenSource().Token);
Assert.False(t1.IsCompleted);
Assert.False(t2.IsCompleted);
Assert.False(t3.IsCompleted);
Assert.False(t4.IsCompleted);
buffer.PostItems(3, 4, 5, 6);
Assert.Equal(expected: 3, actual: await t1);
Assert.Equal(expected: 4, actual: await t2);
Assert.Equal(expected: 5, actual: await t3);
Assert.Equal(expected: 6, actual: await t4);
}
示例2: TestAsObservableAndAsObserver_DataPropagation
public async Task TestAsObservableAndAsObserver_DataPropagation()
{
// Test that preset data flows correctly
{
var bb = new BufferBlock<int>();
bb.PostRange(0, 2);
bb.Complete();
int nextValueExpected = 0;
var ab = new ActionBlock<int>(i => {
Assert.True(i == nextValueExpected, string.Format("Expected next value to be {0} but got {1}", nextValueExpected, i));
nextValueExpected++;
});
bb.AsObservable().Subscribe(ab.AsObserver());
await ab.Completion;
}
// Test that new data flows correctly
{
int nextValueExpected = -2;
var ab = new ActionBlock<int>(i => {
Assert.True(i == nextValueExpected, string.Format("Expected next value to be {0} but got {1}", nextValueExpected, i));
nextValueExpected++;
});
var bb = new BufferBlock<int>();
bb.AsObservable().Subscribe(ab.AsObserver());
bb.PostRange(-2, 0);
bb.Complete();
await ab.Completion;
}
// Test that unsubscribing stops flow of data and stops completion
{
var target = new BufferBlock<int>();
var source = new BufferBlock<int>();
using (source.AsObservable().Subscribe(target.AsObserver()))
{
source.PostItems(1, 2);
Assert.Equal(expected: 1, actual: await target.ReceiveAsync());
Assert.Equal(expected: 2, actual: await target.ReceiveAsync());
}
source.Post(3);
var wb = new WriteOnceBlock<int>(i => i);
source.LinkTo(wb);
await wb.Completion;
source.Complete();
await source.Completion;
Assert.False(target.Completion.IsCompleted);
}
}
示例3: TestReceive_AlreadyAvailable
public async Task TestReceive_AlreadyAvailable()
{
var buffer = new BufferBlock<int>();
buffer.PostItems(1, 2, 3, 4);
Assert.Equal(expected: 1, actual: buffer.Receive());
Assert.Equal(expected: 3, actual: buffer.Count);
Assert.Equal(expected: 2, actual: buffer.Receive(new CancellationTokenSource().Token));
Assert.Equal(expected: 2, actual: buffer.Count);
Assert.Equal(expected: 3, actual: buffer.Receive(TimeSpan.FromDays(1)));
Assert.Equal(expected: 1, actual: buffer.Count);
Assert.Equal(expected: 4, actual: buffer.Receive(TimeSpan.FromDays(1), new CancellationTokenSource().Token));
Assert.Equal(expected: 0, actual: buffer.Count);
buffer.PostItems(1, 2, 3, 4);
Assert.Equal(expected: 1, actual: await buffer.ReceiveAsync());
Assert.Equal(expected: 3, actual: buffer.Count);
Assert.Equal(expected: 2, actual: await buffer.ReceiveAsync(new CancellationTokenSource().Token));
Assert.Equal(expected: 2, actual: buffer.Count);
Assert.Equal(expected: 3, actual: await buffer.ReceiveAsync(TimeSpan.FromDays(1)));
Assert.Equal(expected: 1, actual: buffer.Count);
Assert.Equal(expected: 4, actual: await buffer.ReceiveAsync(TimeSpan.FromDays(1), new CancellationTokenSource().Token));
Assert.Equal(expected: 0, actual: buffer.Count);
}
示例4: TestChoose3_BasicFunctionality
public async Task TestChoose3_BasicFunctionality()
{
for (int chooseTestCase = 0; chooseTestCase < 5; chooseTestCase++)
{
var cesp = new ConcurrentExclusiveSchedulerPair(); // ensure BufferBlocks are serialized
var source1 = new BufferBlock<int>(new DataflowBlockOptions { TaskScheduler = cesp.ExclusiveScheduler });
var source2 = new BufferBlock<string>(new DataflowBlockOptions { TaskScheduler = cesp.ExclusiveScheduler });
var source3 = new BufferBlock<double>(new DataflowBlockOptions { TaskScheduler = cesp.ExclusiveScheduler });
int intValue = 0;
string stringValue = null;
double doubleValue = 0.0;
TaskScheduler usedScheduler = null, requestedScheduler = new ConcurrentExclusiveSchedulerPair().ConcurrentScheduler;
var cts = new CancellationTokenSource();
var t = chooseTestCase < 7 ?
DataflowBlock.Choose(
source1, i => intValue = i,
source2, s => stringValue = s,
source3, d => doubleValue = d) :
DataflowBlock.Choose(
source1, i => usedScheduler = TaskScheduler.Current,
source2, s => usedScheduler = TaskScheduler.Current,
source3, d => usedScheduler = TaskScheduler.Current,
new DataflowBlockOptions { TaskScheduler = requestedScheduler, MaxMessagesPerTask = 1, CancellationToken = cts.Token });
switch (chooseTestCase)
{
case 0: // Test data on the first source
source1.PostItems(42, 43);
Assert.Equal(expected: 0, actual: await t);
Assert.Equal(expected: 42, actual: intValue);
Assert.Null(stringValue);
Assert.Equal(expected: 0, actual: doubleValue);
Assert.Equal(expected: 1, actual: source1.Count);
break;
case 1: // Test data on the second source
source2.PostItems("42", "43");
Assert.Equal(expected: 1, actual: await t);
Assert.Equal(expected: "42", actual: stringValue);
Assert.Equal(expected: 0, actual: intValue);
Assert.Equal(expected: 0, actual: doubleValue);
Assert.Equal(expected: 1, actual: source2.Count);
break;
case 2: // Test data on the third source
source3.PostItems(42.0, 43.0);
Assert.Equal(expected: 2, actual: await t);
Assert.Equal(expected: 42.0, actual: doubleValue);
Assert.Equal(expected: 0, actual: intValue);
Assert.Null(stringValue);
Assert.Equal(expected: 1, actual: source3.Count);
break;
case 3: // Test first source complete and data on second
source1.Complete();
source2.Post("42");
Assert.Equal(expected: 1, actual: await t);
Assert.Equal(expected: "42", actual: stringValue);
Assert.Equal(expected: 0, actual: intValue);
Assert.Equal(expected: 0, actual: doubleValue);
Assert.Equal(expected: 0, actual: source2.Count);
break;
case 4: // Test second source complete and data on third
source2.Complete();
source3.Post(42.0);
Assert.Equal(expected: 2, actual: await t);
Assert.Equal(expected: 42.0, actual: doubleValue);
Assert.Equal(expected: 0, actual: intValue);
Assert.Null(stringValue);
Assert.Equal(expected: 0, actual: source3.Count);
break;
case 5: // Test third source complete and data on first
source3.Complete();
source1.Post(42);
Assert.Equal(expected: 0, actual: await t);
Assert.Equal(expected: 42, actual: intValue);
Assert.Null(stringValue);
Assert.Equal(expected: 0, actual: doubleValue);
Assert.Equal(expected: 0, actual: source1.Count);
break;
case 6: // Test all sources complete
source1.Complete();
source2.Complete();
source3.Complete();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => t);
Assert.Equal(expected: 0, actual: intValue);
Assert.Null(stringValue);
Assert.Equal(expected: 0, actual: doubleValue);
break;
// >= 7 TEST USING DATAFLOW BLOCK OPTIONS
case 7: // Test correct TaskScheduler is used
source3.Post(42);
await t;
//.........这里部分代码省略.........