123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- import os
- import threading
- import shutil
- import lockfile
- class ComplianceTest(object):
- def __init__(self):
- self.saved_class = lockfile.LockFile
- def _testfile(self):
- """Return platform-appropriate file. Helper for tests."""
- import tempfile
- return os.path.join(tempfile.gettempdir(), 'trash-%s' % os.getpid())
- def setup(self):
- lockfile.LockFile = self.class_to_test
- def teardown(self):
- try:
- tf = self._testfile()
- if os.path.isdir(tf):
- shutil.rmtree(tf)
- elif os.path.isfile(tf):
- os.unlink(tf)
- elif not os.path.exists(tf):
- pass
- else:
- raise SystemError("unrecognized file: %s" % tf)
- finally:
- lockfile.LockFile = self.saved_class
- def _test_acquire_helper(self, tbool):
- # As simple as it gets.
- lock = lockfile.LockFile(self._testfile(), threaded=tbool)
- lock.acquire()
- assert lock.i_am_locking()
- lock.release()
- assert not lock.is_locked()
- # def test_acquire_basic_threaded(self):
- # self._test_acquire_helper(True)
- def test_acquire_basic_unthreaded(self):
- self._test_acquire_helper(False)
- def _test_acquire_no_timeout_helper(self, tbool):
- # No timeout test
- e1, e2 = threading.Event(), threading.Event()
- t = _in_thread(self._lock_wait_unlock, e1, e2)
- e1.wait() # wait for thread t to acquire lock
- lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
- assert lock2.is_locked()
- if tbool:
- assert not lock2.i_am_locking()
- else:
- assert lock2.i_am_locking()
- try:
- lock2.acquire(timeout=-1)
- except lockfile.AlreadyLocked:
- pass
- else:
- lock2.release()
- raise AssertionError("did not raise AlreadyLocked in"
- " thread %s" %
- threading.current_thread().get_name())
- try:
- lock2.acquire(timeout=0)
- except lockfile.AlreadyLocked:
- pass
- else:
- lock2.release()
- raise AssertionError("did not raise AlreadyLocked in"
- " thread %s" %
- threading.current_thread().get_name())
- e2.set() # tell thread t to release lock
- t.join()
- # def test_acquire_no_timeout_threaded(self):
- # self._test_acquire_no_timeout_helper(True)
- # def test_acquire_no_timeout_unthreaded(self):
- # self._test_acquire_no_timeout_helper(False)
- def _test_acquire_timeout_helper(self, tbool):
- # Timeout test
- e1, e2 = threading.Event(), threading.Event()
- t = _in_thread(self._lock_wait_unlock, e1, e2)
- e1.wait() # wait for thread t to acquire lock
- lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
- assert lock2.is_locked()
- try:
- lock2.acquire(timeout=0.1)
- except lockfile.LockTimeout:
- pass
- else:
- lock2.release()
- raise AssertionError("did not raise LockTimeout in thread %s" %
- threading.current_thread().get_name())
- e2.set()
- t.join()
- def test_acquire_timeout_threaded(self):
- self._test_acquire_timeout_helper(True)
- def test_acquire_timeout_unthreaded(self):
- self._test_acquire_timeout_helper(False)
- def _test_context_timeout_helper(self, tbool):
- # Timeout test
- e1, e2 = threading.Event(), threading.Event()
- t = _in_thread(self._lock_wait_unlock, e1, e2)
- e1.wait() # wait for thread t to acquire lock
- lock2 = lockfile.LockFile(self._testfile(), threaded=tbool,
- timeout=0.2)
- assert lock2.is_locked()
- try:
- lock2.acquire()
- except lockfile.LockTimeout:
- pass
- else:
- lock2.release()
- raise AssertionError("did not raise LockTimeout in thread %s" %
- threading.current_thread().get_name())
- e2.set()
- t.join()
- def test_context_timeout_unthreaded(self):
- self._test_context_timeout_helper(False)
- def _test_release_basic_helper(self, tbool):
- lock = lockfile.LockFile(self._testfile(), threaded=tbool)
- lock.acquire()
- assert lock.is_locked()
- lock.release()
- assert not lock.is_locked()
- assert not lock.i_am_locking()
- try:
- lock.release()
- except lockfile.NotLocked:
- pass
- except lockfile.NotMyLock:
- raise AssertionError('unexpected exception: %s' %
- lockfile.NotMyLock)
- else:
- raise AssertionError('erroneously unlocked file')
- # def test_release_basic_threaded(self):
- # self._test_release_basic_helper(True)
- def test_release_basic_unthreaded(self):
- self._test_release_basic_helper(False)
- # def test_release_from_thread(self):
- # e1, e2 = threading.Event(), threading.Event()
- # t = _in_thread(self._lock_wait_unlock, e1, e2)
- # e1.wait()
- # lock2 = lockfile.LockFile(self._testfile(), threaded=False)
- # assert not lock2.i_am_locking()
- # try:
- # lock2.release()
- # except lockfile.NotMyLock:
- # pass
- # else:
- # raise AssertionError('erroneously unlocked a file locked'
- # ' by another thread.')
- # e2.set()
- # t.join()
- def _test_is_locked_helper(self, tbool):
- lock = lockfile.LockFile(self._testfile(), threaded=tbool)
- lock.acquire(timeout=2)
- assert lock.is_locked()
- lock.release()
- assert not lock.is_locked(), "still locked after release!"
- # def test_is_locked_threaded(self):
- # self._test_is_locked_helper(True)
- def test_is_locked_unthreaded(self):
- self._test_is_locked_helper(False)
- # def test_i_am_locking_threaded(self):
- # self._test_i_am_locking_helper(True)
- def test_i_am_locking_unthreaded(self):
- self._test_i_am_locking_helper(False)
- def _test_i_am_locking_helper(self, tbool):
- lock1 = lockfile.LockFile(self._testfile(), threaded=tbool)
- assert not lock1.is_locked()
- lock1.acquire()
- try:
- assert lock1.i_am_locking()
- lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
- assert lock2.is_locked()
- if tbool:
- assert not lock2.i_am_locking()
- finally:
- lock1.release()
- def _test_break_lock_helper(self, tbool):
- lock = lockfile.LockFile(self._testfile(), threaded=tbool)
- lock.acquire()
- assert lock.is_locked()
- lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
- assert lock2.is_locked()
- lock2.break_lock()
- assert not lock2.is_locked()
- try:
- lock.release()
- except lockfile.NotLocked:
- pass
- else:
- raise AssertionError('break lock failed')
- # def test_break_lock_threaded(self):
- # self._test_break_lock_helper(True)
- def test_break_lock_unthreaded(self):
- self._test_break_lock_helper(False)
- def _lock_wait_unlock(self, event1, event2):
- """Lock from another thread. Helper for tests."""
- l = lockfile.LockFile(self._testfile())
- l.acquire()
- try:
- event1.set() # we're in,
- event2.wait() # wait for boss's permission to leave
- finally:
- l.release()
- def test_enter(self):
- lock = lockfile.LockFile(self._testfile())
- lock.acquire()
- try:
- assert lock.is_locked(), "Not locked after acquire!"
- finally:
- lock.release()
- assert not lock.is_locked(), "still locked after release!"
- def test_decorator(self):
- @lockfile.locked(self._testfile())
- def func(a, b):
- return a + b
- assert func(4, 3) == 7
- def _in_thread(func, *args, **kwargs):
- """Execute func(*args, **kwargs) after dt seconds. Helper for tests."""
- def _f():
- func(*args, **kwargs)
- t = threading.Thread(target=_f, name='/*/*')
- t.setDaemon(True)
- t.start()
- return t
|