From 5a7e29c1c70acd6b1c37db793f3d46b32aacaa1b Mon Sep 17 00:00:00 2001 From: dudu Date: Fri, 8 Aug 2025 20:02:20 +0800 Subject: [PATCH] test: add a test case for GetUptime --- .../Controllers/HomeController.cs | 20 +++++++++++---- sample/SampleWebApp/Startup.cs | 11 +------- .../HomeControllerTests.cs | 25 +++++++++++++------ 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/sample/SampleWebApp/Controllers/HomeController.cs b/sample/SampleWebApp/Controllers/HomeController.cs index 82ba733c..700d925d 100644 --- a/sample/SampleWebApp/Controllers/HomeController.cs +++ b/sample/SampleWebApp/Controllers/HomeController.cs @@ -1,12 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Enyim.Caching; +using Enyim.Caching.Configuration; using Enyim.Caching.SampleWebApp.Models; using Enyim.Caching.SampleWebApp.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System.Linq; +using System.Net; +using System.Threading.Tasks; namespace Enyim.Caching.SampleWebApp.Controllers { @@ -14,6 +14,7 @@ public class HomeController : Controller { private readonly IMemcachedClient _memcachedClient; private readonly IMemcachedClient _postbodyMemcachedClient; + private readonly MemcachedClientOptions options; private readonly IBlogPostService _blogPostService; private readonly ILogger _logger; public static readonly string CacheKey = "blogposts-recent"; @@ -21,11 +22,13 @@ public class HomeController : Controller public HomeController( IMemcachedClient memcachedClient, + IOptions optionsAccessor, IMemcachedClient postbodyMemcachedClient, IBlogPostService blogPostService, ILoggerFactory loggerFactory) { _memcachedClient = memcachedClient; + options = optionsAccessor.Value; _postbodyMemcachedClient = postbodyMemcachedClient; _blogPostService = blogPostService; _logger = loggerFactory.CreateLogger(); @@ -53,5 +56,12 @@ public async Task Postbody() var result = await _postbodyMemcachedClient.GetAsync(PostbodyCacheKey); return result.Success ? Ok() : StatusCode(500); } + + public IActionResult Uptime() + { + var server = options.Servers.First(); + var uptime = _memcachedClient.Stats().GetUptime(new DnsEndPoint(server.Address, server.Port)); + return Json(uptime); + } } } diff --git a/sample/SampleWebApp/Startup.cs b/sample/SampleWebApp/Startup.cs index 34917793..0a5c5a8e 100644 --- a/sample/SampleWebApp/Startup.cs +++ b/sample/SampleWebApp/Startup.cs @@ -1,17 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Enyim.Caching; -using Enyim.Caching.SampleWebApp.Models; +using Enyim.Caching.SampleWebApp.Models; using Enyim.Caching.SampleWebApp.Services; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; namespace Enyim.Caching.SampleWebApp { diff --git a/test/SampleWebApp.IntegrationTests/HomeControllerTests.cs b/test/SampleWebApp.IntegrationTests/HomeControllerTests.cs index 07d998cc..b67c8d80 100644 --- a/test/SampleWebApp.IntegrationTests/HomeControllerTests.cs +++ b/test/SampleWebApp.IntegrationTests/HomeControllerTests.cs @@ -1,15 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using Enyim.Caching; +using Enyim.Caching; using Enyim.Caching.SampleWebApp; using Enyim.Caching.SampleWebApp.Controllers; using Enyim.Caching.SampleWebApp.Models; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http.Json; +using System.Threading.Tasks; using Xunit; namespace SampleWebApp.IntegrationTests @@ -45,5 +45,16 @@ public async Task Get_postbody_from_cache_ok() var response = await httpClient.GetAsync("/home/postbody"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); } + + [Fact] + public async Task Get_uptime_test() + { + var httpClient = _factory.CreateClient(); + var response = await httpClient.GetAsync("/home/uptime"); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var uptime = await response.Content.ReadFromJsonAsync(); + Console.WriteLine("uptime: " + uptime); + Assert.True(uptime > TimeSpan.Zero); + } } }