forked from mtusi2026/mtusi2026lab3
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:
@@ -0,0 +1,268 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { createHash } = require('node:crypto');
|
||||
const { spawn, execFileSync } = require('node:child_process');
|
||||
const ts = require('typescript');
|
||||
|
||||
const root = path.resolve(__dirname, '..');
|
||||
const resultPrefix = process.env.EXPERIMENT_PREFIX ?? 'cache-strategy';
|
||||
assert.match(resultPrefix, /^[a-z0-9-]+$/);
|
||||
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lab2-no-epochs-'));
|
||||
const hashes = {};
|
||||
const volumes = [];
|
||||
const replace = (source, from, to) => {
|
||||
assert.ok(source.includes(from), `Missing replacement: ${from}`);
|
||||
assert.equal(
|
||||
source.indexOf(from),
|
||||
source.lastIndexOf(from),
|
||||
`Ambiguous replacement: ${from}`,
|
||||
);
|
||||
return source.replace(from, to);
|
||||
};
|
||||
const section = (source, from, to, replacement) => {
|
||||
const start = source.indexOf(from);
|
||||
const end = source.indexOf(to, start);
|
||||
assert.ok(start !== -1 && end > start, `Missing section: ${from}`);
|
||||
return source.slice(0, start) + replacement + source.slice(end);
|
||||
};
|
||||
const digest = (value) => createHash('sha256').update(value).digest('hex');
|
||||
|
||||
const transforms = {
|
||||
'Chat/chat.service.ts': (source) => {
|
||||
source = section(
|
||||
source,
|
||||
' await tx.$executeRaw(',
|
||||
' return created;',
|
||||
'',
|
||||
);
|
||||
source = replace(source, ' epoch: string,\n', '');
|
||||
source = replace(source, ' epoch,\n', '');
|
||||
source = section(
|
||||
source,
|
||||
' const [membership] =',
|
||||
' if (!membership)',
|
||||
' const [membership] = await this.prisma.$queryRaw<{ id: string }[]>(Prisma.sql`\n' +
|
||||
' SELECT cm.chat_id AS id FROM chat_members AS cm\n' +
|
||||
' WHERE cm.chat_id = ${id}::uuid AND cm.user_id = ${userId}::uuid\n' +
|
||||
' `);\n',
|
||||
);
|
||||
source = section(
|
||||
source,
|
||||
' const [message] =',
|
||||
' if (!message)',
|
||||
' const [message] = await this.prisma.$queryRaw<StoredMessage[]>(Prisma.sql`\n' +
|
||||
' INSERT INTO messages (id, chat_id, sender_id, text)\n' +
|
||||
' SELECT ${randomUUID()}::uuid, cm.chat_id, ${userId}::uuid, ${text}\n' +
|
||||
' FROM chat_members AS cm\n' +
|
||||
' WHERE cm.chat_id = ${id}::uuid AND cm.user_id = ${userId}::uuid\n' +
|
||||
' RETURNING id, chat_id AS "chatId", sender_id AS "senderId",\n' +
|
||||
' text, created_at AS "createdAt"\n' +
|
||||
' `);\n',
|
||||
);
|
||||
return replace(source, ' chat.messagesEpoch.toString(),\n', '');
|
||||
},
|
||||
'Auth/auth.service.ts': (source) => {
|
||||
source = source.replace(
|
||||
/COALESCE\(\(SELECT SUM\(e\.epoch\)[\s\S]*?0\)::bigint AS "messagesEpoch"/g,
|
||||
'0::bigint AS "messagesEpoch"',
|
||||
);
|
||||
return source
|
||||
.replaceAll('u.chats_epoch', '0::bigint')
|
||||
.replaceAll('c.messages_epoch', '0::bigint');
|
||||
},
|
||||
'User/user.guard.ts': (source) => {
|
||||
source = replace(source, ' currentChatsEpoch: string;\n', '');
|
||||
return replace(
|
||||
source,
|
||||
' request.currentChatsEpoch = session.chatsEpoch;\n',
|
||||
'',
|
||||
);
|
||||
},
|
||||
'Chat/chat.controller.ts': (source) =>
|
||||
replace(source, ' request.currentChatsEpoch,\n', ''),
|
||||
'cache/cache.service.ts': (source) => {
|
||||
source = replace(source, ' epoch?: string,\n', '');
|
||||
return section(
|
||||
source,
|
||||
' if (epoch !== undefined)',
|
||||
' const versionKey =',
|
||||
'',
|
||||
);
|
||||
},
|
||||
'User/user.service.ts': (source) => {
|
||||
source = replace(
|
||||
source,
|
||||
'const user = await this.prisma.user.findUnique({ where: { id } });',
|
||||
'const user = await this.prisma.user.findUnique({ where: { id }, select: { id: true, username: true, displayName: true, createdAt: true } });',
|
||||
);
|
||||
return source;
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
for (const [relative, transform] of Object.entries(transforms)) {
|
||||
const original = fs.readFileSync(path.join(root, 'src', relative), 'utf8');
|
||||
const source = transform(original.replace(/\r\n/g, '\n'));
|
||||
assert.ok(
|
||||
!/messages_epoch|chats_epoch|chat_message_epochs/.test(source),
|
||||
relative,
|
||||
);
|
||||
const compiled = ts.transpileModule(source, {
|
||||
compilerOptions: {
|
||||
target: ts.ScriptTarget.ES2023,
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
experimentalDecorators: true,
|
||||
emitDecoratorMetadata: true,
|
||||
},
|
||||
reportDiagnostics: true,
|
||||
});
|
||||
assert.equal(
|
||||
compiled.diagnostics.filter(
|
||||
(item) => item.category === ts.DiagnosticCategory.Error,
|
||||
).length,
|
||||
0,
|
||||
);
|
||||
const sourcePath = path.join(directory, relative);
|
||||
const compiledPath = sourcePath.replace(/\.ts$/, '.js');
|
||||
fs.mkdirSync(path.dirname(sourcePath), { recursive: true });
|
||||
fs.writeFileSync(sourcePath, source);
|
||||
fs.writeFileSync(compiledPath, compiled.outputText);
|
||||
hashes[relative] = {
|
||||
original_source: digest(original),
|
||||
experiment_source: digest(source),
|
||||
experiment_js: digest(compiled.outputText),
|
||||
};
|
||||
volumes.push({
|
||||
type: 'bind',
|
||||
source: compiledPath,
|
||||
target: `/app/dist/${relative.replace(/\.ts$/, '.js')}`,
|
||||
read_only: true,
|
||||
});
|
||||
}
|
||||
let benchmark = fs
|
||||
.readFileSync(path.join(root, 'test/cache-benchmark.mjs'), 'utf8')
|
||||
.replace(/\r\n/g, '\n');
|
||||
benchmark = replace(
|
||||
benchmark,
|
||||
'process.env.CACHE_URL = proxy.url;',
|
||||
'delete process.env.CACHE_URL;',
|
||||
);
|
||||
benchmark = section(
|
||||
benchmark,
|
||||
' await prisma.chatMessageEpoch.createMany(',
|
||||
' const path =',
|
||||
'',
|
||||
);
|
||||
benchmark = section(
|
||||
benchmark,
|
||||
' const second = await start();',
|
||||
' report.completed = true;',
|
||||
' assert.equal(first.app.get(CacheService).redis, undefined);\n' +
|
||||
' for (const chatId of chatIds) {\n' +
|
||||
' assert.equal(await messageEpoch(prisma, chatId), 0n);\n' +
|
||||
' }\n' +
|
||||
' assert.equal((await prisma.user.findUniqueOrThrow({ where: { id: userId } })).chatsEpoch, 0n);\n' +
|
||||
' for (const chatId of chatIds.slice(1)) {\n' +
|
||||
' const page = await request("GET", `/api/v1/chats/${chatId}/messages`);\n' +
|
||||
' assert.equal(page.items.length, 50);\n' +
|
||||
' assert.equal(new Set(page.items.map(message => message.id)).size, 50);\n' +
|
||||
' }\n' +
|
||||
' report.correctness = { no_redis_client: true, epochs_unchanged: true, read_after_write: true };\n' +
|
||||
' }\n',
|
||||
);
|
||||
const benchmarkPath = path.join(directory, 'cache-benchmark.mjs');
|
||||
fs.writeFileSync(benchmarkPath, benchmark);
|
||||
volumes.push({
|
||||
type: 'bind',
|
||||
source: benchmarkPath,
|
||||
target: '/experiment/cache-benchmark.mjs',
|
||||
read_only: true,
|
||||
});
|
||||
volumes.push({
|
||||
type: 'bind',
|
||||
source: path.join(root, 'test/helpers'),
|
||||
target: '/experiment/helpers',
|
||||
read_only: true,
|
||||
});
|
||||
const override = path.join(directory, 'compose.no-epochs.yaml');
|
||||
fs.writeFileSync(
|
||||
override,
|
||||
JSON.stringify({ services: { benchmark: { volumes } } }, null, 2),
|
||||
);
|
||||
const args = [
|
||||
'compose',
|
||||
'-f',
|
||||
'compose.yaml',
|
||||
'-f',
|
||||
'test/compose.benchmark.yaml',
|
||||
'-f',
|
||||
override,
|
||||
'run',
|
||||
'--rm',
|
||||
'--no-deps',
|
||||
'--pull',
|
||||
'never',
|
||||
'benchmark',
|
||||
'node',
|
||||
'/experiment/cache-benchmark.mjs',
|
||||
'after',
|
||||
`/results/${resultPrefix}-no-redis-no-epochs.json`,
|
||||
...process.argv.slice(2),
|
||||
];
|
||||
const metadata = {
|
||||
recorded_at: new Date().toISOString(),
|
||||
image: execFileSync(
|
||||
'docker',
|
||||
['image', 'inspect', 'mtusi2026lab2-fork-backend', '--format', '{{.Id}}'],
|
||||
{ encoding: 'utf8' },
|
||||
).trim(),
|
||||
modified_sources: hashes,
|
||||
command: ['docker', ...args],
|
||||
implementation:
|
||||
'Separate temporary source copies compiled with TypeScript and mounted over six application modules. Workspace sources and running backend remain unchanged.',
|
||||
schema:
|
||||
'Epoch storage remains in the database; experiment API paths do not update counters or read authoritative epoch values.',
|
||||
redis:
|
||||
'Application starts without CACHE_URL. Observer/proxy are retained only for benchmark setup and cleanup.',
|
||||
full_result: `${resultPrefix}-full.json`,
|
||||
experiment_result: `${resultPrefix}-no-redis-no-epochs.json`,
|
||||
};
|
||||
const child = spawn('docker', args, {
|
||||
cwd: root,
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
});
|
||||
child.once('error', finish);
|
||||
child.once('exit', (code, signal) => finish(null, code, signal));
|
||||
let finished = false;
|
||||
function finish(error, code, signal) {
|
||||
if (finished) return;
|
||||
finished = true;
|
||||
metadata.exit_code = error ? 1 : code;
|
||||
metadata.signal = signal;
|
||||
metadata.sources_unchanged = Object.entries(hashes).every(
|
||||
([relative, hash]) =>
|
||||
digest(fs.readFileSync(path.join(root, 'src', relative), 'utf8')) ===
|
||||
hash.original_source,
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(root, `evidence/${resultPrefix}-experiment.json`),
|
||||
JSON.stringify(metadata, null, 2) + '\n',
|
||||
);
|
||||
cleanup();
|
||||
if (error) console.error(error.message);
|
||||
process.exitCode = error ? 1 : (code ?? 1);
|
||||
}
|
||||
} catch (error) {
|
||||
cleanup();
|
||||
throw error;
|
||||
}
|
||||
|
||||
function cleanup() {
|
||||
const resolved = path.resolve(directory);
|
||||
assert.equal(path.dirname(resolved), path.resolve(os.tmpdir()));
|
||||
assert.ok(path.basename(resolved).startsWith('lab2-no-epochs-'));
|
||||
fs.rmSync(resolved, { recursive: true, force: true });
|
||||
}
|
||||
Reference in New Issue
Block a user