test: cover persistence failover and graceful draining
Add API integration checks, migration compatibility, rolling restart scenarios and a report verification collector.
This commit is contained in:
+292
@@ -0,0 +1,292 @@
|
||||
import 'dotenv/config';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
||||
import { createRequire } from 'node:module';
|
||||
import test from 'node:test';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const Redis = require('ioredis').default;
|
||||
const { NestFactory } = require('@nestjs/core');
|
||||
const { AppModule } = require('../dist/app.module');
|
||||
const { PrismaService } = require('../dist/prisma/prisma.service');
|
||||
const { CacheService } = require('../dist/cache/cache.service');
|
||||
const {
|
||||
createFastifyAdapter,
|
||||
exposeRequestIdHeader,
|
||||
} = require('../dist/common/http/fastify-adapter');
|
||||
|
||||
async function isolatedCache(t) {
|
||||
assert.ok(process.env.CACHE_URL, 'Set CACHE_URL to a running Redis instance');
|
||||
const previous = process.env.CACHE_PREFIX;
|
||||
const prefix = `test:cache:${randomUUID()}`;
|
||||
process.env.CACHE_PREFIX = prefix;
|
||||
const observer = new Redis(process.env.CACHE_URL, {
|
||||
maxRetriesPerRequest: 0,
|
||||
connectTimeout: 1000,
|
||||
commandTimeout: 1000,
|
||||
});
|
||||
observer.on('error', () => {});
|
||||
await observer.ping();
|
||||
t.after(async () => {
|
||||
try {
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [next, keys] = await observer.scan(
|
||||
cursor,
|
||||
'MATCH',
|
||||
`${prefix}:*`,
|
||||
'COUNT',
|
||||
100,
|
||||
);
|
||||
cursor = next;
|
||||
if (keys.length) await observer.del(...keys);
|
||||
} while (cursor !== '0');
|
||||
} finally {
|
||||
observer.disconnect();
|
||||
if (previous === undefined) delete process.env.CACHE_PREFIX;
|
||||
else process.env.CACHE_PREFIX = previous;
|
||||
}
|
||||
});
|
||||
return { prefix, observer };
|
||||
}
|
||||
|
||||
test('Redis cache: hits, expiry, invalidation race and unavailable-server fallback', async (t) => {
|
||||
await isolatedCache(t);
|
||||
const cache = new CacheService();
|
||||
t.after(() => cache.onModuleDestroy());
|
||||
await cache.onModuleInit();
|
||||
let reads = 0;
|
||||
const load = async () => ({ value: ++reads });
|
||||
assert.deepEqual(await cache.remember('hits', 'page', 30, load), {
|
||||
value: 1,
|
||||
});
|
||||
assert.deepEqual(await cache.remember('hits', 'page', 30, load), {
|
||||
value: 1,
|
||||
});
|
||||
assert.equal(reads, 1);
|
||||
assert.deepEqual(await cache.remember('expiry', 'page', 1, load), {
|
||||
value: 2,
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 1100));
|
||||
assert.deepEqual(await cache.remember('expiry', 'page', 1, load), {
|
||||
value: 3,
|
||||
});
|
||||
await cache.invalidate('expiry');
|
||||
assert.deepEqual(await cache.remember('expiry', 'page', 1, load), {
|
||||
value: 4,
|
||||
});
|
||||
|
||||
let resolveOld;
|
||||
let began;
|
||||
const started = new Promise((resolve) => {
|
||||
began = resolve;
|
||||
});
|
||||
const pending = cache.remember('race', 'page', 30, () => {
|
||||
began();
|
||||
return new Promise((resolve) => {
|
||||
resolveOld = resolve;
|
||||
});
|
||||
});
|
||||
await started;
|
||||
await cache.invalidate('race');
|
||||
resolveOld({ value: 'old' });
|
||||
await pending;
|
||||
assert.deepEqual(
|
||||
await cache.remember('race', 'page', 30, async () => ({ value: 'new' })),
|
||||
{ value: 'new' },
|
||||
);
|
||||
assert.deepEqual(
|
||||
await cache.remember('race', 'page', 30, async () => {
|
||||
throw new Error('cache miss');
|
||||
}),
|
||||
{ value: 'new' },
|
||||
);
|
||||
await assert.rejects(
|
||||
cache.remember('db-error', 'page', 30, async () => {
|
||||
throw new Error('database failure');
|
||||
}),
|
||||
/database failure/,
|
||||
);
|
||||
|
||||
const previous = process.env.CACHE_URL;
|
||||
process.env.CACHE_URL = 'redis://127.0.0.1:1/0';
|
||||
const offline = new CacheService();
|
||||
process.env.CACHE_URL = previous;
|
||||
t.after(() => offline.onModuleDestroy());
|
||||
await offline.onModuleInit();
|
||||
assert.deepEqual(
|
||||
await offline.remember('offline', 'page', 30, async () => ({
|
||||
value: 'database',
|
||||
})),
|
||||
{ value: 'database' },
|
||||
);
|
||||
await offline.invalidate('offline');
|
||||
});
|
||||
|
||||
test('API cache: fewer SQL reads, write invalidation, permissions, logout and Redis fallback', async (t) => {
|
||||
assert.ok(process.env.DATABASE_URL);
|
||||
const { prefix, observer } = await isolatedCache(t);
|
||||
const app = await NestFactory.create(AppModule, createFastifyAdapter(), {
|
||||
logger: false,
|
||||
abortOnError: false,
|
||||
});
|
||||
exposeRequestIdHeader(app);
|
||||
await app.init();
|
||||
const server = app.getHttpAdapter().getInstance();
|
||||
await server.ready();
|
||||
const prisma = app.get(PrismaService);
|
||||
const userIds = [];
|
||||
const chatIds = [];
|
||||
t.after(async () => {
|
||||
try {
|
||||
await prisma.chat.deleteMany({ where: { id: { in: chatIds } } });
|
||||
await prisma.user.deleteMany({ where: { id: { in: userIds } } });
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
});
|
||||
const actors = [];
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username: `cache_${randomUUID().replaceAll('-', '').slice(0, 20)}`,
|
||||
displayName: 'Cache test',
|
||||
},
|
||||
});
|
||||
userIds.push(user.id);
|
||||
const token = randomBytes(32).toString('base64url');
|
||||
const session = await prisma.session.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
tokenHash: createHash('sha256').update(token).digest('hex'),
|
||||
expiresAt: new Date(Date.now() + 600000),
|
||||
},
|
||||
});
|
||||
actors.push({ id: user.id, token, sessionId: session.id });
|
||||
}
|
||||
const [alice, outsider] = actors;
|
||||
const request = (method, url, payload, actor = alice) =>
|
||||
server.inject({
|
||||
method,
|
||||
url,
|
||||
headers: {
|
||||
authorization: `Bearer ${actor.token}`,
|
||||
...(method === 'POST' && /\/messages$/.test(url)
|
||||
? { 'idempotency-key': randomUUID() }
|
||||
: {}),
|
||||
},
|
||||
...(payload === undefined ? {} : { payload }),
|
||||
});
|
||||
const counts = new Map();
|
||||
const tracked = prisma.$extends({
|
||||
query: {
|
||||
$allModels: {
|
||||
$allOperations({ model, operation, args, query }) {
|
||||
const key = `${model}:${operation}`;
|
||||
counts.set(key, (counts.get(key) ?? 0) + 1);
|
||||
return query(args);
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
for (const name of [
|
||||
'User/user.service',
|
||||
'Chat/chat.service',
|
||||
'Auth/auth.service',
|
||||
]) {
|
||||
const exported = require(`../dist/${name}`);
|
||||
const Service = Object.values(exported)[0];
|
||||
app.get(Service).prisma = tracked;
|
||||
}
|
||||
const spy = (model, operation) => ({
|
||||
mock: { callCount: () => counts.get(`${model}:${operation}`) ?? 0 },
|
||||
});
|
||||
const profileReads = spy('User', 'findUnique');
|
||||
assert.equal((await request('GET', '/api/v1/users/me')).statusCode, 200);
|
||||
assert.equal((await request('GET', '/api/v1/users/me')).statusCode, 200);
|
||||
assert.equal(profileReads.mock.callCount(), 0);
|
||||
|
||||
const chatListReads = spy('Chat', 'findMany');
|
||||
assert.equal((await request('GET', '/api/v1/chats')).json().items.length, 0);
|
||||
assert.equal((await request('GET', '/api/v1/chats')).json().items.length, 0);
|
||||
assert.equal(chatListReads.mock.callCount(), 1);
|
||||
const created = await request('POST', '/api/v1/chats', {
|
||||
title: 'Cached chat',
|
||||
member_ids: [alice.id],
|
||||
});
|
||||
assert.equal(created.statusCode, 201);
|
||||
const chatId = created.json().id;
|
||||
chatIds.push(chatId);
|
||||
assert.deepEqual(
|
||||
(await request('GET', '/api/v1/chats')).json().items.map((x) => x.id),
|
||||
[chatId],
|
||||
);
|
||||
assert.equal(chatListReads.mock.callCount(), 2);
|
||||
|
||||
const metadataReads = spy('Chat', 'findUnique');
|
||||
const messageReads = spy('Message', 'findMany');
|
||||
const path = `/api/v1/chats/${chatId}/messages`;
|
||||
assert.equal((await request('GET', path)).json().items.length, 0);
|
||||
assert.equal((await request('GET', path)).json().items.length, 0);
|
||||
assert.equal(metadataReads.mock.callCount(), 0);
|
||||
assert.equal(messageReads.mock.callCount(), 1);
|
||||
const message = await request('POST', path, {
|
||||
text: 'Invalidate cached empty history',
|
||||
});
|
||||
assert.equal(message.statusCode, 201);
|
||||
assert.deepEqual(
|
||||
(await request('GET', path)).json().items.map((x) => x.id),
|
||||
[message.json().id],
|
||||
);
|
||||
assert.equal(messageReads.mock.callCount(), 2);
|
||||
assert.equal(
|
||||
(await request('GET', path, undefined, outsider)).statusCode,
|
||||
404,
|
||||
);
|
||||
await prisma.chatMember.delete({
|
||||
where: { chatId_userId: { chatId, userId: alice.id } },
|
||||
});
|
||||
assert.equal((await request('GET', path)).statusCode, 404);
|
||||
await prisma.chatMember.create({
|
||||
data: { chatId, userId: alice.id, position: 0 },
|
||||
});
|
||||
await prisma.session.update({
|
||||
where: { id: outsider.sessionId },
|
||||
data: { revokedAt: new Date() },
|
||||
});
|
||||
assert.equal(
|
||||
(await request('GET', '/api/v1/users/me', undefined, outsider)).statusCode,
|
||||
401,
|
||||
);
|
||||
|
||||
const keys = [];
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [next, batch] = await observer.scan(
|
||||
cursor,
|
||||
'MATCH',
|
||||
`${prefix}:*`,
|
||||
'COUNT',
|
||||
100,
|
||||
);
|
||||
cursor = next;
|
||||
keys.push(...batch);
|
||||
} while (cursor !== '0');
|
||||
assert.ok(keys.length >= 4, 'Redis must contain the cached responses');
|
||||
for (const key of keys) {
|
||||
const value = await observer.get(key);
|
||||
assert.ok(
|
||||
!value.includes('password_hash') && !value.includes('passwordHash'),
|
||||
);
|
||||
for (const actor of actors) assert.ok(!value.includes(actor.token));
|
||||
}
|
||||
app.get(CacheService).onModuleDestroy();
|
||||
assert.equal((await request('GET', path)).statusCode, 200);
|
||||
assert.equal(messageReads.mock.callCount(), 3);
|
||||
assert.equal(
|
||||
(await request('POST', path, { text: 'Write while Redis is unavailable' }))
|
||||
.statusCode,
|
||||
201,
|
||||
);
|
||||
assert.equal((await request('GET', path)).json().items.length, 2);
|
||||
});
|
||||
Reference in New Issue
Block a user