Skip to content

Commit 6f37d61

Browse files
authored
Merge pull request #259 from cnblogs/add-stats-test-case
test: add a test case for GetUptime
2 parents 89179ea + 5a7e29c commit 6f37d61

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

sample/SampleWebApp/Controllers/HomeController.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Enyim.Caching;
1+
using Enyim.Caching.Configuration;
62
using Enyim.Caching.SampleWebApp.Models;
73
using Enyim.Caching.SampleWebApp.Services;
84
using Microsoft.AspNetCore.Mvc;
95
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Options;
7+
using System.Linq;
8+
using System.Net;
9+
using System.Threading.Tasks;
1010

1111
namespace Enyim.Caching.SampleWebApp.Controllers
1212
{
1313
public class HomeController : Controller
1414
{
1515
private readonly IMemcachedClient _memcachedClient;
1616
private readonly IMemcachedClient _postbodyMemcachedClient;
17+
private readonly MemcachedClientOptions options;
1718
private readonly IBlogPostService _blogPostService;
1819
private readonly ILogger _logger;
1920
public static readonly string CacheKey = "blogposts-recent";
2021
public static readonly string PostbodyCacheKey = "postbody";
2122

2223
public HomeController(
2324
IMemcachedClient memcachedClient,
25+
IOptions<MemcachedClientOptions> optionsAccessor,
2426
IMemcachedClient<PostBody> postbodyMemcachedClient,
2527
IBlogPostService blogPostService,
2628
ILoggerFactory loggerFactory)
2729
{
2830
_memcachedClient = memcachedClient;
31+
options = optionsAccessor.Value;
2932
_postbodyMemcachedClient = postbodyMemcachedClient;
3033
_blogPostService = blogPostService;
3134
_logger = loggerFactory.CreateLogger<HomeController>();
@@ -53,5 +56,12 @@ public async Task<IActionResult> Postbody()
5356
var result = await _postbodyMemcachedClient.GetAsync<string>(PostbodyCacheKey);
5457
return result.Success ? Ok() : StatusCode(500);
5558
}
59+
60+
public IActionResult Uptime()
61+
{
62+
var server = options.Servers.First();
63+
var uptime = _memcachedClient.Stats().GetUptime(new DnsEndPoint(server.Address, server.Port));
64+
return Json(uptime);
65+
}
5666
}
5767
}

sample/SampleWebApp/Startup.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Enyim.Caching;
6-
using Enyim.Caching.SampleWebApp.Models;
1+
using Enyim.Caching.SampleWebApp.Models;
72
using Enyim.Caching.SampleWebApp.Services;
83
using Microsoft.AspNetCore.Builder;
9-
using Microsoft.AspNetCore.Hosting;
10-
using Microsoft.AspNetCore.Http;
11-
using Microsoft.Extensions.Caching.Distributed;
124
using Microsoft.Extensions.Configuration;
135
using Microsoft.Extensions.DependencyInjection;
14-
using Microsoft.Extensions.Logging;
156

167
namespace Enyim.Caching.SampleWebApp
178
{

test/SampleWebApp.IntegrationTests/HomeControllerTests.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Net;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
using Enyim.Caching;
1+
using Enyim.Caching;
82
using Enyim.Caching.SampleWebApp;
93
using Enyim.Caching.SampleWebApp.Controllers;
104
using Enyim.Caching.SampleWebApp.Models;
115
using Microsoft.AspNetCore.Mvc.Testing;
126
using Microsoft.Extensions.DependencyInjection;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Net;
11+
using System.Net.Http.Json;
12+
using System.Threading.Tasks;
1313
using Xunit;
1414

1515
namespace SampleWebApp.IntegrationTests
@@ -45,5 +45,16 @@ public async Task Get_postbody_from_cache_ok()
4545
var response = await httpClient.GetAsync("/home/postbody");
4646
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
4747
}
48+
49+
[Fact]
50+
public async Task Get_uptime_test()
51+
{
52+
var httpClient = _factory.CreateClient();
53+
var response = await httpClient.GetAsync("/home/uptime");
54+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
55+
var uptime = await response.Content.ReadFromJsonAsync<TimeSpan>();
56+
Console.WriteLine("uptime: " + uptime);
57+
Assert.True(uptime > TimeSpan.Zero);
58+
}
4859
}
4960
}

0 commit comments

Comments
 (0)