1
0

Merge pull request #10239 from SomberNight/202509_tests_clear_callbacks

tests: clear util.callback_mgr between test cases
This commit is contained in:
ghost43
2025-09-26 16:18:49 +00:00
committed by GitHub
3 changed files with 13 additions and 16 deletions

View File

@@ -1953,20 +1953,24 @@ class CallbackManager(Logger):
def __init__(self):
Logger.__init__(self)
self.callback_lock = threading.Lock()
self.callbacks = defaultdict(list) # note: needs self.callback_lock
self.callbacks = defaultdict(list) # type: Dict[str, List[Callable]] # note: needs self.callback_lock
def register_callback(self, func, events):
def register_callback(self, func: Callable, events: Sequence[str]) -> None:
with self.callback_lock:
for event in events:
self.callbacks[event].append(func)
def unregister_callback(self, callback):
def unregister_callback(self, callback: Callable) -> None:
with self.callback_lock:
for callbacks in self.callbacks.values():
if callback in callbacks:
callbacks.remove(callback)
def trigger_callback(self, event, *args):
def clear_all_callbacks(self) -> None:
with self.callback_lock:
self.callbacks.clear()
def trigger_callback(self, event: str, *args) -> None:
"""Trigger a callback with given arguments.
Can be called from any thread. The callback itself will get scheduled
on the event loop.

View File

@@ -75,6 +75,7 @@ class ElectrumTestCase(unittest.IsolatedAsyncioTestCase, Logger):
util._asyncio_event_loop = loop
def tearDown(self):
util.callback_mgr.clear_all_callbacks()
shutil.rmtree(self.electrum_path)
super().tearDown()
util._asyncio_event_loop = None # cleared here, at the ~last possible moment. asyncTearDown is too early.

View File

@@ -1117,12 +1117,8 @@ class TestPeerDirect(TestPeer):
util.register_callback(on_htlc_fulfilled, ["htlc_fulfilled"])
util.register_callback(on_htlc_failed, ["htlc_failed"])
try:
with self.assertRaises(SuccessfulTest):
await f()
finally:
util.unregister_callback(on_htlc_fulfilled)
util.unregister_callback(on_htlc_failed)
with self.assertRaises(SuccessfulTest):
await f()
async def test_payment_recv_mpp_confusion2(self):
"""Regression test for https://github.com/spesmilo/electrum/security/advisories/GHSA-8r85-vp7r-hjxf"""
@@ -1191,12 +1187,8 @@ class TestPeerDirect(TestPeer):
util.register_callback(on_htlc_fulfilled, ["htlc_fulfilled"])
util.register_callback(on_htlc_failed, ["htlc_failed"])
try:
with self.assertRaises(SuccessfulTest):
await f()
finally:
util.unregister_callback(on_htlc_fulfilled)
util.unregister_callback(on_htlc_failed)
with self.assertRaises(SuccessfulTest):
await f()
async def test_legacy_shutdown_low(self):
await self._test_shutdown(alice_fee=100, bob_fee=150)