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 # Close open file descriptors inherited from parent
# On Unix this is done by the subprocess module # On Unix this is done by the subprocess module
os.closerange(3, 256) 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 # On some OS X computers launchd apparently tries to
# launch the last run process from the bundle # launch the last run process from the bundle
# so launch the gui as usual # so launch the gui as usual

View File

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