Skip to content

Commit ad96952

Browse files
authored
Merge pull request #71 from cnblogs/improve-sample
Rewrite SampleWebApp and add integration tests
2 parents 0bd8c4c + b6f26f9 commit ad96952

File tree

14 files changed

+218
-56
lines changed

14 files changed

+218
-56
lines changed

Enyim.Caching.Tests/MemcachedClientTestsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public MemcachedClientTestsBase(Action<MemcachedClientOptions> onAddEnyimMemcach
2626
onAddEnyimMemcached?.Invoke(options);
2727
});
2828

29-
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Debug).AddConsole());
29+
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Information).AddConsole());
3030
IServiceProvider serviceProvider = services.BuildServiceProvider();
3131
_client = serviceProvider.GetService<IMemcachedClient>() as MemcachedClient;
3232
}

Enyim.Caching/MemcachedClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
220220
var result = await GetAsync<T>(key);
221221
if (result.Success)
222222
{
223+
_logger.LogDebug($"Cache is hint. Key is '{key}'.");
223224
return result.Value;
224225
}
225226
}
@@ -228,12 +229,15 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
228229
_logger.LogError(ex, $"{nameof(GetAsync)}<{typeof(T)}>(\"{key}\")");
229230
}
230231

232+
_logger.LogDebug($"Cache is missed. Key is '{key}'.");
233+
231234
var value = await generator?.Invoke();
232235
if (value != null)
233236
{
234237
try
235238
{
236239
await AddAsync(key, value, cacheSeconds);
240+
_logger.LogDebug($"Added value into cache. Key is '{key}'. " + value);
237241
}
238242
catch (Exception ex)
239243
{

EnyimCachingCore.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{46CD2BBA-9
1717
EndProject
1818
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{9954C92E-50E2-477C-AC47-31C857C46370}"
1919
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApp.IntegrationTests", "SampleWebApp.IntegrationTests\SampleWebApp.IntegrationTests.csproj", "{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -39,6 +41,10 @@ Global
3941
{3DDAC26F-8B56-4F69-8C69-A3CB6CF6B12B}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{3DDAC26F-8B56-4F69-8C69-A3CB6CF6B12B}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{3DDAC26F-8B56-4F69-8C69-A3CB6CF6B12B}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE
@@ -48,5 +54,9 @@ Global
4854
{7F795CB9-6058-4137-B069-12C1B8D4132B} = {9954C92E-50E2-477C-AC47-31C857C46370}
4955
{FAF5E655-9ED4-4934-94F9-F0EDB77143AC} = {46CD2BBA-9846-42FA-823B-D03F68F81647}
5056
{3DDAC26F-8B56-4F69-8C69-A3CB6CF6B12B} = {46CD2BBA-9846-42FA-823B-D03F68F81647}
57+
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C} = {9954C92E-50E2-477C-AC47-31C857C46370}
58+
EndGlobalSection
59+
GlobalSection(ExtensibilityGlobals) = postSolution
60+
SolutionGuid = {D8C07FE8-52FF-48B8-A079-A180058AABC6}
5161
EndGlobalSection
5262
EndGlobal
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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;
8+
using Enyim.Caching.SampleWebApp;
9+
using Enyim.Caching.SampleWebApp.Controllers;
10+
using Enyim.Caching.SampleWebApp.Models;
11+
using Microsoft.AspNetCore.Mvc.Testing;
12+
using Microsoft.Extensions.DependencyInjection;
13+
using Xunit;
14+
15+
namespace SampleWebApp.IntegrationTests
16+
{
17+
public class HomeControllerTests : IClassFixture<WebApplicationFactory<Startup>>
18+
{
19+
private readonly WebApplicationFactory<Startup> _factory;
20+
21+
public HomeControllerTests(WebApplicationFactory<Startup> factory)
22+
{
23+
_factory = factory;
24+
}
25+
26+
[Fact]
27+
public async Task HomeController_Index()
28+
{
29+
var httpClient = _factory.CreateClient();
30+
var response = await httpClient.GetAsync("/");
31+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
32+
33+
var memcachedClient = _factory.Server.Host.Services.GetRequiredService<IMemcachedClient>();
34+
var posts = await memcachedClient.GetValueAsync<IEnumerable<BlogPost>>(HomeController.CacheKey);
35+
Assert.NotNull(posts);
36+
Assert.NotEmpty(posts.First().Title);
37+
38+
await memcachedClient.RemoveAsync(HomeController.CacheKey);
39+
}
40+
}
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:57329/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"SampleWebApp.IntegrationTests": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:57330/"
25+
}
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.App" />
11+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.2" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
13+
<PackageReference Include="xunit" Version="2.4.0" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
17+
</PackageReference>
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\Enyim.Caching\Enyim.Caching.csproj" />
22+
<ProjectReference Include="..\SampleWebApp\SampleWebApp.csproj" />
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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;
7+
using Enyim.Caching.SampleWebApp.Services;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace Enyim.Caching.SampleWebApp.Controllers
12+
{
13+
public class HomeController : Controller
14+
{
15+
private readonly IMemcachedClient _memcachedClient;
16+
private readonly IBlogPostService _blogPostService;
17+
private readonly ILogger _logger;
18+
public static readonly string CacheKey = "blogposts-recent";
19+
20+
public HomeController(
21+
IMemcachedClient memcachedClient,
22+
IBlogPostService blogPostService,
23+
ILoggerFactory loggerFactory)
24+
{
25+
_memcachedClient = memcachedClient;
26+
_blogPostService = blogPostService;
27+
_logger = loggerFactory.CreateLogger<HomeController>();
28+
}
29+
30+
public async Task<IActionResult> Index()
31+
{
32+
_logger.LogDebug("Executing _memcachedClient.GetValueOrCreateAsync...");
33+
34+
var cacheSeconds = 600;
35+
var posts = await _memcachedClient.GetValueOrCreateAsync(
36+
CacheKey,
37+
cacheSeconds,
38+
async () => await _blogPostService.GetRecent(10));
39+
40+
_logger.LogDebug("Done _memcachedClient.GetValueOrCreateAsync");
41+
42+
return Ok(posts);
43+
}
44+
}
45+
}

SampleWebApp/Models/BlogPost.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Enyim.Caching.SampleWebApp.Models
7+
{
8+
public class BlogPost
9+
{
10+
public string Title { get; set; }
11+
public string Body { get; set; }
12+
}
13+
}

SampleWebApp/Program.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@
33
using System.IO;
44
using System.Linq;
55
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore.Hosting;
76
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
88

9-
namespace SampleWebApp
9+
namespace Enyim.Caching.SampleWebApp
1010
{
1111
public class Program
1212
{
1313
public static void Main(string[] args)
1414
{
15-
BuildWebHost(args).Run();
15+
CreateWebHostBuilder(args).Build().Run();
1616
}
1717

18-
public static IWebHost BuildWebHost(string[] args)
19-
{
20-
return WebHost.CreateDefaultBuilder(args)
21-
.UseStartup<Startup>()
22-
.Build();
23-
}
18+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
19+
WebHost.CreateDefaultBuilder(args)
20+
.UseStartup<Startup>();
2421
}
2522
}

SampleWebApp/SampleWebApp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<RootNamespace>Enyim.Caching.SampleWebApp</RootNamespace>
6+
<AssemblyName>Enyim.Caching.SampleWebApp</AssemblyName>
57
</PropertyGroup>
68

79
<ItemGroup>

0 commit comments

Comments
 (0)