Improve auction realtime bid responsiveness
This commit is contained in:
1 parent
c6f5a7077f
commit
b367328df0
16 files changed
+1364
-203
No files matched your search
@@ -8,10 +8,16 @@ import { AuctionGateway } from './auction.gateway';
|
||||
|
||||
describe('AuctionGateway', () => {
|
||||
function createGateway(params?: {
|
||||
placeBid?: jest.Mock;
|
||||
getCurrentRedisTimestampMS?: jest.Mock;
|
||||
placeBidDelta?: jest.Mock;
|
||||
}) {
|
||||
const auctionService = {
|
||||
placeBid: params?.placeBid ?? jest.fn(),
|
||||
placeBidDelta: params?.placeBidDelta ?? jest.fn(),
|
||||
};
|
||||
const auctionRedisService = {
|
||||
getCurrentRedisTimestampMS:
|
||||
params?.getCurrentRedisTimestampMS ??
|
||||
jest.fn().mockResolvedValue(1780000000000),
|
||||
};
|
||||
const gateway = new AuctionGateway(
|
||||
auctionService as never,
|
||||
@@ -20,6 +26,7 @@ describe('AuctionGateway', () => {
|
||||
{ findOrCreateByOid: jest.fn() } as never,
|
||||
{ getModel: jest.fn() } as never,
|
||||
{ attachAuctionNamespace: jest.fn() } as never,
|
||||
auctionRedisService as never,
|
||||
);
|
||||
const emit = jest.fn();
|
||||
const to = jest.fn().mockReturnValue({ emit });
|
||||
@@ -29,6 +36,7 @@ describe('AuctionGateway', () => {
|
||||
auctionService,
|
||||
emit,
|
||||
gateway,
|
||||
auctionRedisService,
|
||||
to,
|
||||
};
|
||||
}
|
||||
@@ -41,8 +49,8 @@ describe('AuctionGateway', () => {
|
||||
bidPlacedAtTimestampMS: 1780000000000,
|
||||
bidPointAmount: 120,
|
||||
};
|
||||
const placeBid = jest.fn().mockResolvedValue({
|
||||
auctionBidHistory: [auctionBidHistoryEntry],
|
||||
const placeBidDelta = jest.fn().mockResolvedValue({
|
||||
auctionBidHistoryEntry,
|
||||
auctionLifecycleId: 'life-1',
|
||||
currentBidRoundDeadlineTimestampMS: 1780000030000,
|
||||
currentHighestBidderTeamIndex: 2,
|
||||
@@ -50,7 +58,7 @@ describe('AuctionGateway', () => {
|
||||
currentServerTimestampMS: 1780000000000,
|
||||
revision: 9,
|
||||
});
|
||||
const { emit, gateway, to } = createGateway({ placeBid });
|
||||
const { emit, gateway, to } = createGateway({ placeBidDelta });
|
||||
const ack = jest.fn();
|
||||
|
||||
await gateway.handleBid(
|
||||
@@ -65,7 +73,7 @@ describe('AuctionGateway', () => {
|
||||
ack,
|
||||
);
|
||||
|
||||
expect(placeBid).toHaveBeenCalledWith({
|
||||
expect(placeBidDelta).toHaveBeenCalledWith({
|
||||
amount: 120,
|
||||
bidRequestId: 'bid-1',
|
||||
clientObservedAuctionLifecycleId: 'life-1',
|
||||
@@ -84,31 +92,40 @@ describe('AuctionGateway', () => {
|
||||
currentServerTimestampMS: 1780000000000,
|
||||
revision: 9,
|
||||
});
|
||||
expect(ack).toHaveBeenCalledWith({
|
||||
bidRequestId: 'bid-1',
|
||||
ok: true,
|
||||
state: expect.objectContaining({
|
||||
auctionLifecycleId: 'life-1',
|
||||
revision: 9,
|
||||
expect(ack).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
bidDelta: {
|
||||
auctionBidHistoryEntry,
|
||||
auctionLifecycleId: 'life-1',
|
||||
bidRequestId: 'bid-1',
|
||||
currentBidRoundDeadlineTimestampMS: 1780000030000,
|
||||
currentHighestBidderTeamIndex: 2,
|
||||
currentHighestBidPointAmount: 120,
|
||||
currentServerTimestampMS: 1780000000000,
|
||||
revision: 9,
|
||||
},
|
||||
bidRequestId: 'bid-1',
|
||||
bidServerTiming: expect.objectContaining({
|
||||
gatewayTotalDurationMS: expect.any(Number),
|
||||
}),
|
||||
ok: true,
|
||||
}),
|
||||
});
|
||||
);
|
||||
});
|
||||
|
||||
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,
|
||||
},
|
||||
],
|
||||
const placeBidDelta = jest.fn().mockResolvedValue({
|
||||
auctionBidHistoryEntry: {
|
||||
bidderTeamIndex: 1,
|
||||
bidPointAmount: 50,
|
||||
},
|
||||
auctionLifecycleId: 'life-1',
|
||||
currentHighestBidderTeamIndex: 1,
|
||||
currentHighestBidPointAmount: 50,
|
||||
revision: 3,
|
||||
});
|
||||
const { emit, gateway } = createGateway({ placeBid });
|
||||
const { emit, gateway } = createGateway({ placeBidDelta });
|
||||
const ack = jest.fn();
|
||||
|
||||
await gateway.handleBid(
|
||||
@@ -120,7 +137,7 @@ describe('AuctionGateway', () => {
|
||||
ack,
|
||||
);
|
||||
|
||||
const generatedBidRequestId = placeBid.mock.calls[0][0].bidRequestId;
|
||||
const generatedBidRequestId = placeBidDelta.mock.calls[0][0].bidRequestId;
|
||||
expect(generatedBidRequestId).toEqual(expect.any(String));
|
||||
expect(generatedBidRequestId).not.toHaveLength(0);
|
||||
expect(emit).toHaveBeenCalledWith(
|
||||
@@ -139,9 +156,23 @@ describe('AuctionGateway', () => {
|
||||
|
||||
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 placeBidDelta = jest.fn().mockRejectedValue({
|
||||
getResponse: () => ({
|
||||
bidDiagnostics: {
|
||||
currentBidRoundDeadlineTimestampMS: 1780000002500,
|
||||
currentServerTimestampMS: 1780000003000,
|
||||
},
|
||||
bidServerTiming: {
|
||||
redisBidCommandDurationMS: 2,
|
||||
redisBidContextReadDurationMS: 1,
|
||||
serviceTotalDurationMS: 4,
|
||||
},
|
||||
code: 'EXPIRED',
|
||||
message: 'Bid deadline has already expired.',
|
||||
}),
|
||||
});
|
||||
const socketEmit = jest.fn();
|
||||
const { gateway } = createGateway({ placeBid });
|
||||
const { gateway } = createGateway({ placeBidDelta });
|
||||
const ack = jest.fn();
|
||||
|
||||
await gateway.handleBid(
|
||||
@@ -155,32 +186,69 @@ describe('AuctionGateway', () => {
|
||||
);
|
||||
|
||||
expect(socketEmit).toHaveBeenCalledWith('error', {
|
||||
message: 'Bid failed',
|
||||
});
|
||||
expect(ack).toHaveBeenCalledWith({
|
||||
bidRequestId: 'bid-1',
|
||||
code: 'Bid failed',
|
||||
rejected: true,
|
||||
message: 'Bid deadline has already expired.',
|
||||
});
|
||||
expect(ack).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
bidDiagnostics: {
|
||||
currentBidRoundDeadlineTimestampMS: 1780000002500,
|
||||
currentServerTimestampMS: 1780000003000,
|
||||
},
|
||||
bidRequestId: 'bid-1',
|
||||
bidServerTiming: expect.objectContaining({
|
||||
gatewayTotalDurationMS: expect.any(Number),
|
||||
redisBidCommandDurationMS: 2,
|
||||
redisBidContextReadDurationMS: 1,
|
||||
serviceTotalDurationMS: 4,
|
||||
}),
|
||||
code: 'EXPIRED',
|
||||
message: 'Bid deadline has already expired.',
|
||||
rejected: true,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('acknowledges time sync with Redis server time', async () => {
|
||||
const getCurrentRedisTimestampMS = jest
|
||||
.fn()
|
||||
.mockResolvedValue(1780000001234);
|
||||
const { gateway } = createGateway({ getCurrentRedisTimestampMS });
|
||||
const ack = jest.fn();
|
||||
|
||||
await gateway.handleTimeSync(
|
||||
{
|
||||
clientSentAtTimestampMS: 1780000000000,
|
||||
sequence: 7,
|
||||
},
|
||||
ack,
|
||||
);
|
||||
|
||||
expect(getCurrentRedisTimestampMS).toHaveBeenCalledTimes(1);
|
||||
expect(ack).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
clientSentAtTimestampMS: 1780000000000,
|
||||
currentServerTimestampMS: 1780000001234,
|
||||
ok: true,
|
||||
sequence: 7,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
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,
|
||||
},
|
||||
],
|
||||
const placeBidDelta = jest.fn().mockResolvedValue({
|
||||
auctionBidHistoryEntry: {
|
||||
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 { gateway } = createGateway({ placeBidDelta });
|
||||
const acknowledgements: Array<Record<string, unknown>> = [];
|
||||
|
||||
for (let index = 0; index < 9; index += 1) {
|
||||
@@ -195,7 +263,7 @@ describe('AuctionGateway', () => {
|
||||
);
|
||||
}
|
||||
|
||||
expect(placeBid).toHaveBeenCalledTimes(8);
|
||||
expect(placeBidDelta).toHaveBeenCalledTimes(8);
|
||||
expect(socketEmit).toHaveBeenCalledWith('error', {
|
||||
message: '입찰 요청이 너무 많습니다. 잠시 후 다시 시도해주세요.',
|
||||
});
|
||||
@@ -211,19 +279,17 @@ describe('AuctionGateway', () => {
|
||||
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,
|
||||
},
|
||||
],
|
||||
const placeBidDelta = jest.fn().mockResolvedValue({
|
||||
auctionBidHistoryEntry: {
|
||||
bidderTeamIndex: 1,
|
||||
bidPointAmount: 50,
|
||||
},
|
||||
currentHighestBidderTeamIndex: 1,
|
||||
currentHighestBidPointAmount: 50,
|
||||
revision: 3,
|
||||
});
|
||||
const socket = { data: { matchId }, emit: jest.fn() };
|
||||
const { gateway } = createGateway({ placeBid });
|
||||
const { gateway } = createGateway({ placeBidDelta });
|
||||
|
||||
for (let index = 0; index < 8; index += 1) {
|
||||
await gateway.handleBid(
|
||||
@@ -248,6 +314,6 @@ describe('AuctionGateway', () => {
|
||||
jest.fn(),
|
||||
);
|
||||
|
||||
expect(placeBid).toHaveBeenCalledTimes(9);
|
||||
expect(placeBidDelta).toHaveBeenCalledTimes(9);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user