Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add (possibly flaky) test for slow handler logging #4987

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
9 changes: 4 additions & 5 deletions src/textual/message_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from __future__ import annotations

import asyncio
import os
import threading
from asyncio import CancelledError, Queue, QueueEmpty, Task, create_task
from contextlib import contextmanager
Expand Down Expand Up @@ -207,7 +206,7 @@ def message_queue_size(self) -> int:
return self._message_queue.qsize()

@property
def is_dom_root(self):
def is_dom_root(self) -> bool:
"""Is this a root node (i.e. the App)?"""
return False

Expand Down Expand Up @@ -264,7 +263,7 @@ def log(self) -> Logger:
Returns:
A logger.
"""
return self.app._logger
return self.app.log

def _attach(self, parent: MessagePump) -> None:
"""Set the parent, and therefore attach this node to the tree.
Expand Down Expand Up @@ -669,11 +668,11 @@ async def _dispatch_message(self, message: Message) -> None:
# Allow apps to treat events and messages separately
if isinstance(message, Event):
await self.on_event(message)
elif "debug" in self.app.features:
elif self.app.debug:
start = perf_counter()
await self._on_message(message)
if perf_counter() - start > SLOW_THRESHOLD / 1000:
log.warning(
self.log.warning(
f"method=<{self.__class__.__name__}."
f"{message.handler_name}>",
f"Took over {SLOW_THRESHOLD}ms to process.",
Expand Down
35 changes: 35 additions & 0 deletions tests/test_message_pump.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from unittest.mock import Mock

import pytest

from textual import Logger
from textual._dispatch_key import dispatch_key
from textual.app import App, ComposeResult
from textual.errors import DuplicateKeyHandlers
Expand Down Expand Up @@ -169,3 +172,35 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
async with app.run_test() as pilot:
await pilot.click(MyButton)
assert app_button_pressed


async def test_slow_handler(monkeypatch):
"""Test that a slow handler results in a logged warning."""

log_mock = Mock()
monkeypatch.setenv("TEXTUAL_SLOW_THRESHOLD", "200")

class SlowHandlerApp(App[None]):
def compose(self) -> ComposeResult:
yield Button("Press me")

async def on_button_pressed(self, event: Button.Pressed) -> None:
import time

time.sleep(0.5)

@property
def log(self) -> Logger:
return log_mock

@property
def debug(self) -> bool:
return True

app = SlowHandlerApp()
async with app.run_test() as pilot:
await pilot.click(Button)
warning_logs = log_mock.warning.call_args_list
assert len(warning_logs) == 1
assert "method=<SlowHandlerApp.on_button_pressed>" in warning_logs[0][0]
assert "consider using a worker" in warning_logs[0][0][2]
Loading