tests: clear util.callback_mgr between test cases
util.callback_mgr.callbacks was not getting properly cleared between tests. Every time an Abstract_Wallet or an LNWorker (or many other subclasses of EventListener) is instantiated, self.register_callbacks() is called in __init__, which puts callbacks into util.callback_mgr.callbacks. These are only cleaned up if we explicitly call Abstract_Wallet.stop() or LNWorker.stop() later, which we usually do not do in the tests. As a result, when running multiple unit tests in a row, lots of objects created in a given testcase are never GC-ed and leak into subsequent tests. This is not only a memory leak, but wastes compute too: when events are triggered and cbs get called, these old objects also have their cbs called. After running all (~1061) unit tests, I observe util.callback_mgr.callbacks had 30 events with a total of 3156 callbacks stored. On my laptop, running all unit tests previously took ~115 sec, and now it takes ~73 sec.
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user