Beispiel #1
0
        public void ThrowingMiddlewareTest()
        {
            ServiceProvider = SetupServices();

            var flowBuilder = new FlowBuilder <TestContext>();

            flowBuilder.Use <MultiplyByFiveMiddleware>();
            flowBuilder.Use <ThrowingMiddleware>();
            flowBuilder.Use <IncrementByOneMiddleware>();

            var flow = flowBuilder.Build();

            var context = new TestContext {
                N = 5
            };
            var exception = Assert.ThrowsAsync <MiddlewareException <TestContext> >(() => flow.InvokeAsync(context, ServiceProvider));

            Assert.AreEqual(ServiceProvider.GetService(typeof(ThrowingMiddleware)), exception.Middleware);
            Assert.AreSame(context, exception.Context);
            Assert.AreEqual(5 * 5, context.N);

            VerifyCreated(true, typeof(MultiplyByFiveMiddleware), typeof(ThrowingMiddleware));
            VerifyReleased(true, typeof(MultiplyByFiveMiddleware), typeof(ThrowingMiddleware));

            VerifyCreated(false, typeof(IncrementByOneMiddleware));
            VerifyReleased(false, typeof(IncrementByOneMiddleware));
        }
Beispiel #2
0
        public async Task ExecutePipeSuccess()
        {
            var element = new Mock <FlowElement>();

            element
            .Protected()
            .Setup <Task <CanExecuteResult> >("OnCanExecuteAsync", ItExpr.IsAny <string>(), ItExpr.IsAny <FlowState>())
            .ReturnsAsync(true);
            element
            .Protected()
            .Setup <Task <FlowElementResult> >("OnExecuteAsync", ItExpr.IsAny <string>(), ItExpr.IsAny <FlowState>())
            .ReturnsAsync(new FlowSuccessResult()
            {
                Result = "res"
            });

            var builder = new FlowBuilder()
                          .Add(element.Object);

            var pipeline = builder.Build();

            var result = await pipeline.RunAsync("test");

            result.Result.Should().Be("res");
        }
Beispiel #3
0
        public async Task PerformanceTest()
        {
            ServiceProvider = SetupServices();

            var flowBuilder = new FlowBuilder <TestContext>();

            flowBuilder.Use <IncrementByOneMiddleware>();
            flowBuilder.Use <MultiplyByFiveMiddleware>();
            flowBuilder.Use <IncrementByOneMiddleware>();
            flowBuilder.Use <MultiplyByFiveMiddleware>();
            flowBuilder.Use <IncrementByOneMiddleware>();
            flowBuilder.Use <MultiplyByFiveMiddleware>();

            var flow    = flowBuilder.Build();
            var context = new TestContext {
                N = 0
            };

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            for (var i = 0; i < 10000; i++)
            {
                await flow.InvokeAsync(context, ServiceProvider);
            }
            stopwatch.Stop();
            Assert.Pass($"Total: {stopwatch.ElapsedMilliseconds}, avg: {stopwatch.ElapsedMilliseconds/10000d}");
        }
Beispiel #4
0
        public async Task StoppingMiddlewareTest()
        {
            ServiceProvider = SetupServices();

            var flowBuilder = new FlowBuilder <TestContext>();

            flowBuilder.Use(typeof(MultiplyByFiveMiddleware));
            flowBuilder.Use <StopMiddleware>();
            flowBuilder.Use <IncrementByOneMiddleware>();

            var flow = flowBuilder.Build();

            var context = new TestContext {
                N = 5
            };
            await flow.InvokeAsync(context, ServiceProvider);

            Assert.AreEqual(5 * 5, context.N);

            VerifyCreated(true, typeof(MultiplyByFiveMiddleware), typeof(StopMiddleware));
            VerifyReleased(true, typeof(MultiplyByFiveMiddleware), typeof(StopMiddleware));

            VerifyCreated(false, typeof(IncrementByOneMiddleware));
            VerifyReleased(false, typeof(IncrementByOneMiddleware));
        }
Beispiel #5
0
        public async Task ExecutePipeFailShouldReturnError()
        {
            var builder = new FlowBuilder()
                          .Add(new FailingFlowElement());

            var pipeline = builder.Build();

            var result = await pipeline.RunAsync("test");

            result
            .Should()
            .BeOfType <FlowErrorResult>();
        }
