88 lines
2.9 KiB
TypeScript
88 lines
2.9 KiB
TypeScript
import { DistributedLockService } from '../global/redis/distributed-lock.service';
|
|
import { TournamentCodeScheduler } from './tournament-code.scheduler';
|
|
|
|
describe('TournamentCodeScheduler', () => {
|
|
function createScheduler(params?: {
|
|
attachedMatches?: unknown[];
|
|
scheduleInitialPoll?: jest.Mock;
|
|
}) {
|
|
const tournamentCodeService = {
|
|
attachPendingMatchDocuments: jest
|
|
.fn()
|
|
.mockResolvedValue(params?.attachedMatches ?? []),
|
|
markExpired: jest.fn().mockResolvedValue(0),
|
|
};
|
|
const autoProvisionService = {
|
|
ensurePool: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
const tournamentResultService = {
|
|
scheduleInitialPoll:
|
|
params?.scheduleInitialPoll ?? jest.fn().mockResolvedValue(null),
|
|
};
|
|
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 scheduler = new TournamentCodeScheduler(
|
|
tournamentCodeService as never,
|
|
autoProvisionService as never,
|
|
tournamentResultService as never,
|
|
distributedLockService as never,
|
|
) as unknown as { attachPending(): Promise<void> };
|
|
|
|
return {
|
|
scheduler,
|
|
tournamentCodeService,
|
|
tournamentResultService,
|
|
distributedLockService,
|
|
};
|
|
}
|
|
|
|
it('starts result polling after attaching delayed tournament codes', async () => {
|
|
const attachedMatches = [{ _id: 'match-1' }, { _id: 'match-2' }];
|
|
const { scheduler, tournamentResultService } = createScheduler({
|
|
attachedMatches,
|
|
});
|
|
|
|
await scheduler.attachPending();
|
|
|
|
expect(tournamentResultService.scheduleInitialPoll).toHaveBeenCalledTimes(
|
|
2,
|
|
);
|
|
expect(tournamentResultService.scheduleInitialPoll).toHaveBeenNthCalledWith(
|
|
1,
|
|
attachedMatches[0],
|
|
);
|
|
expect(tournamentResultService.scheduleInitialPoll).toHaveBeenNthCalledWith(
|
|
2,
|
|
attachedMatches[1],
|
|
);
|
|
});
|
|
|
|
it('does not schedule result polling when no match received new codes', async () => {
|
|
const { scheduler, tournamentResultService } = createScheduler();
|
|
|
|
await scheduler.attachPending();
|
|
|
|
expect(tournamentResultService.scheduleInitialPoll).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('does not attach pending tournament codes when another instance owns the scheduler lock', async () => {
|
|
const { scheduler, tournamentCodeService, distributedLockService } =
|
|
createScheduler();
|
|
distributedLockService.runWithRenewedLock.mockResolvedValueOnce(null);
|
|
|
|
await scheduler.attachPending();
|
|
|
|
expect(
|
|
tournamentCodeService.attachPendingMatchDocuments,
|
|
).not.toHaveBeenCalled();
|
|
});
|
|
});
|