// InvalidArgumentsTest.cs // // Copyright (c) 2012 Petr Onderka // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks.Dataflow; using NUnit.Framework; namespace MonoTests.System.Threading.Tasks.Dataflow { [TestFixture] public class InvalidArgumentsTest { [Test] public void FaultNullTest() { foreach (var block in Blocks.CreateBlocks ()) { AssertEx.Throws (() => block.Fault (null)); } } [Test] public void ActionBlockTest () { AssertEx.Throws ( () => new ActionBlock ((Action)null)); AssertEx.Throws ( () => new ActionBlock (i => { }, null)); } [Test] public void BatchBlockTest() { AssertEx.Throws (() => new BatchBlock (0)); AssertEx.Throws (() => new BatchBlock (-1)); AssertEx.Throws ( () => new BatchBlock (2, new GroupingDataflowBlockOptions { BoundedCapacity = 1 })); AssertEx.Throws (() => new BatchBlock (2, null)); } [Test] public void BatchedJoinBlockTest() { AssertEx.Throws (() => new BatchedJoinBlock (0)); AssertEx.Throws (() => new BatchedJoinBlock (-1)); AssertEx.Throws ( () => new BatchedJoinBlock (1, new GroupingDataflowBlockOptions { BoundedCapacity = 1 })); AssertEx.Throws ( () => new BatchedJoinBlock (1, new GroupingDataflowBlockOptions { Greedy = false })); AssertEx.Throws (() => new BatchedJoinBlock (2, null)); } [Test] public void BatchedJoinBlock3Test() { AssertEx.Throws (() => new BatchedJoinBlock (0)); AssertEx.Throws (() => new BatchedJoinBlock (-1)); AssertEx.Throws ( () => new BatchedJoinBlock (1, new GroupingDataflowBlockOptions { BoundedCapacity = 1 })); AssertEx.Throws ( () => new BatchedJoinBlock (1, new GroupingDataflowBlockOptions { Greedy = false })); AssertEx.Throws (() => new BatchedJoinBlock (2, null)); } [Test] public void BroadcastBlock() { // null is valid argument for BroadcastBlock, so this shouldn't throw new BroadcastBlock (null); AssertEx.Throws (() => new BroadcastBlock (i => i, null)); } [Test] public void BufferBlockTest() { AssertEx.Throws (() => new BufferBlock (null)); } [Test] public void JoinBlockTest() { AssertEx.Throws (() => new JoinBlock (null)); } [Test] public void JoinBlock3Test() { AssertEx.Throws (() => new JoinBlock (null)); } [Test] public void TransformBlockTest() { AssertEx.Throws ( () => new TransformBlock ((Func)null)); AssertEx.Throws ( () => new TransformBlock ((Func)null, new ExecutionDataflowBlockOptions ())); AssertEx.Throws ( () => new TransformBlock (i => i, null)); } [Test] public void TransformManyBlockTest() { AssertEx.Throws ( () => new TransformManyBlock ((Func>)null)); AssertEx.Throws ( () => new TransformManyBlock ((Func>)null, new ExecutionDataflowBlockOptions ())); AssertEx.Throws ( () => new TransformManyBlock (i => new int[0], null)); } [Test] public void WriteOnceBlock() { // null is valid argument for WriteOnceBlock, so this shouldn't throw new WriteOnceBlock (null); AssertEx.Throws (() => new WriteOnceBlock (i => i, null)); } [Test] public void SourceBlockTest() { foreach (var block in Blocks.CreateSimpleSourceBlocks ()) SourceBlockTestInternal (block); SourceBlockTestInternal (new BatchBlock(5)); SourceBlockTestInternal (new BatchedJoinBlock(5)); SourceBlockTestInternal (new BatchedJoinBlock(5)); SourceBlockTestInternal (new JoinBlock ()); SourceBlockTestInternal (new JoinBlock ()); } static void SourceBlockTestInternal(ISourceBlock block) { var target = new BufferBlock (); bool consumed; // invalid header AssertEx.Throws ( () => block.ConsumeMessage (new DataflowMessageHeader (), target, out consumed)); // header that wasn't sent by the block doesn't throw block.ConsumeMessage (new DataflowMessageHeader (1), target, out consumed); AssertEx.Throws ( () => block.ConsumeMessage (new DataflowMessageHeader (1), null, out consumed)); AssertEx.Throws ( () => block.ReserveMessage (new DataflowMessageHeader (), target)); // header that wasn't sent by the block doesn't throw block.ReserveMessage (new DataflowMessageHeader (1), target); AssertEx.Throws ( () => block.ReserveMessage (new DataflowMessageHeader (1), null)); AssertEx.Throws ( () => block.ReleaseReservation (new DataflowMessageHeader (), target)); AssertEx.Throws(() => block.LinkTo(null, new DataflowLinkOptions())); AssertEx.Throws(() => block.LinkTo(target, null)); } [Test] public void TargetBlockTest() { foreach (var block in Blocks.CreateTargetBlocks ()) { // invalid header AssertEx.Throws ( () => block.OfferMessage (new DataflowMessageHeader (), 42, null, false)); // consumeToAccept with null source AssertEx.Throws ( () => block.OfferMessage (new DataflowMessageHeader (1), 42, null, true)); } } [Test] public void ReactiveTest() { IPropagatorBlock block = null; AssertEx.Throws (() => block.AsObservable ()); AssertEx.Throws (() => block.AsObserver ()); } [Test] public void ChooseTest() { ISourceBlock nullSource = null; var realSource = new BufferBlock (); var options = new DataflowBlockOptions (); AssertEx.Throws ( () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { })); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, null, realSource, i => { })); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { })); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, realSource, null)); AssertEx.Throws ( () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, null, realSource, i => { }, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, realSource, null, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, null)); AssertEx.Throws ( () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, realSource, i => { })); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, null, realSource, i => { }, realSource, i => { })); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }, realSource, i => { })); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, realSource, null, realSource, i => { })); AssertEx.Throws ( () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, nullSource, i => { })); AssertEx.Throws ( () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, realSource, null)); AssertEx.Throws ( () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, realSource, i => { }, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, null, realSource, i => { }, realSource, i => { }, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }, realSource, i => { }, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, realSource, null, realSource, i => { }, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, nullSource, i => { }, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, realSource, null, options)); AssertEx.Throws ( () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, realSource, i => { }, null)); } [Test] public void EncapsulateTest() { AssertEx.Throws ( () => DataflowBlock.Encapsulate ((ITargetBlock)null, new BufferBlock ())); AssertEx.Throws ( () => DataflowBlock.Encapsulate (new BufferBlock (), (ISourceBlock)null)); } [Test] public void LinkToTest() { IPropagatorBlock nullBlock = null; var realBlock = new BufferBlock (); AssertEx.Throws ( () => DataflowBlock.LinkTo (nullBlock, realBlock)); AssertEx.Throws ( () => DataflowBlock.LinkTo (realBlock, nullBlock)); AssertEx.Throws ( () => DataflowBlock.LinkTo (nullBlock, realBlock, i => true)); AssertEx.Throws ( () => DataflowBlock.LinkTo (realBlock, nullBlock, i => true)); AssertEx.Throws ( () => DataflowBlock.LinkTo (realBlock, realBlock, null)); AssertEx.Throws ( () => DataflowBlock.LinkTo (nullBlock, realBlock, new DataflowLinkOptions (), i => true)); AssertEx.Throws ( () => DataflowBlock.LinkTo (realBlock, nullBlock, new DataflowLinkOptions (), i => true)); AssertEx.Throws ( () => DataflowBlock.LinkTo (realBlock, realBlock, null, i => true)); AssertEx.Throws ( () => DataflowBlock.LinkTo (realBlock, realBlock, new DataflowLinkOptions (), null)); } [Test] public void PostTest() { AssertEx.Throws (() => DataflowBlock.Post (null, 42)); } [Test] public void ReceiveTest() { var source = new BufferBlock (); source.Post(1); source.Post(2); source.Post(3); source.Post(4); AssertEx.Throws ( () => DataflowBlock.Receive ((ISourceBlock)null)); AssertEx.Throws ( () => DataflowBlock.Receive ((ISourceBlock)null, new CancellationToken (false))); AssertEx.Throws ( () => DataflowBlock.Receive (source, new CancellationToken (true))); AssertEx.Throws ( () => DataflowBlock.Receive ((ISourceBlock)null, TimeSpan.FromMinutes (1))); // shouldn't throw DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-1)); AssertEx.Throws ( () => DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-2))); // shouldn't throw DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (int.MaxValue)); AssertEx.Throws ( () => DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (int.MaxValue + 1L))); AssertEx.Throws ( () => DataflowBlock.Receive ((ISourceBlock)null, TimeSpan.FromMinutes (1), new CancellationToken(false))); AssertEx.Throws ( () => DataflowBlock.Receive (source, TimeSpan.FromMinutes (1), new CancellationToken (true))); // shouldn't throw DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-1), new CancellationToken (false)); AssertEx.Throws ( () => DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-2), new CancellationToken (false))); // shouldn't throw DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (int.MaxValue), new CancellationToken (false)); AssertEx.Throws ( () => DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (int.MaxValue + 1L), new CancellationToken (false))); } [Test] public void ReceiveAsyncTest() { var source = new BufferBlock (); AssertEx.Throws ( () => DataflowBlock.ReceiveAsync ((ISourceBlock)null)); AssertEx.Throws ( () => DataflowBlock.ReceiveAsync ((ISourceBlock)null, new CancellationToken (false))); AssertEx.Throws ( () => DataflowBlock.ReceiveAsync ((ISourceBlock)null, TimeSpan.FromMinutes (1))); // shouldn't throw DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-1)); AssertEx.Throws ( () => DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-2))); // shouldn't throw DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (int.MaxValue)); AssertEx.Throws ( () => DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (int.MaxValue + 1L))); AssertEx.Throws ( () => DataflowBlock.ReceiveAsync ((ISourceBlock)null, TimeSpan.FromMinutes (1), new CancellationToken(false))); // shouldn't throw DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-1), new CancellationToken (false)); AssertEx.Throws ( () => DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-2), new CancellationToken (false))); // shouldn't throw DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (int.MaxValue), new CancellationToken (false)); AssertEx.Throws ( () => DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (int.MaxValue + 1L), new CancellationToken (false))); } [Test] public void TryReceiveTest() { int i; AssertEx.Throws ( () => DataflowBlock.TryReceive (null, out i)); } [Test] public void DataflowBlockOptionsTest() { var options = new DataflowBlockOptions (); AssertEx.Throws(() => options.BoundedCapacity = -2); AssertEx.Throws(() => options.MaxMessagesPerTask = -2); AssertEx.Throws(() => options.NameFormat = null); // shouldn't throw options.NameFormat = "{2}"; new BufferBlock(options).ToString(); AssertEx.Throws(() => options.TaskScheduler = null); } [Test] public void ExecutionDataflowBlockOptionsTest() { var options = new ExecutionDataflowBlockOptions (); AssertEx.Throws(() => options.MaxDegreeOfParallelism = -2); } [Test] public void GroupingDataflowBlockOptionsTest() { var options = new GroupingDataflowBlockOptions (); AssertEx.Throws(() => options.MaxNumberOfGroups = -2); } [Test] public void DataflowLinkOptionsTest() { var options = new DataflowLinkOptions (); AssertEx.Throws(() => options.MaxMessages = -2); } } }