Beispiel #6
0
        public void Flow_WhenRetryPolicyIsProvided_ShouldFailAfterTooManyRetries()
        {
            ServiceProvider = SetupServices();

            var flowBuilder = new FlowBuilder <TestContext>();

            flowBuilder.UseWithPolicy <TestContext, SucceedAtFiveMiddleware>();
            flowBuilder.Use <MultiplyByFiveMiddleware>();

            var flow    = flowBuilder.Build();
            var context = new TestContext {
                N = -2
            };

            Assert.ThrowsAsync <MiddlewareException <TestContext> >(() => flow.InvokeAsync(context, ServiceProvider));
        }
Beispiel #7
0
        public async Task Flow_WhenRetryPolicyIsProvided_ShouldSucceedAfterRetrying()
        {
            ServiceProvider = SetupServices();

            var flowBuilder = new FlowBuilder <TestContext>();

            flowBuilder.UseWithPolicy <TestContext, SucceedAtFiveMiddleware>();
            flowBuilder.Use <MultiplyByFiveMiddleware>();

            var flow    = flowBuilder.Build();
            var context = new TestContext {
                N = 1
            };
            await flow.InvokeAsync(context, ServiceProvider);

            Assert.AreEqual(5 * 5, context.N);
        }
Beispiel #8
0
        public async Task CatchingMiddleware_WhenMiddlewareThrows_FlowDoesntThrow()
        {
            ServiceProvider = SetupServices();

            var flowBuilder = new FlowBuilder <TestContext>();

            flowBuilder.Use <MultiplyByFiveMiddleware>();
            flowBuilder.Use <ThrowingMiddleware>();
            flowBuilder.UseCatch <CatchingMiddleware>();
            flowBuilder.Use <IncrementByOneMiddleware>();

            var flow    = flowBuilder.Build();
            var context = new TestContext {
                N = 5
            };
            await flow.InvokeAsync(context, ServiceProvider);
        }
Beispiel #9
0
        public async Task IncrementThenMultiplyMiddlewareTest()
        {
            ServiceProvider = SetupServices();

            var flowBuilder = new FlowBuilder <TestContext>();

            flowBuilder.Use <IncrementByOneMiddleware>();
            flowBuilder.Use(typeof(MultiplyByFiveMiddleware));

            var flow = flowBuilder.Build();

            var context = await flow.InvokeAsync(ServiceProvider);

            Assert.AreEqual((0 + 1) * 5, context.N);

            VerifyCreated(true, typeof(MultiplyByFiveMiddleware), typeof(IncrementByOneMiddleware));
            VerifyReleased(true, typeof(MultiplyByFiveMiddleware), typeof(IncrementByOneMiddleware));
        }
Beispiel #10
0
        public async Task CatchingMiddleware_WhenMiddlewareDoesntThrow_CatchIsNotExecuted()
        {
            ServiceProvider = SetupServices();

            var flowBuilder = new FlowBuilder <TestContext>();

            flowBuilder.Use <MultiplyByFiveMiddleware>();
            flowBuilder.UseCatch <CatchingMiddleware>();
            flowBuilder.Use <IncrementByOneMiddleware>();

            var flow    = flowBuilder.Build();
            var context = new TestContext {
                N = 5
            };
            await flow.InvokeAsync(context, ServiceProvider);

            Assert.IsNull(context.CatchedException);
        }
Beispiel #11
0
        public async Task CatchingMiddleware_WhenMiddlewareThrows_CatchIsExecutedOnlyOnce()
        {
            ServiceProvider = SetupServices();

            var flowBuilder = new FlowBuilder <TestContext>();

            flowBuilder.Use <ThrowingMiddleware>();
            flowBuilder.Use <IncrementByOneMiddleware>();
            flowBuilder.Use <ThrowingMiddleware>();
            flowBuilder.UseCatch <CatchingMiddleware>();

            var flow    = flowBuilder.Build();
            var context = new TestContext {
                N = 5
            };
            await flow.InvokeAsync(context, ServiceProvider);

            Assert.AreEqual(5, context.CatchN);
        }
Beispiel #12
0
        public async Task FlowShouldReturnA()
        {
            var builder = new FlowBuilder()
                          .Add(DecisionBuilder.New().Decision(async value => (int)value == 1)
                               .SetBranches(
                                   new List <IFlowElementExecute> {
                new FakeElementReturnA()
            },
                                   new List <IFlowElementExecute> {
                new FakeElementReturnB()
            }
                                   ).Build());

            var pipeline = builder.Build();

            var result = await pipeline.RunAsync(1);

            result
            .Should()
            .BeOfType <FlowSuccessResult>()
            .Which.Result.Should().Be("A");
        }
Beispiel #13
0
 public void SetLeftFlow(FlowBuilder?builder, int step = 4, int scroll = 0)
 {
     SetLeftFlow(builder?.Build(), step, scroll);
 }