piker/tests/test_logging.py

113 lines
3.1 KiB
Python
Raw Permalink Normal View History

'''
Package logger namespace regression tests.
'''
.data: expose websocket reconnect progress at INFO An INFO pikerd still hid websocket connection-success messages. The shared ._util.log emitter is piker.data, but open_feed_bus() configured piker.data.feed. Errors reached bare stderr while INFO diagnostics remained disabled on the actual emitter. Configure the shared logger by its exact log.name and prefer the actor's loglevel over a quieter per-client request for these actor-wide transport diagnostics. Preserve the selected client level passed to provider feed startup. Also, - retain the existing handshake-success report and add URL-tagged readiness after fixture setup and first-message relay once per connection, including replacement sockets. - include the URL in receive-timeout reset errors so Binance's independent bookTicker and aggTrade routes are distinguishable. - prove real console INFO visibility through production feed startup, stopping before provider lookup, then restore logging state; the original code fails with the emitter at WARNING. - extend the loopback recovery test to verify both readiness reports and the first relay from the replacement connection. First-message relay is raw websocket traffic, not proof that parsed market data or chart updates have recovered. The 18 logging/Binance/websocket cases pass; live reporting qualification remains separate. This is a local follow-up, not a downstream cherry-pick. The 54da2973 websocket cleanup and 5adf77a9 namespace fix do not configure this shared emitter. Relevant local histories and dirty IB-history/TIPC deltas contain no equivalent wiring fix. Prompt-IO: ai/prompt-io/opencode/20261028T165638Z_b95e7c79_reconnect_logging.md (this patch was generated in some part by `opencode` using `gpt-6.1-sol` (`openai`))
2026-10-28 17:13:26 +00:00
import logging
from types import SimpleNamespace
import pytest
.data: expose websocket reconnect progress at INFO An INFO pikerd still hid websocket connection-success messages. The shared ._util.log emitter is piker.data, but open_feed_bus() configured piker.data.feed. Errors reached bare stderr while INFO diagnostics remained disabled on the actual emitter. Configure the shared logger by its exact log.name and prefer the actor's loglevel over a quieter per-client request for these actor-wide transport diagnostics. Preserve the selected client level passed to provider feed startup. Also, - retain the existing handshake-success report and add URL-tagged readiness after fixture setup and first-message relay once per connection, including replacement sockets. - include the URL in receive-timeout reset errors so Binance's independent bookTicker and aggTrade routes are distinguishable. - prove real console INFO visibility through production feed startup, stopping before provider lookup, then restore logging state; the original code fails with the emitter at WARNING. - extend the loopback recovery test to verify both readiness reports and the first relay from the replacement connection. First-message relay is raw websocket traffic, not proof that parsed market data or chart updates have recovered. The 18 logging/Binance/websocket cases pass; live reporting qualification remains separate. This is a local follow-up, not a downstream cherry-pick. The 54da2973 websocket cleanup and 5adf77a9 namespace fix do not configure this shared emitter. Relevant local histories and dirty IB-history/TIPC deltas contain no equivalent wiring fix. Prompt-IO: ai/prompt-io/opencode/20261028T165638Z_b95e7c79_reconnect_logging.md (this patch was generated in some part by `opencode` using `gpt-6.1-sol` (`openai`))
2026-10-28 17:13:26 +00:00
import trio
from piker.log import (
get_console_log,
get_logger,
)
@pytest.mark.parametrize(
'factory',
[
get_logger,
get_console_log,
],
)
@pytest.mark.parametrize(
'name',
[
'piker.service._daemon',
'piker.paper',
],
)
def test_package_logger_name(
factory,
name: str,
):
'''
`str.lstrip('piker.')` treated the package prefix as a set of
removable characters. A submodule beginning with any of those
characters, such as `piker.paper`, was silently renamed to
`piker.aper`, while service code asserted a stale parent name.
Exercise both logger factories with service and prefix-sensitive
submodule names, proving they retain the exact requested package
namespace.
'''
log = factory(name=name)
assert log.name == name
.data: expose websocket reconnect progress at INFO An INFO pikerd still hid websocket connection-success messages. The shared ._util.log emitter is piker.data, but open_feed_bus() configured piker.data.feed. Errors reached bare stderr while INFO diagnostics remained disabled on the actual emitter. Configure the shared logger by its exact log.name and prefer the actor's loglevel over a quieter per-client request for these actor-wide transport diagnostics. Preserve the selected client level passed to provider feed startup. Also, - retain the existing handshake-success report and add URL-tagged readiness after fixture setup and first-message relay once per connection, including replacement sockets. - include the URL in receive-timeout reset errors so Binance's independent bookTicker and aggTrade routes are distinguishable. - prove real console INFO visibility through production feed startup, stopping before provider lookup, then restore logging state; the original code fails with the emitter at WARNING. - extend the loopback recovery test to verify both readiness reports and the first relay from the replacement connection. First-message relay is raw websocket traffic, not proof that parsed market data or chart updates have recovered. The 18 logging/Binance/websocket cases pass; live reporting qualification remains separate. This is a local follow-up, not a downstream cherry-pick. The 54da2973 websocket cleanup and 5adf77a9 namespace fix do not configure this shared emitter. Relevant local histories and dirty IB-history/TIPC deltas contain no equivalent wiring fix. Prompt-IO: ai/prompt-io/opencode/20261028T165638Z_b95e7c79_reconnect_logging.md (this patch was generated in some part by `opencode` using `gpt-6.1-sol` (`openai`))
2026-10-28 17:13:26 +00:00
def test_feed_startup_configures_transport_logger(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture,
) -> None:
'''
Daemon INFO did not enable websocket reconnection diagnostics.
Feed startup configured piker.data.feed, but ._util.log and
._web_bs.log emit on piker.data. Errors fell back to bare stderr
while connection-success INFO stayed hidden. Client verbosity
must also not demote that shared actor-wide transport logger.
Run production feed startup and logger configuration with an INFO
actor and ERROR client. Stop at feed-bus lookup before touching
any provider. Use real logging/console emission, not mocked logger
methods, to prove INFO from the websocket emitter is visible.
'''
from piker.data import feed, _web_bs
saved = {}
for name in ('piker.data', 'piker.data.feed', 'tractor'):
logger = logging.getLogger(name)
saved[logger] = (logger.level, list(logger.handlers))
boundary = RuntimeError('stop before provider lookup')
def stop_at_bus(brokername: str):
raise boundary
monkeypatch.setattr(
feed.tractor,
'current_actor',
lambda **kwargs: SimpleNamespace(
name='brokerd.binance', loglevel='info',
),
)
monkeypatch.setattr(feed, 'get_feed_bus', stop_at_bus)
async def main() -> None:
with pytest.raises(RuntimeError) as caught:
await feed.open_feed_bus(
ctx=None, brokername='binance', symbols=[],
loglevel='error',
)
assert caught.value is boundary
try:
logging.getLogger('piker.data').setLevel(logging.WARNING)
trio.run(main)
assert _web_bs.log.isEnabledFor(logging.INFO)
_web_bs.log.info('websocket INFO visibility probe')
assert 'websocket INFO visibility probe' in (
capsys.readouterr().err
)
finally:
for logger, (level, handlers) in saved.items():
for handler in list(logger.handlers):
if handler not in handlers:
logger.removeHandler(handler)
handler.close()
logger.setLevel(level)