Ensure various Cache objects used in tests are properly closed rather than relying on their __del__ method

This commit is contained in:
Kovid Goyal 2025-12-29 15:30:33 +05:30
parent 0435f0a0c0
commit 669e408cc6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 10 additions and 1 deletions

View File

@ -30,12 +30,16 @@ class BaseTest(unittest.TestCase):
from calibre.utils.recycle_bin import nuke_recycle
nuke_recycle()
self.paths_to_remove = []
self.objects_to_close = []
self.library_path = self.mkdtemp()
self.create_db(self.library_path)
def tearDown(self):
from calibre.utils.recycle_bin import restore_recyle
restore_recyle()
for x in self.objects_to_close:
x.close()
self.objects_to_close = []
gc.collect(), gc.collect()
for x in self.paths_to_remove:
try:
@ -71,6 +75,7 @@ class BaseTest(unittest.TestCase):
backend = DB(library_path or self.library_path)
cache = Cache(backend)
cache.init()
self.objects_to_close.append(cache)
return cache
def mkdtemp(self):

View File

@ -58,11 +58,15 @@ class LibraryBaseTest(BaseTest):
from calibre.utils.recycle_bin import nuke_recycle
nuke_recycle()
self.library_path = self.mkdtemp()
self.objects_to_close = []
self.create_db(self.library_path)
def tearDown(self):
from calibre.utils.recycle_bin import restore_recyle
restore_recyle()
for x in self.objects_to_close:
x.close()
self.objects_to_close = []
gc.collect(), gc.collect()
try:
shutil.rmtree(self.library_path)
@ -92,7 +96,7 @@ class LibraryBaseTest(BaseTest):
db.add_format(1, 'EPUB', src, run_hooks=False)
db.add_format(1, 'FMT2', BytesIO(b'book1fmt2'), run_hooks=False)
db.add_format(2, 'FMT1', BytesIO(b'book2fmt1'), run_hooks=False)
db.backend.conn.close()
db.close()
return dest
def create_server(self, *args, **kwargs):