public async Task TestOrderMaintained() { foreach (bool sync in DataflowTestHelpers.BooleanValues) { foreach (bool singleProducerConstrained in DataflowTestHelpers.BooleanValues) { var options = new ExecutionDataflowBlockOptions { SingleProducerConstrained = singleProducerConstrained }; int prev = -1; Action <int> body = i => { Assert.Equal(expected: prev + 1, actual: i); prev = i; }; ActionBlock <int> ab = sync ? new ActionBlock <int>(body, options) : new ActionBlock <int>(i => TaskShim.Run(() => body(i)), options); ab.PostRange(0, 100); ab.Complete(); await ab.Completion; } } }
public async Task TestOperationCanceledExceptionsIgnored() { foreach (bool sync in DataflowTestHelpers.BooleanValues) { foreach (bool singleProducerConstrained in DataflowTestHelpers.BooleanValues) { var options = new ExecutionDataflowBlockOptions { SingleProducerConstrained = singleProducerConstrained }; int sumOfOdds = 0; Action <int> body = i => { if ((i % 2) == 0) { throw new OperationCanceledException(); } sumOfOdds += i; }; ActionBlock <int> ab = sync ? new ActionBlock <int>(body, options) : new ActionBlock <int>(async i => { await TaskShim.Yield(); body(i); }, options); const int MaxValue = 10; ab.PostRange(0, MaxValue); ab.Complete(); await ab.Completion; Assert.Equal( expected: Enumerable.Range(0, MaxValue).Where(i => i % 2 != 0).Sum(), actual: sumOfOdds); } } }
public async Task TestSchedulerUsage() { foreach (bool singleProducerConstrained in DataflowTestHelpers.BooleanValues) { var scheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler; var sync = new ActionBlock <int>(_ => Assert.Equal(scheduler.Id, TaskScheduler.Current.Id), new ExecutionDataflowBlockOptions { TaskScheduler = scheduler, SingleProducerConstrained = singleProducerConstrained }); sync.PostRange(0, 10); sync.Complete(); await sync.Completion; var async = new ActionBlock <int>(_ => { Assert.Equal(scheduler.Id, TaskScheduler.Current.Id); return(TaskShim.FromResult(0)); }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler, SingleProducerConstrained = singleProducerConstrained }); async.PostRange(0, 10); async.Complete(); await async.Completion; } }
public async Task TestFaulting() { for (int trial = 0; trial < 3; trial++) { foreach (bool singleProducerConstrained in DataflowTestHelpers.BooleanValues) { var options = new ExecutionDataflowBlockOptions { SingleProducerConstrained = singleProducerConstrained }; Action thrower = () => { throw new InvalidOperationException(); }; ActionBlock <int> ab = null; switch (trial) { case 0: ab = new ActionBlock <int>(i => thrower(), options); break; case 1: ab = new ActionBlock <int>(i => { thrower(); return(Task.FromResult(0)); }, options); break; case 2: ab = new ActionBlock <int>(i => Task.Run(thrower), options); break; } ab.PostRange(0, 4); try { await ab.Completion; Assert.True(false, "Should always throw IOE"); } catch (InvalidOperationException) { } Assert.Equal(expected: 0, actual: ab.InputCount); Assert.False(ab.Post(5)); } } }
public async Task TestNullReturnedTasks() { int sumOfOdds = 0; var ab = new ActionBlock <int>(i => { if ((i % 2) == 0) { return(null); } return(Task.Run(() => { sumOfOdds += i; })); }); const int MaxValue = 10; ab.PostRange(0, MaxValue); ab.Complete(); await ab.Completion; Assert.Equal( expected: Enumerable.Range(0, MaxValue).Where(i => i % 2 != 0).Sum(), actual: sumOfOdds); }
public async Task TestParallelExecution() { int dop = 2; foreach (bool sync in DataflowTestHelpers.BooleanValues) { foreach (bool singleProducerConstrained in DataflowTestHelpers.BooleanValues) { Barrier barrier = new Barrier(dop); var options = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = dop, SingleProducerConstrained = singleProducerConstrained }; ActionBlock <int> ab = sync ? new ActionBlock <int>(_ => barrier.SignalAndWait(), options) : new ActionBlock <int>(_ => TaskShim.Run(() => barrier.SignalAndWait()), options); int iters = dop * 4; ab.PostRange(0, iters); ab.Complete(); await ab.Completion; } } }
public async Task TestNullReturnedTasks() { int sumOfOdds = 0; var ab = new ActionBlock<int>(i => { if ((i % 2) == 0) return null; return Task.Run(() => { sumOfOdds += i; }); }); const int MaxValue = 10; ab.PostRange(0, MaxValue); ab.Complete(); await ab.Completion; Assert.Equal( expected: Enumerable.Range(0, MaxValue).Where(i => i % 2 != 0).Sum(), actual: sumOfOdds); }
public async Task TestSchedulerUsage() { foreach (bool singleProducerConstrained in DataflowTestHelpers.BooleanValues) { var scheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler; var sync = new ActionBlock<int>(_ => Assert.Equal(scheduler.Id, TaskScheduler.Current.Id), new ExecutionDataflowBlockOptions { TaskScheduler = scheduler, SingleProducerConstrained = singleProducerConstrained }); sync.PostRange(0, 10); sync.Complete(); await sync.Completion; var async = new ActionBlock<int>(_ => { Assert.Equal(scheduler.Id, TaskScheduler.Current.Id); return Task.FromResult(0); }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler, SingleProducerConstrained = singleProducerConstrained }); async.PostRange(0, 10); async.Complete(); await async.Completion; } }