/Rhino.Etl.Tests/SingleThreadedPipelineExecuterTest.cs

http://github.com/ayende/rhino-etl · C# · 99 lines · 78 code · 21 blank · 0 comment · 1 complexity · 837573ac522d1ca76ca7af5dff4d7bba MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Xunit;
  6. using Rhino.Etl.Core;
  7. using Rhino.Etl.Core.Operations;
  8. using Rhino.Etl.Core.Pipelines;
  9. using Rhino.Etl.Tests.Joins;
  10. using Rhino.Mocks;
  11. namespace Rhino.Etl.Tests
  12. {
  13. public class SingleThreadedPipelineExecuterTest
  14. {
  15. [Fact]
  16. public void OperationsAreExecutedOnce()
  17. {
  18. var iterations = 0;
  19. using (var stubProcess = MockRepository.GenerateStub<EtlProcess>())
  20. {
  21. stubProcess.Stub(x => x.TranslateRows(null)).IgnoreArguments()
  22. .WhenCalled(x => x.ReturnValue = x.Arguments[0]);
  23. stubProcess.PipelineExecuter = new SingleThreadedPipelineExecuter();
  24. stubProcess.Register(new InputSpyOperation(() => iterations++));
  25. stubProcess.Register(new OutputSpyOperation(2));
  26. stubProcess.Execute();
  27. }
  28. Assert.Equal(1, iterations);
  29. }
  30. [Fact]
  31. public void MultipleIterationsYieldSameResults()
  32. {
  33. var accumulator = new ArrayList();
  34. using (var process = MockRepository.GenerateStub<EtlProcess>())
  35. {
  36. process.Stub(x => x.TranslateRows(null)).IgnoreArguments()
  37. .WhenCalled(x => x.ReturnValue = x.Arguments[0]);
  38. process.PipelineExecuter = new SingleThreadedPipelineExecuter();
  39. process.Register(new GenericEnumerableOperation(new[] {Row.FromObject(new {Prop = "Hello"})}));
  40. process.Register(new OutputSpyOperation(2, r => accumulator.Add(r["Prop"])));
  41. process.Execute();
  42. }
  43. Assert.Equal(accumulator.Cast<string>().ToArray(), Enumerable.Repeat("Hello", 2).ToArray());
  44. }
  45. class InputSpyOperation : AbstractOperation
  46. {
  47. private readonly Action onExecute;
  48. public InputSpyOperation(Action onExecute)
  49. {
  50. this.onExecute = onExecute;
  51. }
  52. public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
  53. {
  54. onExecute();
  55. yield break;
  56. }
  57. }
  58. class OutputSpyOperation : AbstractOperation
  59. {
  60. private readonly int numberOfIterations;
  61. private readonly Action<Row> onRow;
  62. public OutputSpyOperation(int numberOfIterations) : this(numberOfIterations, r => {})
  63. {}
  64. public OutputSpyOperation(int numberOfIterations, Action<Row> onRow)
  65. {
  66. this.numberOfIterations = numberOfIterations;
  67. this.onRow = onRow;
  68. }
  69. public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
  70. {
  71. for (var i = 0; i < numberOfIterations; i++)
  72. foreach (var row in rows)
  73. onRow(row);
  74. yield break;
  75. }
  76. }
  77. }
  78. }