Skip to content

Commit 5c5616f

Browse files
committed
test(unit): refine TableWidget deadlock test style
- Refactored test_navigation_to_invalid_page_resets_to_valid_page_without_deadlock to follow established project conventions. - Use internal import from bigframes.display.anywidget. - Employ bigframes.option_context for setting widget state. - Improved docstring to follow behavior-driven (Given/When/Then) style.
1 parent b303072 commit 5c5616f

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

tests/unit/display/test_anywidget.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,58 +18,58 @@
1818
import pandas as pd
1919
import pytest
2020

21-
import bigframes.dataframe
21+
import bigframes
2222

2323
# Skip if anywidget/traitlets not installed, though they should be in the dev env
2424
pytest.importorskip("anywidget")
2525
pytest.importorskip("traitlets")
2626

2727

28-
class TestTableWidget:
29-
def test_navigation_to_invalid_page_resets_to_valid_page_without_deadlock(self):
30-
"""
31-
Verifies that navigating to an invalid page resets to a valid page without deadlock.
28+
def test_navigation_to_invalid_page_resets_to_valid_page_without_deadlock():
29+
"""
30+
Given a widget on a page beyond available data, when navigating,
31+
then it should reset to the last valid page without deadlock.
32+
"""
33+
from bigframes.display.anywidget import TableWidget
3234

33-
This behavior relies on _set_table_html releasing the lock before updating self.page,
34-
preventing re-entrancy issues where the observer triggers a new update on the same thread.
35-
"""
36-
from bigframes.display import TableWidget
35+
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame, instance=True)
36+
mock_df.columns = ["col1"]
37+
mock_df.dtypes = {"col1": "object"}
3738

38-
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame, instance=True)
39-
mock_df.columns = ["col1"]
40-
mock_df.dtypes = {"col1": "object"}
39+
mock_block = mock.Mock()
40+
mock_block.has_index = False
41+
mock_df._block = mock_block
4142

42-
mock_block = mock.Mock()
43-
mock_block.has_index = False
44-
mock_df._block = mock_block
45-
46-
# We mock _initial_load to avoid complex setup
47-
with mock.patch.object(TableWidget, "_initial_load"):
43+
# We mock _initial_load to avoid complex setup
44+
with mock.patch.object(TableWidget, "_initial_load"):
45+
with bigframes.option_context(
46+
"display.repr_mode", "anywidget", "display.max_rows", 10
47+
):
4848
widget = TableWidget(mock_df)
4949

50-
# Simulate "loaded data but unknown total rows" state
51-
widget.page_size = 10
52-
widget.row_count = None
53-
widget._all_data_loaded = True
50+
# Simulate "loaded data but unknown total rows" state
51+
widget.page_size = 10
52+
widget.row_count = None
53+
widget._all_data_loaded = True
5454

55-
# Populate cache with 1 page of data (10 rows). Page 0 is valid, page 1+ are invalid.
56-
widget._cached_batches = [pd.DataFrame({"col1": range(10)})]
55+
# Populate cache with 1 page of data (10 rows). Page 0 is valid, page 1+ are invalid.
56+
widget._cached_batches = [pd.DataFrame({"col1": range(10)})]
5757

58-
# Mark initial load as complete so observers fire
59-
widget._initial_load_complete = True
58+
# Mark initial load as complete so observers fire
59+
widget._initial_load_complete = True
6060

61-
# Setup timeout to fail fast if deadlock occurs
62-
def handler(signum, frame):
63-
raise TimeoutError("Deadlock detected!")
61+
# Setup timeout to fail fast if deadlock occurs
62+
def handler(signum, frame):
63+
raise TimeoutError("Deadlock detected!")
6464

65-
signal.signal(signal.SIGALRM, handler)
66-
signal.alarm(2) # 2 seconds timeout
65+
signal.signal(signal.SIGALRM, handler)
66+
signal.alarm(2) # 2 seconds timeout
6767

68-
try:
69-
# Trigger navigation to page 5 (invalid), which should reset to page 0
70-
widget.page = 5
68+
try:
69+
# Trigger navigation to page 5 (invalid), which should reset to page 0
70+
widget.page = 5
7171

72-
assert widget.page == 0
72+
assert widget.page == 0
7373

74-
finally:
75-
signal.alarm(0)
74+
finally:
75+
signal.alarm(0)

0 commit comments

Comments
 (0)