Skip to content

Commit

Permalink
Merge pull request #5 from sj-distributor/chore-add-tests
Browse files Browse the repository at this point in the history
Chore add tests
  • Loading branch information
pygzfei authored Jul 8, 2022
2 parents c2c3cbc + af9f283 commit bf1661b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Reflection;
using System.Threading.Tasks;
using AspectCore.DynamicProxy;
using FastCache.Core.Attributes;
using FastCache.Core.Driver;
using FastCache.Core.Entity;
using FastCache.Core.Enums;
Expand All @@ -25,11 +24,11 @@ public class MultiSourceCacheableAttribute : AbstractInterceptorAttribute
private static readonly ConcurrentDictionary<Type, MethodInfo>
TypeofTaskResultMethod = new ConcurrentDictionary<Type, MethodInfo>();

private static readonly MethodInfo _taskResultMethod;
private static readonly MethodInfo TaskResultMethod;

static MultiSourceCacheableAttribute()
{
_taskResultMethod = typeof(Task).GetMethods()
TaskResultMethod = typeof(Task).GetMethods()
.First(p => p.Name == "FromResult" && p.ContainsGenericParameters);
}

Expand Down Expand Up @@ -101,7 +100,7 @@ public override async Task Invoke(AspectContext context, AspectDelegate next)
: context.ServiceMethod.ReturnType;

context.ReturnValue = context.IsAsync() ? TypeofTaskResultMethod.GetOrAdd(returnTypeBefore,
t => _taskResultMethod.MakeGenericMethod(returnTypeBefore))
t => TaskResultMethod.MakeGenericMethod(returnTypeBefore))
.Invoke(null, new [] { cacheValue.Value }) : cacheValue.Value;

return;
Expand Down
38 changes: 22 additions & 16 deletions IntegrationTests/MultiSourceApiRequestCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,21 @@ public MultiSourceApiRequestCacheTests(WebApplicationFactory<Program> factory)
}
}

[Fact]
public async void RequestCanCache()
[Theory]
[InlineData("/MultiSource")]
[InlineData("/MultiSourceInMemory")]
public async void RequestCanCache(string baseUrl)
{
var start = DateTime.Now.Ticks;
var resp1 = await _httpClient.GetAsync("/MultiSource?id=1");
var resp1 = await _httpClient.GetAsync($"{baseUrl}/?id=1");
Assert.True(resp1.StatusCode == HttpStatusCode.OK);
await resp1.Content.ReadAsStringAsync();
var end = DateTime.Now.Ticks;

Assert.True(end - start > 1000000);

var start1 = DateTime.Now.Ticks;
var resp2 = await _httpClient.GetAsync("/MultiSource?id=1");
var resp2 = await _httpClient.GetAsync($"{baseUrl}?id=1");
Assert.True(resp2.StatusCode == HttpStatusCode.OK);
await resp2.Content.ReadAsStringAsync();
var end1 = DateTime.Now.Ticks;
Expand All @@ -70,15 +72,17 @@ public async void RequestCanCache()
Assert.True(result < 400000);
}

[Fact]
public async void CacheCanEvict()
[Theory]
[InlineData("/MultiSource")]
[InlineData("/MultiSourceInMemory")]
public async void CacheCanEvict(string baseUrl)
{
var resp1 = await _httpClient.GetAsync("/MultiSource?id=3");
var resp1 = await _httpClient.GetAsync($"{baseUrl}?id=3");
Assert.True(resp1.StatusCode == HttpStatusCode.OK);

var result1 = await resp1.Content.ReadAsStringAsync();

var resultForPost = await _httpClient.PutAsJsonAsync("/MultiSource?id=3", new User()
var resultForPost = await _httpClient.PutAsJsonAsync($"{baseUrl}?id=3", new User()
{
Id = "3",
Name = "anson33"
Expand All @@ -87,40 +91,42 @@ public async void CacheCanEvict()
var stringAsync = await resultForPost.Content.ReadAsStringAsync();
Assert.NotEqual(stringAsync, result1);

var resp2 = await _httpClient.GetAsync("/MultiSource?id=3");
var resp2 = await _httpClient.GetAsync($"{baseUrl}?id=3");
Assert.True(resp2.StatusCode == HttpStatusCode.OK);

var result2 = await resp2.Content.ReadAsStringAsync();

Assert.NotEqual(result1, result2);
}

[Fact]
public async void CacheAndEvictOther()
[Theory]
[InlineData("/MultiSource")]
[InlineData("/MultiSourceInMemory")]
public async void CacheAndEvictOther(string baseUrl)
{
await _httpClient.PostAsJsonAsync("/MultiSource", new User()
await _httpClient.PostAsJsonAsync($"{baseUrl}", new User()
{
Id = "5",
Name = "anson5"
});

var resp1 = await _httpClient.GetAsync("/MultiSource/users?page=1");
var resp1 = await _httpClient.GetAsync($"{baseUrl}/users?page=1");
Assert.True(resp1.StatusCode == HttpStatusCode.OK);

var result1 = await resp1.Content.ReadAsStringAsync();

var resp2 = await _httpClient.DeleteAsync("/MultiSource?id=1");
var resp2 = await _httpClient.DeleteAsync($"{baseUrl}?id=1");
Assert.True(resp2.StatusCode == HttpStatusCode.OK);

await resp2.Content.ReadAsStringAsync();

var resp3 = await _httpClient.GetAsync("/MultiSource/users?page=1");
var resp3 = await _httpClient.GetAsync($"{baseUrl}/users?page=1");
Assert.True(resp3.StatusCode == HttpStatusCode.OK);

var result3 = await resp3.Content.ReadAsStringAsync();

var start = DateTime.Now.Ticks;
var resp4 = await _httpClient.GetAsync("/MultiSource/users?page=1");
var resp4 = await _httpClient.GetAsync($"{baseUrl}/users?page=1");
Assert.True(resp4.StatusCode == HttpStatusCode.OK);

var result4 = await resp4.Content.ReadAsStringAsync();
Expand Down
54 changes: 54 additions & 0 deletions TestApi/Controllers/MultiSourceInMemoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using FastCache.Core.Enums;
using FastCache.MultiSource.Attributes;
using Microsoft.AspNetCore.Mvc;
using TestApi.Entity;
using TestApi.Service;

namespace TestApi.Controllers;

[ApiController]
[Route("/[controller]")]
public class MultiSourceInMemoryController : ControllerBase
{
private readonly IMultiSourceService _userService;

public MultiSourceInMemoryController(IMultiSourceService userService)
{
_userService = userService;
}

[HttpGet]
[MultiSourceCacheable("MultiSource-single", "{id}", Target.InMemory, 5)]
public virtual async Task<User> Get(string id)
{
return await _userService.Single(id);
}

[HttpPost]
public User Add(User user)
{
return _userService.Add(user);
}

[HttpPut]
[MultiSourceCacheable("MultiSource-single", "{user:id}", Target.InMemory, 5)]
[MultiSourceEvictable(new[] { "MultiSource-single", "MultiSources" }, "{user:id}", Target.InMemory)]
public virtual async Task<User> Update(User user)
{
return await _userService.Update(user);
}

[HttpDelete]
[MultiSourceEvictable(new[] { "MultiSource-single", "MultiSources" }, "{id}", Target.InMemory)]
public virtual bool Delete(string id)
{
return _userService.Delete(id);
}

[HttpGet("users")]
[MultiSourceCacheable("MultiSources", "{page}", Target.InMemory, 5)]
public virtual IEnumerable<User> Users(string page)
{
return _userService.List(page);
}
}

0 comments on commit bf1661b

Please sign in to comment.