Skip to content
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
26 changes: 18 additions & 8 deletions tests/test_ibkr.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
from ib_insync import IB
import pytest
from ib_insync import IB

MAX_ATTEMPTS = 3


class IBKRConnectionError(RuntimeError):
"""Raised when the IB Gateway API does not accept a connection."""


@pytest.mark.asyncio
async def test_ib_connection():
attempts = 0
ib = IB()
while not ib.isConnected():
if attempts >= 3:
raise Exception("Failed to connect to IBKR after 3 attempts")
last_error: OSError | None = None
for _ in range(MAX_ATTEMPTS):
if ib.isConnected():
break
try:
await ib.connectAsync("localhost", 8888, clientId=0)
except Exception:
pass
attempts += 1
except OSError as exc:
last_error = exc

if not ib.isConnected():
raise IBKRConnectionError(
f"Failed to connect to IBKR after {MAX_ATTEMPTS} attempts"
) from last_error

ib.disconnect()
Loading