public void TryReceiveBehaviorTest() { var block = new WriteOnceBlock <int> (null); int foo; Assert.IsFalse(block.TryReceive(null, out foo)); block.Post(42); Assert.IsTrue(block.TryReceive(null, out foo)); Assert.AreEqual(42, foo); Assert.IsTrue(block.TryReceive(null, out foo)); Assert.IsFalse(block.TryReceive(i => i == 0, out foo)); IList <int> bar; Assert.IsTrue(((IReceivableSourceBlock <int>)block).TryReceiveAll(out bar)); CollectionAssert.AreEqual(new[] { 42 }, bar); }
public async Task TestCancellationBeforeAndAfterCtor() { foreach (bool before in DataflowTestHelpers.BooleanValues) { var cts = new CancellationTokenSource(); if (before) { cts.Cancel(); } var wob = new WriteOnceBlock <int>(null, new DataflowBlockOptions { CancellationToken = cts.Token }); if (!before) { cts.Cancel(); } int ignoredValue; IList <int> ignoredValues; Assert.NotNull(wob.LinkTo(DataflowBlock.NullTarget <int>())); Assert.False(wob.Post(42)); Task <bool> sendTask = wob.SendAsync(43); Assert.True(sendTask.IsCompleted); Assert.False(sendTask.Result); Assert.False(wob.TryReceive(out ignoredValue)); Assert.False(((IReceivableSourceBlock <int>)wob).TryReceiveAll(out ignoredValues)); Assert.NotNull(wob.Completion); await Assert.ThrowsAnyAsync <OperationCanceledException>(() => wob.Completion); } }
public async Task TestTryReceiveWithFilter() { var wob = new WriteOnceBlock <int>(null); wob.Post(1); int item; Assert.True(wob.TryReceive(out item)); Assert.Equal(expected: 1, actual: item); Assert.True(wob.TryReceive(i => i == 1, out item)); Assert.Equal(expected: 1, actual: item); Assert.False(wob.TryReceive(i => i == 0, out item)); await wob.Completion; }
public void TryReceiveBehavior() { var block = new WriteOnceBlock <int> (null); int foo; Assert.IsFalse(block.TryReceive(null, out foo)); block.Post(42); Assert.IsTrue(block.TryReceive(null, out foo)); Assert.AreEqual(42, foo); Assert.IsTrue(block.TryReceive(null, out foo)); Assert.IsFalse(block.TryReceive(i => i == 0, out foo)); IList <int> bar; Assert.IsTrue(block.TryReceiveAll(out bar)); Assert.IsNotNull(bar); Assert.AreEqual(1, bar.Count); Assert.AreEqual(42, bar[0]); }
public void TestWriteOnceCloning() { // Test cloning when a clone function is provided { var writeOnce = new WriteOnceBlock <int>(x => - x); Assert.True(writeOnce.Post(42), "Expected initial post on cloning WriteOnce to succeed"); Assert.False(writeOnce.Post(43), "Expected secondary post on cloning WriteOnce to fail"); Assert.True(writeOnce.Receive() == -42, "Expected Receive'd data to be a clone"); int item; Assert.True(writeOnce.TryReceive(out item) && item == -42, "Expected TryReceive'd data to be a clone"); IList <int> items; Assert.True(((IReceivableSourceBlock <int>)writeOnce).TryReceiveAll(out items) && items.Count == 1 && items[0] == -42, "Expected TryReceiveAll'd data to be a clone"); var ab = new ActionBlock <int>(i => { Assert.True(i == -42, "Expected propagated data to be a clone."); }); writeOnce.LinkTo(ab); ab.Complete(); Assert.True(ab.Completion.Wait(4000), "Expected action block to complete after cloned data flowed to it"); } // Test successful processing when no clone function exists { var data = new object(); var writeOnce = new WriteOnceBlock <object>(null); Assert.True(writeOnce.Post(data), "Expected initial post on non-cloning WriteOnce to succeed"); Assert.False(writeOnce.Post(new object()), "Expected secondary post on non-cloning WriteOnce to fail"); Assert.True(writeOnce.Receive() == data, "Expected Receive'd data to be original data"); object item; Assert.True(writeOnce.TryReceive(out item) && item == data, "Expected TryReceive'd data to be original data"); IList <object> items; Assert.True(((IReceivableSourceBlock <object>)writeOnce).TryReceiveAll(out items) && items.Count == 1 && items[0] == data, "Expected TryReceiveAll'd data to be original data"); var ab = new ActionBlock <object>(i => { Assert.True(i == data, "Expected propagated data to be original data."); }); writeOnce.LinkTo(ab); ab.Complete(); Assert.True(ab.Completion.Wait(4000), "Expected action block to complete after original data flowed to it"); } }
public async Task TestPostThenReceive() { var wob = new WriteOnceBlock <int>(i => i); for (int i = 10; i < 15; i++) { bool posted = wob.Post(i); Assert.Equal(expected: i == 10, actual: posted); } int item; Assert.True(wob.TryReceive(out item)); Assert.Equal(expected: 10, actual: item); await wob.Completion; wob = new WriteOnceBlock <int>(null); wob.Post(42); Task <int> t = wob.ReceiveAsync(); Assert.True(t.IsCompleted); Assert.Equal(expected: 42, actual: t.Result); await wob.Completion; }
public void RunWriteOnceBlockConformanceTests() { bool passed = true, localPassed = true; { // Test posting then receiving localPassed = true; var wob = new WriteOnceBlock <int>(i => i); int successfulPosts = 0; for (int i = 10; i <= 20; i++) { successfulPosts += wob.Post(i) ? 1 : 0; } localPassed |= successfulPosts == 1; localPassed |= wob.Receive() == 10; Console.WriteLine("{0}: Posting then receiving", localPassed ? "Success" : "Failure"); passed &= localPassed; } { // Test receiving then posting localPassed = true; var wob = new WriteOnceBlock <int>(i => i); Task.Factory.StartNew(() => { Task.Delay(1000).Wait(); wob.Post(42); }); localPassed |= wob.Receive() == 42; localPassed |= wob.Post(43) == false; wob.Completion.Wait(); Console.WriteLine("{0}: Receiving then posting", localPassed ? "Success" : "Failure"); passed &= localPassed; } { // Test broadcasting localPassed = true; var wob = new WriteOnceBlock <int>(i => i + 1); var tb1 = new TransformBlock <int, int>(i => i); var tb2 = new TransformBlock <int, int>(i => i); var tb3 = new TransformBlock <int, int>(i => i); wob.LinkTo(tb1); wob.LinkTo(tb2); wob.LinkTo(tb3); wob.Post(42); localPassed |= tb1.Receive() == 43; localPassed |= tb2.Receive() == 43; localPassed |= tb3.Receive() == 43; Console.WriteLine("{0}: Broadcasting", localPassed ? "Success" : "Failure"); passed &= localPassed; } { // Test using a precanceled token localPassed = true; try { var cts = new CancellationTokenSource(); cts.Cancel(); var dbo = new DataflowBlockOptions { CancellationToken = cts.Token }; var wob = new WriteOnceBlock <int>(i => i, dbo); int ignoredValue; IList <int> ignoredValues; localPassed &= wob.LinkTo(new ActionBlock <int>(delegate { })) != null; localPassed &= wob.SendAsync(42).Result == false; localPassed &= ((IReceivableSourceBlock <int>)wob).TryReceiveAll(out ignoredValues) == false; localPassed &= wob.Post(42) == false; localPassed &= wob.TryReceive(out ignoredValue) == false; localPassed &= wob.Completion != null; wob.Complete(); } catch (Exception) { localPassed = false; } Console.WriteLine("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure"); passed &= localPassed; } { // Test using token canceled after construction localPassed = true; try { var cts = new CancellationTokenSource(); var dbo = new DataflowBlockOptions { CancellationToken = cts.Token }; var wob = new WriteOnceBlock <int>(i => i, dbo); cts.Cancel(); int ignoredValue; IList <int> ignoredValues; localPassed &= wob.LinkTo(new ActionBlock <int>(delegate { })) != null; localPassed &= wob.SendAsync(42).Result == false; localPassed &= ((IReceivableSourceBlock <int>)wob).TryReceiveAll(out ignoredValues) == false; localPassed &= wob.Post(42) == false; localPassed &= wob.TryReceive(out ignoredValue) == false; localPassed &= wob.Completion != null; wob.Complete(); } catch (Exception) { localPassed = false; } Console.WriteLine("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure"); passed &= localPassed; } Assert.True(passed, "Test failed."); }
public async Task TestCloning() { // Test cloning when a clone function is provided { int data = 42; var wob = new WriteOnceBlock <int>(x => - x); Assert.True(wob.Post(data)); Assert.False(wob.Post(data + 1)); for (int i = 0; i < 3; i++) { int item; Assert.True(wob.TryReceive(out item)); Assert.Equal(expected: -data, actual: item); Assert.Equal(expected: -data, actual: wob.Receive()); Assert.Equal(expected: -data, actual: await wob.ReceiveAsync()); IList <int> items; Assert.True(((IReceivableSourceBlock <int>)wob).TryReceiveAll(out items)); Assert.Equal(expected: items.Count, actual: 1); Assert.Equal(expected: -data, actual: items[0]); } int result = 0; var target = new ActionBlock <int>(i => { Assert.Equal(expected: 0, actual: result); result = i; Assert.Equal(expected: -data, actual: i); }); wob.LinkTo(target, new DataflowLinkOptions { PropagateCompletion = true }); await target.Completion; } // Test successful processing when no clone function exists { var data = new object(); var wob = new WriteOnceBlock <object>(null); Assert.True(wob.Post(data)); Assert.False(wob.Post(new object())); object result; for (int i = 0; i < 3; i++) { Assert.True(wob.TryReceive(out result)); Assert.Equal(expected: data, actual: result); Assert.Equal(expected: data, actual: wob.Receive()); Assert.Equal(expected: data, actual: await wob.ReceiveAsync()); IList <object> items; Assert.True(((IReceivableSourceBlock <object>)wob).TryReceiveAll(out items)); Assert.Equal(expected: 1, actual: items.Count); Assert.Equal(expected: data, actual: items[0]); } result = null; var target = new ActionBlock <object>(o => { Assert.Null(result); result = o; Assert.Equal(expected: data, actual: o); }); wob.LinkTo(target, new DataflowLinkOptions { PropagateCompletion = true }); await target.Completion; } }
public void RunWriteOnceBlockConformanceTests() { bool passed = true, localPassed = true; { // Test posting then receiving localPassed = true; var wob = new WriteOnceBlock<int>(i => i); int successfulPosts = 0; for (int i = 10; i <= 20; i++) { successfulPosts += wob.Post(i) ? 1 : 0; } localPassed |= successfulPosts == 1; localPassed |= wob.Receive() == 10; Console.WriteLine("{0}: Posting then receiving", localPassed ? "Success" : "Failure"); passed &= localPassed; } { // Test receiving then posting localPassed = true; var wob = new WriteOnceBlock<int>(i => i); Task.Factory.StartNew(() => { Task.Delay(1000).Wait(); wob.Post(42); }); localPassed |= wob.Receive() == 42; localPassed |= wob.Post(43) == false; wob.Completion.Wait(); Console.WriteLine("{0}: Receiving then posting", localPassed ? "Success" : "Failure"); passed &= localPassed; } { // Test broadcasting localPassed = true; var wob = new WriteOnceBlock<int>(i => i + 1); var tb1 = new TransformBlock<int, int>(i => i); var tb2 = new TransformBlock<int, int>(i => i); var tb3 = new TransformBlock<int, int>(i => i); wob.LinkTo(tb1); wob.LinkTo(tb2); wob.LinkTo(tb3); wob.Post(42); localPassed |= tb1.Receive() == 43; localPassed |= tb2.Receive() == 43; localPassed |= tb3.Receive() == 43; Console.WriteLine("{0}: Broadcasting", localPassed ? "Success" : "Failure"); passed &= localPassed; } { // Test using a precanceled token localPassed = true; try { var cts = new CancellationTokenSource(); cts.Cancel(); var dbo = new DataflowBlockOptions { CancellationToken = cts.Token }; var wob = new WriteOnceBlock<int>(i => i, dbo); int ignoredValue; IList<int> ignoredValues; localPassed &= wob.LinkTo(new ActionBlock<int>(delegate { })) != null; localPassed &= wob.SendAsync(42).Result == false; localPassed &= ((IReceivableSourceBlock<int>)wob).TryReceiveAll(out ignoredValues) == false; localPassed &= wob.Post(42) == false; localPassed &= wob.TryReceive(out ignoredValue) == false; localPassed &= wob.Completion != null; wob.Complete(); } catch (Exception) { localPassed = false; } Console.WriteLine("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure"); passed &= localPassed; } { // Test using token canceled after construction localPassed = true; try { var cts = new CancellationTokenSource(); var dbo = new DataflowBlockOptions { CancellationToken = cts.Token }; var wob = new WriteOnceBlock<int>(i => i, dbo); cts.Cancel(); int ignoredValue; IList<int> ignoredValues; localPassed &= wob.LinkTo(new ActionBlock<int>(delegate { })) != null; localPassed &= wob.SendAsync(42).Result == false; localPassed &= ((IReceivableSourceBlock<int>)wob).TryReceiveAll(out ignoredValues) == false; localPassed &= wob.Post(42) == false; localPassed &= wob.TryReceive(out ignoredValue) == false; localPassed &= wob.Completion != null; wob.Complete(); } catch (Exception) { localPassed = false; } Console.WriteLine("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure"); passed &= localPassed; } Assert.True(passed, "Test failed."); }
public void TestWriteOnceCloning() { // Test cloning when a clone function is provided { var writeOnce = new WriteOnceBlock<int>(x => -x); Assert.True(writeOnce.Post(42), "Expected initial post on cloning WriteOnce to succeed"); Assert.False(writeOnce.Post(43), "Expected secondary post on cloning WriteOnce to fail"); Assert.True(writeOnce.Receive() == -42, "Expected Receive'd data to be a clone"); int item; Assert.True(writeOnce.TryReceive(out item) && item == -42, "Expected TryReceive'd data to be a clone"); IList<int> items; Assert.True(((IReceivableSourceBlock<int>)writeOnce).TryReceiveAll(out items) && items.Count == 1 && items[0] == -42, "Expected TryReceiveAll'd data to be a clone"); var ab = new ActionBlock<int>(i => { Assert.True(i == -42, "Expected propagated data to be a clone."); }); writeOnce.LinkTo(ab); ab.Complete(); Assert.True(ab.Completion.Wait(4000), "Expected action block to complete after cloned data flowed to it"); } // Test successful processing when no clone function exists { var data = new object(); var writeOnce = new WriteOnceBlock<object>(null); Assert.True(writeOnce.Post(data), "Expected initial post on non-cloning WriteOnce to succeed"); Assert.False(writeOnce.Post(new object()), "Expected secondary post on non-cloning WriteOnce to fail"); Assert.True(writeOnce.Receive() == data, "Expected Receive'd data to be original data"); object item; Assert.True(writeOnce.TryReceive(out item) && item == data, "Expected TryReceive'd data to be original data"); IList<object> items; Assert.True(((IReceivableSourceBlock<object>)writeOnce).TryReceiveAll(out items) && items.Count == 1 && items[0] == data, "Expected TryReceiveAll'd data to be original data"); var ab = new ActionBlock<object>(i => { Assert.True(i == data, "Expected propagated data to be original data."); }); writeOnce.LinkTo(ab); ab.Complete(); Assert.True(ab.Completion.Wait(4000), "Expected action block to complete after original data flowed to it"); } }
public void TryReceiveBehavior () { var block = new WriteOnceBlock<int> (null); int foo; Assert.IsFalse (block.TryReceive (null, out foo)); block.Post (42); Assert.IsTrue (block.TryReceive (null, out foo)); Assert.AreEqual (42, foo); Assert.IsTrue (block.TryReceive (null, out foo)); Assert.IsFalse (block.TryReceive (i => i == 0, out foo)); IList<int> bar; Assert.IsTrue (block.TryReceiveAll (out bar)); Assert.IsNotNull (bar); Assert.AreEqual (1, bar.Count); Assert.AreEqual (42, bar[0]); }
public void TryReceiveBehaviorTest () { var block = new WriteOnceBlock<int> (null); int foo; Assert.IsFalse (block.TryReceive (null, out foo)); block.Post (42); Assert.IsTrue (block.TryReceive (null, out foo)); Assert.AreEqual (42, foo); Assert.IsTrue (block.TryReceive (null, out foo)); Assert.IsFalse (block.TryReceive (i => i == 0, out foo)); IList<int> bar; Assert.IsTrue (((IReceivableSourceBlock<int>)block).TryReceiveAll (out bar)); CollectionAssert.AreEqual (new[] { 42 }, bar); }
public async Task TestCloning() { // Test cloning when a clone function is provided { int data = 42; var wob = new WriteOnceBlock<int>(x => -x); Assert.True(wob.Post(data)); Assert.False(wob.Post(data + 1)); for (int i = 0; i < 3; i++) { int item; Assert.True(wob.TryReceive(out item)); Assert.Equal(expected: -data, actual: item); Assert.Equal(expected: -data, actual: wob.Receive()); Assert.Equal(expected: -data, actual: await wob.ReceiveAsync()); IList<int> items; Assert.True(((IReceivableSourceBlock<int>)wob).TryReceiveAll(out items)); Assert.Equal(expected: items.Count, actual: 1); Assert.Equal(expected: -data, actual: items[0]); } int result = 0; var target = new ActionBlock<int>(i => { Assert.Equal(expected: 0, actual: result); result = i; Assert.Equal(expected: -data, actual: i); }); wob.LinkTo(target, new DataflowLinkOptions { PropagateCompletion = true }); await target.Completion; } // Test successful processing when no clone function exists { var data = new object(); var wob = new WriteOnceBlock<object>(null); Assert.True(wob.Post(data)); Assert.False(wob.Post(new object())); object result; for (int i = 0; i < 3; i++) { Assert.True(wob.TryReceive(out result)); Assert.Equal(expected: data, actual: result); Assert.Equal(expected: data, actual: wob.Receive()); Assert.Equal(expected: data, actual: await wob.ReceiveAsync()); IList<object> items; Assert.True(((IReceivableSourceBlock<object>)wob).TryReceiveAll(out items)); Assert.Equal(expected: 1, actual: items.Count); Assert.Equal(expected: data, actual: items[0]); } result = null; var target = new ActionBlock<object>(o => { Assert.Null(result); result = o; Assert.Equal(expected: data, actual: o); }); wob.LinkTo(target, new DataflowLinkOptions { PropagateCompletion = true }); await target.Completion; } }
public async Task TestCancellationBeforeAndAfterCtor() { foreach (bool before in DataflowTestHelpers.BooleanValues) { var cts = new CancellationTokenSource(); if (before) { cts.Cancel(); } var wob = new WriteOnceBlock<int>(null, new DataflowBlockOptions { CancellationToken = cts.Token }); if (!before) { cts.Cancel(); } int ignoredValue; IList<int> ignoredValues; Assert.NotNull(wob.LinkTo(DataflowBlock.NullTarget<int>())); Assert.False(wob.Post(42)); Task<bool> sendTask = wob.SendAsync(43); Assert.True(sendTask.IsCompleted); Assert.False(sendTask.Result); Assert.False(wob.TryReceive(out ignoredValue)); Assert.False(((IReceivableSourceBlock<int>)wob).TryReceiveAll(out ignoredValues)); Assert.NotNull(wob.Completion); await Assert.ThrowsAnyAsync<OperationCanceledException>(() => wob.Completion); } }
public async Task TestTryReceiveWithFilter() { var wob = new WriteOnceBlock<int>(null); wob.Post(1); int item; Assert.True(wob.TryReceive(out item)); Assert.Equal(expected: 1, actual: item); Assert.True(wob.TryReceive(i => i == 1, out item)); Assert.Equal(expected: 1, actual: item); Assert.False(wob.TryReceive(i => i == 0, out item)); await wob.Completion; }
public async Task TestPostThenReceive() { var wob = new WriteOnceBlock<int>(i => i); for (int i = 10; i < 15; i++) { bool posted = wob.Post(i); Assert.Equal(expected: i == 10, actual: posted); } int item; Assert.True(wob.TryReceive(out item)); Assert.Equal(expected: 10, actual: item); await wob.Completion; wob = new WriteOnceBlock<int>(null); wob.Post(42); Task<int> t = wob.ReceiveAsync(); Assert.True(t.IsCompleted); Assert.Equal(expected: 42, actual: t.Result); await wob.Completion; }