From e2c18aeb354c4b1396a42bfd074042e477ae665d Mon Sep 17 00:00:00 2001 From: Julien Jehannet Date: Mon, 19 Jan 2009 20:49:53 +0100 Subject: improve acquire_lock() method --- test/unittest_shellutils.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/unittest_shellutils.py b/test/unittest_shellutils.py index 396d7c6..903312d 100644 --- a/test/unittest_shellutils.py +++ b/test/unittest_shellutils.py @@ -5,7 +5,8 @@ from os.path import join from logilab.common.testlib import TestCase, unittest_main -from logilab.common.shellutils import globfind, find, ProgressBar +from logilab.common.shellutils import globfind, find, ProgressBar, acquire_lock, release_lock +from logilab.common.proc import NoSuchProcess from StringIO import StringIO DATA_DIR = join('data','find_test') @@ -123,5 +124,31 @@ class ProgressBarTC(TestCase): self._update_test(5, (8, 16, 25, 33, 42, (42, True)), size=42) +class AcquireLockTC(TestCase): + + def setUp(self): + self.tmpdir = tempfile.mkdtemp() + self.lock = join(self.tmpdir, 'LOCK') + + def tearDown(self): + shutil.rmtree(self.tmpdir) + + def test_acquire_normal(self): + self.assertTrue(acquire_lock(self.lock, 1, 1)) + self.assertTrue(os.path.exists(self.lock)) + release_lock(self.lock) + self.assertFalse(os.path.exists(self.lock)) + + def test_no_possible_acquire(self): + self.assertRaises(Exception, acquire_lock, self.lock, 0) + + def test_wrong_process(self): + fd = os.open(self.lock, os.O_EXCL | os.O_RDWR | os.O_CREAT) + os.write(fd, '1111111111') + os.close(fd) + self.assertTrue(os.path.exists(self.lock)) + self.assertRaises(NoSuchProcess, acquire_lock, self.lock) + + if __name__ == '__main__': unittest_main() -- cgit v1.2.1