Пример #1
0
        public void BotPolicyTest_Config()
        {
            var policy = new BotPolicy();

            policy.SetCapturedContextContinuation(true);
            policy.Execute((ctx, t) => Assert.IsTrue(ctx.BotPolicyConfiguration.ContinueOnCapturedContext), CancellationToken.None);
        }
Пример #2
0
        public void FallbackTests_Ok_Config()
        {
            var policy = new BotPolicy <int>(config => config
                                             .Configure(botconfig => botconfig
                                                        .Fallback(conf => conf.WhenExceptionOccurs(ex => true))));
            var result = policy.Execute((ex, t) => 5, CancellationToken.None);

            Assert.AreEqual(5, result);
        }
Пример #3
0
        public void TimeoutTest_Config()
        {
            var policy = new BotPolicy(config => config
                                       .Configure(botconfig => botconfig
                                                  .Timeout(timeoutConfig => timeoutConfig
                                                           .After(TimeSpan.FromSeconds(.2)))));

            Assert.ThrowsException <OperationTimeoutException>(() => policy.Execute((ctx, t) =>
                                                                                    Task.Delay(500, t).Wait(t), CancellationToken.None));
        }
Пример #4
0
        public void FallbackTests_Ok_Config()
        {
            var counter = 0;
            var policy  = new BotPolicy(config => config
                                        .Configure(botconfig => botconfig
                                                   .Fallback(conf => conf.WhenExceptionOccurs(ex => true))));

            policy.Execute((ex, t) => counter++, CancellationToken.None);

            Assert.AreEqual(1, counter);
        }
Пример #5
0
        public async Task BotPolicyTest_ExecuteAsync_Action()
        {
            var policy  = new BotPolicy();
            var mockBot = this.CreateMockBot(policy);

            var source = new CancellationTokenSource();

            await policy.ExecuteAsync((ctx, t) => { }, source.Token);

            mockBot.Verify(m => m.ExecuteAsync(It.IsAny <IAsyncBotOperation>(), It.IsAny <ExecutionContext>(), source.Token));
        }
Пример #6
0
        public void BotPolicyTest_Execute_Func()
        {
            var policy  = new BotPolicy <int>();
            var mockBot = this.CreateMockBot(policy);

            var source = new CancellationTokenSource();

            policy.Execute((ctx, t) => 0, source.Token);

            mockBot.Verify(m => m.Execute(It.IsAny <IBotOperation <int> >(), It.IsAny <ExecutionContext>(), source.Token));
        }
Пример #7
0
        public void BotPolicyTest_Execute_Action_Without_Parameters()
        {
            var policy  = new BotPolicy();
            var mockBot = this.CreateMockBot(policy);

            mockBot.Setup(m => m.Execute(It.IsAny <IBotOperation>(), It.IsAny <ExecutionContext>(), CancellationToken.None))
            .Callback <IBotOperation, ExecutionContext, CancellationToken>((op, ctx, t) => op.Execute(ctx, t))
            .Verifiable();

            policy.Execute(() => { });
        }
Пример #8
0
        public void BotPolicyTest_Config_Result()
        {
            var policy = new BotPolicy <int>();

            policy.SetCapturedContextContinuation(true);
            policy.Execute((ctx, t) =>
            {
                Assert.IsTrue(ctx.BotPolicyConfiguration.ContinueOnCapturedContext);
                return(0);
            }, CancellationToken.None);
        }
Пример #9
0
        public async Task BotPolicyTest_ExecuteAsync_Func_Task_Result()
        {
            var policy  = new BotPolicy <int>();
            var mockBot = this.CreateMockBot(policy);

            var source = new CancellationTokenSource();

            await policy.ExecuteAsync((ctx, t) => Task.FromResult(0), source.Token);

            mockBot.Verify(m => m.ExecuteAsync(It.IsAny <IAsyncBotOperation <int> >(), It.IsAny <ExecutionContext>(), source.Token));
        }
Пример #10
0
        public void RetryTests_Action_Ok_Config()
        {
            var policy = new BotPolicy(config => config
                                       .Configure(botconfig => botconfig
                                                  .Retry(retryConfig => retryConfig.WithMaxAttemptCount(2).WhenExceptionOccurs(ex => true))));
            var counter = 0;

            policy.Execute((ctx, t) => counter++, CancellationToken.None);

            Assert.AreEqual(1, counter);
        }
Пример #11
0
        public void BotPolicyTest_Execute_Action_Without_Parameters_CorrelationId()
        {
            var policy        = new BotPolicy();
            var mockBot       = this.CreateMockBot(policy);
            var correlationId = new object();

            mockBot.Setup(m => m.Execute(It.IsAny <IBotOperation>(), It.IsAny <ExecutionContext>(), CancellationToken.None))
            .Callback <IBotOperation, ExecutionContext, CancellationToken>((o, ctx, t) => Assert.AreEqual(correlationId, ctx.CorrelationId))
            .Verifiable();

            policy.Execute(() => { }, correlationId);
        }
Пример #12
0
        public async Task BotPolicyTest_ExecuteAsync_Func_Task_Result_Without_Parameters()
        {
            var policy  = new BotPolicy <int>();
            var mockBot = this.CreateMockBot(policy);

            mockBot.Setup(m => m.ExecuteAsync(It.IsAny <IAsyncBotOperation <int> >(), It.IsAny <ExecutionContext>(), CancellationToken.None))
            .Callback <IAsyncBotOperation <int>, ExecutionContext, CancellationToken>((op, ctx, t) => op.ExecuteAsync(ctx, t))
            .Returns(Task.FromResult(0))
            .Verifiable();

            await policy.ExecuteAsync(() => Task.FromResult(0));
        }
