Skip to content

Commit f5c0739

Browse files
committed
Copilot review
1 parent 0b948d8 commit f5c0739

2 files changed

Lines changed: 32 additions & 80 deletions

File tree

test/asynchronous/test_periodic_executor.py

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async def target():
4141
)
4242

4343

44-
class AsyncPeriodicExecutorTestBase(AsyncUnitTest):
44+
class TestAsyncPeriodicExecutor(AsyncUnitTest):
4545
async def asyncSetUp(self):
4646
self.executor = None
4747

@@ -50,14 +50,6 @@ async def asyncTearDown(self):
5050
self.executor.close()
5151
await self.executor.join(timeout=2)
5252

53-
54-
class TestAsyncPeriodicExecutor(AsyncPeriodicExecutorTestBase):
55-
async def test_repr_contains_class_and_name(self):
56-
executor = _make_executor(name="exec")
57-
executor_repr = repr(executor)
58-
self.assertIn("AsyncPeriodicExecutor", executor_repr)
59-
self.assertIn("exec", executor_repr)
60-
6153
async def test_join_without_open_is_safe(self):
6254
self.executor = _make_executor()
6355
try:
@@ -81,41 +73,25 @@ async def target():
8173
self.assertTrue(ran.is_set(), "target never ran")
8274

8375
async def test_target_exception_stops_executor(self):
84-
if _IS_SYNC:
85-
ran = threading.Event()
86-
captured_exc: list = []
87-
orig_excepthook = threading.excepthook
88-
89-
def _capture_excepthook(args):
90-
captured_exc.append(args.exc_value)
91-
92-
threading.excepthook = _capture_excepthook
93-
try:
94-
95-
def target():
96-
ran.set()
97-
raise RuntimeError("error")
98-
99-
self.executor = _make_executor(target=target)
100-
self.executor.open()
101-
self.executor.join(timeout=2)
102-
self.assertTrue(ran.is_set(), "target never ran")
103-
finally:
104-
threading.excepthook = orig_excepthook
105-
self.assertEqual(len(captured_exc), 1)
106-
self.assertIsInstance(captured_exc[0], RuntimeError)
107-
else:
108-
call_count = 0
76+
call_count = 0
10977

110-
async def target():
111-
nonlocal call_count
112-
call_count += 1
113-
raise RuntimeError("error")
78+
async def target():
79+
nonlocal call_count
80+
call_count += 1
81+
raise RuntimeError("error")
11482

115-
self.executor = _make_executor(target=target)
83+
self.executor = _make_executor(target=target)
84+
# Suppress threading.excepthook so the intentional RuntimeError raised
85+
# in the worker thread is not surfaced by pytest's threadexception
86+
# plugin (no-op for the async executor since no thread is involved).
87+
orig_excepthook = threading.excepthook
88+
threading.excepthook = lambda _args: None
89+
try:
11690
self.executor.open()
11791
await self.executor.join(timeout=2)
118-
self.assertEqual(call_count, 1, "target should stop after exception")
92+
finally:
93+
threading.excepthook = orig_excepthook
94+
self.assertEqual(call_count, 1, "target should stop after exception")
11995

12096
async def test_skip_sleep_flag_skips_interval(self):
12197
call_times = []

test/test_periodic_executor.py

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def target():
3939
return PeriodicExecutor(interval=interval, min_interval=min_interval, target=target, name=name)
4040

4141

42-
class PeriodicExecutorTestBase(UnitTest):
42+
class TestPeriodicExecutor(UnitTest):
4343
def setUp(self):
4444
self.executor = None
4545

@@ -48,14 +48,6 @@ def tearDown(self):
4848
self.executor.close()
4949
self.executor.join(timeout=2)
5050

51-
52-
class TestPeriodicExecutor(PeriodicExecutorTestBase):
53-
def test_repr_contains_class_and_name(self):
54-
executor = _make_executor(name="exec")
55-
executor_repr = repr(executor)
56-
self.assertIn("PeriodicExecutor", executor_repr)
57-
self.assertIn("exec", executor_repr)
58-
5951
def test_join_without_open_is_safe(self):
6052
self.executor = _make_executor()
6153
try:
@@ -79,41 +71,25 @@ def target():
7971
self.assertTrue(ran.is_set(), "target never ran")
8072

8173
def test_target_exception_stops_executor(self):
82-
if _IS_SYNC:
83-
ran = threading.Event()
84-
captured_exc: list = []
85-
orig_excepthook = threading.excepthook
86-
87-
def _capture_excepthook(args):
88-
captured_exc.append(args.exc_value)
89-
90-
threading.excepthook = _capture_excepthook
91-
try:
92-
93-
def target():
94-
ran.set()
95-
raise RuntimeError("error")
96-
97-
self.executor = _make_executor(target=target)
98-
self.executor.open()
99-
self.executor.join(timeout=2)
100-
self.assertTrue(ran.is_set(), "target never ran")
101-
finally:
102-
threading.excepthook = orig_excepthook
103-
self.assertEqual(len(captured_exc), 1)
104-
self.assertIsInstance(captured_exc[0], RuntimeError)
105-
else:
106-
call_count = 0
74+
call_count = 0
10775

108-
def target():
109-
nonlocal call_count
110-
call_count += 1
111-
raise RuntimeError("error")
76+
def target():
77+
nonlocal call_count
78+
call_count += 1
79+
raise RuntimeError("error")
11280

113-
self.executor = _make_executor(target=target)
81+
self.executor = _make_executor(target=target)
82+
# Suppress threading.excepthook so the intentional RuntimeError raised
83+
# in the worker thread is not surfaced by pytest's threadexception
84+
# plugin (no-op for the async executor since no thread is involved).
85+
orig_excepthook = threading.excepthook
86+
threading.excepthook = lambda _args: None
87+
try:
11488
self.executor.open()
11589
self.executor.join(timeout=2)
116-
self.assertEqual(call_count, 1, "target should stop after exception")
90+
finally:
91+
threading.excepthook = orig_excepthook
92+
self.assertEqual(call_count, 1, "target should stop after exception")
11793

11894
def test_skip_sleep_flag_skips_interval(self):
11995
call_times = []

0 commit comments

Comments
 (0)