203 lines
6.5 KiB
TypeScript
203 lines
6.5 KiB
TypeScript
import mongoose from 'mongoose';
|
|
import { ConnectionService } from '../global/database/connection.service';
|
|
import { DistributedLockService } from '../global/redis/distributed-lock.service';
|
|
import { RedisService } from '../global/redis/redis.service';
|
|
import { LegacyImportService } from '../legacy-import/legacy-import.service';
|
|
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
|
|
import { RiotRecentMatchesService } from '../riot/riot-recent-matches.service';
|
|
import { TournamentResultService } from '../tournament-result/tournament-result.service';
|
|
import { RIOT_JOB_GROUP, RiotRefreshSubJob } from './riot-job-queue.types';
|
|
import { RiotJobQueueService } from './riot-job-queue.service';
|
|
import { RiotJobWorkerService } from './riot-job-worker.service';
|
|
import { RiotRefreshGateway } from './riot-refresh.gateway';
|
|
|
|
function execResult<T>(value: T) {
|
|
return { exec: jest.fn().mockResolvedValue(value) };
|
|
}
|
|
|
|
function makeWorker() {
|
|
const redisService = {
|
|
acknowledgeStream: jest.fn().mockResolvedValue(undefined),
|
|
deleteStreamEntries: jest.fn().mockResolvedValue(undefined),
|
|
} as unknown as jest.Mocked<RedisService>;
|
|
const distributedLockService = {
|
|
runWithRenewedLock: jest
|
|
.fn()
|
|
.mockImplementation(async (options, operation) =>
|
|
operation({
|
|
lockKey: options.lockKey,
|
|
lockOwnershipToken: 'lock-owner',
|
|
lockExpirationDurationMilliseconds:
|
|
options.lockExpirationDurationMilliseconds,
|
|
}),
|
|
),
|
|
} as unknown as jest.Mocked<DistributedLockService>;
|
|
const jobQueueService = {
|
|
markProcessing: jest.fn().mockResolvedValue(undefined),
|
|
markCompleted: jest.fn().mockResolvedValue({
|
|
parentJobId: 'parent-1',
|
|
jobId: 'job-1',
|
|
queueGroup: 'summoners-rift',
|
|
puuid: 'puuid-1',
|
|
state: 'completed',
|
|
}),
|
|
markFailed: jest.fn(),
|
|
} as unknown as jest.Mocked<RiotJobQueueService>;
|
|
const recentMatchesService = {
|
|
ensureFreshForQueue: jest.fn().mockResolvedValue(undefined),
|
|
} as unknown as jest.Mocked<RiotRecentMatchesService>;
|
|
const userModel = {
|
|
updateOne: jest.fn().mockReturnValue(execResult({ modifiedCount: 1 })),
|
|
};
|
|
const connectionService = {
|
|
getModel: jest.fn().mockResolvedValue(userModel),
|
|
} as unknown as jest.Mocked<ConnectionService>;
|
|
const refreshGateway = {
|
|
broadcastRefreshEvent: jest.fn(),
|
|
} as unknown as jest.Mocked<RiotRefreshGateway>;
|
|
const tournamentResultService = {
|
|
processPollJob: jest.fn(),
|
|
} as unknown as jest.Mocked<TournamentResultService>;
|
|
const legacyImportService = {
|
|
processMemberJob: jest.fn(),
|
|
} as unknown as jest.Mocked<LegacyImportService>;
|
|
const enrichmentService = {
|
|
refreshLeagueEntries: jest.fn().mockResolvedValue('updated'),
|
|
} as unknown as jest.Mocked<RiotAccountEnrichmentService>;
|
|
|
|
const worker = new RiotJobWorkerService(
|
|
redisService,
|
|
distributedLockService,
|
|
jobQueueService,
|
|
recentMatchesService,
|
|
connectionService,
|
|
refreshGateway,
|
|
tournamentResultService,
|
|
legacyImportService,
|
|
enrichmentService,
|
|
);
|
|
|
|
return {
|
|
worker,
|
|
redisService,
|
|
distributedLockService,
|
|
jobQueueService,
|
|
recentMatchesService,
|
|
connectionService,
|
|
refreshGateway,
|
|
enrichmentService,
|
|
};
|
|
}
|
|
|
|
describe('RiotJobWorkerService', () => {
|
|
it('acknowledges and deletes invalid stream entries', async () => {
|
|
const { worker, redisService } = makeWorker();
|
|
|
|
await (
|
|
worker as unknown as {
|
|
processEntry: (
|
|
stream: string,
|
|
entry: { id: string; message: Record<string, string> },
|
|
) => Promise<void>;
|
|
}
|
|
).processEntry('riot:queue:high', {
|
|
id: 'bad-1',
|
|
message: { job: 'not-json' },
|
|
});
|
|
|
|
expect(redisService.acknowledgeStream).toHaveBeenCalledWith(
|
|
'riot:queue:high',
|
|
RIOT_JOB_GROUP,
|
|
'bad-1',
|
|
);
|
|
expect(redisService.deleteStreamEntries).toHaveBeenCalledWith(
|
|
'riot:queue:high',
|
|
'bad-1',
|
|
);
|
|
});
|
|
|
|
it('refreshes league entries while processing user history refresh jobs', async () => {
|
|
const { worker, enrichmentService, recentMatchesService, redisService } =
|
|
makeWorker();
|
|
const userId = new mongoose.Types.ObjectId().toString();
|
|
const job: RiotRefreshSubJob = {
|
|
jobId: 'job-1',
|
|
parentJobId: 'parent-1',
|
|
userId,
|
|
puuid: 'puuid-1',
|
|
region: 'asia',
|
|
platform: 'kr',
|
|
queueGroup: 'summoners-rift',
|
|
queueIds: [420, 430],
|
|
requestedAt: Date.now(),
|
|
priority: 'high',
|
|
};
|
|
|
|
await (
|
|
worker as unknown as {
|
|
processEntry: (
|
|
stream: string,
|
|
entry: { id: string; message: Record<string, string> },
|
|
) => Promise<void>;
|
|
}
|
|
).processEntry('riot:queue:high', {
|
|
id: '1-0',
|
|
message: { job: JSON.stringify(job) },
|
|
});
|
|
|
|
expect(recentMatchesService.ensureFreshForQueue).toHaveBeenCalledTimes(2);
|
|
expect(enrichmentService.refreshLeagueEntries).toHaveBeenCalledWith({
|
|
userId: expect.any(mongoose.Types.ObjectId),
|
|
puuid: 'puuid-1',
|
|
platform: 'kr',
|
|
});
|
|
expect(redisService.acknowledgeStream).toHaveBeenCalledWith(
|
|
'riot:queue:high',
|
|
RIOT_JOB_GROUP,
|
|
'1-0',
|
|
);
|
|
expect(redisService.deleteStreamEntries).toHaveBeenCalledWith(
|
|
'riot:queue:high',
|
|
'1-0',
|
|
);
|
|
expect(
|
|
redisService.acknowledgeStream.mock.invocationCallOrder[0],
|
|
).toBeLessThan(
|
|
redisService.deleteStreamEntries.mock.invocationCallOrder[0],
|
|
);
|
|
});
|
|
|
|
it('leaves a stream entry pending when another worker owns the job processing lock', async () => {
|
|
const { worker, distributedLockService, redisService } = makeWorker();
|
|
distributedLockService.runWithRenewedLock.mockResolvedValueOnce(null);
|
|
const userId = new mongoose.Types.ObjectId().toString();
|
|
const job: RiotRefreshSubJob = {
|
|
jobId: 'job-1',
|
|
parentJobId: 'parent-1',
|
|
userId,
|
|
puuid: 'puuid-1',
|
|
region: 'asia',
|
|
platform: 'kr',
|
|
queueGroup: 'summoners-rift',
|
|
queueIds: [420],
|
|
requestedAt: Date.now(),
|
|
priority: 'high',
|
|
};
|
|
|
|
await (
|
|
worker as unknown as {
|
|
processEntry: (
|
|
stream: string,
|
|
entry: { id: string; message: Record<string, string> },
|
|
) => Promise<void>;
|
|
}
|
|
).processEntry('riot:queue:high', {
|
|
id: '1-0',
|
|
message: { job: JSON.stringify(job) },
|
|
});
|
|
|
|
expect(redisService.acknowledgeStream).not.toHaveBeenCalled();
|
|
expect(redisService.deleteStreamEntries).not.toHaveBeenCalled();
|
|
});
|
|
});
|