Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

test: api timeout handling for plugin-binance #2504

Merged
merged 3 commits into from
Jan 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion packages/plugin-binance/__tests__/trade.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { TradeService } from '../src/services/trade';
import { AuthenticationError, InvalidSymbolError, MinNotionalError } from '../src/types/internal/error';
import { AuthenticationError, InvalidSymbolError, MinNotionalError, ApiError } from '../src/types/internal/error';
import { ORDER_TYPES, TIME_IN_FORCE } from '../src/constants/api';

// Mock the Binance client
Expand Down Expand Up @@ -207,5 +207,31 @@ describe('TradeService', () => {
// price is missing
})).rejects.toThrow('Price is required for LIMIT orders');
});

it('should handle API timeout', async () => {
// Mock successful exchange info response first
mockExchangeInfo.mockResolvedValueOnce(mockExchangeInfoResponse);

// Mock order request to timeout
mockNewOrder.mockImplementationOnce(() =>
Promise.reject(new ApiError('Request timed out', -1001))
);

const service = new TradeService({
apiKey: 'test',
secretKey: 'test',
timeout: 100 // Lower timeout as we're mocking the error
});

await expect(() => service.executeTrade({
symbol: 'BTCUSDT',
side: 'BUY',
type: ORDER_TYPES.MARKET,
quantity: 1
})).rejects.toMatchObject({
message: 'Request timed out',
code: -1001
});
});
});
});
Loading