PageRenderTime 114ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/desktop/core/ext-py/Twisted/twisted/test/test_lockfile.py

https://github.com/jcrobak/hue
Python | 422 lines | 215 code | 75 blank | 132 comment | 14 complexity | b722db5842e17cd4822f27353f30dfaf MD5 | raw file
  1. # Copyright (c) 2005 Divmod, Inc.
  2. # Copyright (c) 2008 Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Tests for L{twisted.python.lockfile}.
  6. """
  7. import os, errno
  8. from twisted.trial import unittest
  9. from twisted.python import lockfile
  10. from twisted.python.runtime import platform
  11. class UtilTests(unittest.TestCase):
  12. """
  13. Tests for the helper functions used to implement L{FilesystemLock}.
  14. """
  15. def test_symlinkEEXIST(self):
  16. """
  17. L{lockfile.symlink} raises L{OSError} with C{errno} set to L{EEXIST}
  18. when an attempt is made to create a symlink which already exists.
  19. """
  20. name = self.mktemp()
  21. lockfile.symlink('foo', name)
  22. exc = self.assertRaises(OSError, lockfile.symlink, 'foo', name)
  23. self.assertEqual(exc.errno, errno.EEXIST)
  24. def test_symlinkEIOWindows(self):
  25. """
  26. L{lockfile.symlink} raises L{OSError} with C{errno} set to L{EIO} when
  27. the underlying L{rename} call fails with L{EIO}.
  28. Renaming a file on Windows may fail if the target of the rename is in
  29. the process of being deleted (directory deletion appears not to be
  30. atomic).
  31. """
  32. name = self.mktemp()
  33. def fakeRename(src, dst):
  34. raise IOError(errno.EIO, None)
  35. self.patch(lockfile, 'rename', fakeRename)
  36. exc = self.assertRaises(IOError, lockfile.symlink, name, "foo")
  37. self.assertEqual(exc.errno, errno.EIO)
  38. if not platform.isWindows():
  39. test_symlinkEIOWindows.skip = (
  40. "special rename EIO handling only necessary and correct on "
  41. "Windows.")
  42. def test_readlinkENOENT(self):
  43. """
  44. L{lockfile.readlink} raises L{OSError} with C{errno} set to L{ENOENT}
  45. when an attempt is made to read a symlink which does not exist.
  46. """
  47. name = self.mktemp()
  48. exc = self.assertRaises(OSError, lockfile.readlink, name)
  49. self.assertEqual(exc.errno, errno.ENOENT)
  50. def test_readlinkEACCESWindows(self):
  51. """
  52. L{lockfile.readlink} raises L{OSError} with C{errno} set to L{EACCES}
  53. on Windows when the underlying file open attempt fails with C{EACCES}.
  54. Opening a file on Windows may fail if the path is inside a directory
  55. which is in the process of being deleted (directory deletion appears
  56. not to be atomic).
  57. """
  58. name = self.mktemp()
  59. def fakeOpen(path, mode):
  60. raise IOError(errno.EACCES, None)
  61. self.patch(lockfile, '_open', fakeOpen)
  62. exc = self.assertRaises(IOError, lockfile.readlink, name)
  63. self.assertEqual(exc.errno, errno.EACCES)
  64. if not platform.isWindows():
  65. test_readlinkEACCESWindows.skip = (
  66. "special readlink EACCES handling only necessary and correct on "
  67. "Windows.")
  68. def test_kill(self):
  69. """
  70. L{lockfile.kill} returns without error if passed the PID of a
  71. process which exists and signal C{0}.
  72. """
  73. lockfile.kill(os.getpid(), 0)
  74. def test_killESRCH(self):
  75. """
  76. L{lockfile.kill} raises L{OSError} with errno of L{ESRCH} if
  77. passed a PID which does not correspond to any process.
  78. """
  79. # Hopefully there is no process with PID 2 ** 31 - 1
  80. exc = self.assertRaises(OSError, lockfile.kill, 2 ** 31 - 1, 0)
  81. self.assertEqual(exc.errno, errno.ESRCH)
  82. class LockingTestCase(unittest.TestCase):
  83. def _symlinkErrorTest(self, errno):
  84. def fakeSymlink(source, dest):
  85. raise OSError(errno, None)
  86. self.patch(lockfile, 'symlink', fakeSymlink)
  87. lockf = self.mktemp()
  88. lock = lockfile.FilesystemLock(lockf)
  89. exc = self.assertRaises(OSError, lock.lock)
  90. self.assertEqual(exc.errno, errno)
  91. def test_symlinkError(self):
  92. """
  93. An exception raised by C{symlink} other than C{EEXIST} is passed up to
  94. the caller of L{FilesystemLock.lock}.
  95. """
  96. self._symlinkErrorTest(errno.ENOSYS)
  97. def test_symlinkErrorPOSIX(self):
  98. """
  99. An L{OSError} raised by C{symlink} on a POSIX platform with an errno of
  100. C{EACCES} or C{EIO} is passed to the caller of L{FilesystemLock.lock}.
  101. On POSIX, unlike on Windows, these are unexpected errors which cannot
  102. be handled by L{FilesystemLock}.
  103. """
  104. self._symlinkErrorTest(errno.EACCES)
  105. self._symlinkErrorTest(errno.EIO)
  106. if platform.isWindows():
  107. test_symlinkErrorPOSIX.skip = (
  108. "POSIX-specific error propagation not expected on Windows.")
  109. def test_cleanlyAcquire(self):
  110. """
  111. If the lock has never been held, it can be acquired and the C{clean}
  112. and C{locked} attributes are set to C{True}.
  113. """
  114. lockf = self.mktemp()
  115. lock = lockfile.FilesystemLock(lockf)
  116. self.assertTrue(lock.lock())
  117. self.assertTrue(lock.clean)
  118. self.assertTrue(lock.locked)
  119. def test_cleanlyRelease(self):
  120. """
  121. If a lock is released cleanly, it can be re-acquired and the C{clean}
  122. and C{locked} attributes are set to C{True}.
  123. """
  124. lockf = self.mktemp()
  125. lock = lockfile.FilesystemLock(lockf)
  126. self.assertTrue(lock.lock())
  127. lock.unlock()
  128. self.assertFalse(lock.locked)
  129. lock = lockfile.FilesystemLock(lockf)
  130. self.assertTrue(lock.lock())
  131. self.assertTrue(lock.clean)
  132. self.assertTrue(lock.locked)
  133. def test_cannotLockLocked(self):
  134. """
  135. If a lock is currently locked, it cannot be locked again.
  136. """
  137. lockf = self.mktemp()
  138. firstLock = lockfile.FilesystemLock(lockf)
  139. self.assertTrue(firstLock.lock())
  140. secondLock = lockfile.FilesystemLock(lockf)
  141. self.assertFalse(secondLock.lock())
  142. self.assertFalse(secondLock.locked)
  143. def test_uncleanlyAcquire(self):
  144. """
  145. If a lock was held by a process which no longer exists, it can be
  146. acquired, the C{clean} attribute is set to C{False}, and the
  147. C{locked} attribute is set to C{True}.
  148. """
  149. owner = 12345
  150. def fakeKill(pid, signal):
  151. if signal != 0:
  152. raise OSError(errno.EPERM, None)
  153. if pid == owner:
  154. raise OSError(errno.ESRCH, None)
  155. lockf = self.mktemp()
  156. self.patch(lockfile, 'kill', fakeKill)
  157. lockfile.symlink(str(owner), lockf)
  158. lock = lockfile.FilesystemLock(lockf)
  159. self.assertTrue(lock.lock())
  160. self.assertFalse(lock.clean)
  161. self.assertTrue(lock.locked)
  162. self.assertEqual(lockfile.readlink(lockf), str(os.getpid()))
  163. def test_lockReleasedBeforeCheck(self):
  164. """
  165. If the lock is initially held but then released before it can be
  166. examined to determine if the process which held it still exists, it is
  167. acquired and the C{clean} and C{locked} attributes are set to C{True}.
  168. """
  169. def fakeReadlink(name):
  170. # Pretend to be another process releasing the lock.
  171. lockfile.rmlink(lockf)
  172. # Fall back to the real implementation of readlink.
  173. readlinkPatch.restore()
  174. return lockfile.readlink(name)
  175. readlinkPatch = self.patch(lockfile, 'readlink', fakeReadlink)
  176. def fakeKill(pid, signal):
  177. if signal != 0:
  178. raise OSError(errno.EPERM, None)
  179. if pid == 43125:
  180. raise OSError(errno.ESRCH, None)
  181. self.patch(lockfile, 'kill', fakeKill)
  182. lockf = self.mktemp()
  183. lock = lockfile.FilesystemLock(lockf)
  184. lockfile.symlink(str(43125), lockf)
  185. self.assertTrue(lock.lock())
  186. self.assertTrue(lock.clean)
  187. self.assertTrue(lock.locked)
  188. def test_lockReleasedDuringAcquireSymlink(self):
  189. """
  190. If the lock is released while an attempt is made to acquire
  191. it, the lock attempt fails and C{FilesystemLock.lock} returns
  192. C{False}. This can happen on Windows when L{lockfile.symlink}
  193. fails with L{IOError} of C{EIO} because another process is in
  194. the middle of a call to L{os.rmdir} (implemented in terms of
  195. RemoveDirectory) which is not atomic.
  196. """
  197. def fakeSymlink(src, dst):
  198. # While another process id doing os.rmdir which the Windows
  199. # implementation of rmlink does, a rename call will fail with EIO.
  200. raise OSError(errno.EIO, None)
  201. self.patch(lockfile, 'symlink', fakeSymlink)
  202. lockf = self.mktemp()
  203. lock = lockfile.FilesystemLock(lockf)
  204. self.assertFalse(lock.lock())
  205. self.assertFalse(lock.locked)
  206. if not platform.isWindows():
  207. test_lockReleasedDuringAcquireSymlink.skip = (
  208. "special rename EIO handling only necessary and correct on "
  209. "Windows.")
  210. def test_lockReleasedDuringAcquireReadlink(self):
  211. """
  212. If the lock is initially held but is released while an attempt
  213. is made to acquire it, the lock attempt fails and
  214. L{FilesystemLock.lock} returns C{False}.
  215. """
  216. def fakeReadlink(name):
  217. # While another process is doing os.rmdir which the
  218. # Windows implementation of rmlink does, a readlink call
  219. # will fail with EACCES.
  220. raise IOError(errno.EACCES, None)
  221. readlinkPatch = self.patch(lockfile, 'readlink', fakeReadlink)
  222. lockf = self.mktemp()
  223. lock = lockfile.FilesystemLock(lockf)
  224. lockfile.symlink(str(43125), lockf)
  225. self.assertFalse(lock.lock())
  226. self.assertFalse(lock.locked)
  227. if not platform.isWindows():
  228. test_lockReleasedDuringAcquireReadlink.skip = (
  229. "special readlink EACCES handling only necessary and correct on "
  230. "Windows.")
  231. def _readlinkErrorTest(self, exceptionType, errno):
  232. def fakeReadlink(name):
  233. raise exceptionType(errno, None)
  234. self.patch(lockfile, 'readlink', fakeReadlink)
  235. lockf = self.mktemp()
  236. # Make it appear locked so it has to use readlink
  237. lockfile.symlink(str(43125), lockf)
  238. lock = lockfile.FilesystemLock(lockf)
  239. exc = self.assertRaises(exceptionType, lock.lock)
  240. self.assertEqual(exc.errno, errno)
  241. self.assertFalse(lock.locked)
  242. def test_readlinkError(self):
  243. """
  244. An exception raised by C{readlink} other than C{ENOENT} is passed up to
  245. the caller of L{FilesystemLock.lock}.
  246. """
  247. self._readlinkErrorTest(OSError, errno.ENOSYS)
  248. self._readlinkErrorTest(IOError, errno.ENOSYS)
  249. def test_readlinkErrorPOSIX(self):
  250. """
  251. Any L{IOError} raised by C{readlink} on a POSIX platform passed to the
  252. caller of L{FilesystemLock.lock}.
  253. On POSIX, unlike on Windows, these are unexpected errors which cannot
  254. be handled by L{FilesystemLock}.
  255. """
  256. self._readlinkErrorTest(IOError, errno.ENOSYS)
  257. self._readlinkErrorTest(IOError, errno.EACCES)
  258. if platform.isWindows():
  259. test_readlinkErrorPOSIX.skip = (
  260. "POSIX-specific error propagation not expected on Windows.")
  261. def test_lockCleanedUpConcurrently(self):
  262. """
  263. If a second process cleans up the lock after a first one checks the
  264. lock and finds that no process is holding it, the first process does
  265. not fail when it tries to clean up the lock.
  266. """
  267. def fakeRmlink(name):
  268. rmlinkPatch.restore()
  269. # Pretend to be another process cleaning up the lock.
  270. lockfile.rmlink(lockf)
  271. # Fall back to the real implementation of rmlink.
  272. return lockfile.rmlink(name)
  273. rmlinkPatch = self.patch(lockfile, 'rmlink', fakeRmlink)
  274. def fakeKill(pid, signal):
  275. if signal != 0:
  276. raise OSError(errno.EPERM, None)
  277. if pid == 43125:
  278. raise OSError(errno.ESRCH, None)
  279. self.patch(lockfile, 'kill', fakeKill)
  280. lockf = self.mktemp()
  281. lock = lockfile.FilesystemLock(lockf)
  282. lockfile.symlink(str(43125), lockf)
  283. self.assertTrue(lock.lock())
  284. self.assertTrue(lock.clean)
  285. self.assertTrue(lock.locked)
  286. def test_rmlinkError(self):
  287. """
  288. An exception raised by L{rmlink} other than C{ENOENT} is passed up
  289. to the caller of L{FilesystemLock.lock}.
  290. """
  291. def fakeRmlink(name):
  292. raise OSError(errno.ENOSYS, None)
  293. self.patch(lockfile, 'rmlink', fakeRmlink)
  294. def fakeKill(pid, signal):
  295. if signal != 0:
  296. raise OSError(errno.EPERM, None)
  297. if pid == 43125:
  298. raise OSError(errno.ESRCH, None)
  299. self.patch(lockfile, 'kill', fakeKill)
  300. lockf = self.mktemp()
  301. # Make it appear locked so it has to use readlink
  302. lockfile.symlink(str(43125), lockf)
  303. lock = lockfile.FilesystemLock(lockf)
  304. exc = self.assertRaises(OSError, lock.lock)
  305. self.assertEqual(exc.errno, errno.ENOSYS)
  306. self.assertFalse(lock.locked)
  307. def test_killError(self):
  308. """
  309. If L{kill} raises an exception other than L{OSError} with errno set to
  310. C{ESRCH}, the exception is passed up to the caller of
  311. L{FilesystemLock.lock}.
  312. """
  313. def fakeKill(pid, signal):
  314. raise OSError(errno.EPERM, None)
  315. self.patch(lockfile, 'kill', fakeKill)
  316. lockf = self.mktemp()
  317. # Make it appear locked so it has to use readlink
  318. lockfile.symlink(str(43125), lockf)
  319. lock = lockfile.FilesystemLock(lockf)
  320. exc = self.assertRaises(OSError, lock.lock)
  321. self.assertEqual(exc.errno, errno.EPERM)
  322. self.assertFalse(lock.locked)
  323. def test_unlockOther(self):
  324. """
  325. L{FilesystemLock.unlock} raises L{ValueError} if called for a lock
  326. which is held by a different process.
  327. """
  328. lockf = self.mktemp()
  329. lockfile.symlink(str(os.getpid() + 1), lockf)
  330. lock = lockfile.FilesystemLock(lockf)
  331. self.assertRaises(ValueError, lock.unlock)
  332. def test_isLocked(self):
  333. """
  334. L{isLocked} returns C{True} if the named lock is currently locked,
  335. C{False} otherwise.
  336. """
  337. lockf = self.mktemp()
  338. self.assertFalse(lockfile.isLocked(lockf))
  339. lock = lockfile.FilesystemLock(lockf)
  340. self.assertTrue(lock.lock())
  341. self.assertTrue(lockfile.isLocked(lockf))
  342. lock.unlock()
  343. self.assertFalse(lockfile.isLocked(lockf))