2
0

system-config-redis-cache.integration.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { describe, it, expect, beforeEach, vi } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import {
  4. IntegrationTestDatabase,
  5. setupIntegrationDatabaseHooksWithEntities
  6. } from '@d8d/shared-test-util';
  7. import { systemConfigRoutes } from '../../src/routes/system-config.routes';
  8. import { SystemConfig } from '../../src/entities/system-config.entity';
  9. import { SystemConfigService } from '../../src/services/system-config.service';
  10. import { UserEntity, Role } from '@d8d/core-module/user-module';
  11. import { File } from '@d8d/core-module/file-module';
  12. import { TestDataFactory } from '../utils/integration-test-db';
  13. import { AuthService } from '@d8d/core-module/auth-module';
  14. import { UserService } from '@d8d/core-module/user-module';
  15. import { redisUtil } from '@d8d/shared-utils';
  16. // 设置集成测试钩子
  17. setupIntegrationDatabaseHooksWithEntities([SystemConfig, UserEntity, Role, File])
  18. describe('系统配置Redis缓存集成测试', () => {
  19. let client: ReturnType<typeof testClient<typeof systemConfigRoutes>>;
  20. let authService: AuthService;
  21. let userService: UserService;
  22. let systemConfigService: SystemConfigService;
  23. let testToken: string;
  24. let testUser: any;
  25. beforeEach(async () => {
  26. // 创建测试客户端
  27. client = testClient(systemConfigRoutes);
  28. // 获取数据源
  29. const dataSource = await IntegrationTestDatabase.getDataSource();
  30. if (!dataSource) throw new Error('Database not initialized');
  31. // 初始化服务
  32. userService = new UserService(dataSource);
  33. authService = new AuthService(userService);
  34. systemConfigService = new SystemConfigService(dataSource);
  35. // 创建测试用户并生成token
  36. testUser = await TestDataFactory.createTestUser(dataSource, {
  37. username: 'testuser_rediscache',
  38. password: 'TestPassword123!',
  39. email: 'testuser_rediscache@example.com'
  40. });
  41. // 生成测试用户的token
  42. testToken = authService.generateToken(testUser);
  43. // 清除测试前的缓存
  44. });
  45. describe('缓存命中测试', () => {
  46. it('应该从缓存中获取配置值', async () => {
  47. // 先创建配置
  48. const config = await systemConfigService.create({
  49. configKey: 'app.cache.test',
  50. configValue: 'cached-value',
  51. description: '缓存测试配置',
  52. } as SystemConfig);
  53. // 第一次查询 - 应该从数据库获取并写入缓存
  54. const firstResult = await systemConfigService.getConfigByKey('app.cache.test');
  55. expect(firstResult).toBe('cached-value');
  56. // 验证缓存已写入
  57. const cachedValue = await redisUtil.getSystemConfig('app.cache.test');
  58. expect(cachedValue).toBe('cached-value');
  59. // 第二次查询 - 应该从缓存获取
  60. const secondResult = await systemConfigService.getConfigByKey('app.cache.test');
  61. expect(secondResult).toBe('cached-value');
  62. });
  63. it('应该批量从缓存中获取配置值', async () => {
  64. // 创建多个配置
  65. await systemConfigService.create({
  66. configKey: 'app.feature1.enabled',
  67. configValue: 'true',
  68. } as SystemConfig);
  69. await systemConfigService.create({
  70. configKey: 'app.feature2.enabled',
  71. configValue: 'false',
  72. } as SystemConfig);
  73. // 第一次批量查询 - 应该从数据库获取并写入缓存
  74. const firstResult = await systemConfigService.getConfigsByKeys(
  75. ['app.feature1.enabled', 'app.feature2.enabled']
  76. );
  77. expect(firstResult['app.feature1.enabled']).toBe('true');
  78. expect(firstResult['app.feature2.enabled']).toBe('false');
  79. // 验证缓存已写入
  80. const cachedValues = await redisUtil.getSystemConfigs(['app.feature1.enabled', 'app.feature2.enabled']);
  81. expect(cachedValues['app.feature1.enabled']).toBe('true');
  82. expect(cachedValues['app.feature2.enabled']).toBe('false');
  83. // 第二次批量查询 - 应该从缓存获取
  84. const secondResult = await systemConfigService.getConfigsByKeys(
  85. ['app.feature1.enabled', 'app.feature2.enabled']
  86. );
  87. expect(secondResult['app.feature1.enabled']).toBe('true');
  88. expect(secondResult['app.feature2.enabled']).toBe('false');
  89. });
  90. });
  91. describe('缓存失效测试', () => {
  92. it('应该在配置更新时清除缓存', async () => {
  93. // 清除可能存在的缓存
  94. await redisUtil.deleteSystemConfig('app.update.test');
  95. // 创建配置
  96. const config = await systemConfigService.create({
  97. configKey: 'app.update.test',
  98. configValue: 'initial-value',
  99. } as SystemConfig);
  100. // 查询一次以填充缓存
  101. await systemConfigService.getConfigByKey('app.update.test');
  102. // 验证缓存已写入
  103. const cachedValue = await redisUtil.getSystemConfig('app.update.test');
  104. console.debug('缓存值:', cachedValue, '期望值: initial-value');
  105. expect(cachedValue).toBe('initial-value');
  106. // 更新配置
  107. await systemConfigService.update(config.id, { configValue: 'updated-value' });
  108. // 等待一小段时间确保缓存操作完成
  109. await new Promise(resolve => setTimeout(resolve, 50));
  110. // 验证缓存已清除
  111. const clearedCache = await redisUtil.getSystemConfig('app.update.test');
  112. expect(clearedCache).toBeNull();
  113. // 再次查询应该从数据库获取新值
  114. const result = await systemConfigService.getConfigByKey('app.update.test');
  115. expect(result).toBe('updated-value');
  116. });
  117. it('应该在配置删除时清除缓存', async () => {
  118. // 创建配置
  119. const config = await systemConfigService.create({
  120. configKey: 'app.delete.test',
  121. configValue: 'to-be-deleted',
  122. } as SystemConfig);
  123. // 查询一次以填充缓存
  124. await systemConfigService.getConfigByKey('app.delete.test');
  125. // 验证缓存已写入
  126. const cachedValue = await redisUtil.getSystemConfig('app.delete.test');
  127. expect(cachedValue).toBe('to-be-deleted');
  128. // 删除配置
  129. await systemConfigService.delete(config.id);
  130. // 验证缓存已清除
  131. const clearedCache = await redisUtil.getSystemConfig('app.delete.test');
  132. expect(clearedCache).toBeNull();
  133. });
  134. });
  135. describe('缓存穿透保护测试', () => {
  136. it('应该防止缓存穿透攻击', async () => {
  137. const nonExistentKey = 'app.nonexistent.config';
  138. // 第一次查询不存在的配置
  139. const firstResult = await systemConfigService.getConfigByKey('app.nonexistent.config');
  140. expect(firstResult).toBeNull();
  141. // 验证空值缓存已设置
  142. const cachedValue = await redisUtil.getSystemConfig('app.nonexistent.config');
  143. expect(redisUtil.isNullValue(cachedValue)).toBe(true);
  144. // 第二次查询应该从空值缓存返回null
  145. const secondResult = await systemConfigService.getConfigByKey('app.nonexistent.config');
  146. expect(secondResult).toBeNull();
  147. });
  148. it('应该在批量查询中防止缓存穿透', async () => {
  149. const existentKey = 'app.existent.config';
  150. const nonExistentKey = 'app.nonexistent.config';
  151. // 创建一个存在的配置
  152. await systemConfigService.create({
  153. configKey: existentKey,
  154. configValue: 'existent-value',
  155. } as SystemConfig);
  156. // 批量查询包含存在和不存在的配置
  157. const result = await systemConfigService.getConfigsByKeys(
  158. [existentKey, nonExistentKey]
  159. );
  160. expect(result[existentKey]).toBe('existent-value');
  161. expect(result[nonExistentKey]).toBeUndefined();
  162. // 验证空值缓存已设置
  163. const cachedValues = await redisUtil.getSystemConfigs([existentKey, nonExistentKey]);
  164. expect(cachedValues[existentKey]).toBe('existent-value');
  165. expect(redisUtil.isNullValue(cachedValues[nonExistentKey])).toBe(true);
  166. });
  167. });
  168. // 多租户缓存隔离测试已移除,因为core-module是非多租户版本
  169. describe('缓存预热测试', () => {
  170. it('应该成功预热缓存', async () => {
  171. // 创建一些常用配置
  172. await systemConfigService.create({
  173. configKey: 'app.login.enabled',
  174. configValue: 'true',
  175. } as SystemConfig);
  176. await systemConfigService.create({
  177. configKey: 'app.payment.enabled',
  178. configValue: 'false',
  179. } as SystemConfig);
  180. // 预热缓存
  181. await systemConfigService.warmUpCache();
  182. // 验证缓存已预热
  183. const cachedValues = await redisUtil.getSystemConfigs([
  184. 'app.login.enabled',
  185. 'app.payment.enabled',
  186. 'app.notification.enabled'
  187. ]);
  188. expect(cachedValues['app.login.enabled']).toBe('true');
  189. expect(cachedValues['app.payment.enabled']).toBe('false');
  190. expect(redisUtil.isNullValue(cachedValues['app.notification.enabled'])).toBe(true);
  191. });
  192. });
  193. });