PageRenderTime 20ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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