feat: redesign auction engine with redis sot
This commit is contained in:
1 parent
737702d78c
commit
e8bb20d988
75 files changed
+12737
-542
No files matched your search
@@ -0,0 +1,253 @@
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
jest.mock('../auth/jwt.service', () => ({
|
||||
JwtService: class JwtService {},
|
||||
}));
|
||||
|
||||
import { AuctionGateway } from './auction.gateway';
|
||||
|
||||
describe('AuctionGateway', () => {
|
||||
function createGateway(params?: {
|
||||
placeBid?: jest.Mock;
|
||||
}) {
|
||||
const auctionService = {
|
||||
placeBid: params?.placeBid ?? jest.fn(),
|
||||
};
|
||||
const gateway = new AuctionGateway(
|
||||
auctionService as never,
|
||||
{ resolveDevUserById: jest.fn() } as never,
|
||||
{ verify: jest.fn() } as never,
|
||||
{ findOrCreateByOid: jest.fn() } as never,
|
||||
{ getModel: jest.fn() } as never,
|
||||
{ attachAuctionNamespace: jest.fn() } as never,
|
||||
);
|
||||
const emit = jest.fn();
|
||||
const to = jest.fn().mockReturnValue({ emit });
|
||||
(gateway as unknown as { server: { to: jest.Mock } }).server = { to };
|
||||
|
||||
return {
|
||||
auctionService,
|
||||
emit,
|
||||
gateway,
|
||||
to,
|
||||
};
|
||||
}
|
||||
|
||||
it('broadcasts a bid delta and acknowledges the matching bid request', async () => {
|
||||
const matchId = new mongoose.Types.ObjectId().toString();
|
||||
const auctionBidHistoryEntry = {
|
||||
bidderTeamIndex: 2,
|
||||
bidderUserId: 'captain-2',
|
||||
bidPlacedAtTimestampMS: 1780000000000,
|
||||
bidPointAmount: 120,
|
||||
};
|
||||
const placeBid = jest.fn().mockResolvedValue({
|
||||
auctionBidHistory: [auctionBidHistoryEntry],
|
||||
auctionLifecycleId: 'life-1',
|
||||
currentBidRoundDeadlineTimestampMS: 1780000030000,
|
||||
currentHighestBidderTeamIndex: 2,
|
||||
currentHighestBidPointAmount: 120,
|
||||
currentServerTimestampMS: 1780000000000,
|
||||
revision: 9,
|
||||
});
|
||||
const { emit, gateway, to } = createGateway({ placeBid });
|
||||
const ack = jest.fn();
|
||||
|
||||
await gateway.handleBid(
|
||||
{ data: { matchId }, emit: jest.fn() } as never,
|
||||
{
|
||||
bidPointAmount: 120,
|
||||
bidderTeamIndex: 2,
|
||||
bidRequestId: 'bid-1',
|
||||
clientObservedAuctionLifecycleId: 'life-1',
|
||||
clientObservedCurrentBidRoundPlayerUserId: 'player-1',
|
||||
},
|
||||
ack,
|
||||
);
|
||||
|
||||
expect(placeBid).toHaveBeenCalledWith({
|
||||
amount: 120,
|
||||
bidRequestId: 'bid-1',
|
||||
clientObservedAuctionLifecycleId: 'life-1',
|
||||
clientObservedCurrentBidRoundPlayerUserId: 'player-1',
|
||||
matchId: new mongoose.Types.ObjectId(matchId),
|
||||
teamIndex: 2,
|
||||
});
|
||||
expect(to).toHaveBeenCalledWith(matchId);
|
||||
expect(emit).toHaveBeenCalledWith('bid', {
|
||||
auctionBidHistoryEntry,
|
||||
auctionLifecycleId: 'life-1',
|
||||
bidRequestId: 'bid-1',
|
||||
currentBidRoundDeadlineTimestampMS: 1780000030000,
|
||||
currentHighestBidderTeamIndex: 2,
|
||||
currentHighestBidPointAmount: 120,
|
||||
currentServerTimestampMS: 1780000000000,
|
||||
revision: 9,
|
||||
});
|
||||
expect(ack).toHaveBeenCalledWith({
|
||||
bidRequestId: 'bid-1',
|
||||
ok: true,
|
||||
state: expect.objectContaining({
|
||||
auctionLifecycleId: 'life-1',
|
||||
revision: 9,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
it('generates one bid request id when the client omits it', async () => {
|
||||
const matchId = new mongoose.Types.ObjectId().toString();
|
||||
const placeBid = jest.fn().mockResolvedValue({
|
||||
auctionBidHistory: [
|
||||
{
|
||||
bidderTeamIndex: 1,
|
||||
bidPointAmount: 50,
|
||||
},
|
||||
],
|
||||
auctionLifecycleId: 'life-1',
|
||||
currentHighestBidderTeamIndex: 1,
|
||||
currentHighestBidPointAmount: 50,
|
||||
revision: 3,
|
||||
});
|
||||
const { emit, gateway } = createGateway({ placeBid });
|
||||
const ack = jest.fn();
|
||||
|
||||
await gateway.handleBid(
|
||||
{ data: { matchId }, emit: jest.fn() } as never,
|
||||
{
|
||||
bidPointAmount: 50,
|
||||
bidderTeamIndex: 1,
|
||||
},
|
||||
ack,
|
||||
);
|
||||
|
||||
const generatedBidRequestId = placeBid.mock.calls[0][0].bidRequestId;
|
||||
expect(generatedBidRequestId).toEqual(expect.any(String));
|
||||
expect(generatedBidRequestId).not.toHaveLength(0);
|
||||
expect(emit).toHaveBeenCalledWith(
|
||||
'bid',
|
||||
expect.objectContaining({
|
||||
bidRequestId: generatedBidRequestId,
|
||||
}),
|
||||
);
|
||||
expect(ack).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
bidRequestId: generatedBidRequestId,
|
||||
ok: true,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('acknowledges bid failures with the request id used for the attempt', async () => {
|
||||
const matchId = new mongoose.Types.ObjectId().toString();
|
||||
const placeBid = jest.fn().mockRejectedValue(new Error('Bid failed'));
|
||||
const socketEmit = jest.fn();
|
||||
const { gateway } = createGateway({ placeBid });
|
||||
const ack = jest.fn();
|
||||
|
||||
await gateway.handleBid(
|
||||
{ data: { matchId }, emit: socketEmit } as never,
|
||||
{
|
||||
bidPointAmount: 50,
|
||||
bidderTeamIndex: 1,
|
||||
bidRequestId: 'bid-1',
|
||||
},
|
||||
ack,
|
||||
);
|
||||
|
||||
expect(socketEmit).toHaveBeenCalledWith('error', {
|
||||
message: 'Bid failed',
|
||||
});
|
||||
expect(ack).toHaveBeenCalledWith({
|
||||
bidRequestId: 'bid-1',
|
||||
code: 'Bid failed',
|
||||
rejected: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('rate limits excessive bid requests from the same socket', async () => {
|
||||
jest.spyOn(Date, 'now').mockReturnValue(1780000000000);
|
||||
const matchId = new mongoose.Types.ObjectId().toString();
|
||||
const placeBid = jest.fn().mockResolvedValue({
|
||||
auctionBidHistory: [
|
||||
{
|
||||
bidderTeamIndex: 1,
|
||||
bidPointAmount: 50,
|
||||
},
|
||||
],
|
||||
currentHighestBidderTeamIndex: 1,
|
||||
currentHighestBidPointAmount: 50,
|
||||
revision: 3,
|
||||
});
|
||||
const socketEmit = jest.fn();
|
||||
const socket = { data: { matchId }, emit: socketEmit };
|
||||
const { gateway } = createGateway({ placeBid });
|
||||
const acknowledgements: Array<Record<string, unknown>> = [];
|
||||
|
||||
for (let index = 0; index < 9; index += 1) {
|
||||
await gateway.handleBid(
|
||||
socket as never,
|
||||
{
|
||||
bidPointAmount: 50,
|
||||
bidderTeamIndex: 1,
|
||||
bidRequestId: `bid-${index}`,
|
||||
},
|
||||
(response) => acknowledgements.push(response),
|
||||
);
|
||||
}
|
||||
|
||||
expect(placeBid).toHaveBeenCalledTimes(8);
|
||||
expect(socketEmit).toHaveBeenCalledWith('error', {
|
||||
message: '입찰 요청이 너무 많습니다. 잠시 후 다시 시도해주세요.',
|
||||
});
|
||||
expect(acknowledgements[8]).toEqual({
|
||||
bidRequestId: 'bid-8',
|
||||
code: 'BID_RATE_LIMITED',
|
||||
message: '입찰 요청이 너무 많습니다. 잠시 후 다시 시도해주세요.',
|
||||
rejected: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('allows bid requests again after the rate limit window resets', async () => {
|
||||
const nowSpy = jest.spyOn(Date, 'now');
|
||||
nowSpy.mockReturnValue(1780000000000);
|
||||
const matchId = new mongoose.Types.ObjectId().toString();
|
||||
const placeBid = jest.fn().mockResolvedValue({
|
||||
auctionBidHistory: [
|
||||
{
|
||||
bidderTeamIndex: 1,
|
||||
bidPointAmount: 50,
|
||||
},
|
||||
],
|
||||
currentHighestBidderTeamIndex: 1,
|
||||
currentHighestBidPointAmount: 50,
|
||||
revision: 3,
|
||||
});
|
||||
const socket = { data: { matchId }, emit: jest.fn() };
|
||||
const { gateway } = createGateway({ placeBid });
|
||||
|
||||
for (let index = 0; index < 8; index += 1) {
|
||||
await gateway.handleBid(
|
||||
socket as never,
|
||||
{
|
||||
bidPointAmount: 50,
|
||||
bidderTeamIndex: 1,
|
||||
bidRequestId: `bid-${index}`,
|
||||
},
|
||||
jest.fn(),
|
||||
);
|
||||
}
|
||||
|
||||
nowSpy.mockReturnValue(1780000001000);
|
||||
await gateway.handleBid(
|
||||
socket as never,
|
||||
{
|
||||
bidPointAmount: 50,
|
||||
bidderTeamIndex: 1,
|
||||
bidRequestId: 'bid-reset',
|
||||
},
|
||||
jest.fn(),
|
||||
);
|
||||
|
||||
expect(placeBid).toHaveBeenCalledTimes(9);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user