From e65966962e587aa7c3571f5e3b88c700dbb0b93b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 3 May 2017 19:49:32 +0530 Subject: [PATCH] test_lock now passes on my OS X machine --- src/calibre/utils/ipc/worker.py | 2 +- src/calibre/utils/test_lock.py | 57 +++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/calibre/utils/ipc/worker.py b/src/calibre/utils/ipc/worker.py index 27fa48c3de..fc56a2c1e8 100644 --- a/src/calibre/utils/ipc/worker.py +++ b/src/calibre/utils/ipc/worker.py @@ -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 diff --git a/src/calibre/utils/test_lock.py b/src/calibre/utils/test_lock.py index a9eda35197..675bf4e0cf 100644 --- a/src/calibre/utils/test_lock.py +++ b/src/calibre/utils/test_lock.py @@ -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): - 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['CALIBRE_SIMPLE_WORKER'] = mod + ':' + func if iswindows: @@ -56,41 +59,53 @@ 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) - while child.poll() is None: - if os.path.exists('ready'): - break - time.sleep(0.01) - self.assertIsNone(child.poll(), 'child died without creating ready dir') - ef = FastFailEF('test') - self.assertRaises(EnvironmentError, ef.__enter__) - child.kill() - self.assertIsNotNone(child.wait()) - with ExclusiveFile('test'): - pass + try: + while child.poll() is None: + if os.path.exists('ready'): + break + time.sleep(0.01) + 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', 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():