mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 14:08:15 +08:00
working on region clearing cache, if using cachemanager back plance this would clear all servers in cluster
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CacheManager.Core;
|
||||
using Moq;
|
||||
using Ocelot.Cache;
|
||||
@ -16,12 +17,14 @@ namespace Ocelot.UnitTests.Cache
|
||||
private string _value;
|
||||
private string _resultGet;
|
||||
private TimeSpan _ttlSeconds;
|
||||
private List<string> _resultKeys;
|
||||
|
||||
public CacheManagerCacheTests()
|
||||
{
|
||||
_mockCacheManager = new Mock<ICacheManager<string>>();
|
||||
_ocelotOcelotCacheManager = new OcelotCacheManagerCache<string>(_mockCacheManager.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_from_cache()
|
||||
{
|
||||
@ -29,7 +32,6 @@ namespace Ocelot.UnitTests.Cache
|
||||
.When(x => x.WhenIGetFromTheCache())
|
||||
.Then(x => x.ThenTheResultIs("someValue"))
|
||||
.BDDfy();
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -40,13 +42,37 @@ namespace Ocelot.UnitTests.Cache
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_delete_key_from_cache()
|
||||
{
|
||||
this.Given(_ => GivenTheFollowingRegion("fookey"))
|
||||
.When(_ => WhenIDeleteTheRegion("fookey"))
|
||||
.Then(_ => ThenTheRegionIsDeleted("fookey"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenIDeleteTheRegion(string region)
|
||||
{
|
||||
_ocelotOcelotCacheManager.ClearRegion(region);
|
||||
}
|
||||
|
||||
private void ThenTheRegionIsDeleted(string region)
|
||||
{
|
||||
_mockCacheManager
|
||||
.Verify(x => x.ClearRegion(region), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingRegion(string key)
|
||||
{
|
||||
_ocelotOcelotCacheManager.Add(key, "doesnt matter", TimeSpan.FromSeconds(10), "region");
|
||||
}
|
||||
|
||||
private void WhenIAddToTheCache(string key, string value, TimeSpan ttlSeconds)
|
||||
{
|
||||
_key = key;
|
||||
_value = value;
|
||||
_ttlSeconds = ttlSeconds;
|
||||
|
||||
_ocelotOcelotCacheManager.Add(_key, _value, _ttlSeconds);
|
||||
_ocelotOcelotCacheManager.Add(_key, _value, _ttlSeconds, "region");
|
||||
}
|
||||
|
||||
private void ThenTheCacheIsCalledCorrectly()
|
||||
|
@ -124,7 +124,7 @@ namespace Ocelot.UnitTests.Cache
|
||||
private void ThenTheCacheAddIsCalledCorrectly()
|
||||
{
|
||||
_cacheManager
|
||||
.Verify(x => x.Add(It.IsAny<string>(), It.IsAny<HttpResponseMessage>(), It.IsAny<TimeSpan>()), Times.Once);
|
||||
.Verify(x => x.Add(It.IsAny<string>(), It.IsAny<HttpResponseMessage>(), It.IsAny<TimeSpan>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenResponseIsNotCached()
|
||||
|
@ -0,0 +1,70 @@
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Ocelot.Controllers;
|
||||
using System;
|
||||
using Moq;
|
||||
using Ocelot.Cache;
|
||||
using System.Net.Http;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Ocelot.UnitTests.Controllers
|
||||
{
|
||||
public class OutputCacheControllerTests
|
||||
{
|
||||
private OutputCacheController _controller;
|
||||
private Mock<IOcelotCache<HttpResponseMessage>> _cache;
|
||||
private IActionResult _result;
|
||||
|
||||
public OutputCacheControllerTests()
|
||||
{
|
||||
_cache = new Mock<IOcelotCache<HttpResponseMessage>>();
|
||||
_controller = new OutputCacheController(_cache.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_all_keys_from_server()
|
||||
{
|
||||
this.Given(_ => GivenTheFollowingKeys(new List<string>{"b", "a"}))
|
||||
.When(_ => WhenIGetTheKeys())
|
||||
.Then(_ => ThenTheKeysAreReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_delete_key()
|
||||
{
|
||||
this.When(_ => WhenIDeleteTheKey("a"))
|
||||
.Then(_ => ThenTheKeyIsDeleted("a"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheKeyIsDeleted(string key)
|
||||
{
|
||||
_result.ShouldBeOfType<NoContentResult>();
|
||||
_cache
|
||||
.Verify(x => x.ClearRegion(key), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenIDeleteTheKey(string key)
|
||||
{
|
||||
_result = _controller.Delete(key);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingKeys(List<string> keys)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void WhenIGetTheKeys()
|
||||
{
|
||||
_result = _controller.Get();
|
||||
}
|
||||
|
||||
private void ThenTheKeysAreReturned()
|
||||
{
|
||||
_result.ShouldBeOfType<OkObjectResult>();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user