public void GreedyJoin3Test () { var scheduler = new TestScheduler (); var block = new JoinBlock<int, int, int> (new GroupingDataflowBlockOptions { TaskScheduler = scheduler }); var source1 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source3 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull (source1.LinkTo (block.Target1)); Assert.IsNotNull (source2.LinkTo (block.Target2)); Assert.IsNotNull (source3.LinkTo (block.Target3)); Assert.IsTrue (source1.Post (1)); scheduler.ExecuteAll (); int i; Assert.IsFalse (source1.TryReceive (out i)); Assert.IsTrue (source2.Post (11)); Assert.IsTrue (source3.Post (21)); scheduler.ExecuteAll (); Assert.IsFalse (source2.TryReceive (out i)); Assert.IsFalse (source3.TryReceive (out i)); Tuple<int, int, int> tuple; Assert.IsTrue (block.TryReceive (out tuple)); Assert.AreEqual (Tuple.Create (1, 11, 21), tuple); }
public void LinkToPredicateMaxMessagesTest() { var scheduler = new TestScheduler(); var source = new BufferBlock <int> ( new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock <int> (); source.LinkTo(target, new DataflowLinkOptions { MaxMessages = 1 }, i => i % 2 == 1); Assert.IsTrue(source.Post(2)); Assert.IsTrue(source.Post(1)); Assert.IsTrue(source.Post(3)); scheduler.ExecuteAll(); int item; Assert.IsFalse(target.TryReceive(out item)); Assert.IsTrue(source.TryReceive(out item)); Assert.AreEqual(2, item); scheduler.ExecuteAll(); Assert.IsTrue(target.TryReceive(out item)); Assert.AreEqual(1, item); scheduler.ExecuteAll(); Assert.IsFalse(target.TryReceive(out item)); }
public void ConditionalDecliningTest() { var scheduler = new TestScheduler(); var source = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new TestTargetBlock <int> { Decline = true }; Assert.IsNotNull(source.LinkTo(target)); Assert.AreEqual(default(int), target.DirectlyAccepted); Assert.IsTrue(source.Post(42)); scheduler.ExecuteAll(); Assert.AreEqual(default(int), target.DirectlyAccepted); target.Decline = false; Assert.IsTrue(source.Post(43)); scheduler.ExecuteAll(); Assert.AreEqual(default(int), target.DirectlyAccepted); Assert.AreEqual(42, source.Receive(TimeSpan.FromMilliseconds(100))); scheduler.ExecuteAll(); Assert.AreEqual(43, target.DirectlyAccepted); }
public void MaxNumberOfGroupsTest() { var scheduler = new TestScheduler(); var block = new BatchBlock <int> (2, new GroupingDataflowBlockOptions { MaxNumberOfGroups = 2, TaskScheduler = scheduler }); Assert.IsTrue(block.Post(1)); Assert.IsTrue(block.Post(2)); Assert.IsTrue(block.Post(3)); Assert.IsTrue(block.Post(4)); Assert.IsFalse(block.Post(5)); scheduler.ExecuteAll(); int[] batch; Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.AreEqual(new[] { 1, 2 }, batch); Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.AreEqual(new[] { 3, 4 }, batch); scheduler.ExecuteAll(); Assert.IsTrue(block.Completion.Wait(100)); }
public void DontOfferTwiceTest() { var scheduler = new TestScheduler(); var block = new WriteOnceBlock <int> (null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new TestTargetBlock <int> { Postpone = true }; block.LinkTo(target); Assert.IsFalse(target.HasPostponed); Assert.IsTrue(block.Post(1)); scheduler.ExecuteAll(); Assert.IsTrue(target.HasPostponed); target.Postpone = false; int value; Assert.IsTrue(target.RetryPostponed(out value)); Assert.AreEqual(1, value); block.LinkTo(new BufferBlock <int> ()); scheduler.ExecuteAll(); Assert.AreEqual(default(int), target.DirectlyAccepted); }
public void ConsumeAfterReceiveTest() { var scheduler = new TestScheduler(); var source = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new TestTargetBlock <int> { Postpone = true }; Assert.IsNotNull(source.LinkTo(target)); Assert.IsFalse(target.HasPostponed); Assert.IsTrue(source.Post(42)); scheduler.ExecuteAll(); Assert.IsTrue(target.HasPostponed); target.Postpone = false; Assert.AreEqual(42, source.Receive()); Assert.IsTrue(source.Post(43)); Assert.AreEqual(default(int), target.DirectlyAccepted); int i; Assert.IsFalse(target.RetryPostponed(out i)); Assert.AreEqual(default(int), i); scheduler.ExecuteAll(); Assert.AreEqual(43, target.DirectlyAccepted); }
public void ReserveChangedTest() { var scheduler = new TestScheduler(); var broadcast = new BroadcastBlock <int> (null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new TestTargetBlock <int> { Postpone = true }; broadcast.LinkTo(target); Assert.IsFalse(target.HasPostponed); Assert.IsTrue(broadcast.Post(1)); scheduler.ExecuteAll(); Assert.IsTrue(target.HasPostponed); Assert.IsTrue(broadcast.Post(2)); scheduler.ExecuteAll(); Assert.IsTrue(target.ReservePostponed()); int value; Assert.IsTrue(target.RetryPostponed(out value)); Assert.AreEqual(2, value); }
public void LinkToPredicatePostponed() { var scheduler = new TestScheduler(); var source = new BufferBlock <int> ( new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock <int> ( new DataflowBlockOptions { BoundedCapacity = 1, TaskScheduler = scheduler }); source.LinkTo(target, i => true); Assert.IsTrue(target.Post(1)); Assert.IsTrue(source.Post(2)); scheduler.ExecuteAll(); int item; Assert.IsTrue(target.TryReceive(out item)); Assert.AreEqual(1, item); scheduler.ExecuteAll(); Assert.IsTrue(target.TryReceive(out item)); Assert.AreEqual(2, item); }
public void ReserveFaultConsumeBroadcast() { var scheduler = new TestScheduler(); var source = new BroadcastBlock <int> (null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new TestTargetBlock <int> { Postpone = true }; Assert.IsNotNull(source.LinkTo(target)); Assert.IsTrue(source.Post(42)); scheduler.ExecuteAll(); Assert.IsTrue(target.HasPostponed); Assert.IsTrue(target.ReservePostponed()); ((IDataflowBlock)source).Fault(new Exception()); scheduler.ExecuteAll(); Thread.Sleep(100); int value; Assert.IsTrue(target.RetryPostponed(out value)); }
public void MaxMessagesDirectTest() { var scheduler = new TestScheduler(); var source = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull( source.LinkTo(target, new DataflowLinkOptions { MaxMessages = 1 })); Assert.IsTrue(source.Post(42)); scheduler.ExecuteAll(); int item; Assert.IsTrue(target.TryReceive(null, out item)); Assert.AreEqual(42, item); Assert.IsTrue(source.Post(43)); scheduler.ExecuteAll(); Assert.IsFalse(target.TryReceive(null, out item)); Assert.IsTrue(source.TryReceive(null, out item)); Assert.AreEqual(43, item); }
public void NonGreedyBatchWithBoundedCapacityTriggerTest2() { var scheduler = new TestScheduler(); var block = new BatchBlock <int> (3, new GroupingDataflowBlockOptions { Greedy = false, BoundedCapacity = 3, TaskScheduler = scheduler }); var source1 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull(source1.LinkTo(block)); Assert.IsNotNull(source2.LinkTo(block)); // trigger 2, then trigger another 2 and then trigger 2 once more // while havaing capacity of 3 Assert.IsTrue(source1.Post(11)); Assert.IsTrue(source2.Post(21)); block.TriggerBatch(); scheduler.ExecuteAll(); Assert.IsTrue(source1.Post(12)); Assert.IsTrue(source2.Post(22)); block.TriggerBatch(); scheduler.ExecuteAll(); Assert.IsTrue(source1.Post(13)); Assert.IsTrue(source2.Post(23)); block.TriggerBatch(); scheduler.ExecuteAll(); int i; Assert.IsTrue(source1.TryReceive(out i)); Assert.AreEqual(13, i); Assert.IsTrue(source2.TryReceive(out i)); Assert.AreEqual(23, i); int[] batch; Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.AreEquivalent(new[] { 11, 21 }, batch); Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.AreEquivalent(new[] { 12, 22 }, batch); Assert.IsFalse(block.TryReceive(out batch)); }
public void NonGreedyJoin3WithBoundedCapacityTest() { var scheduler = new TestScheduler(); var block = new JoinBlock <int, int, int> ( new GroupingDataflowBlockOptions { Greedy = false, BoundedCapacity = 1, TaskScheduler = scheduler }); var source1 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source3 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull(source1.LinkTo(block.Target1)); Assert.IsNotNull(source2.LinkTo(block.Target2)); Assert.IsNotNull(source3.LinkTo(block.Target3)); Assert.IsTrue(source1.Post(11)); Assert.IsTrue(source2.Post(21)); Assert.IsTrue(source3.Post(31)); scheduler.ExecuteAll(); Assert.IsTrue(source1.Post(12)); Assert.IsTrue(source2.Post(22)); Assert.IsTrue(source3.Post(32)); scheduler.ExecuteAll(); int i; Assert.IsTrue(source1.TryReceive(out i)); Assert.AreEqual(12, i); Assert.IsTrue(source1.Post(13)); Tuple <int, int, int> tuple; Assert.IsTrue(block.TryReceive(out tuple)); Assert.AreEqual(Tuple.Create(11, 21, 31), tuple); scheduler.ExecuteAll(); Assert.IsTrue(block.TryReceive(out tuple)); Assert.AreEqual(Tuple.Create(13, 22, 32), tuple); }
public void NonGreedyBatchTest() { var scheduler = new TestScheduler(); var block = new BatchBlock <int> (3, new GroupingDataflowBlockOptions { Greedy = false, TaskScheduler = scheduler }); Assert.IsFalse(block.Post(42)); var source = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull(source.LinkTo(block)); Assert.IsTrue(source.Post(43)); scheduler.ExecuteAll(); int i; Assert.IsTrue(source.TryReceive(null, out i)); Assert.AreEqual(43, i); Assert.IsTrue(source.Post(44)); Assert.IsTrue(source.Post(45)); var source2 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull(source2.LinkTo(block)); Assert.IsTrue(source2.Post(142)); scheduler.ExecuteAll(); int[] batch; Assert.IsFalse(block.TryReceive(null, out batch)); Assert.IsNull(batch); block.TriggerBatch(); scheduler.ExecuteAll(); Assert.IsTrue(block.TryReceive(null, out batch)); CollectionAssert.AreEquivalent(new[] { 44, 142 }, batch); }
public void GreedyBatchWithBoundedCapacityTest() { var scheduler = new TestScheduler(); var block = new BatchBlock <int> (3, new GroupingDataflowBlockOptions { Greedy = true, BoundedCapacity = 3, TaskScheduler = scheduler }); Assert.IsTrue(block.Post(1)); Assert.IsTrue(block.Post(2)); block.TriggerBatch(); scheduler.ExecuteAll(); Assert.IsTrue(block.Post(3)); Assert.IsFalse(block.Post(4)); int[] batch; Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.AreEqual(new[] { 1, 2 }, batch); Assert.IsTrue(block.Post(5)); Assert.IsTrue(block.Post(6)); }
public void AsyncExceptionTest() { var scheduler = new TestScheduler(); var exception = new Exception(); var blocks = GetExecutionBlocksWithAsyncAction( i => { var tcs = new TaskCompletionSource <int> (); tcs.SetException(exception); return(tcs.Task); }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler }); foreach (var block in blocks) { Assert.IsTrue(block.Post(1)); // the task should be executed on the configured TaskScheduler Assert.IsFalse(block.Completion.Wait(100)); scheduler.ExecuteAll(); var ae = AssertEx.Throws <AggregateException> (() => block.Completion.Wait(1000)). Flatten(); Assert.AreEqual(1, ae.InnerExceptions.Count); Assert.AreSame(exception, ae.InnerException); } }
public void QueuedMessagesTest() { var scheduler = new TestScheduler(); var broadcast = new BroadcastBlock <int> (null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock <int> (); broadcast.LinkTo(target); Assert.IsTrue(broadcast.Post(1)); Assert.IsTrue(broadcast.Post(2)); AssertEx.Throws <TimeoutException> ( () => target.Receive(TimeSpan.FromMilliseconds(100))); scheduler.ExecuteAll(); int item; Assert.IsTrue(target.TryReceive(out item)); Assert.AreEqual(1, item); Assert.IsTrue(target.TryReceive(out item)); Assert.AreEqual(2, item); }
public void MaxNumberOfGroupsTest() { var scheduler = new TestScheduler(); var block = new JoinBlock <int, int> ( new GroupingDataflowBlockOptions { MaxNumberOfGroups = 1, TaskScheduler = scheduler }); Assert.IsTrue(block.Target1.Post(1)); Assert.IsFalse(block.Target1.Post(2)); Assert.IsTrue(block.Target2.Post(3)); Assert.IsFalse(block.Target2.Post(4)); Tuple <int, int> batch; Assert.IsTrue(block.TryReceive(out batch)); Assert.AreEqual(Tuple.Create(1, 3), batch); Assert.IsFalse(block.TryReceive(out batch)); scheduler.ExecuteAll(); Assert.IsTrue(block.Completion.Wait(1000)); }
public void MaxNumberOfGroupsTest() { var scheduler = new TestScheduler(); var block = new BatchedJoinBlock <int, int> (1, new GroupingDataflowBlockOptions { MaxNumberOfGroups = 2, TaskScheduler = scheduler }); Assert.IsTrue(block.Target1.Post(1)); Assert.IsTrue(block.Target2.Post(2)); Assert.IsFalse(block.Target2.Post(3)); Assert.IsFalse(block.Target1.Post(4)); Tuple <IList <int>, IList <int> > batch; Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.AreEqual(new[] { 1 }, batch.Item1); CollectionAssert.IsEmpty(batch.Item2); Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.IsEmpty(batch.Item1); CollectionAssert.AreEqual(new[] { 2 }, batch.Item2); scheduler.ExecuteAll(); Assert.IsTrue(block.Completion.Wait(100)); }
public void DontConsumePostponedAfterCompleteTest() { var scheduler = new TestScheduler(); var block = new BufferBlock <int> ( new DataflowBlockOptions { BoundedCapacity = 1, TaskScheduler = scheduler }); var target = (ITargetBlock <int>)block; var source = new TestSourceBlock <int> (); Assert.IsTrue(block.Post(11)); var header = new DataflowMessageHeader(1); source.AddMessage(header, 12); Assert.AreEqual(DataflowMessageStatus.Postponed, target.OfferMessage(header, 12, source, false)); block.Complete(); Assert.AreEqual(11, block.Receive()); scheduler.ExecuteAll(); Assert.IsFalse(source.WasConsumed(header)); }
public void TransformManyOverfullTest2() { var scheduler = new TestScheduler(); n = 0; var transform = new TransformManyBlock <int, int> ( i => ComputeResults(), new ExecutionDataflowBlockOptions { BoundedCapacity = 100, TaskScheduler = scheduler }); for (int i = 0; i < 100; i++) { Assert.IsTrue(transform.Post(i)); } Assert.IsFalse(transform.Post(101)); Assert.GreaterOrEqual(scheduler.ExecuteAll(), 1); Assert.IsFalse(transform.Post(102)); Assert.AreEqual(10000, Volatile.Read(ref n)); }
public void MultipleOffersTest() { var scheduler = new TestScheduler(); var block = new BufferBlock <int> ( new DataflowBlockOptions { BoundedCapacity = 1, TaskScheduler = scheduler }); var target = (ITargetBlock <int>)block; var source = new TestSourceBlock <int> (); var header1 = new DataflowMessageHeader(1); Assert.AreEqual(DataflowMessageStatus.Accepted, target.OfferMessage(header1, 41, source, false)); var header2 = new DataflowMessageHeader(2); source.AddMessage(header2, 42); Assert.AreEqual(DataflowMessageStatus.Postponed, target.OfferMessage(header2, 42, source, false)); var header3 = new DataflowMessageHeader(3); source.AddMessage(header3, 43); Assert.AreEqual(DataflowMessageStatus.Postponed, target.OfferMessage(header3, 43, source, false)); Assert.AreEqual(41, block.Receive()); scheduler.ExecuteAll(); Assert.IsTrue(source.WasConsumed(header3)); Assert.IsFalse(source.WasConsumed(header2)); }
public void PostAfterCancellation() { var scheduler = new TestScheduler(); var block = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var tokenSource = new CancellationTokenSource(); Task.Factory.StartNew( () => { Thread.Sleep(100); tokenSource.Cancel(); }); AssertEx.Throws <OperationCanceledException> ( () => block.Receive(tokenSource.Token)); block.Post(1); scheduler.ExecuteAll(); int item; Assert.IsTrue(block.TryReceive(out item)); Assert.AreEqual(1, item); }
public void PostponeTwoTargetsTest() { var scheduler = new TestScheduler(); var source = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var target1 = new TestTargetBlock <int> { Postpone = true }; var target2 = new TestTargetBlock <int> { Postpone = true }; Assert.IsNotNull(source.LinkTo(target1)); Assert.IsNotNull(source.LinkTo(target2)); Assert.IsFalse(target1.HasPostponed); Assert.IsFalse(target2.HasPostponed); Assert.IsTrue(source.Post(42)); scheduler.ExecuteAll(); Assert.IsTrue(target1.HasPostponed); Assert.IsTrue(target2.HasPostponed); int i; Assert.IsTrue(target2.RetryPostponed(out i)); Assert.AreEqual(42, i); Assert.IsFalse(target1.RetryPostponed(out i)); Assert.AreEqual(default(int), i); }
public void ReserveTest() { var scheduler = new TestScheduler(); var source = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new TestTargetBlock <int> { Postpone = true }; Assert.IsNotNull(source.LinkTo(target)); Assert.IsFalse(target.HasPostponed); Assert.IsTrue(source.Post(42)); Assert.IsTrue(source.Post(43)); scheduler.ExecuteAll(); Assert.IsTrue(target.HasPostponed); Assert.IsTrue(target.ReservePostponed()); int i; Assert.IsFalse(source.TryReceive(null, out i)); Assert.AreEqual(default(int), i); IList <int> items; Assert.IsFalse(source.TryReceiveAll(out items)); Assert.AreEqual(default(IList <int>), items); Assert.IsTrue(target.RetryPostponed(out i)); Assert.AreEqual(42, i); Assert.IsTrue(source.TryReceive(null, out i)); Assert.AreEqual(43, i); }
public void NonGreedyJoin3Test() { var scheduler = new TestScheduler(); var block = new JoinBlock <int, int, int> (new GroupingDataflowBlockOptions { TaskScheduler = scheduler, Greedy = false }); var source1 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source3 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull(source1.LinkTo(block.Target1)); Assert.IsNotNull(source2.LinkTo(block.Target2)); Assert.IsNotNull(source3.LinkTo(block.Target3)); Assert.IsTrue(source1.Post(1)); scheduler.ExecuteAll(); int i; Assert.IsTrue(source1.TryReceive(out i)); Assert.IsTrue(source1.Post(2)); Assert.IsTrue(source2.Post(11)); Assert.IsTrue(source3.Post(21)); scheduler.ExecuteAll(); Assert.IsFalse(source1.TryReceive(out i)); Assert.IsFalse(source2.TryReceive(out i)); Assert.IsFalse(source3.TryReceive(out i)); Tuple <int, int, int> tuple; Assert.IsTrue(block.TryReceive(out tuple)); Assert.AreEqual(Tuple.Create(2, 11, 21), tuple); }
public void NonGreedyBatchWithBoundedCapacityTest() { var scheduler = new TestScheduler(); var block = new BatchBlock <int> (2, new GroupingDataflowBlockOptions { Greedy = false, BoundedCapacity = 2, TaskScheduler = scheduler }); var source1 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull(source1.LinkTo(block)); Assert.IsNotNull(source2.LinkTo(block)); Assert.IsTrue(source1.Post(11)); Assert.IsTrue(source2.Post(21)); scheduler.ExecuteAll(); Assert.IsTrue(source1.Post(12)); Assert.IsTrue(source2.Post(22)); scheduler.ExecuteAll(); int i; Assert.IsTrue(source1.TryReceive(out i)); Assert.AreEqual(12, i); Assert.IsTrue(source1.Post(13)); int[] batch; Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.AreEquivalent(new[] { 11, 21 }, batch); scheduler.ExecuteAll(); Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.AreEquivalent(new[] { 13, 22 }, batch); }
public void NonGreedyBatchWithMoreSourcesTest() { var scheduler = new TestScheduler(); var block = new BatchBlock <int> (2, new GroupingDataflowBlockOptions { Greedy = false, TaskScheduler = scheduler }); var source1 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock <int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull(source1.LinkTo(block)); Assert.IsNotNull(source2.LinkTo(block)); Assert.IsTrue(source1.Post(43)); scheduler.ExecuteAll(); int i; Assert.IsTrue(source1.TryReceive(out i)); Assert.AreEqual(43, i); Assert.IsTrue(source1.Post(44)); Assert.IsTrue(source2.Post(45)); scheduler.ExecuteAll(); int[] batch; Assert.IsTrue(block.TryReceive(out batch)); CollectionAssert.AreEquivalent(new[] { 44, 45 }, batch); }
public void AsyncTest() { var tcs = new TaskCompletionSource <int> (); int result = 0; var scheduler = new TestScheduler(); var blocks = GetExecutionBlocksWithAsyncAction( i => tcs.Task.ContinueWith(t => Volatile.Write(ref result, i + t.Result)), new ExecutionDataflowBlockOptions { TaskScheduler = scheduler }); foreach (var block in blocks) { Assert.IsTrue(block.Post(1)); scheduler.ExecuteAll(); Thread.Sleep(100); Thread.MemoryBarrier(); Assert.AreEqual(0, result); tcs.SetResult(10); Thread.Sleep(100); // the continuation should be executed on the configured TaskScheduler Assert.AreEqual(0, result); scheduler.ExecuteAll(); Assert.AreEqual(11, result); tcs = new TaskCompletionSource <int> (); Volatile.Write(ref result, 0); } }
public void MaxMessagesBroadcastTest() { var scheduler = new TestScheduler(); var source = new BroadcastBlock <int> ( null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock <int>( new DataflowBlockOptions { TaskScheduler = scheduler, BoundedCapacity = 1 }); Assert.IsNotNull( source.LinkTo(target, new DataflowLinkOptions { MaxMessages = 2 })); // should be accepted Assert.IsTrue(source.Post(42)); scheduler.ExecuteAll(); // should be postponed, but counted into the limit Assert.IsTrue(source.Post(43)); scheduler.ExecuteAll(); // shouldn't be even offered for now Assert.IsTrue(source.Post(44)); scheduler.ExecuteAll(); int item; Assert.IsTrue(target.TryReceive(out item)); Assert.AreEqual(42, item); scheduler.ExecuteAll(); Assert.IsTrue(target.TryReceive(out item)); Assert.AreEqual(44, item); }
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); }
public void FaultConsumeBroadcast() { var scheduler = new TestScheduler(); var source = new BroadcastBlock <int> (null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new TestTargetBlock <int> { Postpone = true }; Assert.IsNotNull(source.LinkTo(target)); Assert.IsTrue(source.Post(42)); scheduler.ExecuteAll(); Assert.IsTrue(target.HasPostponed); var exception = new Exception(); ((IDataflowBlock)source).Fault(exception); scheduler.ExecuteAll(); try { source.Completion.Wait(1000); Assert.Fail("Task must be faulted"); } catch (AggregateException ex) { Assert.AreEqual(exception, ex.InnerException, "#9"); } Assert.IsTrue(source.Completion.IsFaulted); int value; Assert.IsTrue(target.RetryPostponed(out value)); Assert.AreEqual(42, value); }
public void TriggerBatchWhenEmpty() { var scheduler = new TestScheduler(); var block = new BatchBlock <int> (5, new GroupingDataflowBlockOptions { TaskScheduler = scheduler }); block.TriggerBatch(); scheduler.ExecuteAll(); int[] batch; Assert.IsFalse(block.TryReceive(out batch)); Assert.IsNull(batch); }
public void AsyncNullTest() { var scheduler = new TestScheduler (); var block = new ActionBlock<int> ( i => null, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsTrue (block.Post (1)); scheduler.ExecuteAll (); Assert.IsFalse (block.Completion.Wait (100)); block.Complete (); Assert.IsTrue (block.Completion.Wait (100)); }
public void AsyncCancelledTest () { var scheduler = new TestScheduler (); var block = new TransformBlock<int, int> ( i => { var tcs = new TaskCompletionSource<int> (); tcs.SetCanceled (); return tcs.Task; }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsTrue (block.Post (1)); scheduler.ExecuteAll (); Assert.IsFalse (block.Completion.Wait (100)); }
public void LinkToPredicatePostponed () { var scheduler = new TestScheduler (); var source = new BufferBlock<int> ( new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock<int> ( new DataflowBlockOptions { BoundedCapacity = 1, TaskScheduler = scheduler }); source.LinkTo (target, i => true); Assert.IsTrue (target.Post (1)); Assert.IsTrue (source.Post (2)); scheduler.ExecuteAll (); int item; Assert.IsTrue (target.TryReceive (out item)); Assert.AreEqual (1, item); scheduler.ExecuteAll (); Assert.IsTrue (target.TryReceive (out item)); Assert.AreEqual (2, item); }
public void TryReceiveAllTest() { var scheduler = new TestScheduler(); var block = new BroadcastBlock<int>(null, new DataflowBlockOptions { TaskScheduler = scheduler }); IReceivableSourceBlock<int> source = block; Assert.IsTrue(block.Post(1)); Assert.IsTrue(block.Post(2)); scheduler.ExecuteAll(); IList<int> items; Assert.IsTrue(source.TryReceiveAll(out items)); CollectionAssert.AreEqual(new[] { 2 }, items); }
public void BoundedPostponedTest () { var scheduler = new TestScheduler (); var broadcast = new BroadcastBlock<int> ( null, new DataflowBlockOptions { TaskScheduler = scheduler, BoundedCapacity = 1 }); ITargetBlock<int> target = broadcast; var source = new TestSourceBlock<int> (); Assert.IsTrue (broadcast.Post (1)); var header = new DataflowMessageHeader (1); source.AddMessage (header, 2); Assert.AreEqual (DataflowMessageStatus.Postponed, target.OfferMessage (header, 2, source, false)); Assert.IsFalse (source.WasConsumed (header)); scheduler.ExecuteAll (); Assert.IsTrue (source.WasConsumed (header)); }
public void ReserveChangedTest () { var scheduler = new TestScheduler (); var broadcast = new BroadcastBlock<int> (null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new TestTargetBlock<int> { Postpone = true }; broadcast.LinkTo (target); Assert.IsFalse (target.HasPostponed); Assert.IsTrue (broadcast.Post (1)); scheduler.ExecuteAll (); Assert.IsTrue (target.HasPostponed); Assert.IsTrue(broadcast.Post(2)); scheduler.ExecuteAll (); Assert.IsTrue (target.ReservePostponed ()); int value; Assert.IsTrue (target.RetryPostponed (out value)); Assert.AreEqual (2, value); }
public void NonGreedyBatchWithMoreSourcesTest () { var scheduler = new TestScheduler (); var block = new BatchBlock<int> (2, new GroupingDataflowBlockOptions { Greedy = false, TaskScheduler = scheduler }); var source1 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull (source1.LinkTo (block)); Assert.IsNotNull (source2.LinkTo (block)); Assert.IsTrue (source1.Post (43)); scheduler.ExecuteAll (); int i; Assert.IsTrue (source1.TryReceive (out i)); Assert.AreEqual (43, i); Assert.IsTrue (source1.Post (44)); Assert.IsTrue (source2.Post (45)); scheduler.ExecuteAll (); int[] batch; Assert.IsTrue (block.TryReceive (out batch)); CollectionAssert.AreEquivalent (new[] { 44, 45 }, batch); }
public void MaxMessagesPostponedUnconsumedTest () { var scheduler = new TestScheduler (); var source = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock<int> ( new DataflowBlockOptions { TaskScheduler = scheduler, BoundedCapacity = 1 }); Assert.IsNotNull ( source.LinkTo (target, new DataflowLinkOptions { MaxMessages = 2 })); Assert.IsTrue (source.Post (42)); Assert.IsTrue (source.Post (43)); Assert.IsTrue (source.Post (44)); Assert.IsTrue (source.Post (45)); scheduler.ExecuteAll (); int item; Assert.IsTrue (source.TryReceive (null, out item)); Assert.AreEqual (43, item); Assert.IsTrue (target.TryReceive (null, out item)); Assert.AreEqual (42, item); Assert.IsFalse (target.TryReceive (null, out item)); scheduler.ExecuteAll (); Assert.IsTrue (target.TryReceive (null, out item)); Assert.AreEqual (44, item); scheduler.ExecuteAll (); Assert.IsFalse (target.TryReceive (null, out item)); Assert.IsTrue (source.TryReceive (null, out item)); Assert.AreEqual (45, item); }
public void TriggerBatchWhenEmpty () { var scheduler = new TestScheduler (); var block = new BatchBlock<int> (5, new GroupingDataflowBlockOptions { TaskScheduler = scheduler }); block.TriggerBatch (); scheduler.ExecuteAll (); int[] batch; Assert.IsFalse (block.TryReceive (out batch)); Assert.IsNull (batch); }
public void NonGreedyJoin3WithBoundedCapacityTest () { var scheduler = new TestScheduler (); var block = new JoinBlock<int, int, int> ( new GroupingDataflowBlockOptions { Greedy = false, BoundedCapacity = 1, TaskScheduler = scheduler }); var source1 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source3 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull (source1.LinkTo (block.Target1)); Assert.IsNotNull (source2.LinkTo (block.Target2)); Assert.IsNotNull (source3.LinkTo (block.Target3)); Assert.IsTrue (source1.Post (11)); Assert.IsTrue (source2.Post (21)); Assert.IsTrue (source3.Post (31)); scheduler.ExecuteAll (); Assert.IsTrue (source1.Post (12)); Assert.IsTrue (source2.Post (22)); Assert.IsTrue (source3.Post (32)); scheduler.ExecuteAll (); int i; Assert.IsTrue (source1.TryReceive (out i)); Assert.AreEqual (12, i); Assert.IsTrue (source1.Post (13)); Tuple<int, int, int> tuple; Assert.IsTrue (block.TryReceive (out tuple)); Assert.AreEqual (Tuple.Create (11, 21, 31), tuple); scheduler.ExecuteAll (); Assert.IsTrue (block.TryReceive (out tuple)); Assert.AreEqual (Tuple.Create (13, 22, 32), tuple); }
public void AsyncTest () { var tcs = new TaskCompletionSource<int> (); int result = 0; var scheduler = new TestScheduler (); var blocks = GetExecutionBlocksWithAsyncAction ( i => tcs.Task.ContinueWith (t => Volatile.Write (ref result, i + t.Result)), new ExecutionDataflowBlockOptions { TaskScheduler = scheduler }); foreach (var block in blocks) { Assert.IsTrue (block.Post (1)); scheduler.ExecuteAll (); Thread.Sleep (100); Thread.MemoryBarrier (); Assert.AreEqual (0, result); tcs.SetResult (10); Thread.Sleep (100); // the continuation should be executed on the configured TaskScheduler Assert.AreEqual (0, result); scheduler.ExecuteAll (); Assert.AreEqual (11, result); tcs = new TaskCompletionSource<int> (); Volatile.Write (ref result, 0); } }
public void AsyncExceptionTest () { var scheduler = new TestScheduler (); var exception = new Exception (); var blocks = GetExecutionBlocksWithAsyncAction ( i => { var tcs = new TaskCompletionSource<int> (); tcs.SetException (exception); return tcs.Task; }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler }); foreach (var block in blocks) { Assert.IsTrue (block.Post (1)); // the task should be executed on the configured TaskScheduler Assert.IsFalse (block.Completion.Wait (100)); scheduler.ExecuteAll (); var ae = AssertEx.Throws<AggregateException> (() => block.Completion.Wait (1000)). Flatten (); Assert.AreEqual (1, ae.InnerExceptions.Count); Assert.AreSame (exception, ae.InnerException); } }
public void MaxNumberOfGroupsTest () { var scheduler = new TestScheduler (); var block = new JoinBlock<int, int> ( new GroupingDataflowBlockOptions { MaxNumberOfGroups = 1, TaskScheduler = scheduler }); Assert.IsTrue (block.Target1.Post (1)); Assert.IsFalse (block.Target1.Post (2)); Assert.IsTrue (block.Target2.Post (3)); Assert.IsFalse (block.Target2.Post (4)); Tuple<int, int> batch; Assert.IsTrue (block.TryReceive (out batch)); Assert.AreEqual (Tuple.Create (1, 3), batch); Assert.IsFalse (block.TryReceive (out batch)); scheduler.ExecuteAll (); Assert.IsTrue (block.Completion.Wait (1000)); }
public void NonGreedyMaxNumberOfGroupsTest () { var scheduler = new TestScheduler (); var block = new JoinBlock<int, int> ( new GroupingDataflowBlockOptions { MaxNumberOfGroups = 1, Greedy = false, TaskScheduler = scheduler }); var source1 = new TestSourceBlock<int> (); var source2 = new TestSourceBlock<int> (); var header1 = new DataflowMessageHeader (1); source1.AddMessage (header1, 11); source2.AddMessage (header1, 21); Assert.AreEqual (DataflowMessageStatus.Postponed, block.Target1.OfferMessage (header1, 11, source1, false)); Assert.AreEqual (DataflowMessageStatus.Postponed, block.Target2.OfferMessage (header1, 21, source2, false)); scheduler.ExecuteAll (); Assert.IsTrue (source1.WasConsumed (header1)); Assert.IsTrue (source2.WasConsumed (header1)); var header2 = new DataflowMessageHeader (2); Assert.AreEqual (DataflowMessageStatus.DecliningPermanently, block.Target1.OfferMessage (header2, 21, source1, false)); Tuple<int, int> tuple; Assert.IsTrue (block.TryReceive (out tuple)); Assert.AreEqual (Tuple.Create (11, 21), tuple); Assert.IsTrue (block.Completion.Wait (1000)); }
public void LinkToPredicateMaxMessagesTest () { var scheduler = new TestScheduler (); var source = new BufferBlock<int> ( new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock<int> (); source.LinkTo (target, new DataflowLinkOptions { MaxMessages = 1 }, i => i % 2 == 1); Assert.IsTrue (source.Post (2)); Assert.IsTrue (source.Post (1)); Assert.IsTrue (source.Post (3)); scheduler.ExecuteAll (); int item; Assert.IsFalse (target.TryReceive (out item)); Assert.IsTrue (source.TryReceive (out item)); Assert.AreEqual (2, item); scheduler.ExecuteAll (); Assert.IsTrue (target.TryReceive (out item)); Assert.AreEqual (1, item); scheduler.ExecuteAll (); Assert.IsFalse (target.TryReceive (out item)); }
public void TaskSchedulerTest () { var scheduler = new TestScheduler (); int n = 0; var action = new ActionBlock<int> ( i => Interlocked.Increment (ref n), new ExecutionDataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsTrue (action.Post (1)); Assert.AreEqual (0, Volatile.Read (ref n)); Assert.AreEqual (1, scheduler.ExecuteAll ()); Assert.AreEqual (1, Volatile.Read (ref n)); }
public void DefaultSchedulerIsDefaultTest () { var scheduler = new TestScheduler (); var factory = new TaskFactory (scheduler); ActionBlock<int> action = null; var task = factory.StartNew (() => { Assert.AreEqual (scheduler, TaskScheduler.Current); action = new ActionBlock<int> ( i => Assert.AreNotEqual (scheduler, TaskScheduler.Current)); Assert.IsTrue (action.Post (1)); action.Complete (); }); Assert.AreEqual (1, scheduler.ExecuteAll ()); Assert.IsNotNull (action); Assert.IsTrue (action.Completion.Wait (1000)); Assert.IsTrue (task.Wait (0)); }
public void NonGreedyBatchWithBoundedCapacityTest () { var scheduler = new TestScheduler (); var block = new BatchBlock<int> (2, new GroupingDataflowBlockOptions { Greedy = false, BoundedCapacity = 2, TaskScheduler = scheduler }); var source1 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull (source1.LinkTo (block)); Assert.IsNotNull (source2.LinkTo (block)); Assert.IsTrue (source1.Post (11)); Assert.IsTrue (source2.Post (21)); scheduler.ExecuteAll (); Assert.IsTrue (source1.Post (12)); Assert.IsTrue (source2.Post (22)); scheduler.ExecuteAll (); int i; Assert.IsTrue (source1.TryReceive (out i)); Assert.AreEqual (12, i); Assert.IsTrue (source1.Post (13)); int[] batch; Assert.IsTrue (block.TryReceive (out batch)); CollectionAssert.AreEquivalent (new[] { 11, 21 }, batch); scheduler.ExecuteAll (); Assert.IsTrue (block.TryReceive (out batch)); CollectionAssert.AreEquivalent (new[] { 13, 22 }, batch); }
public void MaxMessagesBroadcastTest () { var scheduler = new TestScheduler (); var source = new BroadcastBlock<int> ( null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock<int>( new DataflowBlockOptions { TaskScheduler = scheduler, BoundedCapacity = 1 }); Assert.IsNotNull ( source.LinkTo (target, new DataflowLinkOptions { MaxMessages = 2 })); // should be accepted Assert.IsTrue (source.Post (42)); scheduler.ExecuteAll (); // should be postponed, but counted into the limit Assert.IsTrue (source.Post (43)); scheduler.ExecuteAll (); // shouldn't be even offered for now Assert.IsTrue (source.Post (44)); scheduler.ExecuteAll (); int item; Assert.IsTrue (target.TryReceive (out item)); Assert.AreEqual (42, item); scheduler.ExecuteAll (); Assert.IsTrue (target.TryReceive (out item)); Assert.AreEqual (44, item); }
public void GreedyBatchWithBoundedCapacityTest () { var scheduler = new TestScheduler (); var block = new BatchBlock<int> (3, new GroupingDataflowBlockOptions { Greedy = true, BoundedCapacity = 3, TaskScheduler = scheduler }); Assert.IsTrue (block.Post (1)); Assert.IsTrue (block.Post (2)); block.TriggerBatch (); scheduler.ExecuteAll (); Assert.IsTrue (block.Post (3)); Assert.IsFalse (block.Post (4)); int[] batch; Assert.IsTrue (block.TryReceive (out batch)); CollectionAssert.AreEqual (new[] { 1, 2 }, batch); Assert.IsTrue (block.Post (5)); Assert.IsTrue (block.Post (6)); }
public void NonGreedyBatchTest () { var scheduler = new TestScheduler (); var block = new BatchBlock<int> (3, new GroupingDataflowBlockOptions { Greedy = false, TaskScheduler = scheduler }); Assert.IsFalse (block.Post (42)); var source = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull (source.LinkTo (block)); Assert.IsTrue (source.Post (43)); scheduler.ExecuteAll (); int i; Assert.IsTrue (source.TryReceive (null, out i)); Assert.AreEqual (43, i); Assert.IsTrue (source.Post (44)); Assert.IsTrue (source.Post (45)); var source2 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull (source2.LinkTo (block)); Assert.IsTrue (source2.Post (142)); scheduler.ExecuteAll (); int[] batch; Assert.IsFalse (block.TryReceive (null, out batch)); Assert.IsNull (batch); block.TriggerBatch (); scheduler.ExecuteAll (); Assert.IsTrue (block.TryReceive (null, out batch)); CollectionAssert.AreEquivalent (new[] { 44, 142 }, batch); }
public void NonGreedyBatchWithBoundedCapacityTriggerTest2 () { var scheduler = new TestScheduler (); var block = new BatchBlock<int> (3, new GroupingDataflowBlockOptions { Greedy = false, BoundedCapacity = 3, TaskScheduler = scheduler }); var source1 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); var source2 = new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler }); Assert.IsNotNull (source1.LinkTo (block)); Assert.IsNotNull (source2.LinkTo (block)); // trigger 2, then trigger another 2 and then trigger 2 once more // while havaing capacity of 3 Assert.IsTrue (source1.Post (11)); Assert.IsTrue (source2.Post (21)); block.TriggerBatch (); scheduler.ExecuteAll (); Assert.IsTrue (source1.Post (12)); Assert.IsTrue (source2.Post (22)); block.TriggerBatch (); scheduler.ExecuteAll (); Assert.IsTrue (source1.Post (13)); Assert.IsTrue (source2.Post (23)); block.TriggerBatch (); scheduler.ExecuteAll (); int i; Assert.IsTrue (source1.TryReceive (out i)); Assert.AreEqual (13, i); Assert.IsTrue (source2.TryReceive (out i)); Assert.AreEqual (23, i); int[] batch; Assert.IsTrue (block.TryReceive (out batch)); CollectionAssert.AreEquivalent (new[] { 11, 21 }, batch); Assert.IsTrue (block.TryReceive (out batch)); CollectionAssert.AreEquivalent (new[] { 12, 22 }, batch); Assert.IsFalse (block.TryReceive (out batch)); }
public void QueuedMessagesTest () { var scheduler = new TestScheduler (); var broadcast = new BroadcastBlock<int> (null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock<int> (); broadcast.LinkTo (target); Assert.IsTrue (broadcast.Post (1)); Assert.IsTrue (broadcast.Post (2)); AssertEx.Throws<TimeoutException> ( () => target.Receive (TimeSpan.FromMilliseconds (100))); scheduler.ExecuteAll (); int item; Assert.IsTrue (target.TryReceive (out item)); Assert.AreEqual (1, item); Assert.IsTrue (target.TryReceive (out item)); Assert.AreEqual (2, item); }
public void MaxNumberOfGroupsTest () { var scheduler = new TestScheduler (); var block = new BatchBlock<int> (2, new GroupingDataflowBlockOptions { MaxNumberOfGroups = 2, TaskScheduler = scheduler }); Assert.IsTrue (block.Post (1)); Assert.IsTrue (block.Post (2)); Assert.IsTrue (block.Post (3)); Assert.IsTrue (block.Post (4)); Assert.IsFalse (block.Post (5)); scheduler.ExecuteAll (); int[] batch; Assert.IsTrue (block.TryReceive (out batch)); CollectionAssert.AreEqual (new[] { 1, 2 }, batch); Assert.IsTrue (block.TryReceive (out batch)); CollectionAssert.AreEqual (new[] { 3, 4 }, batch); scheduler.ExecuteAll (); Assert.IsTrue (block.Completion.Wait (1000)); }
public void TryReceiveTest() { var scheduler = new TestScheduler(); var block = new BroadcastBlock<int>(i => i * 10, new DataflowBlockOptions { TaskScheduler = scheduler }); int item; Assert.IsFalse(block.TryReceive(null, out item)); Assert.IsTrue(block.Post(1)); Assert.IsTrue(block.Post(2)); scheduler.ExecuteAll(); Assert.IsTrue(block.TryReceive(null, out item)); Assert.AreEqual(20, item); // predicate is tested on original value, but returned is cloned Assert.IsTrue(block.TryReceive(i => i < 10, out item)); Assert.AreEqual(20, item); }
public void NonGreedyMaxNumberOfGroupsTest () { var scheduler = new TestScheduler (); var block = new BatchBlock<int> (2, new GroupingDataflowBlockOptions { MaxNumberOfGroups = 1, Greedy = false, TaskScheduler = scheduler }); ITargetBlock<int> target = block; var source1 = new TestSourceBlock<int> (); var source2 = new TestSourceBlock<int> (); var header1 = new DataflowMessageHeader (1); source1.AddMessage (header1, 11); source2.AddMessage (header1, 21); Assert.AreEqual (DataflowMessageStatus.Postponed, target.OfferMessage (header1, 11, source1, false)); Assert.AreEqual (DataflowMessageStatus.Postponed, target.OfferMessage (header1, 21, source2, false)); scheduler.ExecuteAll (); Assert.IsTrue (source1.WasConsumed (header1)); Assert.IsTrue (source2.WasConsumed (header1)); var header2 = new DataflowMessageHeader (2); Assert.AreEqual (DataflowMessageStatus.DecliningPermanently, target.OfferMessage (header2, 21, source1, false)); int[] batch; Assert.IsTrue (block.TryReceive (out batch)); CollectionAssert.AreEquivalent (new[] { 11, 21 }, batch); Assert.IsTrue (block.Completion.Wait (1000)); }
public void DontOfferTwiceTest() { var scheduler = new TestScheduler (); var block = new BroadcastBlock<int> (null, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new TestTargetBlock<int> { Postpone = true }; block.LinkTo (target); Assert.IsFalse (target.HasPostponed); Assert.IsTrue (block.Post (1)); scheduler.ExecuteAll(); Assert.IsTrue (target.HasPostponed); target.Postpone = false; int value; Assert.IsTrue(target.RetryPostponed(out value)); Assert.AreEqual(1, value); block.LinkTo(new BufferBlock<int>()); scheduler.ExecuteAll(); Assert.AreEqual(default(int), target.DirectlyAccepted); }
public void LinkToPredicateClonerTest () { var scheduler = new TestScheduler (); var source = new BroadcastBlock<int> (i => i * 10, new DataflowBlockOptions { TaskScheduler = scheduler }); var target = new BufferBlock<int> (); source.LinkTo (target, i => i < 10); Assert.IsTrue (source.Post (1)); scheduler.ExecuteAll (); int item; Assert.IsTrue (target.TryReceive (out item)); Assert.AreEqual (10, item); }