Blocks.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Blocks.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.Collections.Generic;
  23. using System.Threading;
  24. using System.Threading.Tasks.Dataflow;
  25. namespace MonoTests {
  26. public static class Blocks {
  27. public static IEnumerable<IDataflowBlock> CreateBlocksWithOptions (
  28. DataflowBlockOptions dataflowBlockOptions,
  29. ExecutionDataflowBlockOptions executionDataflowBlockOptions,
  30. GroupingDataflowBlockOptions groupingDataflowBlockOptions)
  31. {
  32. yield return new ActionBlock<int> (i => { }, executionDataflowBlockOptions);
  33. yield return new BatchBlock<double> (10, groupingDataflowBlockOptions);
  34. yield return new BatchedJoinBlock<dynamic, object> (
  35. 10, groupingDataflowBlockOptions);
  36. yield return new BatchedJoinBlock<dynamic, object, char> (
  37. 10, groupingDataflowBlockOptions);
  38. yield return new BroadcastBlock<byte> (x => x, dataflowBlockOptions);
  39. yield return new BufferBlock<int> (dataflowBlockOptions);
  40. yield return new JoinBlock<double, dynamic> (groupingDataflowBlockOptions);
  41. yield return new JoinBlock<object, char, byte> (
  42. groupingDataflowBlockOptions);
  43. yield return new TransformBlock<int, int> (
  44. i => i, executionDataflowBlockOptions);
  45. yield return new TransformManyBlock<double, dynamic>(
  46. x => new dynamic[0], executionDataflowBlockOptions);
  47. yield return new WriteOnceBlock<object> (x => x, dataflowBlockOptions);
  48. }
  49. public static IEnumerable<IDataflowBlock> CreateBlocks()
  50. {
  51. return CreateBlocksWithOptions (new DataflowBlockOptions (),
  52. new ExecutionDataflowBlockOptions (), new GroupingDataflowBlockOptions ());
  53. }
  54. public static IEnumerable<IDataflowBlock> CreateBlocksWithNameFormat(string nameFormat)
  55. {
  56. var dataflowBlockOptions = new DataflowBlockOptions { NameFormat = nameFormat };
  57. var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions { NameFormat = nameFormat };
  58. var groupingDataflowBlockOptions = new GroupingDataflowBlockOptions { NameFormat = nameFormat };
  59. return CreateBlocksWithOptions (dataflowBlockOptions,
  60. executionDataflowBlockOptions, groupingDataflowBlockOptions);
  61. }
  62. public static IEnumerable<IDataflowBlock> CreateBlocksWithCancellationToken(CancellationToken cancellationToken)
  63. {
  64. var dataflowBlockOptions = new DataflowBlockOptions { CancellationToken = cancellationToken};
  65. var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions { CancellationToken = cancellationToken};
  66. var groupingDataflowBlockOptions = new GroupingDataflowBlockOptions { CancellationToken = cancellationToken};
  67. return CreateBlocksWithOptions (dataflowBlockOptions,
  68. executionDataflowBlockOptions, groupingDataflowBlockOptions);
  69. }
  70. public static IEnumerable<IReceivableSourceBlock<T>> CreateSimpleSourceBlocks<T>()
  71. {
  72. yield return new BroadcastBlock<T> (x => x);
  73. yield return new BufferBlock<T> ();
  74. yield return new TransformBlock<T, T> (
  75. x => x);
  76. yield return new TransformManyBlock<T, T>(
  77. x => new T[0]);
  78. yield return new WriteOnceBlock<T> (x => x);
  79. }
  80. public static IEnumerable<ITargetBlock<T>> CreateTargetBlocks<T> ()
  81. {
  82. yield return new ActionBlock<T> (i => { });
  83. yield return new BatchBlock<T> (10);
  84. yield return new BatchedJoinBlock<T, T> (10).Target1;
  85. yield return new BatchedJoinBlock<T, T> (10).Target2;
  86. yield return new BatchedJoinBlock<T, T, T> (10).Target1;
  87. yield return new BatchedJoinBlock<T, T, T> (10).Target2;
  88. yield return new BatchedJoinBlock<T, T, T> (10).Target3;
  89. yield return new BroadcastBlock<T> (x => x);
  90. yield return new BufferBlock<T> ();
  91. yield return new JoinBlock<T, T> ().Target1;
  92. yield return new JoinBlock<T, T> ().Target2;
  93. yield return new JoinBlock<T, T, T> ().Target1;
  94. yield return new JoinBlock<T, T, T> ().Target2;
  95. yield return new JoinBlock<T, T, T> ().Target3;
  96. yield return new TransformBlock<T, T> (i => i);
  97. yield return new TransformManyBlock<T, T>(x => new T[0]);
  98. yield return new WriteOnceBlock<T> (x => x);
  99. yield return DataflowBlock.NullTarget<T> ();
  100. }
  101. }
  102. }