Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_lockfile.py 15KB

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