mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 12:30:49 +08:00 
			
		
		
		
	removed old memory cache and added a few tests for new asp memory cache
This commit is contained in:
		@@ -1,12 +1,11 @@
 | 
			
		||||
using Microsoft.Extensions.Caching.Memory;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Cache
 | 
			
		||||
namespace Ocelot.UnitTests.Cache
 | 
			
		||||
{
 | 
			
		||||
    using Ocelot.Cache;
 | 
			
		||||
    using Shouldly;
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Threading;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
    using Microsoft.Extensions.Caching.Memory;
 | 
			
		||||
 | 
			
		||||
    public class AspMemoryCacheTests
 | 
			
		||||
    {
 | 
			
		||||
@@ -27,6 +26,13 @@ namespace Ocelot.UnitTests.Cache
 | 
			
		||||
            fake.Value.ShouldBe(1);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void doesnt_exist()
 | 
			
		||||
        {
 | 
			
		||||
            var result = _cache.Get("1", "region");
 | 
			
		||||
            result.ShouldBeNull();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_and_delete()
 | 
			
		||||
        {
 | 
			
		||||
@@ -42,11 +48,15 @@ namespace Ocelot.UnitTests.Cache
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_clear_region()
 | 
			
		||||
        {
 | 
			
		||||
            var fake = new Fake(1);
 | 
			
		||||
            _cache.Add("1", fake, TimeSpan.FromSeconds(100), "region");
 | 
			
		||||
            var fake1 = new Fake(1);
 | 
			
		||||
            var fake2 = new Fake(2);
 | 
			
		||||
            _cache.Add("1", fake1, TimeSpan.FromSeconds(100), "region");
 | 
			
		||||
            _cache.Add("2", fake2, TimeSpan.FromSeconds(100), "region");
 | 
			
		||||
            _cache.ClearRegion("region");
 | 
			
		||||
            var result = _cache.Get("1", "region");
 | 
			
		||||
            result.ShouldBeNull();
 | 
			
		||||
            var result1 = _cache.Get("1", "region");
 | 
			
		||||
            result1.ShouldBeNull();
 | 
			
		||||
            var result2 = _cache.Get("2", "region");
 | 
			
		||||
            result2.ShouldBeNull();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
 
 | 
			
		||||
@@ -1,81 +0,0 @@
 | 
			
		||||
namespace Ocelot.UnitTests.Cache
 | 
			
		||||
{
 | 
			
		||||
    using Ocelot.Cache;
 | 
			
		||||
    using Shouldly;
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Threading;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
 | 
			
		||||
    public class InMemoryCacheTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly InMemoryCache<Fake> _cache;
 | 
			
		||||
 | 
			
		||||
        public InMemoryCacheTests()
 | 
			
		||||
        {
 | 
			
		||||
            _cache = new InMemoryCache<Fake>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_cache()
 | 
			
		||||
        {
 | 
			
		||||
            var fake = new Fake(1);
 | 
			
		||||
            _cache.Add("1", fake, TimeSpan.FromSeconds(100), "region");
 | 
			
		||||
            var result = _cache.Get("1", "region");
 | 
			
		||||
            result.ShouldBe(fake);
 | 
			
		||||
            fake.Value.ShouldBe(1);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_and_delete()
 | 
			
		||||
        {
 | 
			
		||||
            var fake = new Fake(1);
 | 
			
		||||
            _cache.Add("1", fake, TimeSpan.FromSeconds(100), "region");
 | 
			
		||||
            var newFake = new Fake(1);
 | 
			
		||||
            _cache.AddAndDelete("1", newFake, TimeSpan.FromSeconds(100), "region");
 | 
			
		||||
            var result = _cache.Get("1", "region");
 | 
			
		||||
            result.ShouldBe(newFake);
 | 
			
		||||
            newFake.Value.ShouldBe(1);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_clear_region()
 | 
			
		||||
        {
 | 
			
		||||
            var fake = new Fake(1);
 | 
			
		||||
            _cache.Add("1", fake, TimeSpan.FromSeconds(100), "region");
 | 
			
		||||
            _cache.ClearRegion("region");
 | 
			
		||||
            var result = _cache.Get("1", "region");
 | 
			
		||||
            result.ShouldBeNull();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_clear_key_if_ttl_expired()
 | 
			
		||||
        {
 | 
			
		||||
            var fake = new Fake(1);
 | 
			
		||||
            _cache.Add("1", fake, TimeSpan.FromMilliseconds(50), "region");
 | 
			
		||||
            Thread.Sleep(200);
 | 
			
		||||
            var result = _cache.Get("1", "region");
 | 
			
		||||
            result.ShouldBeNull();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Theory]
 | 
			
		||||
        [InlineData(0)]
 | 
			
		||||
        [InlineData(-1)]
 | 
			
		||||
        public void should_not_add_to_cache_if_timespan_empty(int ttl)
 | 
			
		||||
        {
 | 
			
		||||
            var fake = new Fake(1);
 | 
			
		||||
            _cache.Add("1", fake, TimeSpan.FromSeconds(ttl), "region");
 | 
			
		||||
            var result = _cache.Get("1", "region");
 | 
			
		||||
            result.ShouldBeNull();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private class Fake
 | 
			
		||||
        {
 | 
			
		||||
            public Fake(int value)
 | 
			
		||||
            {
 | 
			
		||||
                Value = value;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public int Value { get; }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -3,8 +3,8 @@
 | 
			
		||||
    using global::CacheManager.Core;
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.Cache.CacheManager;
 | 
			
		||||
    using Shouldly;
 | 
			
		||||
    using System;
 | 
			
		||||
    using Shouldly;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user