public void PipelineContext_OnReadAllInputs_Overridable() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); MockPipelineContext context = pipeline.Context; bool processorCalled = false; context.OnReadAllInputsCalled = (inProc, inValues) => { // Inject a '3' into the inputs for proc 1 if (inProc == pipeline.Processors[1]) { processorCalled = true; Assert.AreEqual(4, inValues[0], "Expected to see '4' as the pipeline input"); inValues[0] = 3; } }; ProcessorResult procResult = pipeline.Execute(new object[] { 4 }); Assert.IsNotNull(procResult.Output, "Expected output array"); Assert.AreEqual(1, procResult.Output.Length, "Output size mismatch"); Assert.AreEqual(3.ToString(), procResult.Output[0], "Pipeline did not use context we injected"); Assert.IsTrue(processorCalled, "Our ReadAllInputs override was not called"); }
public void PipelineContext_SetProcessorOutput() { Pipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); PipelineContext context = new PipelineContext(pipeline); Assert.IsTrue(context.AdvanceToNextProcessor(), "Failed to advance to first"); // Mimic entry processor producing its output of an intValue=5 context.SetProcessorOutputs(context.CurrentProcessor, new object[] { 5 }); // This should have been copied to the input slots for second processor Assert.IsTrue(context.AdvanceToNextProcessor(), "Failed to advance to second"); object[] input = context.ReadAllInputs(context.CurrentProcessor); Assert.IsNotNull(input, "Input cannot be null"); Assert.AreEqual(1, input.Length, "Expected 1 element in input"); Assert.AreEqual(5, input[0], "Input should have contained a 5"); // Mimic processor 1 producing an output of theResult="aString" context.SetProcessorOutputs(context.CurrentProcessor, new object[] { "aString" }); // This should have been copied to the input slots for third processor Assert.IsTrue(context.AdvanceToNextProcessor(), "Failed to advance to third"); input = context.ReadAllInputs(context.CurrentProcessor); Assert.IsNotNull(input, "Input cannot be null"); Assert.AreEqual(1, input.Length, "Expected 1 element in input"); Assert.AreEqual("aString", input[0], "Input should have contained this string"); }
public void Pipeline_Execute_Throws_Wrong_Output_Length() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); ProcessorResult errorResult = null; // Override the OnExecute to return a ProcessorResult // which contains the wrong ((MockProcessor1)pipeline.Processors[1]).OnExecuteCalled = i => { ProcessorResult pr = new ProcessorResult <string>() { }; pr.Output = new object[] { "string1", "string2" }; return(pr as ProcessorResult <string>); }; pipeline.OnErrorCalled = r => errorResult = r; // The final pipeline result should reflect that error status ProcessorResult result = pipeline.Execute(new object[] { 5 }); Assert.IsNotNull(result, "Failed to get a result"); Assert.AreEqual(ProcessorStatus.Error, result.Status, "Expected failure status"); Assert.IsNotNull(result.Error, "Expected error result"); Assert.AreEqual(typeof(InvalidOperationException), result.Error.GetType(), "Expected InvalidOperationException"); Assert.IsNotNull(errorResult, "Pipeline.OnError should have been called"); }
public void Pipeline_Executes_One_Input_Output_Processor() { Pipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); ProcessorResult result = pipeline.Execute(new object[] { 5 }); object[] output = result.Output; Assert.IsNotNull(output, "Processing output was null"); Assert.AreEqual(1, output.Length, "Should have received 1 output"); Assert.AreEqual(5.ToString(), output[0], "Should have seen this output"); }
public void PipelineContext_ReadInput_Returns_WriteInput() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); MockPipelineContext context = pipeline.Context; context.WriteInput(pipeline.Processors[1].InArguments[0], 7); object value = context.ReadInput(pipeline.Processors[1].InArguments[0]); Assert.AreEqual(7, value, "Expected processor 1 to have this input value after we wrote it"); }
public void PipelineContext_ReadAllInputs_Throws_If_Null_Processor() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); PipelineContext context = new PipelineContext(pipeline); ExceptionAssert.ThrowsArgumentNull( "PipelineContext should throw if null processor specified", "processor", () => context.ReadAllInputs(null) ); }
public void PipelineContext_WriteInput_Throws_If_Null_ProcessorArgument() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); PipelineContext context = new PipelineContext(pipeline); ExceptionAssert.ThrowsArgumentNull( "PipelineContext should throw if null processor argument specified", "inArgument", () => context.WriteInput(null, new object()) ); }
public void PipelineContext_ReadInput_Returns_Result_After_Execution() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); MockPipelineContext context = pipeline.Context; object value = context.ReadInput(pipeline.Processors[1].InArguments[0]); Assert.IsNull(value, "Expected null prior to execution"); ProcessorResult result = pipeline.Execute(new object[] { 7 }); value = context.ReadInput(pipeline.Processors[1].InArguments[0]); Assert.AreEqual(7, value, "Expected processor 1 to have this input value after execution"); }
public void PipelineContext_WriteInput_Throws_If_Containerless_ProcessorArgument() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); PipelineContext context = new PipelineContext(pipeline); ExceptionAssert.Throws( typeof(ArgumentException), "PipelineContext should throw if processor argument belongs to no collection", () => context.WriteInput(new ProcessorArgument("foo", typeof(string)), new object()), "inArgument" ); }
public void PipelineContext_ReadAllInputs_Throws_If_Processor_Not_In_Pipeline() { Processor processor = new MockProcessor1(); MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); PipelineContext context = new PipelineContext(pipeline); ExceptionAssert.Throws( typeof(ArgumentException), "PipelineContext should throw if processor from different pipeline specified", () => context.ReadAllInputs(processor), "processor" ); }
public void PipelineContext_WriteInput_Throws_If_External_ProcessorArgument() { Processor processor = new MockProcessor1(); MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); PipelineContext context = new PipelineContext(pipeline); ExceptionAssert.Throws( typeof(ArgumentException), "PipelineContext should throw if processor argument belongs to different pipeline", () => context.WriteInput(processor.InArguments[0], new object()), "inArgument" ); }
public void PipelineContext_CurrentProcessor_Property() { Pipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); PipelineContext context = new PipelineContext(pipeline); Assert.IsNull(context.CurrentProcessor, "CurrentProcessor should be null before advance to first"); Assert.IsTrue(context.AdvanceToNextProcessor(), "Failed to advance to first"); Assert.AreSame(pipeline.Processors[0], context.CurrentProcessor, "First processor incorrect"); Assert.IsTrue(context.AdvanceToNextProcessor(), "Failed to advance to second"); Assert.AreSame(pipeline.Processors[1], context.CurrentProcessor, "Second processor incorrect"); Assert.IsTrue(context.AdvanceToNextProcessor(), "Failed to advance to third"); Assert.AreSame(pipeline.Processors[2], context.CurrentProcessor, "Third processor incorrect"); Assert.IsFalse(context.AdvanceToNextProcessor(), "Moving off end should have returned false"); Assert.IsNull(context.CurrentProcessor, "CurrentProcessor should be null after final advance"); }
public void PipelineContext_Can_Redirect_Storage() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); // Create our own special context and redirect to it when pipeline executes MockPipelineContextWithOwnStore context = new MockPipelineContextWithOwnStore(pipeline); pipeline.OnCreateContextCalled = () => context; ProcessorResult procResult = pipeline.Execute(new object[] { 4 }); Assert.IsNotNull(procResult.Output, "Expected output array"); Assert.AreEqual(1, procResult.Output.Length, "Output size mismatch"); Assert.AreEqual(4.ToString(), procResult.Output[0], "Pipeline did produce expected value"); Dictionary <ProcessorArgument, object> cache = context.Cache; Assert.AreEqual(2, cache.Count, "Expected 2 cached input values"); }
public void Pipeline_Execute_Base_Creates_Context() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); bool wasCalled = false; // Called back from mock. Null says "call the base.OnCreateContext" pipeline.OnCreateContextCalled = () => { wasCalled = true; return(null); }; ProcessorResult result = pipeline.Execute(new object[] { 5 }); Assert.IsTrue(wasCalled, "OnCreateContext was not called"); object[] output = result.Output; Assert.IsNotNull(output, "Processing output was null"); Assert.AreEqual(1, output.Length, "Should have received 1 output"); Assert.AreEqual(5.ToString(), output[0], "Should have seen this output"); }
public void Pipeline_Executes_Null_Output_Processor() { Pipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); // Override the OnExecute to return a default ProcessorResult // which contains a null Output ((MockProcessor1)pipeline.Processors[1]).OnExecuteCalled = i => new ProcessorResult <string>() { }; // The final pipeline result should reflect that error status ProcessorResult result = pipeline.Execute(new object[] { 5 }); Assert.IsNotNull(result, "Failed to get a result"); Assert.AreEqual(ProcessorStatus.Ok, result.Status, "Expected successful status"); Assert.IsNotNull(result.Output, "Pipeline should have set exit processor outputs"); Assert.AreEqual(1, result.Output.Length, "Expected one output"); Assert.IsNull(result.Output[0], "Expected exit processor's inputs never to have been set"); }
public void PipelineContext_ReadAllInputs() { MockPipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); PipelineContext context = pipeline.Context; Assert.IsTrue(context.AdvanceToNextProcessor(), "Failed to advance to first"); object[] input = context.ReadAllInputs(context.CurrentProcessor); Assert.IsNotNull(input, "Inputs for first processor should not be null"); Assert.AreEqual(0, input.Length, "First processor should have zero inputs"); Assert.IsTrue(context.AdvanceToNextProcessor(), "Failed to advance to second"); input = context.ReadAllInputs(context.CurrentProcessor); Assert.IsNotNull(input, "Inputs for second processor should not be null"); Assert.AreEqual(1, input.Length, "Second processor should have one input"); Assert.IsTrue(context.AdvanceToNextProcessor(), "Failed to advance to third"); input = context.ReadAllInputs(context.CurrentProcessor); Assert.IsNotNull(input, "Inputs for third processor should not be null"); Assert.AreEqual(1, input.Length, "Third processor should have one input"); }
public void Pipeline_Executes_Throwing_Processor() { Pipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); ProcessorResult errorResult = null; // Override the OnExecute to return a failure ((MockProcessor1)pipeline.Processors[1]).OnExecuteCalled = i => { throw new InvalidOperationException("TestException"); }; // Override OnError to capture the error result ((MockProcessor1)pipeline.Processors[1]).OnErrorCalledAction = r => errorResult = r; // The final pipeline result should reflect that error status ProcessorResult result = pipeline.Execute(new object[] { 5 }); Assert.IsNotNull(result, "Failed to get a result"); Assert.AreEqual(ProcessorStatus.Error, result.Status, "Expected error status"); Assert.IsNotNull(errorResult, "OnError should have been called"); }
public void PipelineContextInfo_Ctor_Initializes() { Pipeline pipeline = TestPipelines.CreateMockProcessor1Pipeline(); PipelineContextInfo info = new PipelineContextInfo(pipeline); Assert.AreEqual(pipeline, info.Pipeline, "Pipeline property incorrect"); Assert.AreEqual(2, info.TotalInputValueCount, "Context should have discovered 2 inputs"); // Entry processor should have zero inputs int inputValueOffset; int inputValueCount = info.GetInputValueInfo(0, out inputValueOffset); Assert.AreEqual(0, inputValueCount, "Entry processor should have no inputs"); Assert.AreEqual(inputValueCount, pipeline.Processors[0].InArguments.Count, "Incorrect in arg count for processor 0"); // MockProcessor1 should have 1 input inputValueCount = info.GetInputValueInfo(1, out inputValueOffset); Assert.AreEqual(1, inputValueCount, "MockProcessor1 should have 1 input"); Assert.AreEqual(inputValueCount, pipeline.Processors[1].InArguments.Count, "Incorrect in arg count for processor 1"); // Exit processor should have 1 input inputValueCount = info.GetInputValueInfo(2, out inputValueOffset); Assert.AreEqual(1, inputValueCount, "Exit processor should have 1 input"); Assert.AreEqual(inputValueCount, pipeline.Processors[2].InArguments.Count, "Incorrect in arg count for processor 2"); // // Entry processor should have 1 out arg that maps to 1 in arg in processor 1 // ProcessorArgument outArg; ProcessorArgument[] inArgs; int[] inputValueIndices = info.GetOutputValueInfo(0, 0, out outArg, out inArgs); // That outArg should be "IntValue" and belong to the entry processor Assert.IsNotNull(outArg, "OutputArgument was not returned"); Assert.AreEqual("intValue", outArg.Name, "Processor[0] out arg should have been intValue"); Assert.AreEqual(pipeline.Processors[0], outArg.ContainingCollection.Processor, "OutArg for processor 0 should belong to processor 0"); // That outArg should map to a single inArg in processor 1 Assert.IsNotNull(inArgs, "InputArg array was not returned"); Assert.IsNotNull(inputValueIndices, "Input value indices were not returned"); Assert.AreEqual(inArgs.Length, inputValueIndices.Length, "Input args and their indices arrays should match in length"); Assert.AreEqual(1, inArgs.Length, "Should have found 1 input arg mapped to processor 1's output"); Assert.AreEqual("intValue", inArgs[0].Name, "Entry processor output should have mapped to intValue input"); Assert.AreEqual(pipeline.Processors[1], inArgs[0].ContainingCollection.Processor, "Entry processor output maps to inArg of wrong processor"); // // Processor 1 should have 1 out arg that maps to in arg in exit processor // info.GetOutputValueInfo(1, 0, out outArg, out inArgs); // That outArg should be "theResult" and belong to processor 1 Assert.IsNotNull(outArg, "OutputArgument was not returned"); Assert.AreEqual("MockProcessor1Result", outArg.Name, "Processor 1 out arg should have been MockProcessor1Result"); Assert.AreEqual(pipeline.Processors[1], outArg.ContainingCollection.Processor, "OutArg for processor 1 should belong to processor 1"); // That outArg should map to a single inArg from processor 2 Assert.IsNotNull(inArgs, "InputArg array was not returned"); Assert.IsNotNull(inputValueIndices, "Input value indices were not returned"); Assert.AreEqual(inArgs.Length, inputValueIndices.Length, "Input args and their indices arrays should match in length"); Assert.AreEqual(1, inArgs.Length, "Should have found 1 input arg mapped to entry processor's output"); Assert.AreEqual("theResult", inArgs[0].Name, "Entry processor output should have mapped to intValue input"); Assert.AreEqual(pipeline.Processors[2], inArgs[0].ContainingCollection.Processor, "processor 1 output maps to inArg of wrong processor"); }