public void Should_release_throttle_on_exception() { //arrange var nextProcessor = new Mock <IMessageProcessor>(); var messageStateHandler = new Mock <IMessageStateHandler <TestCommand> >(); nextProcessor.Setup(x => x.ProcessAsync(messageStateHandler.Object, CancellationToken.None)).Throws <Exception>(); var middleware = new ThrottlingMiddleware(1); //act var action = new Func <Task>(() => middleware.ProcessAsync(messageStateHandler.Object, Mock.Of <IPipelineInformation>(), nextProcessor.Object, CancellationToken.None)); action.Should().Throw <Exception>(); //assert middleware.CurrentCount.Should().Be(1); }
public ThrottlingMiddlewareTests() { _cts = new CancellationTokenSource(); _httpContext.RequestAborted = _cts.Token; var throttlingConfiguration = new ThrottlingConfiguration { ConcurrentRequestLimit = 5, }; throttlingConfiguration.ExcludedEndpoints.Add("get:/health/check"); _middleware = new ThrottlingMiddleware( async x => { x.Response.StatusCode = 200; await Task.Delay(5000, _cts.Token); }, Options.Create(throttlingConfiguration), NullLogger<ThrottlingMiddleware>.Instance); }
public void Should_release_throttle_when_cancellation_token_cancelled() { //arrange var nextProcessor = new Mock <IMessageProcessor>(); var messageStateHandler = new Mock <IMessageStateHandler <TestCommand> >(); var cts = new CancellationTokenSource(); cts.Cancel(); nextProcessor.Setup(x => x.ProcessAsync(messageStateHandler.Object, cts.Token)).Throws <Exception>(); var middleware = new ThrottlingMiddleware(1); //act middleware.Awaiting(x => x.ProcessAsync(messageStateHandler.Object, Mock.Of <IPipelineInformation>(), nextProcessor.Object, cts.Token)) .Should().Throw <OperationCanceledException>(); //assert middleware.CurrentCount.Should().Be(1); }
private static void Init() { server = new Server(); server.Register("*****@*****.**", "admin_pass"); server.Register("*****@*****.**", "user_pass"); // EN: All checks are linked. Client can build various chains using the // same components. // // RU: Проверки связаны в одну цепь. Клиент может строить различные // цепи, используя одни и те же компоненты. Middleware middleware = new ThrottlingMiddleware(2) .LinkWith(new UserExistsMiddleware()) .LinkWith(new RoleCheckMiddleware()); // EN: Server gets a chain from client code. // // RU: Сервер получает цепочку от клиентского кода. server.SetMiddleware(middleware); }
private void Init(bool securityEnabled) { _cts = new CancellationTokenSource(); _httpContext.RequestAborted = _cts.Token; _httpContext.User = new ClaimsPrincipal(new ClaimsIdentity("authenticationType", "nametype", "roletype")); var throttlingConfiguration = new ThrottlingConfiguration { ConcurrentRequestLimit = 5, }; throttlingConfiguration.ExcludedEndpoints.Add(new ExcludedEndpoint { Method = "get", Path = "/health/check" }); _middleware = new ThrottlingMiddleware( async x => { x.Response.StatusCode = 200; try { await Task.Delay(5000, _cts.Token); } catch (TaskCanceledException) when(_cts.Token.IsCancellationRequested) { } }, Options.Create(throttlingConfiguration), Options.Create(new Microsoft.Health.Fhir.Core.Configs.SecurityConfiguration { Enabled = securityEnabled }), NullLogger <ThrottlingMiddleware> .Instance); _executor = Substitute.For <IActionResultExecutor <ObjectResult> >(); _executor.ExecuteAsync(Arg.Any <ActionContext>(), Arg.Any <ObjectResult>()).ReturnsForAnyArgs(Task.CompletedTask); _collection.AddSingleton <IActionResultExecutor <ObjectResult> >(_executor); _provider = _collection.BuildServiceProvider(); _httpContext.RequestServices = _provider; }
public async Task Should_throttle() { //arrange var nextProcessor = new Mock <IMessageProcessor>(); var messageStateHandler = new Mock <IMessageStateHandler <TestCommand> >(); nextProcessor.Setup(x => x.ProcessAsync(messageStateHandler.Object, CancellationToken.None)) .Returns(Task.Delay(TimeSpan.FromSeconds(1))); var middleware = new ThrottlingMiddleware(2); //act for (int i = 0; i < 10; i++) { #pragma warning disable 4014 middleware.ProcessAsync(messageStateHandler.Object, Mock.Of <IPipelineInformation>(), nextProcessor.Object, CancellationToken.None); #pragma warning restore 4014 } await Task.Delay(100); //assert middleware.CurrentCount.Should().Be(0); nextProcessor.Verify(x => x.ProcessAsync(messageStateHandler.Object, CancellationToken.None), Times.Exactly(2)); }