InvalidArgumentsTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // InvalidArgumentsTest.cs
  2. //
  3. // Copyright (c) 2012 Petr Onderka
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Threading;
  25. using System.Threading.Tasks.Dataflow;
  26. using NUnit.Framework;
  27. namespace MonoTests.System.Threading.Tasks.Dataflow {
  28. [TestFixture]
  29. public class InvalidArgumentsTest {
  30. [Test]
  31. public void FaultNullTest()
  32. {
  33. foreach (var block in Blocks.CreateBlocks ()) {
  34. AssertEx.Throws<ArgumentNullException> (() => block.Fault (null));
  35. }
  36. }
  37. [Test]
  38. public void ActionBlockTest ()
  39. {
  40. AssertEx.Throws<ArgumentNullException> (
  41. () => new ActionBlock<int> ((Action<int>)null));
  42. AssertEx.Throws<ArgumentNullException> (
  43. () => new ActionBlock<int> (i => { }, null));
  44. }
  45. [Test]
  46. public void BatchBlockTest()
  47. {
  48. AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchBlock<int> (0));
  49. AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchBlock<int> (-1));
  50. AssertEx.Throws<ArgumentOutOfRangeException> (
  51. () => new BatchBlock<int> (2,
  52. new GroupingDataflowBlockOptions { BoundedCapacity = 1 }));
  53. AssertEx.Throws<ArgumentNullException> (() => new BatchBlock<int> (2, null));
  54. }
  55. [Test]
  56. public void BatchedJoinBlockTest()
  57. {
  58. AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchedJoinBlock<int, int> (0));
  59. AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchedJoinBlock<int, int> (-1));
  60. AssertEx.Throws<ArgumentException> (
  61. () => new BatchedJoinBlock<int, int> (1,
  62. new GroupingDataflowBlockOptions { BoundedCapacity = 1 }));
  63. AssertEx.Throws<ArgumentException> (
  64. () => new BatchedJoinBlock<int, int> (1,
  65. new GroupingDataflowBlockOptions { Greedy = false }));
  66. AssertEx.Throws<ArgumentNullException> (() => new BatchedJoinBlock<int, int> (2, null));
  67. }
  68. [Test]
  69. public void BatchedJoinBlock3Test()
  70. {
  71. AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchedJoinBlock<int, int, int> (0));
  72. AssertEx.Throws<ArgumentOutOfRangeException> (() => new BatchedJoinBlock<int, int, int> (-1));
  73. AssertEx.Throws<ArgumentException> (
  74. () => new BatchedJoinBlock<int, int, int> (1,
  75. new GroupingDataflowBlockOptions { BoundedCapacity = 1 }));
  76. AssertEx.Throws<ArgumentException> (
  77. () => new BatchedJoinBlock<int, int, int> (1,
  78. new GroupingDataflowBlockOptions { Greedy = false }));
  79. AssertEx.Throws<ArgumentNullException> (() => new BatchedJoinBlock<int, int, int> (2, null));
  80. }
  81. [Test]
  82. public void BroadcastBlock()
  83. {
  84. // null is valid argument for BroadcastBlock, so this shouldn't throw
  85. new BroadcastBlock<int> (null);
  86. AssertEx.Throws<ArgumentNullException> (() => new BroadcastBlock<int> (i => i, null));
  87. }
  88. [Test]
  89. public void BufferBlockTest()
  90. {
  91. AssertEx.Throws<ArgumentNullException> (() => new BufferBlock<int> (null));
  92. }
  93. [Test]
  94. public void JoinBlockTest()
  95. {
  96. AssertEx.Throws<ArgumentNullException> (() => new JoinBlock<int, int> (null));
  97. }
  98. [Test]
  99. public void JoinBlock3Test()
  100. {
  101. AssertEx.Throws<ArgumentNullException> (() => new JoinBlock<int, int, int> (null));
  102. }
  103. [Test]
  104. public void TransformBlockTest()
  105. {
  106. AssertEx.Throws<ArgumentNullException> (
  107. () => new TransformBlock<int, int> ((Func<int, int>)null));
  108. AssertEx.Throws<ArgumentNullException> (
  109. () => new TransformBlock<int, int> ((Func<int, int>)null,
  110. new ExecutionDataflowBlockOptions ()));
  111. AssertEx.Throws<ArgumentNullException> (
  112. () => new TransformBlock<int, int> (i => i, null));
  113. }
  114. [Test]
  115. public void TransformManyBlockTest()
  116. {
  117. AssertEx.Throws<ArgumentNullException> (
  118. () => new TransformManyBlock<int, int> ((Func<int, IEnumerable<int>>)null));
  119. AssertEx.Throws<ArgumentNullException> (
  120. () => new TransformManyBlock<int, int> ((Func<int, IEnumerable<int>>)null,
  121. new ExecutionDataflowBlockOptions ()));
  122. AssertEx.Throws<ArgumentNullException> (
  123. () => new TransformManyBlock<int, int> (i => new int[0], null));
  124. }
  125. [Test]
  126. public void WriteOnceBlock()
  127. {
  128. // null is valid argument for WriteOnceBlock, so this shouldn't throw
  129. new WriteOnceBlock<int> (null);
  130. AssertEx.Throws<ArgumentNullException> (() => new WriteOnceBlock<int> (i => i, null));
  131. }
  132. [Test]
  133. public void SourceBlockTest()
  134. {
  135. foreach (var block in Blocks.CreateSimpleSourceBlocks<int> ())
  136. SourceBlockTestInternal (block);
  137. SourceBlockTestInternal (new BatchBlock<int>(5));
  138. SourceBlockTestInternal (new BatchedJoinBlock<int, int>(5));
  139. SourceBlockTestInternal (new BatchedJoinBlock<int, int, int>(5));
  140. SourceBlockTestInternal (new JoinBlock<int, int> ());
  141. SourceBlockTestInternal (new JoinBlock<int, int, int> ());
  142. }
  143. static void SourceBlockTestInternal<T>(ISourceBlock<T> block)
  144. {
  145. var target = new BufferBlock<T> ();
  146. bool consumed;
  147. // invalid header
  148. AssertEx.Throws<ArgumentException> (
  149. () =>
  150. block.ConsumeMessage (new DataflowMessageHeader (), target, out consumed));
  151. // header that wasn't sent by the block doesn't throw
  152. block.ConsumeMessage (new DataflowMessageHeader (1), target, out consumed);
  153. AssertEx.Throws<ArgumentNullException> (
  154. () =>
  155. block.ConsumeMessage (new DataflowMessageHeader (1), null, out consumed));
  156. AssertEx.Throws<ArgumentException> (
  157. () =>
  158. block.ReserveMessage (new DataflowMessageHeader (), target));
  159. // header that wasn't sent by the block doesn't throw
  160. block.ReserveMessage (new DataflowMessageHeader (1), target);
  161. AssertEx.Throws<ArgumentNullException> (
  162. () =>
  163. block.ReserveMessage (new DataflowMessageHeader (1), null));
  164. AssertEx.Throws<ArgumentException> (
  165. () =>
  166. block.ReleaseReservation (new DataflowMessageHeader (), target));
  167. AssertEx.Throws<ArgumentNullException>(() => block.LinkTo(null, new DataflowLinkOptions()));
  168. AssertEx.Throws<ArgumentNullException>(() => block.LinkTo(target, null));
  169. }
  170. [Test]
  171. public void TargetBlockTest()
  172. {
  173. foreach (var block in Blocks.CreateTargetBlocks<int> ()) {
  174. // invalid header
  175. AssertEx.Throws<ArgumentException> (
  176. () => block.OfferMessage (new DataflowMessageHeader (), 42, null, false));
  177. // consumeToAccept with null source
  178. AssertEx.Throws<ArgumentException> (
  179. () => block.OfferMessage (new DataflowMessageHeader (1), 42, null, true));
  180. }
  181. }
  182. [Test]
  183. public void ReactiveTest()
  184. {
  185. IPropagatorBlock<int, int> block = null;
  186. AssertEx.Throws<ArgumentNullException> (() => block.AsObservable ());
  187. AssertEx.Throws<ArgumentNullException> (() => block.AsObserver ());
  188. }
  189. [Test]
  190. public void ChooseTest()
  191. {
  192. ISourceBlock<int> nullSource = null;
  193. var realSource = new BufferBlock<int> ();
  194. var options = new DataflowBlockOptions ();
  195. AssertEx.Throws<ArgumentNullException> (
  196. () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }));
  197. AssertEx.Throws<ArgumentNullException> (
  198. () => DataflowBlock.Choose (realSource, null, realSource, i => { }));
  199. AssertEx.Throws<ArgumentNullException> (
  200. () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }));
  201. AssertEx.Throws<ArgumentNullException> (
  202. () => DataflowBlock.Choose (realSource, i => { }, realSource, null));
  203. AssertEx.Throws<ArgumentNullException> (
  204. () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, options));
  205. AssertEx.Throws<ArgumentNullException> (
  206. () => DataflowBlock.Choose (realSource, null, realSource, i => { }, options));
  207. AssertEx.Throws<ArgumentNullException> (
  208. () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }, options));
  209. AssertEx.Throws<ArgumentNullException> (
  210. () => DataflowBlock.Choose (realSource, i => { }, realSource, null, options));
  211. AssertEx.Throws<ArgumentNullException> (
  212. () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, null));
  213. AssertEx.Throws<ArgumentNullException> (
  214. () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, realSource, i => { }));
  215. AssertEx.Throws<ArgumentNullException> (
  216. () => DataflowBlock.Choose (realSource, null, realSource, i => { }, realSource, i => { }));
  217. AssertEx.Throws<ArgumentNullException> (
  218. () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }, realSource, i => { }));
  219. AssertEx.Throws<ArgumentNullException> (
  220. () => DataflowBlock.Choose (realSource, i => { }, realSource, null, realSource, i => { }));
  221. AssertEx.Throws<ArgumentNullException> (
  222. () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, nullSource, i => { }));
  223. AssertEx.Throws<ArgumentNullException> (
  224. () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, realSource, null));
  225. AssertEx.Throws<ArgumentNullException> (
  226. () => DataflowBlock.Choose (nullSource, i => { }, realSource, i => { }, realSource, i => { }, options));
  227. AssertEx.Throws<ArgumentNullException> (
  228. () => DataflowBlock.Choose (realSource, null, realSource, i => { }, realSource, i => { }, options));
  229. AssertEx.Throws<ArgumentNullException> (
  230. () => DataflowBlock.Choose (realSource, i => { }, nullSource, i => { }, realSource, i => { }, options));
  231. AssertEx.Throws<ArgumentNullException> (
  232. () => DataflowBlock.Choose (realSource, i => { }, realSource, null, realSource, i => { }, options));
  233. AssertEx.Throws<ArgumentNullException> (
  234. () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, nullSource, i => { }, options));
  235. AssertEx.Throws<ArgumentNullException> (
  236. () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, realSource, null, options));
  237. AssertEx.Throws<ArgumentNullException> (
  238. () => DataflowBlock.Choose (realSource, i => { }, realSource, i => { }, realSource, i => { }, null));
  239. }
  240. [Test]
  241. public void EncapsulateTest()
  242. {
  243. AssertEx.Throws<ArgumentNullException> (
  244. () =>
  245. DataflowBlock.Encapsulate ((ITargetBlock<int>)null, new BufferBlock<int> ()));
  246. AssertEx.Throws<ArgumentNullException> (
  247. () =>
  248. DataflowBlock.Encapsulate (new BufferBlock<int> (), (ISourceBlock<int>)null));
  249. }
  250. [Test]
  251. public void LinkToTest()
  252. {
  253. IPropagatorBlock<int, int> nullBlock = null;
  254. var realBlock = new BufferBlock<int> ();
  255. AssertEx.Throws<ArgumentNullException> (
  256. () => DataflowBlock.LinkTo (nullBlock, realBlock));
  257. AssertEx.Throws<ArgumentNullException> (
  258. () => DataflowBlock.LinkTo (realBlock, nullBlock));
  259. AssertEx.Throws<ArgumentNullException> (
  260. () => DataflowBlock.LinkTo (nullBlock, realBlock, i => true));
  261. AssertEx.Throws<ArgumentNullException> (
  262. () => DataflowBlock.LinkTo (realBlock, nullBlock, i => true));
  263. AssertEx.Throws<ArgumentNullException> (
  264. () => DataflowBlock.LinkTo (realBlock, realBlock, null));
  265. AssertEx.Throws<ArgumentNullException> (
  266. () => DataflowBlock.LinkTo (nullBlock, realBlock, new DataflowLinkOptions (), i => true));
  267. AssertEx.Throws<ArgumentNullException> (
  268. () => DataflowBlock.LinkTo (realBlock, nullBlock, new DataflowLinkOptions (), i => true));
  269. AssertEx.Throws<ArgumentNullException> (
  270. () => DataflowBlock.LinkTo (realBlock, realBlock, null, i => true));
  271. AssertEx.Throws<ArgumentNullException> (
  272. () => DataflowBlock.LinkTo (realBlock, realBlock, new DataflowLinkOptions (), null));
  273. }
  274. [Test]
  275. public void PostTest()
  276. {
  277. AssertEx.Throws<ArgumentNullException> (() => DataflowBlock.Post (null, 42));
  278. }
  279. [Test]
  280. public void ReceiveTest()
  281. {
  282. var source = new BufferBlock<int> ();
  283. source.Post(1);
  284. source.Post(2);
  285. source.Post(3);
  286. source.Post(4);
  287. AssertEx.Throws<ArgumentNullException> (
  288. () => DataflowBlock.Receive ((ISourceBlock<int>)null));
  289. AssertEx.Throws<ArgumentNullException> (
  290. () => DataflowBlock.Receive ((ISourceBlock<int>)null,
  291. new CancellationToken (false)));
  292. AssertEx.Throws<OperationCanceledException> (
  293. () => DataflowBlock.Receive (source, new CancellationToken (true)));
  294. AssertEx.Throws<ArgumentNullException> (
  295. () => DataflowBlock.Receive ((ISourceBlock<int>)null,
  296. TimeSpan.FromMinutes (1)));
  297. // shouldn't throw
  298. DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-1));
  299. AssertEx.Throws<ArgumentOutOfRangeException> (
  300. () => DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-2)));
  301. // shouldn't throw
  302. DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (int.MaxValue));
  303. AssertEx.Throws<ArgumentOutOfRangeException> (
  304. () => DataflowBlock.Receive (source,
  305. TimeSpan.FromMilliseconds (int.MaxValue + 1L)));
  306. AssertEx.Throws<ArgumentNullException> (
  307. () => DataflowBlock.Receive ((ISourceBlock<int>)null,
  308. TimeSpan.FromMinutes (1), new CancellationToken(false)));
  309. AssertEx.Throws<OperationCanceledException> (
  310. () => DataflowBlock.Receive (source, TimeSpan.FromMinutes (1),
  311. new CancellationToken (true)));
  312. // shouldn't throw
  313. DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-1),
  314. new CancellationToken (false));
  315. AssertEx.Throws<ArgumentOutOfRangeException> (
  316. () => DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (-2),
  317. new CancellationToken (false)));
  318. // shouldn't throw
  319. DataflowBlock.Receive (source, TimeSpan.FromMilliseconds (int.MaxValue),
  320. new CancellationToken (false));
  321. AssertEx.Throws<ArgumentOutOfRangeException> (
  322. () => DataflowBlock.Receive (source,
  323. TimeSpan.FromMilliseconds (int.MaxValue + 1L),
  324. new CancellationToken (false)));
  325. }
  326. [Test]
  327. public void ReceiveAsyncTest()
  328. {
  329. var source = new BufferBlock<int> ();
  330. AssertEx.Throws<ArgumentNullException> (
  331. () => DataflowBlock.ReceiveAsync ((ISourceBlock<int>)null));
  332. AssertEx.Throws<ArgumentNullException> (
  333. () => DataflowBlock.ReceiveAsync ((ISourceBlock<int>)null,
  334. new CancellationToken (false)));
  335. AssertEx.Throws<ArgumentNullException> (
  336. () => DataflowBlock.ReceiveAsync ((ISourceBlock<int>)null,
  337. TimeSpan.FromMinutes (1)));
  338. // shouldn't throw
  339. DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-1));
  340. AssertEx.Throws<ArgumentOutOfRangeException> (
  341. () => DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-2)));
  342. // shouldn't throw
  343. DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (int.MaxValue));
  344. AssertEx.Throws<ArgumentOutOfRangeException> (
  345. () => DataflowBlock.ReceiveAsync (source,
  346. TimeSpan.FromMilliseconds (int.MaxValue + 1L)));
  347. AssertEx.Throws<ArgumentNullException> (
  348. () => DataflowBlock.ReceiveAsync ((ISourceBlock<int>)null,
  349. TimeSpan.FromMinutes (1), new CancellationToken(false)));
  350. // shouldn't throw
  351. DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-1),
  352. new CancellationToken (false));
  353. AssertEx.Throws<ArgumentOutOfRangeException> (
  354. () => DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (-2),
  355. new CancellationToken (false)));
  356. // shouldn't throw
  357. DataflowBlock.ReceiveAsync (source, TimeSpan.FromMilliseconds (int.MaxValue),
  358. new CancellationToken (false));
  359. AssertEx.Throws<ArgumentOutOfRangeException> (
  360. () => DataflowBlock.ReceiveAsync (source,
  361. TimeSpan.FromMilliseconds (int.MaxValue + 1L),
  362. new CancellationToken (false)));
  363. }
  364. [Test]
  365. public void TryReceiveTest()
  366. {
  367. int i;
  368. AssertEx.Throws<ArgumentNullException> (
  369. () => DataflowBlock.TryReceive (null, out i));
  370. }
  371. [Test]
  372. public void DataflowBlockOptionsTest()
  373. {
  374. var options = new DataflowBlockOptions ();
  375. AssertEx.Throws<ArgumentOutOfRangeException>(() => options.BoundedCapacity = -2);
  376. AssertEx.Throws<ArgumentOutOfRangeException>(() => options.MaxMessagesPerTask = -2);
  377. AssertEx.Throws<ArgumentNullException>(() => options.NameFormat = null);
  378. // shouldn't throw
  379. options.NameFormat = "{2}";
  380. new BufferBlock<int>(options).ToString();
  381. AssertEx.Throws<ArgumentNullException>(() => options.TaskScheduler = null);
  382. }
  383. [Test]
  384. public void ExecutionDataflowBlockOptionsTest()
  385. {
  386. var options = new ExecutionDataflowBlockOptions ();
  387. AssertEx.Throws<ArgumentOutOfRangeException>(() => options.MaxDegreeOfParallelism = -2);
  388. }
  389. [Test]
  390. public void GroupingDataflowBlockOptionsTest()
  391. {
  392. var options = new GroupingDataflowBlockOptions ();
  393. AssertEx.Throws<ArgumentOutOfRangeException>(() => options.MaxNumberOfGroups = -2);
  394. }
  395. [Test]
  396. public void DataflowLinkOptionsTest()
  397. {
  398. var options = new DataflowLinkOptions ();
  399. AssertEx.Throws<ArgumentOutOfRangeException>(() => options.MaxMessages = -2);
  400. }
  401. }
  402. }