本文整理汇总了C#中BufferBlock.Receive方法的典型用法代码示例。如果您正苦于以下问题:C# BufferBlock.Receive方法的具体用法?C# BufferBlock.Receive怎么用?C# BufferBlock.Receive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BufferBlock
的用法示例。
在下文中一共展示了BufferBlock.Receive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReceiveCompletedTest
public void ReceiveCompletedTest ()
{
var block = new BufferBlock<int> ();
block.Complete ();
AssertEx.Throws<InvalidOperationException> (
() => block.Receive (TimeSpan.FromMilliseconds (1000)));
}
示例2: ReceiveCancelledTest
public void ReceiveCancelledTest ()
{
var block = new BufferBlock<int> ();
var tokenSource = new CancellationTokenSource (200);
AssertEx.Throws<OperationCanceledException> (
() => block.Receive (tokenSource.Token));
}
示例3: ProgressReports_AreSentToBlock
public void ProgressReports_AreSentToBlock()
{
Test.Async(async () =>
{
var block = new BufferBlock<int>();
IProgress<int> progress = new DataflowProgress<int>(block);
await TaskShim.Run(() =>
{
progress.Report(13);
progress.Report(17);
});
var report1 = block.Receive();
var report2 = block.Receive();
Assert.AreEqual(13, report1);
Assert.AreEqual(17, report2);
});
}
示例4: EmptyAfterReceive
public void EmptyAfterReceive ()
{
var block = new BufferBlock<int> ();
block.Post (42);
block.Complete ();
Assert.IsFalse (block.Completion.IsCompleted);
Assert.AreEqual (TaskStatus.WaitingForActivation, block.Completion.Status);
block.Receive ();
Assert.IsTrue (block.Completion.IsCompleted);
Assert.AreEqual (TaskStatus.RanToCompletion, block.Completion.Status);
}
示例5: WithElementsStillLingering
public void WithElementsStillLingering ()
{
var block = new BufferBlock<int> ();
block.Post (42);
block.Complete ();
Console.WriteLine (block.Completion.IsCompleted);
Console.WriteLine (block.Completion.Status);
block.Receive ();
Console.WriteLine (block.Completion.IsCompleted);
Console.WriteLine (block.Completion.Status);
}
示例6: ObserveTask_Completed_CompletesBlock
public void ObserveTask_Completed_CompletesBlock()
{
Test.Async(async () =>
{
var block = new BufferBlock<int>();
var dataflowProgress = new DataflowProgress<int>(block);
IProgress<int> progress = dataflowProgress;
var task = TaskShim.Run(() =>
{
progress.Report(13);
});
dataflowProgress.ObserveTaskForCompletion(task);
var report1 = block.Receive();
Assert.AreEqual(13, report1);
await block.Completion;
});
}
示例7: 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);
}
示例8: TestReceive_ArgumentValidation
public void TestReceive_ArgumentValidation()
{
var buffer = new BufferBlock<int>();
int item;
Assert.Throws<ArgumentNullException>(() => ((IReceivableSourceBlock<int>)null).TryReceive(out item));
Assert.Throws<ArgumentNullException>(() => ((IReceivableSourceBlock<int>)null).Receive());
Assert.Throws<ArgumentNullException>(() => ((IReceivableSourceBlock<int>)null).Receive(new CancellationToken(true)));
Assert.Throws<ArgumentNullException>(() => { ((IReceivableSourceBlock<int>)null).ReceiveAsync(); });
Assert.Throws<ArgumentNullException>(() => { ((IReceivableSourceBlock<int>)null).ReceiveAsync(new CancellationToken(true)); });
Assert.Throws<ArgumentOutOfRangeException>(() => buffer.Receive(TimeSpan.FromSeconds(-2)));
Assert.Throws<ArgumentOutOfRangeException>(() => { buffer.ReceiveAsync(TimeSpan.FromSeconds(-2)); });
}
示例9: 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);
}
示例10: TestReceive_CanceledSource
public async Task TestReceive_CanceledSource()
{
foreach (bool beforeReceive in DataflowTestHelpers.BooleanValues)
{
var cts = new CancellationTokenSource();
var bb = new BufferBlock<int>(new DataflowBlockOptions { CancellationToken = cts.Token });
if (beforeReceive)
{
cts.Cancel();
}
else
{
var ignored = Task.Run(() => cts.Cancel()); // as elsewhere, this test should always succeed, but is racy as to what's being tested
}
Assert.Throws<InvalidOperationException>(() => bb.Receive());
}
foreach (bool beforeReceive in DataflowTestHelpers.BooleanValues)
{
var cts = new CancellationTokenSource();
var bb = new BufferBlock<int>(new DataflowBlockOptions { CancellationToken = cts.Token });
if (beforeReceive)
{
cts.Cancel();
}
var t = bb.ReceiveAsync();
if (!beforeReceive)
{
cts.Cancel();
}
await Assert.ThrowsAsync<InvalidOperationException>(() => t);
}
}
示例11: TestEncapsulate_LinkingAndUnlinking
public void TestEncapsulate_LinkingAndUnlinking()
{
var buffer = new BufferBlock<int>();
var action = new ActionBlock<int>(i => buffer.Post(i));
action.Completion.ContinueWith(delegate { buffer.Complete(); }, TaskScheduler.Default);
IPropagatorBlock<int, int> encapsulated = DataflowBlock.Encapsulate(action, buffer);
var endTarget = new BufferBlock<int>();
IDisposable unlink = encapsulated.LinkTo(endTarget);
Assert.NotNull(unlink);
IDisposable unlink2 = encapsulated.LinkTo(endTarget);
Assert.NotNull(unlink);
action.Post(1);
Assert.Equal(expected: 1, actual: endTarget.Receive());
unlink.Dispose();
action.Post(2);
Assert.Equal(expected: 2, actual: endTarget.Receive());
unlink2.Dispose();
}
示例12: TestReceive_TimeoutZero
public async Task TestReceive_TimeoutZero()
{
var bb = new BufferBlock<int>();
Assert.Throws<TimeoutException>(() => bb.Receive(TimeSpan.FromMilliseconds(0)));
await Assert.ThrowsAsync<TimeoutException>(() => bb.ReceiveAsync(TimeSpan.FromMilliseconds(0)));
}
示例13: TestReceive_Cancellation
public async Task TestReceive_Cancellation()
{
var bb = new BufferBlock<int>();
// Cancel before Receive/ReceiveAsync
Assert.ThrowsAny<OperationCanceledException>(() => bb.Receive(new CancellationToken(canceled: true)));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => bb.ReceiveAsync(new CancellationToken(canceled: true)));
// Cancel after Receive/ReceiveAsync but before data
{
var cts = new CancellationTokenSource();
var ignored = Task.Run(() => cts.Cancel()); // as elsewhere, this test should always succeed, but is racy as to what's being tested
Assert.ThrowsAny<OperationCanceledException>(() => bb.Receive(cts.Token));
}
{
var cts = new CancellationTokenSource();
var t = bb.ReceiveAsync(cts.Token);
cts.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => bb.ReceiveAsync(cts.Token));
}
// Cancel after data received
{
var cts = new CancellationTokenSource();
var ignored = Task.Run(() => bb.Post(1)); // as elsewhere, this test should always succeed, but is racy as to what's being tested
Assert.Equal(expected: 1, actual: bb.Receive(cts.Token));
cts.Cancel(); // just checking to make sure there's no exception
}
{
var cts = new CancellationTokenSource();
var t = bb.ReceiveAsync(cts.Token);
bb.Post(2);
Assert.Equal(expected: 2, actual: await t);
cts.Cancel(); // just checking to make sure there's no exception }
}
}
示例14: TestBufferBlockBounding
public void TestBufferBlockBounding()
{
const int WAIT_TIMEOUT = 4000; // wait at most 4 seconds for a particularly race-condition
// Test buffer doesn't exceed limit
{
bool localPassed = true;
for (int boundedCapacity = 1; boundedCapacity <= 3 && localPassed; boundedCapacity += 2)
{
var b = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = boundedCapacity });
for (int i = 0; i < boundedCapacity; i++)
{
var send = b.SendAsync(i);
Assert.True(send.Wait(0) && send.Result, "Send should succeed as capacity not yet reached");
}
for (int i = boundedCapacity; i < boundedCapacity + 2 && localPassed; i++)
{
Assert.True(b.Count == boundedCapacity, "Count should equal bounded capacity after all posts completed");
var t = b.SendAsync(i);
Assert.True(!t.IsCompleted, "Send should not have completed on a full buffer");
b.Receive();
Assert.True(t.Wait(WAIT_TIMEOUT), "The send should have completed before the timeout");
Assert.True(t.IsCompleted && t.Result, "The send should have completed successfully");
Assert.True(SpinWait.SpinUntil(() => b.Count == boundedCapacity, WAIT_TIMEOUT), "The count should be back at the bounded capacity after successful send");
}
int remainingCount = b.Count;
while (b.Count > 0)
{
remainingCount--;
int ignored;
Assert.True(b.TryReceive(out ignored), "Should have been able to successfully remove each item");
}
Assert.True(remainingCount == 0, "Should be no items left after removals");
}
Assert.True(localPassed, string.Format("{0}: Doesn't exceed limits", localPassed ? "Success" : "Failure"));
}
// Test correct ordering
{
bool localPassed = true;
for (int boundedCapacity = 1; boundedCapacity <= 3 && localPassed; boundedCapacity += 2)
{
int iters = boundedCapacity + 2;
var tasks = new Task<bool>[iters];
var b = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = boundedCapacity });
for (int i = 0; i < boundedCapacity; i++)
{
var t = b.SendAsync(i);
Assert.True(t.IsCompleted && t.Result, "Sends until capacity reached should complete immediately and successfully");
tasks[i] = t;
}
for (int i = boundedCapacity; i < iters; i++)
{
var t = b.SendAsync(i);
Assert.True(!t.IsCompleted, "Sends after capacity reached should not be completed");
tasks[i] = t;
}
for (int i = 0; i < iters && localPassed; i++)
{
if (i >= boundedCapacity & i < iters - boundedCapacity)
{
Assert.True(!tasks[i + boundedCapacity].IsCompleted, "Remaining sends should not yet be completed");
}
Assert.True(b.Receive() == i, "Received value should match sent value in correct order");
Assert.True(tasks[i].Wait(WAIT_TIMEOUT) && tasks[i].Result, "Next sender task should have completed");
}
}
Assert.True(localPassed, string.Format("{0}: Correct ordering", localPassed ? "Success" : "Failure"));
}
// Test declining
{
bool localPassed = true;
for (int boundedCapacity = 1; boundedCapacity <= 3 && localPassed; boundedCapacity += 2)
{
var b = new BufferBlock<string>(new DataflowBlockOptions { BoundedCapacity = boundedCapacity });
int total = boundedCapacity + 2;
var tasks = new Task<bool>[total];
for (int i = 0; i < total; i++)
{
tasks[i] = b.SendAsync(i.ToString());
}
for (int i = 0; i < total; i++)
{
Assert.True((i < boundedCapacity) == tasks[i].IsCompleted, "All sends below the capacity should have completed");
//.........这里部分代码省略.........
示例15: TestReceive_Timeout
[OuterLoop] // timeout involved
public async Task TestReceive_Timeout()
{
var bb = new BufferBlock<int>();
Assert.Throws<TimeoutException>(() => bb.Receive(TimeSpan.FromMilliseconds(1)));
await Assert.ThrowsAsync<TimeoutException>(() => bb.ReceiveAsync(TimeSpan.FromMilliseconds(1)));
var cts = new CancellationTokenSource();
Assert.Throws<TimeoutException>(() => bb.Receive(TimeSpan.FromMilliseconds(1), cts.Token));
await Assert.ThrowsAsync<TimeoutException>(() => bb.ReceiveAsync(TimeSpan.FromMilliseconds(1), cts.Token));
}