test_asynchronous_lock: simulate SIGINT from tty
authorZac Medico <zmedico@gentoo.org>
Tue, 17 May 2011 22:19:20 +0000 (15:19 -0700)
committerZac Medico <zmedico@gentoo.org>
Thu, 26 May 2011 03:06:08 +0000 (20:06 -0700)
pym/_emerge/AsynchronousLock.py
pym/portage/tests/locks/test_asynchronous_lock.py

index 86b102b8bda1aef1716d4631c5ce79f660aa2079..3e7600f1751a834b5f23fb6e64fd3138b3bed62d 100644 (file)
@@ -184,7 +184,7 @@ class _LockProcess(AbstractPollTask):
        """
 
        __slots__ = ('path', 'scheduler',) + \
-               ('_acquired', '_proc', '_files', '_reg_id', '_unlocked')
+               ('_acquired', '_kill_test', '_proc', '_files', '_reg_id', '_unlocked')
 
        def _start(self):
                in_pr, in_pw = os.pipe()
@@ -216,7 +216,7 @@ class _LockProcess(AbstractPollTask):
                                # If the lock hasn't been aquired yet, the
                                # caller can check the returncode and handle
                                # this failure appropriately.
-                               if not self.cancelled:
+                               if not (self.cancelled or self._kill_test):
                                        writemsg_level("_LockProcess: %s\n" % \
                                                _("failed to acquire lock on '%s'") % (self.path,),
                                                level=logging.ERROR, noiselevel=-1)
index 50b9c93a5c10981d12441e43669b25c6a8ad553e..6e8bc0a073cc1c01cd92867d67c6218b270eb237 100644 (file)
@@ -1,7 +1,8 @@
-# Copyright 2010 Gentoo Foundation
+# Copyright 2010-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 import shutil
+import signal
 import tempfile
 
 from portage import os
@@ -90,3 +91,34 @@ class AsynchronousLockTestCase(TestCase):
                        lock1.unlock()
                finally:
                        shutil.rmtree(tempdir)
+
+       def testAsynchronousLockWaitKill(self):
+               scheduler = PollScheduler().sched_iface
+               tempdir = tempfile.mkdtemp()
+               try:
+                       path = os.path.join(tempdir, 'lock_me')
+                       lock1 = AsynchronousLock(path=path, scheduler=scheduler)
+                       lock1.start()
+                       self.assertEqual(lock1.wait(), os.EX_OK)
+                       self.assertEqual(lock1.returncode, os.EX_OK)
+                       lock2 = AsynchronousLock(path=path, scheduler=scheduler,
+                               _force_async=True, _force_process=True)
+                       lock2.start()
+                       # lock2 should we waiting for lock1 to release
+                       self.assertEqual(lock2.poll(), None)
+                       self.assertEqual(lock2.returncode, None)
+
+                       # Kill lock2's process and then check wait() and
+                       # returncode results. This is intended to simulate
+                       # a SIGINT sent via the controlling tty.
+                       self.assertEqual(lock2._imp is not None, True)
+                       self.assertEqual(lock2._imp._proc is not None, True)
+                       self.assertEqual(lock2._imp._proc.pid is not None, True)
+                       lock2._imp._kill_test = True
+                       os.kill(lock2._imp._proc.pid, signal.SIGTERM)
+                       self.assertEqual(lock2.wait() == os.EX_OK, False)
+                       self.assertEqual(lock2.returncode == os.EX_OK, False)
+                       self.assertEqual(lock2.returncode is None, False)
+                       lock1.unlock()
+               finally:
+                       shutil.rmtree(tempdir)