diff --git a/modules/tenant-management/test/Volo.Abp.TenantManagement.Application.Tests/Volo/Abp/TenantManagement/TenantAppService_Tests.cs b/modules/tenant-management/test/Volo.Abp.TenantManagement.Application.Tests/Volo/Abp/TenantManagement/TenantAppService_Tests.cs index b797e05566..4288a66aea 100644 --- a/modules/tenant-management/test/Volo.Abp.TenantManagement.Application.Tests/Volo/Abp/TenantManagement/TenantAppService_Tests.cs +++ b/modules/tenant-management/test/Volo.Abp.TenantManagement.Application.Tests/Volo/Abp/TenantManagement/TenantAppService_Tests.cs @@ -2,6 +2,9 @@ using System.Linq; using System.Threading.Tasks; using Shouldly; +using Volo.Abp.Caching; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Uow; using Xunit; namespace Volo.Abp.TenantManagement; @@ -9,10 +12,20 @@ namespace Volo.Abp.TenantManagement; public class TenantAppService_Tests : AbpTenantManagementApplicationTestBase { private readonly ITenantAppService _tenantAppService; + private readonly IUnitOfWorkManager _unitOfWorkManager; + private readonly IDistributedCache _cache; + private readonly ITenantStore _tenantStore; + private readonly ITenantRepository _tenantRepository; + private readonly ITenantNormalizer _tenantNormalizer; public TenantAppService_Tests() { _tenantAppService = GetRequiredService(); + _unitOfWorkManager = GetRequiredService(); + _cache = GetRequiredService>(); + _tenantStore = GetRequiredService(); + _tenantRepository = GetRequiredService(); + _tenantNormalizer = GetRequiredService(); } [Fact] @@ -108,4 +121,46 @@ public class TenantAppService_Tests : AbpTenantManagementApplicationTestBase dbContext.Tenants.Any(t => t.Id == acme.Id).ShouldBeFalse(); }); } + + [Fact] + public async Task Cache_Should_Invalidator_When_Tenant_ConnectionString_Changed() + { + var acme = await _tenantRepository.FindByNameAsync(_tenantNormalizer.NormalizeName("acme")); + + // UpdateDefaultConnectionStringAsync + + // FindAsync will cache tenant. + await _tenantStore.FindAsync(acme.Id); + await _tenantStore.FindAsync(acme.NormalizedName); + + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(acme.Id, null))).ShouldNotBeNull(); + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, acme.NormalizedName))).ShouldNotBeNull(); + + using (var uow = _unitOfWorkManager.Begin(requiresNew: true)) + { + await _tenantAppService.UpdateDefaultConnectionStringAsync(acme.Id, Guid.NewGuid().ToString()); + await uow.CompleteAsync(); + } + + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(acme.Id, null))).ShouldBeNull(); + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, acme.NormalizedName))).ShouldBeNull(); + + // DeleteDefaultConnectionStringAsync + + // FindAsync will cache tenant. + await _tenantStore.FindAsync(acme.Id); + await _tenantStore.FindAsync(acme.NormalizedName); + + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(acme.Id, null))).ShouldNotBeNull(); + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, acme.NormalizedName))).ShouldNotBeNull(); + + using (var uow = _unitOfWorkManager.Begin(requiresNew: true)) + { + await _tenantAppService.DeleteDefaultConnectionStringAsync(acme.Id); + await uow.CompleteAsync(); + } + + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(acme.Id, null))).ShouldBeNull(); + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, acme.NormalizedName))).ShouldBeNull(); + } }