compliancetest.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import os
  2. import threading
  3. import shutil
  4. import lockfile
  5. class ComplianceTest(object):
  6. def __init__(self):
  7. self.saved_class = lockfile.LockFile
  8. def _testfile(self):
  9. """Return platform-appropriate file. Helper for tests."""
  10. import tempfile
  11. return os.path.join(tempfile.gettempdir(), 'trash-%s' % os.getpid())
  12. def setup(self):
  13. lockfile.LockFile = self.class_to_test
  14. def teardown(self):
  15. try:
  16. tf = self._testfile()
  17. if os.path.isdir(tf):
  18. shutil.rmtree(tf)
  19. elif os.path.isfile(tf):
  20. os.unlink(tf)
  21. elif not os.path.exists(tf):
  22. pass
  23. else:
  24. raise SystemError("unrecognized file: %s" % tf)
  25. finally:
  26. lockfile.LockFile = self.saved_class
  27. def _test_acquire_helper(self, tbool):
  28. # As simple as it gets.
  29. lock = lockfile.LockFile(self._testfile(), threaded=tbool)
  30. lock.acquire()
  31. assert lock.i_am_locking()
  32. lock.release()
  33. assert not lock.is_locked()
  34. # def test_acquire_basic_threaded(self):
  35. # self._test_acquire_helper(True)
  36. def test_acquire_basic_unthreaded(self):
  37. self._test_acquire_helper(False)
  38. def _test_acquire_no_timeout_helper(self, tbool):
  39. # No timeout test
  40. e1, e2 = threading.Event(), threading.Event()
  41. t = _in_thread(self._lock_wait_unlock, e1, e2)
  42. e1.wait() # wait for thread t to acquire lock
  43. lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
  44. assert lock2.is_locked()
  45. if tbool:
  46. assert not lock2.i_am_locking()
  47. else:
  48. assert lock2.i_am_locking()
  49. try:
  50. lock2.acquire(timeout=-1)
  51. except lockfile.AlreadyLocked:
  52. pass
  53. else:
  54. lock2.release()
  55. raise AssertionError("did not raise AlreadyLocked in"
  56. " thread %s" %
  57. threading.current_thread().get_name())
  58. try:
  59. lock2.acquire(timeout=0)
  60. except lockfile.AlreadyLocked:
  61. pass
  62. else:
  63. lock2.release()
  64. raise AssertionError("did not raise AlreadyLocked in"
  65. " thread %s" %
  66. threading.current_thread().get_name())
  67. e2.set() # tell thread t to release lock
  68. t.join()
  69. # def test_acquire_no_timeout_threaded(self):
  70. # self._test_acquire_no_timeout_helper(True)
  71. # def test_acquire_no_timeout_unthreaded(self):
  72. # self._test_acquire_no_timeout_helper(False)
  73. def _test_acquire_timeout_helper(self, tbool):
  74. # Timeout test
  75. e1, e2 = threading.Event(), threading.Event()
  76. t = _in_thread(self._lock_wait_unlock, e1, e2)
  77. e1.wait() # wait for thread t to acquire lock
  78. lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
  79. assert lock2.is_locked()
  80. try:
  81. lock2.acquire(timeout=0.1)
  82. except lockfile.LockTimeout:
  83. pass
  84. else:
  85. lock2.release()
  86. raise AssertionError("did not raise LockTimeout in thread %s" %
  87. threading.current_thread().get_name())
  88. e2.set()
  89. t.join()
  90. def test_acquire_timeout_threaded(self):
  91. self._test_acquire_timeout_helper(True)
  92. def test_acquire_timeout_unthreaded(self):
  93. self._test_acquire_timeout_helper(False)
  94. def _test_context_timeout_helper(self, tbool):
  95. # Timeout test
  96. e1, e2 = threading.Event(), threading.Event()
  97. t = _in_thread(self._lock_wait_unlock, e1, e2)
  98. e1.wait() # wait for thread t to acquire lock
  99. lock2 = lockfile.LockFile(self._testfile(), threaded=tbool,
  100. timeout=0.2)
  101. assert lock2.is_locked()
  102. try:
  103. lock2.acquire()
  104. except lockfile.LockTimeout:
  105. pass
  106. else:
  107. lock2.release()
  108. raise AssertionError("did not raise LockTimeout in thread %s" %
  109. threading.current_thread().get_name())
  110. e2.set()
  111. t.join()
  112. def test_context_timeout_unthreaded(self):
  113. self._test_context_timeout_helper(False)
  114. def _test_release_basic_helper(self, tbool):
  115. lock = lockfile.LockFile(self._testfile(), threaded=tbool)
  116. lock.acquire()
  117. assert lock.is_locked()
  118. lock.release()
  119. assert not lock.is_locked()
  120. assert not lock.i_am_locking()
  121. try:
  122. lock.release()
  123. except lockfile.NotLocked:
  124. pass
  125. except lockfile.NotMyLock:
  126. raise AssertionError('unexpected exception: %s' %
  127. lockfile.NotMyLock)
  128. else:
  129. raise AssertionError('erroneously unlocked file')
  130. # def test_release_basic_threaded(self):
  131. # self._test_release_basic_helper(True)
  132. def test_release_basic_unthreaded(self):
  133. self._test_release_basic_helper(False)
  134. # def test_release_from_thread(self):
  135. # e1, e2 = threading.Event(), threading.Event()
  136. # t = _in_thread(self._lock_wait_unlock, e1, e2)
  137. # e1.wait()
  138. # lock2 = lockfile.LockFile(self._testfile(), threaded=False)
  139. # assert not lock2.i_am_locking()
  140. # try:
  141. # lock2.release()
  142. # except lockfile.NotMyLock:
  143. # pass
  144. # else:
  145. # raise AssertionError('erroneously unlocked a file locked'
  146. # ' by another thread.')
  147. # e2.set()
  148. # t.join()
  149. def _test_is_locked_helper(self, tbool):
  150. lock = lockfile.LockFile(self._testfile(), threaded=tbool)
  151. lock.acquire(timeout=2)
  152. assert lock.is_locked()
  153. lock.release()
  154. assert not lock.is_locked(), "still locked after release!"
  155. # def test_is_locked_threaded(self):
  156. # self._test_is_locked_helper(True)
  157. def test_is_locked_unthreaded(self):
  158. self._test_is_locked_helper(False)
  159. # def test_i_am_locking_threaded(self):
  160. # self._test_i_am_locking_helper(True)
  161. def test_i_am_locking_unthreaded(self):
  162. self._test_i_am_locking_helper(False)
  163. def _test_i_am_locking_helper(self, tbool):
  164. lock1 = lockfile.LockFile(self._testfile(), threaded=tbool)
  165. assert not lock1.is_locked()
  166. lock1.acquire()
  167. try:
  168. assert lock1.i_am_locking()
  169. lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
  170. assert lock2.is_locked()
  171. if tbool:
  172. assert not lock2.i_am_locking()
  173. finally:
  174. lock1.release()
  175. def _test_break_lock_helper(self, tbool):
  176. lock = lockfile.LockFile(self._testfile(), threaded=tbool)
  177. lock.acquire()
  178. assert lock.is_locked()
  179. lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
  180. assert lock2.is_locked()
  181. lock2.break_lock()
  182. assert not lock2.is_locked()
  183. try:
  184. lock.release()
  185. except lockfile.NotLocked:
  186. pass
  187. else:
  188. raise AssertionError('break lock failed')
  189. # def test_break_lock_threaded(self):
  190. # self._test_break_lock_helper(True)
  191. def test_break_lock_unthreaded(self):
  192. self._test_break_lock_helper(False)
  193. def _lock_wait_unlock(self, event1, event2):
  194. """Lock from another thread. Helper for tests."""
  195. l = lockfile.LockFile(self._testfile())
  196. l.acquire()
  197. try:
  198. event1.set() # we're in,
  199. event2.wait() # wait for boss's permission to leave
  200. finally:
  201. l.release()
  202. def test_enter(self):
  203. lock = lockfile.LockFile(self._testfile())
  204. lock.acquire()
  205. try:
  206. assert lock.is_locked(), "Not locked after acquire!"
  207. finally:
  208. lock.release()
  209. assert not lock.is_locked(), "still locked after release!"
  210. def test_decorator(self):
  211. @lockfile.locked(self._testfile())
  212. def func(a, b):
  213. return a + b
  214. assert func(4, 3) == 7
  215. def _in_thread(func, *args, **kwargs):
  216. """Execute func(*args, **kwargs) after dt seconds. Helper for tests."""
  217. def _f():
  218. func(*args, **kwargs)
  219. t = threading.Thread(target=_f, name='/*/*')
  220. t.setDaemon(True)
  221. t.start()
  222. return t