WMS_MAIN/test/app/extend/application.cache.test.js
2026-04-16 20:00:01 +08:00

239 lines
5.8 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const applicationExtension = require('../../../app/extend/application');
function createAppHarness(options = {}) {
const redisStore = new Map();
const loggerMessages = [];
const logger = {
info() {},
warn(...args) {
loggerMessages.push([ 'warn', ...args ]);
},
error(...args) {
loggerMessages.push([ 'error', ...args ]);
},
};
const redisClient = {
async get(cacheKey) {
if (typeof options.redisGet === 'function') {
return await options.redisGet(cacheKey, redisStore);
}
if (options.redisGetError) {
throw options.redisGetError;
}
return redisStore.has(cacheKey) ? redisStore.get(cacheKey) : null;
},
async setex(cacheKey, expireSeconds, value) {
if (typeof options.redisSetex === 'function') {
return await options.redisSetex(cacheKey, expireSeconds, value, redisStore);
}
if (options.redisSetexError) {
throw options.redisSetexError;
}
redisStore.set(cacheKey, value);
},
async del(cacheKey) {
if (typeof options.redisDel === 'function') {
return await options.redisDel(cacheKey, redisStore);
}
if (options.redisDelError) {
throw options.redisDelError;
}
redisStore.delete(cacheKey);
},
};
const app = {
...applicationExtension,
logger,
coreLogger: logger,
redis: {
get(name) {
if (name !== 'cache') {
return null;
}
if (options.redisClientError) {
throw options.redisClientError;
}
return redisClient;
},
},
};
return {
app,
loggerMessages,
redisStore,
};
}
function destroyAppHarness(app) {
const state = app.__wmsLocalCacheState;
if (state && state.cleanupTimer) {
clearInterval(state.cleanupTimer);
}
}
describe('test/app/extend/application.cache.test.js', () => {
it('should fall back to in-memory cache when redis read and write fail', async () => {
const { app, redisStore } = createAppHarness({
redisSetexError: new Error('redis set failed'),
redisGetError: new Error('redis get failed'),
});
try {
const cacheKey = 'cache:fallback:test';
const cacheValue = {
id: 1,
name: 'fallback',
};
await app.setCacheAsync(cacheKey, cacheValue, 30);
assert.equal(redisStore.has(cacheKey), false);
assert.equal(app.__wmsLocalCacheState.store.has(cacheKey), true);
const result = await app.getCacheAsync(cacheKey);
assert.deepEqual(result, cacheValue);
} finally {
destroyAppHarness(app);
}
});
it('should return null on redis miss without falling back to local cache', async () => {
const { app } = createAppHarness();
try {
const cacheKey = 'cache:miss:test';
app.__wmsLocalCacheState = {
store: new Map([
[
cacheKey,
{
value: {
source: 'local-stale',
},
expireAt: Date.now() + 60000,
},
],
]),
cleanupTimer: null,
lastCleanupAt: 0,
};
const result = await app.getCacheAsync(cacheKey);
assert.equal(result, null);
assert.equal(app.__wmsLocalCacheState.store.has(cacheKey), true);
} finally {
destroyAppHarness(app);
}
});
it('should refresh local cache after redis write succeeds', async () => {
const { app, redisStore } = createAppHarness();
try {
const cacheKey = 'cache:sync:test';
const staleValue = {
source: 'local-stale',
};
const freshValue = {
source: 'redis-fresh',
};
app.__wmsLocalCacheState = {
store: new Map([
[
cacheKey,
{
value: staleValue,
expireAt: Date.now() + 60000,
},
],
]),
cleanupTimer: null,
lastCleanupAt: 0,
};
await app.setCacheAsync(cacheKey, freshValue, 30);
assert.equal(redisStore.get(cacheKey), JSON.stringify(freshValue));
assert.deepEqual(app.__wmsLocalCacheState.store.get(cacheKey).value, freshValue);
} finally {
destroyAppHarness(app);
}
});
it('should prefer redis when redis cache is available', async () => {
const { app } = createAppHarness();
try {
const cacheKey = 'cache:priority:test';
app.__wmsLocalCacheState = {
store: new Map([
[
cacheKey,
{
value: {
source: 'local',
},
expireAt: Date.now() + 60000,
},
],
]),
cleanupTimer: null,
lastCleanupAt: 0,
};
await app.redis.get('cache').setex(cacheKey, 30, JSON.stringify({
source: 'redis',
}));
const result = await app.getCacheAsync(cacheKey);
assert.deepEqual(result, {
source: 'redis',
});
} finally {
destroyAppHarness(app);
}
});
it('should recycle expired in-memory cache entries automatically', async () => {
const fixedNow = 1760000000000;
const originalNow = Date.now;
Date.now = () => fixedNow;
let app;
try {
({ app } = createAppHarness({
redisSetexError: new Error('redis unavailable'),
redisGetError: new Error('redis unavailable'),
}));
const cacheKey = 'cache:expiry:test';
await app.setCacheAsync(cacheKey, {
expired: false,
}, 1);
assert.equal(app.__wmsLocalCacheState.store.has(cacheKey), true);
Date.now = () => fixedNow + 2000;
const result = await app.getCacheAsync(cacheKey);
assert.equal(result, null);
assert.equal(app.__wmsLocalCacheState.store.has(cacheKey), false);
} finally {
Date.now = originalNow;
destroyAppHarness(app);
}
});
});