Commit 79fe4737 authored by Jason Madden's avatar Jason Madden Committed by GitHub

Merge pull request #267 from zopefoundation/issue266

Fix ResourceWarnings seen during testing, and a testrunner orphaned thread warning
parents cf15ac88 9533ce76
...@@ -11,3 +11,4 @@ exclude_lines = ...@@ -11,3 +11,4 @@ exclude_lines =
pragma: no cover pragma: no cover
if __name__ == ['"]__main__['"]: if __name__ == ['"]__main__['"]:
assert False assert False
self.fail
...@@ -131,6 +131,7 @@ Demo storages are configured using the ``demostorage`` section:: ...@@ -131,6 +131,7 @@ Demo storages are configured using the ``demostorage`` section::
'base.fs' 'base.fs'
>>> storage.changes.getName() >>> storage.changes.getName()
'Changes' 'Changes'
>>> storage.close()
``demostorage`` sections can contain up to 2 storage subsections, ``demostorage`` sections can contain up to 2 storage subsections,
named ``base`` and ``changes``, specifying the demo storage's base and named ``base`` and ``changes``, specifying the demo storage's base and
......
...@@ -667,9 +667,11 @@ def do_recover(options): ...@@ -667,9 +667,11 @@ def do_recover(options):
repofiles = find_files(options) repofiles = find_files(options)
if not repofiles: if not repofiles:
if options.date: if options.date:
raise NoFiles('No files in repository before %s', options.date) raise NoFiles('No files in repository before %s' % (options.date,))
else: else:
raise NoFiles('No files in repository') raise NoFiles('No files in repository')
files_to_close = ()
if options.output is None: if options.output is None:
log('Recovering file to stdout') log('Recovering file to stdout')
outfp = sys.stdout outfp = sys.stdout
...@@ -682,6 +684,9 @@ def do_recover(options): ...@@ -682,6 +684,9 @@ def do_recover(options):
log('Recovering file to %s', options.output) log('Recovering file to %s', options.output)
temporary_output_file = options.output + '.part' temporary_output_file = options.output + '.part'
outfp = open(temporary_output_file, 'wb') outfp = open(temporary_output_file, 'wb')
files_to_close += (outfp,)
try:
if options.withverify: if options.withverify:
datfile = os.path.splitext(repofiles[0])[0] + '.dat' datfile = os.path.splitext(repofiles[0])[0] + '.dat'
with open(datfile) as fp: with open(datfile) as fp:
...@@ -724,9 +729,11 @@ def do_recover(options): ...@@ -724,9 +729,11 @@ def do_recover(options):
shutil.copyfile(source_index, target_index) shutil.copyfile(source_index, target_index)
else: else:
log('No index file to restore: %s', source_index) log('No index file to restore: %s', source_index)
finally:
for f in files_to_close:
f.close()
if outfp != sys.stdout: if options.output is not None:
outfp.close()
try: try:
os.rename(temporary_output_file, options.output) os.rename(temporary_output_file, options.output)
except OSError: except OSError:
......
...@@ -7,6 +7,8 @@ import ZODB ...@@ -7,6 +7,8 @@ import ZODB
class ZODBClientThread(threading.Thread): class ZODBClientThread(threading.Thread):
sleep_time = 15
def __init__(self, db, test): def __init__(self, db, test):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self._exc_info = None self._exc_info = None
...@@ -19,7 +21,7 @@ class ZODBClientThread(threading.Thread): ...@@ -19,7 +21,7 @@ class ZODBClientThread(threading.Thread):
conn = self.db.open() conn = self.db.open()
conn.sync() conn.sync()
self.event.set() self.event.set()
time.sleep(15) time.sleep(self.sleep_time)
# conn.close calls self.transaction_manager.unregisterSynch(self) # conn.close calls self.transaction_manager.unregisterSynch(self)
# and this succeeds. # and this succeeds.
...@@ -37,6 +39,9 @@ class ShutdownTest(ZODB.tests.util.TestCase): ...@@ -37,6 +39,9 @@ class ShutdownTest(ZODB.tests.util.TestCase):
'ZODBTests.fs', create=1) 'ZODBTests.fs', create=1)
self._db = ZODB.DB(self._storage) self._db = ZODB.DB(self._storage)
def tearDown(self):
ZODB.tests.util.TestCase.tearDown(self)
def check_shutdown(self): def check_shutdown(self):
client_thread = ZODBClientThread(self._db, self) client_thread = ZODBClientThread(self._db, self)
client_thread.start() client_thread.start()
...@@ -47,8 +52,11 @@ class ShutdownTest(ZODB.tests.util.TestCase): ...@@ -47,8 +52,11 @@ class ShutdownTest(ZODB.tests.util.TestCase):
# have different contents. # have different contents.
self._db.close() self._db.close()
def tearDown(self): # Be sure not to 'leak' the running thread; if it hasn't
ZODB.tests.util.TestCase.tearDown(self) # finished after this, something went very wrong
client_thread.join(client_thread.sleep_time + 10)
if client_thread.is_alive():
self.fail("Client thread failed to close connection")
def test_suite(): def test_suite():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment