|
|
|
@ -7,6 +7,7 @@ |
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using FakeItEasy; |
|
|
|
using Microsoft.Extensions.Caching.Memory; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using Xunit; |
|
|
|
@ -15,12 +16,13 @@ namespace Squidex.Infrastructure.Caching |
|
|
|
{ |
|
|
|
public class ReplicatedCacheTests |
|
|
|
{ |
|
|
|
private readonly IPubSub pubSub = new SimplePubSub(); |
|
|
|
private readonly IPubSub pubSub = A.Fake<SimplePubSub>(options => options.CallsBaseMethods()); |
|
|
|
private readonly ReplicatedCacheOptions options = new ReplicatedCacheOptions { Enable = true }; |
|
|
|
private readonly ReplicatedCache sut; |
|
|
|
|
|
|
|
public ReplicatedCacheTests() |
|
|
|
{ |
|
|
|
sut = new ReplicatedCache(CreateMemoryCache(), pubSub); |
|
|
|
sut = new ReplicatedCache(CreateMemoryCache(), pubSub, Options.Create(options)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
@ -36,7 +38,17 @@ namespace Squidex.Infrastructure.Caching |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_not_served_when_expired() |
|
|
|
public void Should_not_serve_from_cache_disabled() |
|
|
|
{ |
|
|
|
options.Enable = false; |
|
|
|
|
|
|
|
sut.Add("Key", 1, TimeSpan.FromMilliseconds(100), true); |
|
|
|
|
|
|
|
AssertCache(sut, "Key", null, false); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_not_serve_from_cache_when_expired() |
|
|
|
{ |
|
|
|
sut.Add("Key", 1, TimeSpan.FromMilliseconds(1), true); |
|
|
|
|
|
|
|
@ -48,8 +60,8 @@ namespace Squidex.Infrastructure.Caching |
|
|
|
[Fact] |
|
|
|
public void Should_not_invalidate_other_instances_when_item_added_and_flag_is_false() |
|
|
|
{ |
|
|
|
var cache1 = new ReplicatedCache(CreateMemoryCache(), pubSub); |
|
|
|
var cache2 = new ReplicatedCache(CreateMemoryCache(), pubSub); |
|
|
|
var cache1 = new ReplicatedCache(CreateMemoryCache(), pubSub, Options.Create(options)); |
|
|
|
var cache2 = new ReplicatedCache(CreateMemoryCache(), pubSub, Options.Create(options)); |
|
|
|
|
|
|
|
cache1.Add("Key", 1, TimeSpan.FromMinutes(1), false); |
|
|
|
cache2.Add("Key", 2, TimeSpan.FromMinutes(1), false); |
|
|
|
@ -61,8 +73,8 @@ namespace Squidex.Infrastructure.Caching |
|
|
|
[Fact] |
|
|
|
public void Should_invalidate_other_instances_when_item_added_and_flag_is_true() |
|
|
|
{ |
|
|
|
var cache1 = new ReplicatedCache(CreateMemoryCache(), pubSub); |
|
|
|
var cache2 = new ReplicatedCache(CreateMemoryCache(), pubSub); |
|
|
|
var cache1 = new ReplicatedCache(CreateMemoryCache(), pubSub, Options.Create(options)); |
|
|
|
var cache2 = new ReplicatedCache(CreateMemoryCache(), pubSub, Options.Create(options)); |
|
|
|
|
|
|
|
cache1.Add("Key", 1, TimeSpan.FromMinutes(1), true); |
|
|
|
cache2.Add("Key", 2, TimeSpan.FromMinutes(1), true); |
|
|
|
@ -74,8 +86,8 @@ namespace Squidex.Infrastructure.Caching |
|
|
|
[Fact] |
|
|
|
public void Should_invalidate_other_instances_when_item_removed() |
|
|
|
{ |
|
|
|
var cache1 = new ReplicatedCache(CreateMemoryCache(), pubSub); |
|
|
|
var cache2 = new ReplicatedCache(CreateMemoryCache(), pubSub); |
|
|
|
var cache1 = new ReplicatedCache(CreateMemoryCache(), pubSub, Options.Create(options)); |
|
|
|
var cache2 = new ReplicatedCache(CreateMemoryCache(), pubSub, Options.Create(options)); |
|
|
|
|
|
|
|
cache1.Add("Key", 1, TimeSpan.FromMinutes(1), true); |
|
|
|
cache2.Remove("Key"); |
|
|
|
@ -84,6 +96,35 @@ namespace Squidex.Infrastructure.Caching |
|
|
|
AssertCache(cache2, "Key", null, false); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_send_invalidation_message_when_added_and_flag_is_true() |
|
|
|
{ |
|
|
|
sut.Add("Key", 1, TimeSpan.FromMinutes(1), true); |
|
|
|
|
|
|
|
A.CallTo(() => pubSub.Publish(A<object>._)) |
|
|
|
.MustHaveHappened(); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_not_send_invalidation_message_when_added_flag_is_false() |
|
|
|
{ |
|
|
|
sut.Add("Key", 1, TimeSpan.FromMinutes(1), false); |
|
|
|
|
|
|
|
A.CallTo(() => pubSub.Publish(A<object>._)) |
|
|
|
.MustNotHaveHappened(); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_not_send_invalidation_message_when_added_but_disabled() |
|
|
|
{ |
|
|
|
options.Enable = false; |
|
|
|
|
|
|
|
sut.Add("Key", 1, TimeSpan.FromMinutes(1), true); |
|
|
|
|
|
|
|
A.CallTo(() => pubSub.Publish(A<object>._)) |
|
|
|
.MustNotHaveHappened(); |
|
|
|
} |
|
|
|
|
|
|
|
private static void AssertCache(IReplicatedCache cache, string key, object? expectedValue, bool expectedFound) |
|
|
|
{ |
|
|
|
var found = cache.TryGetValue(key, out var value); |
|
|
|
|