| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- // 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<ArgumentNullException> (() => block.Fault (null));
- }
- }
- [Test]
- public void ActionBlockTest ()
- {
- AssertEx.Throws<ArgumentNullException> (
- () => new ActionBlock<int> ((Action<int>)null));
- AssertEx.Throws<ArgumentNullException> (
- () => new ActionBlock<int> (i => { }, null));
- }
- [Test]
- public void BatchBlockTest()
- {
- AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchBlock<int> (0));
- AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchBlock<int> (-1));
- AssertEx.Throws<ArgumentOutOfRangeException> (
- () => new BatchBlock<int> (2,
- new GroupingDataflowBlockOptions { BoundedCapacity = 1 }));
- AssertEx.Throws<ArgumentNullException> (() => new BatchBlock<int> (2, null));
- }
- [Test]
- public void BatchedJoinBlockTest()
- {
- AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchedJoinBlock<int, int> (0));
- AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchedJoinBlock<int, int> (-1));
- AssertEx.Throws<ArgumentException> (
- () => new BatchedJoinBlock<int, int> (1,
- new GroupingDataflowBlockOptions { BoundedCapacity = 1 }));
- AssertEx.Throws<ArgumentException> (
- () => new BatchedJoinBlock<int, int> (1,
- new GroupingDataflowBlockOptions { Greedy = false }));
- AssertEx.Throws<ArgumentNullException> (() => new BatchedJoinBlock<int, int> (2, null));
- }
- [Test]
- public void BatchedJoinBlock3Test()
- {
- AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchedJoinBlock<int, int, int> (0));
- AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchedJoinBlock<int, int, int> (-1));
- AssertEx.Throws<ArgumentException> (
- () => new BatchedJoinBlock<int, int, int> (1,
- new GroupingDataflowBlockOptions { BoundedCapacity = 1 }));
- AssertEx.Throws<ArgumentException> (
- () => new BatchedJoinBlock<int, int, int> (1,
- new GroupingDataflowBlockOptions { Greedy = false }));
- AssertEx.Throws<ArgumentNullException> (() => new BatchedJoinBlock<int, int, int> (2, null));
- }
- [Test]
- public void BroadcastBlock()
- {
- // null is valid argument for BroadcastBlock, so this shouldn't throw
- new BroadcastBlock<int> (null);
- AssertEx.Throws<ArgumentNullException> (() => new BroadcastBlock<int> (i => i, null));
- }
- [Test]
- public void BufferBlockTest()
- {
- AssertEx.Throws<ArgumentNullException> (() => new BufferBlock<int> (null));
- }
- [Test]
- public void JoinBlockTest()
- {
- AssertEx.Throws<ArgumentNullException> (() => new JoinBlock<int, int> (null));
- }
- [Test]
- public void JoinBlock3Test()
- {
- AssertEx.Throws<ArgumentNullException> (() => new JoinBlock<int, int, int> (null));
- }
- [Test]
- public void TransformBlockTest()
- {
- AssertEx.Throws<ArgumentNullException> (
- () => new TransformBlock<int, int> ((Func<int, int>)null));
- AssertEx.Throws<ArgumentNullException> (
- () => new TransformBlock<int, int> ((Func<int, int>)null,
- new ExecutionDataflowBlockOptions ()));
- AssertEx.Throws<ArgumentNullException> (
- () => new TransformBlock<int, int> (i => i, null));
- }
- [Test]
- public void TransformManyBlockTest()
- {
- AssertEx.Throws<ArgumentNullException> (
- () => new TransformManyBlock<int, int> ((Func<int, IEnumerable<int>>)null));
- AssertEx.Throws<ArgumentNullException> (
- () => new TransformManyBlock<int, int> ((Func<int, IEnumerable<int>>)null,
- new ExecutionDataflowBlockOptions ()));
- AssertEx.Throws<ArgumentNullException> (
- () => new TransformManyBlock<int, int> (i => new int[0], null));
- }
- [Test]
- public void WriteOnceBlock()
- {
- // null is valid argument for WriteOnceBlock, so this shouldn't throw
- new WriteOnceBlock<int> (null);
- AssertEx.Throws<ArgumentNullException> (() => new WriteOnceBlock<int> (i => i, null));
- }
- [Test]
- public void SourceBlockTest()
- {
- foreach (var block in Blocks.CreateSimpleSourceBlocks<int> ())
- SourceBlockTestInternal (block);
- SourceBlockTestInternal (new BatchBlock<int>(5));
- SourceBlockTestInternal (new BatchedJoinBlock<int, int>(5));
- SourceBlockTestInternal (new BatchedJoinBlock<int, int, int>(5));
- SourceBlockTestInternal (new JoinBlock<int, int> ());
- SourceBlockTestInternal (new JoinBlock<int, int, int> ());
- }
- static void SourceBlockTestInternal<T>(ISourceBlock<T> block)
- {
- var target = new BufferBlock<T> ();
- bool consumed;
- // invalid header
- AssertEx.Throws<ArgumentException> (
- () =>
- 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<ArgumentNullException> (
- () =>
- block.ConsumeMessage (new DataflowMessageHeader (1), null, out consumed));
- AssertEx.Throws<ArgumentException> (
- () =>
- block.ReserveMessage (new DataflowMessageHeader (), target));
- // header that wasn't sent by the block doesn't throw
- block.ReserveMessage (new DataflowMessageHeader (1), target);
- AssertEx.Throws<ArgumentNullException> (
- () =>
- block.ReserveMessage (new DataflowMessageHeader (1), null));
- AssertEx.Throws<ArgumentException> (
- () =>
- block.ReleaseReservation (new DataflowMessageHeader (), target));
- AssertEx.Throws<ArgumentNullException>(() => block.LinkTo(null, new DataflowLinkOptions()));
- AssertEx.Throws<ArgumentNullException>(() => block.LinkTo(target, null));
- }
- [Test]
- public void TargetBlockTest()
- {
- foreach (var block in Blocks.CreateTargetBlocks<int> ()) {
- // invalid header
- AssertEx.Throws<ArgumentException> (
- () => block.OfferMessage (new DataflowMessageHeader (), 42, null, false));
- // consumeToAccept with null source
- AssertEx.Throws<ArgumentException> (
- () => block.OfferMessage (new DataflowMessageHeader (1), 42, null, true));
- }
- }
- [Test]
- public void ReactiveTest()
- {
- IPropagatorBlock<int, int> block = null;
- AssertEx.Throws<ArgumentNullException> (() => block.AsObservable ());
- AssertEx.Throws<ArgumentNullException> (() => block.AsObserver ());
- }
- [Test]
- public void ChooseTest()
- {
- ISourceBlock<int> nullSource = null;
- var realSource = new BufferBlock<int> ();
- var options = new DataflowBlockOptions ();
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, null, realSource, i => { }));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, realSource, null));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, null, realSource, i => { }, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, realSource, null, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, null));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, realSource, i => { }));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, null, realSource, i => { }, realSource, i => { }));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }, realSource, i => { }));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, realSource, null, realSource, i => { }));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, nullSource, i => { }));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, realSource, null));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, realSource, i => { }, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, null, realSource, i => { }, realSource, i => { }, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }, realSource, i => { }, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, realSource, null, realSource, i => { }, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, nullSource, i => { }, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, realSource, null, options));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, realSource, i => { }, null));
- }
- [Test]
- public void EncapsulateTest()
- {
- AssertEx.Throws<ArgumentNullException> (
- () =>
- DataflowBlock.Encapsulate ((ITargetBlock<int>)null, new BufferBlock<int> ()));
- AssertEx.Throws<ArgumentNullException> (
- () =>
- DataflowBlock.Encapsulate (new BufferBlock<int> (), (ISourceBlock<int>)null));
- }
- [Test]
- public void LinkToTest()
- {
- IPropagatorBlock<int, int> nullBlock = null;
- var realBlock = new BufferBlock<int> ();
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.LinkTo (nullBlock, realBlock));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.LinkTo (realBlock, nullBlock));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.LinkTo (nullBlock, realBlock, i => true));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.LinkTo (realBlock, nullBlock, i => true));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.LinkTo (realBlock, realBlock, null));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.LinkTo (nullBlock, realBlock, new DataflowLinkOptions (), i => true));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.LinkTo (realBlock, nullBlock, new DataflowLinkOptions (), i => true));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.LinkTo (realBlock, realBlock, null, i => true));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.LinkTo (realBlock, realBlock, new DataflowLinkOptions (), null));
- }
- [Test]
- public void PostTest()
- {
- AssertEx.Throws<ArgumentNullException> (() => DataflowBlock.Post (null, 42));
- }
- [Test]
- public void ReceiveTest()
- {
- var source = new BufferBlock<int> ();
- source.Post(1);
- source.Post(2);
- source.Post(3);
- source.Post(4);
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Receive ((ISourceBlock<int>)null));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Receive ((ISourceBlock<int>)null,
- new CancellationToken (false)));
- AssertEx.Throws<OperationCanceledException> (
- () => DataflowBlock.Receive (source, new CancellationToken (true)));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Receive ((ISourceBlock<int>)null,
- TimeSpan.FromMinutes (1)));
- // shouldn't throw
- DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-1));
- AssertEx.Throws<ArgumentOutOfRangeException> (
- () => DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-2)));
- // shouldn't throw
- DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (int.MaxValue));
- AssertEx.Throws<ArgumentOutOfRangeException> (
- () => DataflowBlock.Receive (source,
- TimeSpan.FromMilliseconds (int.MaxValue + 1L)));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.Receive ((ISourceBlock<int>)null,
- TimeSpan.FromMinutes (1), new CancellationToken(false)));
- AssertEx.Throws<OperationCanceledException> (
- () => DataflowBlock.Receive (source, TimeSpan.FromMinutes (1),
- new CancellationToken (true)));
- // shouldn't throw
- DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-1),
- new CancellationToken (false));
- AssertEx.Throws<ArgumentOutOfRangeException> (
- () => DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-2),
- new CancellationToken (false)));
- // shouldn't throw
- DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (int.MaxValue),
- new CancellationToken (false));
- AssertEx.Throws<ArgumentOutOfRangeException> (
- () => DataflowBlock.Receive (source,
- TimeSpan.FromMilliseconds (int.MaxValue + 1L),
- new CancellationToken (false)));
- }
- [Test]
- public void ReceiveAsyncTest()
- {
- var source = new BufferBlock<int> ();
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.ReceiveAsync ((ISourceBlock<int>)null));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.ReceiveAsync ((ISourceBlock<int>)null,
- new CancellationToken (false)));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.ReceiveAsync ((ISourceBlock<int>)null,
- TimeSpan.FromMinutes (1)));
- // shouldn't throw
- DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-1));
- AssertEx.Throws<ArgumentOutOfRangeException> (
- () => DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-2)));
- // shouldn't throw
- DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (int.MaxValue));
- AssertEx.Throws<ArgumentOutOfRangeException> (
- () => DataflowBlock.ReceiveAsync (source,
- TimeSpan.FromMilliseconds (int.MaxValue + 1L)));
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.ReceiveAsync ((ISourceBlock<int>)null,
- TimeSpan.FromMinutes (1), new CancellationToken(false)));
- // shouldn't throw
- DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-1),
- new CancellationToken (false));
- AssertEx.Throws<ArgumentOutOfRangeException> (
- () => DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-2),
- new CancellationToken (false)));
- // shouldn't throw
- DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (int.MaxValue),
- new CancellationToken (false));
- AssertEx.Throws<ArgumentOutOfRangeException> (
- () => DataflowBlock.ReceiveAsync (source,
- TimeSpan.FromMilliseconds (int.MaxValue + 1L),
- new CancellationToken (false)));
- }
- [Test]
- public void TryReceiveTest()
- {
- int i;
- AssertEx.Throws<ArgumentNullException> (
- () => DataflowBlock.TryReceive (null, out i));
- }
- [Test]
- public void DataflowBlockOptionsTest()
- {
- var options = new DataflowBlockOptions ();
- AssertEx.Throws<ArgumentOutOfRangeException>(() => options.BoundedCapacity = -2);
- AssertEx.Throws<ArgumentOutOfRangeException>(() => options.MaxMessagesPerTask = -2);
- AssertEx.Throws<ArgumentNullException>(() => options.NameFormat = null);
- // shouldn't throw
- options.NameFormat = "{2}";
- new BufferBlock<int>(options).ToString();
- AssertEx.Throws<ArgumentNullException>(() => options.TaskScheduler = null);
- }
- [Test]
- public void ExecutionDataflowBlockOptionsTest()
- {
- var options = new ExecutionDataflowBlockOptions ();
- AssertEx.Throws<ArgumentOutOfRangeException>(() => options.MaxDegreeOfParallelism = -2);
- }
- [Test]
- public void GroupingDataflowBlockOptionsTest()
- {
- var options = new GroupingDataflowBlockOptions ();
- AssertEx.Throws<ArgumentOutOfRangeException>(() => options.MaxNumberOfGroups = -2);
- }
- [Test]
- public void DataflowLinkOptionsTest()
- {
- var options = new DataflowLinkOptions ();
- AssertEx.Throws<ArgumentOutOfRangeException>(() => options.MaxMessages = -2);
- }
- }
- }
|