144 lines
4.5 KiB
Python
144 lines
4.5 KiB
Python
'''
|
|
IB metadata resolution without feed allocation or a live Gateway.
|
|
|
|
'''
|
|
from collections.abc import AsyncIterator
|
|
from contextlib import asynccontextmanager
|
|
from types import SimpleNamespace
|
|
from typing import cast
|
|
from uuid import uuid4
|
|
|
|
import ib_async as ibis
|
|
import pytest
|
|
import trio
|
|
|
|
from piker.accounting import MktPair
|
|
from piker.brokers.ib import feed, symbols
|
|
from piker.brokers.ib.api import MethodProxy
|
|
|
|
|
|
@pytest.mark.parametrize('borrow_proxy', [False, True])
|
|
def test_metadata_client_ownership_and_cache(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
borrow_proxy: bool,
|
|
) -> None:
|
|
'''
|
|
Resolve uncached IB metadata with owned or borrowed clients.
|
|
|
|
``symbols.get_mkt_info()`` assigned the ``open_data_client``
|
|
factory instead of calling it when no proxy was supplied. That
|
|
untested branch raised ``TypeError`` at ``async with``; existing
|
|
supplied-proxy calls used ``nullcontext()`` and worked.
|
|
|
|
- Exercise the actual decorated resolver with real
|
|
``ib_async.Future`` and ``ib_async.ContractDetails`` records.
|
|
- Replace only client I/O, requiring a fresh entered/exited
|
|
context for each uncached owned lookup and none for borrowing.
|
|
- Verify native-name qualification and canonical ``MktPair``
|
|
identity, including contract-cache registration.
|
|
- Repeat a warmed request spelling with/without a proxy; require
|
|
result identity and no extra query or client-context entry.
|
|
|
|
Unique request names avoid interference with the process-local
|
|
cache or other tests. No feed allocator, SHM, actor or Gateway is
|
|
involved, so this test can ship independently of feed changes.
|
|
|
|
'''
|
|
symbol: str = f'resolvertest{uuid4().hex}'
|
|
native: str = f'{symbol}.cme'
|
|
canonical: str = f'{native}.20261218.ib'
|
|
contract = ibis.Future(
|
|
conId=815824267,
|
|
symbol=symbol.upper(),
|
|
exchange='CME',
|
|
currency='USD',
|
|
lastTradeDateOrContractMonth='20261218',
|
|
multiplier='2',
|
|
)
|
|
details = ibis.ContractDetails(
|
|
contract=contract,
|
|
minTick=0.25,
|
|
minSize=1,
|
|
)
|
|
queries: list[str] = []
|
|
entries: list[object] = []
|
|
exits: list[object] = []
|
|
client = SimpleNamespace(_contracts={}, _cons2mkts={})
|
|
|
|
async def get_sym_details(
|
|
fqme: str,
|
|
) -> tuple[ibis.Contract, ibis.ContractDetails]:
|
|
'''
|
|
Record qualified requests at the client I/O boundary.
|
|
|
|
'''
|
|
queries.append(fqme)
|
|
return contract, details
|
|
|
|
proxy = cast(MethodProxy, SimpleNamespace(
|
|
get_sym_details=get_sym_details,
|
|
_aio_ns=client,
|
|
))
|
|
|
|
@asynccontextmanager
|
|
async def open_client() -> AsyncIterator[MethodProxy]:
|
|
'''
|
|
Give each manager invocation a distinct lifetime marker.
|
|
|
|
'''
|
|
marker = object()
|
|
entries.append(marker)
|
|
try:
|
|
yield proxy
|
|
finally:
|
|
exits.append(marker)
|
|
|
|
monkeypatch.setattr(feed, 'open_data_client', open_client)
|
|
|
|
async def main() -> None:
|
|
'''
|
|
Query two cold spellings, then repeat the first cached one.
|
|
|
|
'''
|
|
supplied: MethodProxy|None = None
|
|
expected_contexts: int = 2
|
|
if borrow_proxy:
|
|
supplied = proxy
|
|
expected_contexts = 0
|
|
|
|
first_mkt: MktPair
|
|
first_details: ibis.ContractDetails
|
|
first_mkt, first_details = await symbols.get_mkt_info(
|
|
native,
|
|
proxy=supplied,
|
|
)
|
|
dated_mkt: MktPair
|
|
dated_details: ibis.ContractDetails
|
|
dated_mkt, dated_details = await symbols.get_mkt_info(
|
|
canonical,
|
|
proxy=supplied,
|
|
)
|
|
assert first_mkt.fqme == dated_mkt.fqme == canonical
|
|
assert first_mkt.bs_mktid == '815824267'
|
|
assert first_details is dated_details is details
|
|
assert queries == [f'{native}.ib', canonical]
|
|
assert len(entries) == expected_contexts
|
|
assert exits == entries
|
|
assert len({id(marker) for marker in entries}) == len(entries)
|
|
assert client._contracts[canonical] is contract
|
|
assert client._cons2mkts[contract] is dated_mkt
|
|
|
|
repeated: tuple[MktPair, ibis.ContractDetails]
|
|
for supplied in (None, proxy):
|
|
repeated = await symbols.get_mkt_info(
|
|
native,
|
|
proxy=supplied,
|
|
)
|
|
assert repeated[0] is first_mkt
|
|
assert repeated[1] is details
|
|
assert queries == [f'{native}.ib', canonical]
|
|
assert len(entries) == expected_contexts
|
|
assert exits == entries
|
|
|
|
trio.run(main)
|