From f7c9a638edffbccbc1a50c994d5ca876f0639092 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Wed, 14 Aug 2019 11:05:59 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=81=A5=E5=BA=B7?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HealthChecks/GiteeHttpHealthCheck.cs | 30 ++++++++++++------- .../Bootstrap.Admin/Api/HealthCheckTest.cs | 23 ++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs diff --git a/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs b/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs index 32db6cf7..5bcd8ddf 100644 --- a/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs +++ b/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Diagnostics.HealthChecks; using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -23,7 +24,7 @@ namespace Bootstrap.Admin.HealthChecks public GiteeHttpHealthCheck(IHttpClientFactory factory, IHttpContextAccessor accessor) { _client = factory.CreateClient(); - _client.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}/{accessor.HttpContext.Request.PathBase}"); + _client.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}{accessor.HttpContext.Request.PathBase}"); } /// @@ -32,21 +33,30 @@ namespace Bootstrap.Admin.HealthChecks /// /// /// - public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) + public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { var urls = new string[] { "Issues", "Pulls", "Releases", "Builds" }; var data = new Dictionary(); - var sw = Stopwatch.StartNew(); - foreach (var url in urls) + urls.ToList().ForEach(url => { - sw.Restart(); - await _client.GetStringAsync($"api/Gitee/{url}").ConfigureAwait(false); - sw.Stop(); - data.Add(url, sw.Elapsed); - } + var sw = Stopwatch.StartNew(); + try + { + var task = _client.GetStringAsync($"/api/Gitee/{url}"); + task.Wait(cancellationToken); + } + catch (Exception) + { - return HealthCheckResult.Healthy("Ok", data); + } + finally + { + sw.Stop(); + data.Add(url, sw.Elapsed); + } + }); + return Task.FromResult(HealthCheckResult.Healthy("Ok", data)); } } } diff --git a/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs b/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs new file mode 100644 index 00000000..5871acfc --- /dev/null +++ b/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs @@ -0,0 +1,23 @@ +using Xunit; + +namespace Bootstrap.Admin.Api +{ + public class HealthCheckTest : ControllerTest + { + public HealthCheckTest(BAWebHost factory) : base(factory, "") { } + + [Fact] + public async void Get_Ok() + { + var cates = await Client.GetAsJsonAsync("/Healths"); + Assert.NotNull(cates); + } + + [Fact] + public async void UI_Ok() + { + var cates = await Client.GetStringAsync("/Healths-ui"); + Assert.Contains("健康检查", cates); + } + } +}