test_lock now passes on my OS X machine

This commit is contained in:
Kovid Goyal 2017-05-03 19:49:32 +05:30
parent ba21c51e97
commit e65966962e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 37 additions and 22 deletions

View File

@ -158,7 +158,7 @@ def main():
# Close open file descriptors inherited from parent
# On Unix this is done by the subprocess module
os.closerange(3, 256)
if isosx and 'CALIBRE_WORKER_ADDRESS' not in os.environ and '--pipe-worker' not in sys.argv:
if isosx and 'CALIBRE_WORKER_ADDRESS' not in os.environ and 'CALIBRE_SIMPLE_WORKER' not in os.environ and '--pipe-worker' not in sys.argv:
# On some OS X computers launchd apparently tries to
# launch the last run process from the bundle
# so launch the gui as usual

View File

@ -27,14 +27,17 @@ class Other(Thread):
def run(self):
try:
with FastFailEF('test'):
with FastFailEF('testsp'):
self.locked = True
except EnvironmentError:
self.locked = False
def run_worker(mod, func, **kw):
try:
exe = [sys.executable, os.path.join(sys.setup_dir, 'run-calibre-worker.py')]
except AttributeError:
exe = [os.path.join(os.path.dirname(os.path.abspath(sys.executable)), 'calibre-parallel' + ('.exe' if iswindows else ''))]
env = kw.get('env', os.environ.copy())
env['CALIBRE_SIMPLE_WORKER'] = mod + ':' + func
if iswindows:
@ -56,21 +59,22 @@ class IPCLockTest(unittest.TestCase):
shutil.rmtree(self.tdir)
def test_exclusive_file_same_process(self):
with ExclusiveFile('test'):
ef = FastFailEF('test')
fname = 'testsp'
with ExclusiveFile(fname):
ef = FastFailEF(fname)
self.assertRaises(EnvironmentError, ef.__enter__)
t = Other()
t.start(), t.join()
self.assertIs(t.locked, False)
if not iswindows:
with unix_open('test') as f:
with unix_open(fname) as f:
self.assertEqual(
1, fcntl.fcntl(f.fileno(), fcntl.F_GETFD) & fcntl.FD_CLOEXEC
)
def test_exclusive_file_other_process(self):
def run_other_ef_op(self, clean_exit):
child = run_worker('calibre.utils.test_lock', 'other1')
print(1111111, sys.executable, sys.setup_dir, child)
try:
while child.poll() is None:
if os.path.exists('ready'):
break
@ -78,19 +82,30 @@ class IPCLockTest(unittest.TestCase):
self.assertIsNone(child.poll(), 'child died without creating ready dir')
ef = FastFailEF('test')
self.assertRaises(EnvironmentError, ef.__enter__)
if clean_exit:
os.mkdir('quit')
else:
child.kill()
self.assertIsNotNone(child.wait())
with ExclusiveFile('test'):
with ExclusiveFile('test', timeout=3):
pass
finally:
if child.poll() is None:
child.kill()
def test_exclusive_file_other_process_clean(self):
self.run_other_ef_op(True)
def test_exclusive_file_other_process_kill(self):
self.run_other_ef_op(False)
def other1():
print('in other')
e = ExclusiveFile('test')
with e:
os.mkdir('ready')
print(22222222)
time.sleep(30)
while not os.path.exists('quit'):
time.sleep(0.02)
def find_tests():