Permalink
Browse files

resolve failing tests due to MemoryCache cycling

  • Loading branch information...
rdefreitas committed Jul 13, 2016
1 parent b3bfbb1 commit ac739faa11bb92305fa6be453fdcc3db09d3d493
Showing with 53 additions and 28 deletions.
  1. +53 −28 tests/SaasKit.Multitenancy.Tests/MemoryCacheResolverTests.cs
@@ -72,13 +72,20 @@ public async Task Should_dispose_tenant_on_eviction_from_cache_by_default()
var tenantContext = await harness.Resolver.ResolveAsync(context);
TenantContext<TestTenant> cachedTenant;
TenantContext<TestTenant> cachedTenant = null;
Thread.Sleep(1000);
Thread.Sleep(3 * 1000);
// force MemoryCache to examine itself cache for pending evictions
harness.Cache.Get("/foobar");
Assert.False(harness.Cache.TryGetValue("/pear", out cachedTenant));
Assert.True(tenantContext.Tenant.Disposed);
Assert.Null(cachedTenant);
// and give it a moment to works its magic
Thread.Sleep(100);
// should also expire tenant context by default
Assert.False(harness.Cache.TryGetValue("/apple", out cachedTenant), "Apple Exists");
Assert.True(tenantContext.Tenant.Disposed);
Assert.Null(cachedTenant);
}
[Fact]
@@ -99,21 +106,18 @@ public async Task Should_evict_all_cache_entries_of_tenant_context_by_default()
// expire apple
harness.Cache.Remove("/apple");
// look it up again so it registers
harness.Cache.TryGetValue("/apple", out cachedTenant);
Thread.Sleep(500);
// need to spin up a new task as "long running"
// so that MemoryCache can fire the eviction callbacks first
await Task.Factory.StartNew(state =>
{
Thread.Sleep(500);
// look it up again so it registers
harness.Cache.TryGetValue("/apple", out cachedTenant);
// pear is expired - because apple is
Assert.False(harness.Cache.TryGetValue("/pear", out cachedTenant), "Pear Exists");
// should also expire tenant context by default
Assert.True(tenantContext.Tenant.Disposed);
Thread.Sleep(500);
}, this, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.FromCurrentSynchronizationContext());
// pear is expired - because apple is
Assert.False(harness.Cache.TryGetValue("/pear", out cachedTenant), "Pear Exists");
// should also expire tenant context by default
Assert.True(tenantContext.Tenant.Disposed);
}
[Fact]
@@ -140,23 +144,44 @@ public async Task Can_evict_single_cache_entry_of_tenant_context()
// pear is not expired
Assert.True(harness.Cache.TryGetValue("/pear", out cachedTenant), "Pear Does Not Exist");
}
}
[Fact]
public async Task Can_not_dispose_on_eviction()
{
var harness = new TestHarness(cacheExpirationInSeconds: 1, disposeOnEviction: false);
var context = CreateContext("/apple");
[Fact]
public async Task Can_dispose_on_eviction()
{
var harness = new TestHarness(cacheExpirationInSeconds: 1, disposeOnEviction: true);
var context = CreateContext("/apple");
var tenantContext = await harness.Resolver.ResolveAsync(context);
var tenantContext = await harness.Resolver.ResolveAsync(context);
Thread.Sleep(3 * 1000);
Thread.Sleep(2 * 1000);
// access it again so that MemoryCache examines it's cache for pending evictions
harness.Cache.Get("/foobar");
Assert.False(tenantContext.Tenant.Disposed);
}
Thread.Sleep(1 * 1000);
// access it again and we should see the eviction
Assert.True(tenantContext.Tenant.Disposed);
}
[Fact]
public async Task Can_not_dispose_on_eviction()
{
var harness = new TestHarness(cacheExpirationInSeconds: 1, disposeOnEviction: false);
var context = CreateContext("/apple");
var tenantContext = await harness.Resolver.ResolveAsync(context);
Thread.Sleep(1 * 1000);
// access it again so that MemoryCache examines it's cache for pending evictions
harness.Cache.Get("/foobar");
Thread.Sleep(1 * 1000);
// access it again and even though it's disposed, it should not be evicted
Assert.False(tenantContext.Tenant.Disposed);
}
class TestTenant : IDisposable
class TestTenant : IDisposable
{
public bool Disposed { get; set; }

0 comments on commit ac739fa

Please sign in to comment.