Пример #13
0
        public async Task BotPolicyTest_ExecuteAsync_Func_Task_Result_Without_Parameters_With_CorrelationId()
        {
            var policy        = new BotPolicy <int>();
            var mockBot       = this.CreateMockBot(policy);
            var correlationId = new object();

            mockBot.Setup(m => m.ExecuteAsync(It.IsAny <IAsyncBotOperation <int> >(), It.IsAny <ExecutionContext>(), CancellationToken.None))
            .Callback <IAsyncBotOperation <int>, ExecutionContext, CancellationToken>((o, ctx, t) => Assert.AreEqual(correlationId, ctx.CorrelationId))
            .Returns(Task.FromResult(0))
            .Verifiable();

            await policy.ExecuteAsync(() => Task.FromResult(0), correlationId);
        }
Пример #14
0
        public void BotPolicyTest_Execute_Func_With_CorrelationId()
        {
            var policy        = new BotPolicy <int>();
            var mockBot       = this.CreateMockBot(policy);
            var correlationId = new object();

            mockBot.Setup(m => m.Execute(It.IsAny <IBotOperation <int> >(), It.IsAny <ExecutionContext>(), CancellationToken.None))
            .Callback <IBotOperation <int>, ExecutionContext, CancellationToken>((o, ctx, t) => Assert.AreEqual(correlationId, ctx.CorrelationId))
            .Returns(0)
            .Verifiable();

            policy.Execute((ctx, t) => 0, correlationId);
        }
Пример #15
0
        public void RateLimit_Sliding_Reject()
        {
            var policy = new BotPolicy(config => config
                                       .Configure(botconfig => botconfig
                                                  .RateLimit(c => c.SlidingWindow(2, TimeSpan.FromSeconds(2)))));

            policy.Execute(() => { });
            policy.Execute(() => { });
            var exception = Assert.ThrowsException <RateLimitExceededException>(() => policy.Execute(() => { }));

            Assert.IsTrue(exception.RetryAfter > TimeSpan.Zero);
            Thread.Sleep(exception.RetryAfter.Add(TimeSpan.FromMilliseconds(10)));
            policy.Execute(() => { });
        }
Пример #16
0
        public void CircuitBreakerTests_Ok()
        {
            var mockStore = new Mock <ICircuitStateHandler>();

            mockStore.Setup(s => s.Read()).Returns(CircuitState.Closed).Verifiable();
            var policy = new BotPolicy(config => config
                                       .Configure(botconfig => botconfig
                                                  .CircuitBreaker(conf => conf
                                                                  .WithStateHandler(mockStore.Object)
                                                                  .DurationOfOpen(TimeSpan.FromMilliseconds(200)),
                                                                  defConfig => defConfig
                                                                  .FailureThresholdBeforeOpen(2)
                                                                  .SuccessThresholdInHalfOpen(2))));
            var counter = 0;

            policy.Execute((ex, t) => counter++, CancellationToken.None);

            Assert.AreEqual(1, counter);
        }
Пример #17
0
        public void CustomCircuitBreakerTests_Closed_Open_HalfOpen_Then_Closed_WithConfig()
        {
            var state  = State.Closed;
            var policy = new BotPolicy(config => config
                                       .Configure(botConfig => botConfig
                                                  .CustomCircuitBreaker(cbConfig => new CustomStrategy(cbConfig),
                                                                        new CircuitBreakerConfiguration().BrakeWhenExceptionOccurs(ex => true)
                                                                        .OnClosed(() => state   = State.Closed)
                                                                        .OnHalfOpen(() => state = State.HalfOpen)
                                                                        .OnOpen(ts => state     = State.Open))));

            Assert.ThrowsException <InvalidOperationException>(() =>
                                                               policy.Execute((ctx, t) => throw new InvalidOperationException(), CancellationToken.None));

            Assert.AreEqual(State.Open, state);

            policy.Execute((ctx, t) => Assert.AreEqual(State.HalfOpen, state), CancellationToken.None);

            Assert.AreEqual(State.Closed, state);
        }
Пример #18
0
        public void CustomCircuitBreakerTests_Result_Closed_Open_HalfOpen_Then_Closed_WithConfig()
        {
            var state  = State.Closed;
            var policy = new BotPolicy <int>(config => config
                                             .Configure(botConfig => botConfig
                                                        .CustomCircuitBreaker(cbConfig => new CustomStrategy(cbConfig),
                                                                              new CircuitBreakerConfiguration <int>().BrakeWhenResultIs(r => r != 5)
                                                                              .OnClosed(() => state   = State.Closed)
                                                                              .OnHalfOpen(() => state = State.HalfOpen)
                                                                              .OnOpen(ts => state     = State.Open))));

            policy.Execute((ctx, t) => 6, CancellationToken.None);

            Assert.AreEqual(State.Open, state);

            policy.Execute((ctx, t) =>
            {
                Assert.AreEqual(State.HalfOpen, state);
                return(5);
            }, CancellationToken.None);

            Assert.AreEqual(State.Closed, state);
        }