public void BotPolicyTest_Config() { var policy = new BotPolicy(); policy.SetCapturedContextContinuation(true); policy.Execute((ctx, t) => Assert.IsTrue(ctx.BotPolicyConfiguration.ContinueOnCapturedContext), CancellationToken.None); }
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); }
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)); }
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); }
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)); }
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)); }
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(() => { }); }
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); }
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)); }
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); }
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); }
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)); }
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); }
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); }
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(() => { }); }
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); }
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); }
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); }