Harden match and tournament code concurrency
This commit is contained in:
1 parent
292e37aaac
commit
df7dcd3a9c
9 files changed
+2339
-128
No files matched your search
@@ -51,9 +51,13 @@ describe('MatchService', () => {
|
||||
};
|
||||
}
|
||||
|
||||
function createService(match: Record<string, unknown>) {
|
||||
function createService(
|
||||
match: Record<string, unknown>,
|
||||
updatedMatch: Record<string, unknown> | null = match,
|
||||
) {
|
||||
const MatchModel = {
|
||||
findById: jest.fn().mockResolvedValue(match),
|
||||
findOneAndUpdate: jest.fn().mockResolvedValue(updatedMatch),
|
||||
};
|
||||
const connectionService = {
|
||||
getModel: jest.fn().mockImplementation(async (name: string) => {
|
||||
@@ -80,6 +84,7 @@ describe('MatchService', () => {
|
||||
communityStatsService as never,
|
||||
matchStatsService as never,
|
||||
),
|
||||
MatchModel,
|
||||
tournamentCodeService,
|
||||
matchStatsService,
|
||||
};
|
||||
@@ -132,12 +137,16 @@ describe('MatchService', () => {
|
||||
function createServiceWithJoinModels(params: {
|
||||
user: Record<string, unknown> | null;
|
||||
match: Record<string, unknown> | null;
|
||||
updatedMatch?: Record<string, unknown> | null;
|
||||
}) {
|
||||
const UserModel = {
|
||||
findById: jest.fn().mockResolvedValue(params.user),
|
||||
};
|
||||
const MatchModel = {
|
||||
findById: jest.fn().mockResolvedValue(params.match),
|
||||
findOneAndUpdate: jest
|
||||
.fn()
|
||||
.mockResolvedValue(params.updatedMatch ?? params.match),
|
||||
};
|
||||
const connectionService = {
|
||||
getModel: jest.fn().mockImplementation(async (name: string) => {
|
||||
@@ -282,25 +291,185 @@ describe('MatchService', () => {
|
||||
participants: [],
|
||||
save: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const { service } = createServiceWithJoinModels({ user, match });
|
||||
const updatedMatch = {
|
||||
...match,
|
||||
participants: [
|
||||
{
|
||||
userId,
|
||||
puuid: 'puuid-1',
|
||||
gameName: 'Dahyeon',
|
||||
tagLine: 'KR1',
|
||||
profileIconId: 7,
|
||||
preferredPositions: [{ position: 'MID', priority: 1 }],
|
||||
excluded: false,
|
||||
},
|
||||
],
|
||||
};
|
||||
const { service, MatchModel } = createServiceWithJoinModels({
|
||||
user,
|
||||
match,
|
||||
updatedMatch,
|
||||
});
|
||||
|
||||
await service.join({
|
||||
const joined = await service.join({
|
||||
matchId,
|
||||
userId,
|
||||
});
|
||||
|
||||
expect(match.participants).toEqual([
|
||||
expect(joined).toBe(updatedMatch);
|
||||
expect(MatchModel.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId,
|
||||
puuid: 'puuid-1',
|
||||
gameName: 'Dahyeon',
|
||||
tagLine: 'KR1',
|
||||
profileIconId: 7,
|
||||
preferredPositions: [{ position: 'MID', priority: 1 }],
|
||||
excluded: false,
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'recruiting',
|
||||
'participants.userId': { $ne: userId },
|
||||
$expr: expect.any(Object),
|
||||
}),
|
||||
]);
|
||||
expect(match.save).toHaveBeenCalledTimes(1);
|
||||
{
|
||||
$push: {
|
||||
participants: expect.objectContaining({
|
||||
userId,
|
||||
puuid: 'puuid-1',
|
||||
gameName: 'Dahyeon',
|
||||
tagLine: 'KR1',
|
||||
profileIconId: 7,
|
||||
preferredPositions: [{ position: 'MID', priority: 1 }],
|
||||
excluded: false,
|
||||
}),
|
||||
},
|
||||
},
|
||||
expect.objectContaining({ returnDocument: 'after', runValidators: true }),
|
||||
);
|
||||
expect(match.participants).toEqual([]);
|
||||
expect(match.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('cancels a join with an atomic participant pull', async () => {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const user = {
|
||||
_id: userId,
|
||||
communityId,
|
||||
riotAccounts: [],
|
||||
preferredPositions: [],
|
||||
};
|
||||
const match = {
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'recruiting',
|
||||
participants: [{ userId }],
|
||||
save: jest.fn(),
|
||||
};
|
||||
const updatedMatch = { ...match, participants: [] };
|
||||
const { service, MatchModel } = createServiceWithJoinModels({
|
||||
user,
|
||||
match,
|
||||
updatedMatch,
|
||||
});
|
||||
|
||||
await expect(service.cancelJoin({ matchId, userId })).resolves.toBe(
|
||||
updatedMatch,
|
||||
);
|
||||
expect(MatchModel.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'recruiting',
|
||||
'participants.userId': userId,
|
||||
},
|
||||
{ $pull: { participants: { userId } } },
|
||||
expect.objectContaining({ returnDocument: 'after', runValidators: true }),
|
||||
);
|
||||
expect(match.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('removes a participant with an atomic participant pull', async () => {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const targetUserId = new mongoose.Types.ObjectId();
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const match = {
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'recruiting',
|
||||
participants: [{ userId: targetUserId }],
|
||||
save: jest.fn(),
|
||||
};
|
||||
const updatedMatch = { ...match, participants: [] };
|
||||
const { service, MatchModel } = createService(match, updatedMatch);
|
||||
|
||||
await expect(
|
||||
service.removeParticipant({ matchId, communityId, targetUserId }),
|
||||
).resolves.toBe(updatedMatch);
|
||||
expect(MatchModel.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'recruiting',
|
||||
'participants.userId': targetUserId,
|
||||
},
|
||||
{ $pull: { participants: { userId: targetUserId } } },
|
||||
expect.objectContaining({ returnDocument: 'after', runValidators: true }),
|
||||
);
|
||||
expect(match.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('updates participant positions with an array-filtered atomic update', async () => {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const targetUserId = new mongoose.Types.ObjectId();
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const match = {
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'recruiting',
|
||||
participants: [
|
||||
{
|
||||
userId: targetUserId,
|
||||
preferredPositions: [
|
||||
{ position: 'TOP', priority: 1 },
|
||||
{ position: 'JUNGLE', priority: 2 },
|
||||
{ position: 'MID', priority: 3 },
|
||||
],
|
||||
},
|
||||
],
|
||||
save: jest.fn(),
|
||||
};
|
||||
const updatedMatch = { ...match };
|
||||
const { service, MatchModel } = createService(match, updatedMatch);
|
||||
|
||||
await expect(
|
||||
service.updateParticipantPositions({
|
||||
matchId,
|
||||
communityId,
|
||||
targetUserId,
|
||||
mainPosition: 'MID',
|
||||
subPosition: 'SUPPORT',
|
||||
}),
|
||||
).resolves.toBe(updatedMatch);
|
||||
expect(MatchModel.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'recruiting',
|
||||
'participants.userId': targetUserId,
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
'participants.$[participant].preferredPositions': [
|
||||
{ position: 'MID', priority: 1 },
|
||||
{ position: 'SUPPORT', priority: 2 },
|
||||
{ position: 'TOP', priority: 3 },
|
||||
{ position: 'JUNGLE', priority: 4 },
|
||||
],
|
||||
},
|
||||
},
|
||||
expect.objectContaining({
|
||||
arrayFilters: [{ 'participant.userId': targetUserId }],
|
||||
returnDocument: 'after',
|
||||
runValidators: true,
|
||||
}),
|
||||
);
|
||||
expect(match.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('normalizes double-elimination match creation', async () => {
|
||||
|
||||
Reference in new issue
Block a user