本文整理汇总了C#中BufferBlock.OutputAvailableAsync方法的典型用法代码示例。如果您正苦于以下问题:C# BufferBlock.OutputAvailableAsync方法的具体用法?C# BufferBlock.OutputAvailableAsync怎么用?C# BufferBlock.OutputAvailableAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BufferBlock
的用法示例。
在下文中一共展示了BufferBlock.OutputAvailableAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompletedImmediateTest
public void CompletedImmediateTest()
{
var source = new BufferBlock<int>();
source.Complete();
var task = source.OutputAvailableAsync();
Assert.IsTrue(task.Wait(1000));
Assert.IsFalse(task.Result);
}
示例2: CompletedPostponedTest
public void CompletedPostponedTest()
{
var source = new BufferBlock<int>(new DataflowBlockOptions());
var task = source.OutputAvailableAsync();
Assert.IsFalse(task.Wait(100));
source.Complete();
Assert.IsTrue(task.Wait(1000));
Assert.IsFalse(task.Result);
}
示例3: ImmediateTest
public void ImmediateTest()
{
var scheduler = new TestScheduler();
var source = new BufferBlock<int>(new DataflowBlockOptions{TaskScheduler = scheduler});
Assert.IsTrue(source.Post(1));
var task = source.OutputAvailableAsync();
// not necessary in MS.NET
scheduler.ExecuteAll();
Assert.IsTrue(task.IsCompleted);
Assert.IsTrue(task.Result);
}
示例4: TestOutputAvailableAsync_Cancellation
public async Task TestOutputAvailableAsync_Cancellation()
{
var buffer = new BufferBlock<int>();
Assert.True(buffer.OutputAvailableAsync(new CancellationToken(true)).IsCanceled);
var cts = new CancellationTokenSource();
var t = buffer.OutputAvailableAsync(cts.Token);
Assert.False(t.IsCompleted);
cts.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => t);
cts = new CancellationTokenSource();
t = buffer.OutputAvailableAsync(cts.Token);
buffer.Post(42);
await t;
cts.Cancel(); // make sure no exception happens
}
示例5: TestOutputAvailableAsync_LongSequence
public async Task TestOutputAvailableAsync_LongSequence()
{
const int iterations = 10000; // enough to stack overflow if there's a problem
var source = new BufferBlock<int>();
Task t = source.OutputAvailableAsync();
for (int i = 1; i < iterations; i++)
{
t = t.ContinueWith(_ => source.OutputAvailableAsync(),
CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default).Unwrap();
}
source.Post(42);
await t;
}
示例6: TestOutputAvailableAsync_NoDataAfterCompletion
public async Task TestOutputAvailableAsync_NoDataAfterCompletion()
{
foreach (bool withUncanceledToken in DataflowTestHelpers.BooleanValues)
foreach (bool completeBefore in DataflowTestHelpers.BooleanValues)
{
var buffer = new BufferBlock<int>();
Task<bool> t;
if (completeBefore) buffer.Complete();
t = withUncanceledToken ?
buffer.OutputAvailableAsync(new CancellationTokenSource().Token) :
buffer.OutputAvailableAsync();
if (!completeBefore) buffer.Complete();
Assert.False(await t);
}
}
示例7: TestOutputAvailableAsync_AvailabilityBeforeAfterData
public async Task TestOutputAvailableAsync_AvailabilityBeforeAfterData()
{
var buffer = new BufferBlock<int>();
foreach (bool withUncanceledToken in DataflowTestHelpers.BooleanValues)
foreach (bool beforeData in DataflowTestHelpers.BooleanValues)
{
if (beforeData)
{
var t = withUncanceledToken ?
buffer.OutputAvailableAsync(new CancellationTokenSource().Token) :
buffer.OutputAvailableAsync();
Assert.False(t.IsCompleted);
buffer.Post(42);
Assert.True(await t);
}
else
{
buffer.Post(42);
var t = withUncanceledToken ?
buffer.OutputAvailableAsync(new CancellationTokenSource().Token) :
buffer.OutputAvailableAsync();
Assert.True(t.IsCompleted);
Assert.True(t.Result);
}
Assert.Equal(expected: 1, actual: buffer.Count);
Assert.Equal(expected: 42, actual: await buffer.ReceiveAsync());
}
}
示例8: FaultedImmediateTest
public void FaultedImmediateTest()
{
var source = new BufferBlock<int>();
((IDataflowBlock)source).Fault(new Exception());
var task = source.OutputAvailableAsync();
Assert.IsTrue(task.Wait(1000));
Assert.IsFalse(task.Result);
}
示例9: WithDecliningTargetTest
public void WithDecliningTargetTest()
{
var source = new BufferBlock<int>();
var target = new BufferBlock<int>();
source.LinkTo(target, i => false);
var task = source.OutputAvailableAsync();
Assert.IsTrue(source.Post(1));
Assert.IsTrue(task.Wait(1000));
Assert.IsTrue(task.Result);
}
示例10: CancellationTest
public void CancellationTest()
{
var source = new BufferBlock<int>();
var tokenSource = new CancellationTokenSource();
var task = source.OutputAvailableAsync(tokenSource.Token);
tokenSource.Cancel();
AssertEx.Throws<AggregateException>(() => task.Wait(1000));
Assert.IsTrue(task.IsCanceled);
}
示例11: TestOutputAvailableAsyncAfterTryReceiveAll
[OuterLoop] // has a timeout
public async Task TestOutputAvailableAsyncAfterTryReceiveAll()
{
Func<Task<bool>> generator = () => {
var buffer = new BufferBlock<object>();
buffer.Post(null);
IList<object> items;
buffer.TryReceiveAll(out items);
var outputAvailableAsync = buffer.OutputAvailableAsync();
buffer.Post(null);
return outputAvailableAsync;
};
var multipleConcurrentTestsTask = Task.WhenAll(Enumerable.Repeat(0, 1000).Select(_ => generator()));
var timeoutTask = Task.Delay(2000);
var completedTask = await Task.WhenAny(multipleConcurrentTestsTask, timeoutTask).ConfigureAwait(false);
Assert.True(completedTask != timeoutTask);
}
示例12: TestProducerConsumer
public async Task TestProducerConsumer()
{
foreach (TaskScheduler scheduler in new[] { TaskScheduler.Default, new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler })
foreach (int maxMessagesPerTask in new[] { DataflowBlockOptions.Unbounded, 1, 2 })
foreach (int boundedCapacity in new[] { DataflowBlockOptions.Unbounded, 1, 2 })
{
const int Messages = 100;
var bb = new BufferBlock<int>(new DataflowBlockOptions
{
BoundedCapacity = boundedCapacity,
MaxMessagesPerTask = maxMessagesPerTask,
TaskScheduler = scheduler
});
await Task.WhenAll(
Task.Run(async delegate { // consumer
int i = 0;
while (await bb.OutputAvailableAsync())
{
Assert.Equal(expected: i, actual: await bb.ReceiveAsync());
i++;
}
}),
Task.Run(async delegate { // producer
for (int i = 0; i < Messages; i++)
{
await bb.SendAsync(i);
}
bb.Complete();
}));
}
}
示例13: TestBoundedReceives
public async Task TestBoundedReceives()
{
for (int test = 0; test < 4; test++)
{
var bb = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 1 });
Assert.True(bb.Post(0));
const int sends = 5;
for (int i = 1; i <= sends; i++)
{
Task<bool> send = bb.SendAsync(i);
Assert.True(await bb.OutputAvailableAsync()); // wait for previously posted/sent item
int item;
switch (test)
{
case 0:
IList<int> items;
Assert.True(bb.TryReceiveAll(out items));
Assert.Equal(expected: 1, actual: items.Count);
Assert.Equal(expected: i - 1, actual: items[0]);
break;
case 1:
Assert.True(bb.TryReceive(f => true, out item));
Assert.Equal(expected: i - 1, actual: item);
break;
case 2:
Assert.False(bb.TryReceive(f => f == i, out item));
Assert.True(bb.TryReceive(f => f == i - 1, out item));
Assert.Equal(expected: i - 1, actual: item);
break;
case 3:
Assert.Equal(expected: i - 1, actual: await bb.ReceiveAsync());
break;
}
}
// Receive remaining item
Assert.Equal(expected: sends, actual: await bb.ReceiveAsync());
bb.Complete();
await bb.Completion;
}
}
示例14: TestOutputAvailableAsyncAfterTryReceiveAll
public async Task TestOutputAvailableAsyncAfterTryReceiveAll()
{
Func<Task<bool>> generator = () => {
var buffer = new BufferBlock<object>();
buffer.Post(null);
IList<object> items;
buffer.TryReceiveAll(out items);
var outputAvailableAsync = buffer.OutputAvailableAsync();
buffer.Post(null);
return outputAvailableAsync;
};
bool[] results = await Task.WhenAll(Enumerable.Repeat(0, 10).Select(_ => generator()));
Assert.All(results, Assert.True);
}
示例15: GetOutputAvailableAsyncTaskAfterTryReceiveAllOnNonEmptyBufferBlock
private Task GetOutputAvailableAsyncTaskAfterTryReceiveAllOnNonEmptyBufferBlock()
{
var buffer = new BufferBlock<object>();
buffer.Post(null);
IList<object> items;
buffer.TryReceiveAll(out items);
var outputAvailableAsync = buffer.OutputAvailableAsync();
buffer.Post(null);
return outputAvailableAsync;
}