def test_cannotLockLocked(self): """ If a lock is currently locked, it cannot be locked again. """ lockf = self.mktemp() firstLock = lockfile.FilesystemLock(lockf) self.assertTrue(firstLock.lock()) secondLock = lockfile.FilesystemLock(lockf) self.assertFalse(secondLock.lock()) self.assertFalse(secondLock.locked)
def test_cleanlyRelease(self): """ If a lock is released cleanly, it can be re-acquired and the C{clean} and C{locked} attributes are set to C{True}. """ lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) self.assertTrue(lock.lock()) lock.unlock() self.assertFalse(lock.locked) lock = lockfile.FilesystemLock(lockf) self.assertTrue(lock.lock()) self.assertTrue(lock.clean) self.assertTrue(lock.locked)
def testBigLoop(self): lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) self.failUnless(lock.lock()) for i in xrange(500): self.failIf(lock.lock()) lock.unlock()
def testProtection(self): lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) self.failUnless(lock.lock()) self.failUnless(lock.clean) self.failIf(lock.lock()) lock.unlock()
def test_lockCleanedUpConcurrently(self): """ If a second process cleans up the lock after a first one checks the lock and finds that no process is holding it, the first process does not fail when it tries to clean up the lock. """ def fakeRmlink(name): rmlinkPatch.restore() # Pretend to be another process cleaning up the lock. lockfile.rmlink(lockf) # Fall back to the real implementation of rmlink. return lockfile.rmlink(name) rmlinkPatch = self.patch(lockfile, 'rmlink', fakeRmlink) def fakeKill(pid, signal): if signal != 0: raise OSError(errno.EPERM, None) if pid == 43125: raise OSError(errno.ESRCH, None) self.patch(lockfile, 'kill', fakeKill) lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) lockfile.symlink(str(43125), lockf) self.assertTrue(lock.lock()) self.assertTrue(lock.clean) self.assertTrue(lock.locked)
def test_lockReleasedBeforeCheck(self): """ If the lock is initially held but then released before it can be examined to determine if the process which held it still exists, it is acquired and the C{clean} and C{locked} attributes are set to C{True}. """ def fakeReadlink(name): # Pretend to be another process releasing the lock. lockfile.rmlink(lockf) # Fall back to the real implementation of readlink. readlinkPatch.restore() return lockfile.readlink(name) readlinkPatch = self.patch(lockfile, 'readlink', fakeReadlink) def fakeKill(pid, signal): if signal != 0: raise OSError(errno.EPERM, None) if pid == 43125: raise OSError(errno.ESRCH, None) self.patch(lockfile, 'kill', fakeKill) lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) lockfile.symlink(str(43125), lockf) self.assertTrue(lock.lock()) self.assertTrue(lock.clean) self.assertTrue(lock.locked)
def test_rmlinkError(self): """ An exception raised by L{rmlink} other than C{ENOENT} is passed up to the caller of L{FilesystemLock.lock}. """ def fakeRmlink(name): raise OSError(errno.ENOSYS, None) self.patch(lockfile, 'rmlink', fakeRmlink) def fakeKill(pid, signal): if signal != 0: raise OSError(errno.EPERM, None) if pid == 43125: raise OSError(errno.ESRCH, None) self.patch(lockfile, 'kill', fakeKill) lockf = self.mktemp() # Make it appear locked so it has to use readlink lockfile.symlink(str(43125), lockf) lock = lockfile.FilesystemLock(lockf) exc = self.assertRaises(OSError, lock.lock) self.assertEqual(exc.errno, errno.ENOSYS) self.assertFalse(lock.locked)
def startListening(self): """Create and bind my socket, and begin listening on it. This is called on unserialization, and must be called after creating a server to begin listening on the specified port. """ log.msg("%s starting on %r" % (self.factory.__class__, repr(self.port))) if self.wantPID: self.lockFile = lockfile.FilesystemLock(self.port + ".lock") if not self.lockFile.lock(): raise CannotListenError, (None, self.port, "Cannot acquire lock") else: if not self.lockFile.clean: try: # This is a best-attempt at cleaning up # left-over unix sockets on the filesystem. # If it fails, there's not much else we can # do. The bind() below will fail with an # exception that actually propegates. if stat.S_ISSOCK(os.stat(self.port).st_mode): os.remove(self.port) except: pass self.factory.doStart() try: skt = self.createInternetSocket() skt.bind(self.port) except socket.error, le: raise CannotListenError, (None, self.port, le)
def testIsLocked(self): lockf = self.mktemp() self.failIf(lockfile.isLocked(lockf)) lock = lockfile.FilesystemLock(lockf) self.failUnless(lock.lock()) self.failUnless(lockfile.isLocked(lockf)) lock.unlock() self.failIf(lockfile.isLocked(lockf))
def testBasics(self): lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) self.failUnless(lock.lock()) self.failUnless(lock.clean) lock.unlock() self.failUnless(lock.lock()) self.failUnless(lock.clean) lock.unlock()
def test_unlockOther(self): """ L{FilesystemLock.unlock} raises L{ValueError} if called for a lock which is held by a different process. """ lockf = self.mktemp() lockfile.symlink(str(os.getpid() + 1), lockf) lock = lockfile.FilesystemLock(lockf) self.assertRaises(ValueError, lock.unlock)
def _symlinkErrorTest(self, errno): def fakeSymlink(source, dest): raise OSError(errno, None) self.patch(lockfile, 'symlink', fakeSymlink) lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) exc = self.assertRaises(OSError, lock.lock) self.assertEqual(exc.errno, errno)
def test_cleanlyAcquire(self): """ If the lock has never been held, it can be acquired and the C{clean} and C{locked} attributes are set to C{True}. """ lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) self.assertTrue(lock.lock()) self.assertTrue(lock.clean) self.assertTrue(lock.locked)
def test_noKillCall(self): """ Verify that when L{lockfile.kill} does end up as None (e.g. on Windows without pywin32), it doesn't end up being called and raising a L{TypeError}. """ self.patch(lockfile, "kill", None) fl = lockfile.FilesystemLock(self.mktemp()) fl.lock() self.assertFalse(fl.lock())
def startListening(self): """ Create and bind my socket, and begin listening on it. This is called on unserialization, and must be called after creating a server to begin listening on the specified port. """ tcp._reservedFD.reserve() log.msg( "%s starting on %r" % ( self._getLogPrefix(self.factory), _coerceToFilesystemEncoding("", self.port), ) ) if self.wantPID: self.lockFile = lockfile.FilesystemLock(self.port + b".lock") if not self.lockFile.lock(): raise error.CannotListenError(None, self.port, "Cannot acquire lock") else: if not self.lockFile.clean: try: # This is a best-attempt at cleaning up # left-over unix sockets on the filesystem. # If it fails, there's not much else we can # do. The bind() below will fail with an # exception that actually propagates. if stat.S_ISSOCK(os.stat(self.port).st_mode): os.remove(self.port) except BaseException: pass self.factory.doStart() try: if self._preexistingSocket is not None: skt = self._preexistingSocket self._preexistingSocket = None else: skt = self.createInternetSocket() skt.bind(self.port) except OSError as le: raise error.CannotListenError(None, self.port, le) else: if _inFilesystemNamespace(self.port): # Make the socket readable and writable to the world. os.chmod(self.port, self.mode) skt.listen(self.backlog) self.connected = True self.socket = skt self.fileno = self.socket.fileno self.numberAccepts = 100 self.startReading()
def test_isLocked(self): """ L{isLocked} returns C{True} if the named lock is currently locked, C{False} otherwise. """ lockf = self.mktemp() self.assertFalse(lockfile.isLocked(lockf)) lock = lockfile.FilesystemLock(lockf) self.assertTrue(lock.lock()) self.assertTrue(lockfile.isLocked(lockf)) lock.unlock() self.assertFalse(lockfile.isLocked(lockf))
def _readlinkErrorTest(self, exceptionType, errno): def fakeReadlink(name): raise exceptionType(errno, None) self.patch(lockfile, 'readlink', fakeReadlink) lockf = self.mktemp() # Make it appear locked so it has to use readlink lockfile.symlink(str(43125), lockf) lock = lockfile.FilesystemLock(lockf) exc = self.assertRaises(exceptionType, lock.lock) self.assertEqual(exc.errno, errno) self.assertFalse(lock.locked)
def event_file(ivoid, dirname=None): # Return a file object into which we can write an event. # If a directory is specified, write into that; otherwise, use the cwd. # We use a lock to ensure we don't clobber other files with the same name. if not dirname: dirname = os.getcwd() fname = os.path.join(dirname, string_to_filename(ivoid)) lock = lockfile.FilesystemLock(string_to_filename(ivoid) + "-lock") lock.lock() try: while os.path.exists(fname): fname += "." with open(fname, 'w') as f: yield f finally: lock.unlock()
def test_lockReleasedDuringAcquireReadlink(self): """ If the lock is initially held but is released while an attempt is made to acquire it, the lock attempt fails and L{FilesystemLock.lock} returns C{False}. """ def fakeReadlink(name): # While another process is doing os.rmdir which the # Windows implementation of rmlink does, a readlink call # will fail with EACCES. raise IOError(errno.EACCES, None) readlinkPatch = self.patch(lockfile, 'readlink', fakeReadlink) lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) lockfile.symlink(str(43125), lockf) self.assertFalse(lock.lock()) self.assertFalse(lock.locked)
def test_killError(self): """ If L{kill} raises an exception other than L{OSError} with errno set to C{ESRCH}, the exception is passed up to the caller of L{FilesystemLock.lock}. """ def fakeKill(pid, signal): raise OSError(errno.EPERM, None) self.patch(lockfile, 'kill', fakeKill) lockf = self.mktemp() # Make it appear locked so it has to use readlink lockfile.symlink(str(43125), lockf) lock = lockfile.FilesystemLock(lockf) exc = self.assertRaises(OSError, lock.lock) self.assertEqual(exc.errno, errno.EPERM) self.assertFalse(lock.locked)
def test_lockReleasedDuringAcquireSymlink(self): """ If the lock is released while an attempt is made to acquire it, the lock attempt fails and C{FilesystemLock.lock} returns C{False}. This can happen on Windows when L{lockfile.symlink} fails with L{IOError} of C{EIO} because another process is in the middle of a call to L{os.rmdir} (implemented in terms of RemoveDirectory) which is not atomic. """ def fakeSymlink(src, dst): # While another process id doing os.rmdir which the Windows # implementation of rmlink does, a rename call will fail with EIO. raise OSError(errno.EIO, None) self.patch(lockfile, 'symlink', fakeSymlink) lockf = self.mktemp() lock = lockfile.FilesystemLock(lockf) self.assertFalse(lock.lock()) self.assertFalse(lock.locked)
def test_uncleanlyAcquire(self): """ If a lock was held by a process which no longer exists, it can be acquired, the C{clean} attribute is set to C{False}, and the C{locked} attribute is set to C{True}. """ owner = 12345 def fakeKill(pid, signal): if signal != 0: raise OSError(errno.EPERM, None) if pid == owner: raise OSError(errno.ESRCH, None) lockf = self.mktemp() self.patch(lockfile, 'kill', fakeKill) lockfile.symlink(str(owner), lockf) lock = lockfile.FilesystemLock(lockf) self.assertTrue(lock.lock()) self.assertFalse(lock.clean) self.assertTrue(lock.locked) self.assertEqual(lockfile.readlink(lockf), str(os.getpid()))
def __init__(self, config, lock=None): self.config = config self.lock = lock or lockfile.FilesystemLock(settings.LOCK_FILE)
def __init__(self, *args, **kwargs): TimedRotatingFileHandler.__init__(self, *args, **kwargs) self.dev, self.ino = -1, -1 self._statstream() self._flock = lockfile.FilesystemLock(self.baseFilename + "~lock")
def _make_flock(self): fn = os.path.join(self.pid_dir, self.appname + ".lock") return lockfile.FilesystemLock(fn)