Files
lolympus-tournament-support/apps/api/test/run-redis-tests.mjs
T
dahyeon 4b8ccf0077 fix: harden result polling and Redis 8.10 operations
Preserve tournament result jobs through lock contention and transient failures,
atomically schedule retries before acknowledgement, and publish initial polls
after releasing the preparation lock.

Fix Redis connection blocking, durability Hash cleanup, duplicate auction
request ordering, and scheduled-job promotion. Use native Redis 8.10 stream,
lock, counter, and field-expiring cache operations with invalidation fencing.

Repair 90 test type errors and add a repeatable API typecheck command.

Validation: 704 API tests, 129 web tests, 31 real Redis integration tests,
3 dependency checks, workspace type checks and builds, and changed-file lint.
2026-11-09 17:33:13 +09:00

121 lines
3.4 KiB
JavaScript

import { spawn } from 'node:child_process';
import { createRequire } from 'node:module';
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, relative, resolve } from 'node:path';
import { createServer } from 'node:net';
import { randomUUID } from 'node:crypto';
import { setTimeout as delay } from 'node:timers/promises';
import { createClient } from 'redis';
const require = createRequire(import.meta.url);
const listener = createServer();
await new Promise((accept, reject) => {
listener.once('error', reject);
listener.listen(0, '127.0.0.1', accept);
});
const port = listener.address().port;
await new Promise((accept) => listener.close(accept));
const directory = await mkdtemp(join(tmpdir(), 'lolympus-redis-test-'));
const password = randomUUID();
const redis = spawn(
process.env.REDIS_TEST_SERVER || 'redis-server',
[
'--bind',
'127.0.0.1',
'--port',
String(port),
'--protected-mode',
'yes',
'--requirepass',
password,
'--save',
'',
'--appendonly',
'no',
'--dir',
directory.replaceAll('\\', '/'),
],
{ windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] },
);
let startupError;
const redisClosed = new Promise((accept) => redis.once('close', accept));
let output = '';
redis.on('error', (error) => {
startupError = error;
});
redis.stdout.on('data', (data) => {
output += data;
});
redis.stderr.on('data', (data) => {
output += data;
});
const url = `redis://:${password}@127.0.0.1:${port}`;
const client = createClient({
url,
socket: { reconnectStrategy: false, connectTimeout: 500 },
});
client.on('error', () => {});
try {
let connected = false;
for (let attempt = 0; attempt < 100; attempt++) {
if (startupError) throw startupError;
if (redis.exitCode !== null) throw new Error(`Redis exited: ${output}`);
try {
await client.connect();
connected = true;
break;
} catch {
await delay(100);
}
}
if (!connected) throw new Error(`Redis did not start: ${output}`);
const version = (await client.info('server')).match(
/redis_version:([^\r\n]+)/,
)?.[1];
if (!version?.startsWith('8.10.'))
throw new Error(`Expected Redis 8.10.x, got ${version}`);
console.log(
`Integration tests using isolated Redis ${version} on loopback port ${port}`,
);
const jest = spawn(
process.execPath,
[
require.resolve('jest/bin/jest'),
'--config',
'test/jest-redis.json',
'--runInBand',
...process.argv.slice(2),
],
{
cwd: resolve(import.meta.dirname, '..'),
windowsHide: true,
stdio: 'inherit',
env: {
...process.env,
NODE_ENV: 'test',
REDIS_TEST_URL: url,
REDIS_TEST_ISOLATED: '1',
},
},
);
process.exitCode = await new Promise((accept, reject) => {
jest.once('error', reject);
jest.once('exit', (code) => accept(code ?? 1));
});
} finally {
if (client.isOpen) {
await client.sendCommand(['SHUTDOWN', 'NOSAVE']).catch(() => {});
if (client.isOpen) client.destroy();
}
if (redis.pid && redis.exitCode === null) redis.kill();
await redisClosed;
const cleanupTarget = relative(resolve(tmpdir()), resolve(directory));
if (!/^lolympus-redis-test-[^\\/]+$/.test(cleanupTarget)) {
throw new Error(
'Refusing to clean up a directory outside the isolated test workspace.',
);
}
await rm(directory, { recursive: true, force: true });
}