Exercise Nginx routing, Mongo transaction ordering, history backfills, Undo, Pub/Sub TCP loss, safety polling, resync, and reconnection. Keep fixtures and results isolated from production and git.
204 lines
6.8 KiB
TypeScript
204 lines
6.8 KiB
TypeScript
import 'reflect-metadata';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { Body, Controller, Get, Module, Post } from '@nestjs/common';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import mongoose from 'mongoose';
|
|
import { createClient } from 'redis';
|
|
import type { Namespace } from 'socket.io';
|
|
import { RedisIoAdapter } from '../../src/redis-io.adapter';
|
|
import { AuctionController } from '../../src/modules/auction/auction.controller';
|
|
import { AuctionGateway } from '../../src/modules/auction/auction.gateway';
|
|
import { AuctionEventBusService } from '../../src/modules/auction/auction-event-bus.service';
|
|
import { AuctionService } from '../../src/modules/auction/auction.service';
|
|
import { AuctionBidArchiveService } from '../../src/modules/auction/auction-bid-archive.service';
|
|
import {
|
|
AuctionDurabilityScheduler,
|
|
type AuctionDurabilityPayload,
|
|
} from '../../src/modules/auction/auction-durability.scheduler';
|
|
import { AuctionRedisService } from '../../src/modules/auction/redis/auction-redis.service';
|
|
import { AuctionRedisCommandService } from '../../src/modules/auction/redis/auction-redis-command.service';
|
|
import { AuctionRedisStateReaderService } from '../../src/modules/auction/redis/auction-redis-state-reader.service';
|
|
import { AuthService } from '../../src/modules/auth/auth.service';
|
|
import { JwtService } from '../../src/modules/auth/jwt.service';
|
|
import { IdentityService } from '../../src/modules/auth/identity.service';
|
|
import { ConnectionService } from '../../src/modules/global/database/connection.service';
|
|
import {
|
|
AuctionSchema,
|
|
AuctionHistoryEntrySchema,
|
|
AuctionSideEffectApplicationSchema,
|
|
MatchSchema,
|
|
} from '../../src/modules/global/database/schema';
|
|
|
|
async function bootstrap() {
|
|
if (
|
|
!process.env.MONGO_TEST_URL?.startsWith(
|
|
'mongodb://mongo:27017/auction_cluster?',
|
|
) ||
|
|
process.env.REDIS_TEST_URL !== 'redis://redis-proxy:6379'
|
|
) {
|
|
throw new Error(
|
|
'This test node only accepts the isolated Compose services.',
|
|
);
|
|
}
|
|
const fixture = JSON.parse(
|
|
await readFile('/results/fixture.json', 'utf8'),
|
|
) as { communityId: string };
|
|
const connection = await mongoose
|
|
.createConnection(process.env.MONGO_TEST_URL, { autoIndex: false })
|
|
.asPromise();
|
|
const models = {
|
|
match: connection.model('match', MatchSchema),
|
|
auction: connection.model('auction', AuctionSchema),
|
|
auctionHistory: connection.model(
|
|
'auctionHistory',
|
|
AuctionHistoryEntrySchema,
|
|
),
|
|
auctionSideEffectApplication: connection.model(
|
|
'auctionSideEffectApplication',
|
|
AuctionSideEffectApplicationSchema,
|
|
),
|
|
};
|
|
let releaseCommit: (() => void) | undefined;
|
|
let commitGate: Promise<void> | undefined;
|
|
let commitsWaiting = 0;
|
|
const database = {
|
|
getModel: async (name: keyof typeof models) => {
|
|
if (!models[name]) throw new Error(`Unexpected test model: ${name}`);
|
|
return models[name];
|
|
},
|
|
createSession: async () => {
|
|
const session = await connection.startSession();
|
|
const commit = session.commitTransaction.bind(session);
|
|
session.commitTransaction = async () => {
|
|
if (commitGate) {
|
|
commitsWaiting += 1;
|
|
await commitGate;
|
|
commitsWaiting -= 1;
|
|
}
|
|
await commit();
|
|
};
|
|
return session;
|
|
},
|
|
};
|
|
const client = createClient({ url: process.env.REDIS_TEST_URL });
|
|
client.on('error', (error) =>
|
|
console.error('command-client:', error.message),
|
|
);
|
|
await client.connect();
|
|
const redis = new AuctionRedisService(client);
|
|
const commands = new AuctionRedisCommandService(redis);
|
|
const reader = new AuctionRedisStateReaderService(redis);
|
|
const bus = new AuctionEventBusService();
|
|
const service = new AuctionService(
|
|
database as never,
|
|
{} as never,
|
|
{} as never,
|
|
{} as never,
|
|
redis,
|
|
commands,
|
|
reader,
|
|
bus,
|
|
);
|
|
// Explicit draining lets the test hold real Mongo commits and reorder durable
|
|
// payloads; no in-memory database or fake Redis adapter is involved.
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
database as never,
|
|
redis,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
bus,
|
|
reader,
|
|
service,
|
|
);
|
|
|
|
@Controller('_cluster')
|
|
class ControlController {
|
|
@Get('health') health() {
|
|
const namespace = Reflect.get(gateway, 'server') as Namespace;
|
|
return {
|
|
node: process.env.CLUSTER_NODE,
|
|
sockets: [...namespace.sockets.keys()],
|
|
pid: process.pid,
|
|
commitsWaiting,
|
|
};
|
|
}
|
|
@Post('hold-commit') hold() {
|
|
if (commitGate) throw new Error('Commit already held');
|
|
commitGate = new Promise<void>((resolve) => {
|
|
releaseCommit = resolve;
|
|
});
|
|
return { held: true };
|
|
}
|
|
@Post('release-commit') release() {
|
|
releaseCommit?.();
|
|
commitGate = undefined;
|
|
releaseCommit = undefined;
|
|
return { released: true };
|
|
}
|
|
@Post('drain') drain() {
|
|
return scheduler.processDueDurabilityPayloads(100);
|
|
}
|
|
@Post('apply') async apply(@Body() payload: AuctionDurabilityPayload) {
|
|
await scheduler.applyDurabilityPayload(payload);
|
|
return { applied: true };
|
|
}
|
|
}
|
|
@Module({
|
|
controllers: [AuctionController, ControlController],
|
|
providers: [
|
|
AuctionGateway,
|
|
{ provide: AuctionService, useValue: service },
|
|
{ provide: ConnectionService, useValue: database },
|
|
{ provide: AuctionRedisService, useValue: redis },
|
|
{ provide: AuctionEventBusService, useValue: bus },
|
|
{ provide: AuctionBidArchiveService, useValue: {} },
|
|
{
|
|
provide: AuthService,
|
|
useValue: { resolveDevUserById: async () => null },
|
|
},
|
|
{ provide: JwtService, useValue: {} },
|
|
{ provide: IdentityService, useValue: {} },
|
|
],
|
|
})
|
|
class TestModule {}
|
|
const app = await NestFactory.create(TestModule, {
|
|
logger: ['error', 'warn'],
|
|
});
|
|
// Identity-provider authentication is outside this transport/durability test.
|
|
// The real controller's RolesGuard still checks this isolated fixture admin.
|
|
app.use(
|
|
(
|
|
request: { user?: unknown },
|
|
response: { setHeader(name: string, value: string): void },
|
|
next: () => void,
|
|
) => {
|
|
request.user = {
|
|
role: 'admin',
|
|
communityId: new mongoose.Types.ObjectId(fixture.communityId),
|
|
};
|
|
response.setHeader(
|
|
'X-Cluster-Node',
|
|
process.env.CLUSTER_NODE ?? 'unknown',
|
|
);
|
|
next();
|
|
},
|
|
);
|
|
const adapter = new RedisIoAdapter(app);
|
|
await adapter.connectToRedis();
|
|
app.useWebSocketAdapter(adapter);
|
|
const gateway = app.get(AuctionGateway);
|
|
await app.listen(3000, '0.0.0.0');
|
|
console.log(`cluster node ready: ${process.env.CLUSTER_NODE}`);
|
|
process.on('SIGTERM', () => {
|
|
void app
|
|
.close()
|
|
.then(() => connection.close())
|
|
.then(() => process.exit(0));
|
|
});
|
|
}
|
|
void bootstrap().catch((error: unknown) => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|