def test_check_running_pid(self): executive = Executive() self.assertTrue(executive.check_running_pid(os.getpid())) # Maximum pid number on Linux is 32768 by default self.assertFalse(executive.check_running_pid(100000))
def serial_test_check_running_pid(self): executive = Executive() self.assertTrue(executive.check_running_pid(os.getpid())) # Maximum pid number on Linux is 32768 by default self.assertFalse(executive.check_running_pid(100000))
def test_check_running_pid(self): executive = Executive() self.assertTrue(executive.check_running_pid(os.getpid())) # According to the proc(5) man page, on 64-bit linux systems, # pid_max can be set to any value up to 2^22 (approximately 4 million). self.assertFalse(executive.check_running_pid(5000000))
class HttpLock(object): def __init__(self, lock_path, lock_file_prefix="WebKitHttpd.lock.", guard_lock="WebKit.lock"): self._lock_path = lock_path if not self._lock_path: self._lock_path = tempfile.gettempdir() self._lock_file_prefix = lock_file_prefix self._lock_file_path_prefix = os.path.join(self._lock_path, self._lock_file_prefix) self._guard_lock_file = os.path.join(self._lock_path, guard_lock) self._guard_lock = FileLock(self._guard_lock_file) self._process_lock_file_name = "" self._executive = Executive() def cleanup_http_lock(self): """Delete the lock file if exists.""" if os.path.exists(self._process_lock_file_name): _log.debug("Removing lock file: %s" % self._process_lock_file_name) FileSystem().remove(self._process_lock_file_name) def _extract_lock_number(self, lock_file_name): """Return the lock number from lock file.""" prefix_length = len(self._lock_file_path_prefix) return int(lock_file_name[prefix_length:]) def _lock_file_list(self): """Return the list of lock files sequentially.""" lock_list = glob.glob(self._lock_file_path_prefix + '*') lock_list.sort(key=self._extract_lock_number) return lock_list def _next_lock_number(self): """Return the next available lock number.""" lock_list = self._lock_file_list() if not lock_list: return 0 return self._extract_lock_number(lock_list[-1]) + 1 def _curent_lock_pid(self): """Return with the current lock pid. If the lock is not valid it deletes the lock file.""" lock_list = self._lock_file_list() if not lock_list: return try: current_lock_file = open(lock_list[0], 'r') current_pid = current_lock_file.readline() current_lock_file.close() if not (current_pid and self._executive.check_running_pid(int(current_pid))): _log.debug("Removing stuck lock file: %s" % lock_list[0]) FileSystem().remove(lock_list[0]) return except (IOError, OSError): return return int(current_pid) def _create_lock_file(self): """The lock files are used to schedule the running test sessions in first come first served order. The guard lock ensures that the lock numbers are sequential.""" if not os.path.exists(self._lock_path): _log.debug("Lock directory does not exist: %s" % self._lock_path) return False if not self._guard_lock.acquire_lock(): _log.debug("Guard lock timed out!") return False self._process_lock_file_name = (self._lock_file_path_prefix + str(self._next_lock_number())) _log.debug("Creating lock file: %s" % self._process_lock_file_name) lock_file = open(self._process_lock_file_name, 'w') lock_file.write(str(os.getpid())) lock_file.close() self._guard_lock.release_lock() return True def wait_for_httpd_lock(self): """Create a lock file and wait until it's turn comes. If something goes wrong it wont do any locking.""" if not self._create_lock_file(): _log.debug("Warning, http locking failed!") return while self._curent_lock_pid() != os.getpid(): time.sleep(1)
class HttpLock(object): def __init__(self, lock_path, lock_file_prefix="WebKitHttpd.lock.", guard_lock="WebKit.lock"): self._lock_path = lock_path if not self._lock_path: self._lock_path = tempfile.gettempdir() self._lock_file_prefix = lock_file_prefix self._lock_file_path_prefix = os.path.join(self._lock_path, self._lock_file_prefix) self._guard_lock_file = os.path.join(self._lock_path, guard_lock) self._guard_lock = FileLock(self._guard_lock_file) self._process_lock_file_name = "" self._executive = Executive() def cleanup_http_lock(self): """Delete the lock file if exists.""" if os.path.exists(self._process_lock_file_name): _log.debug("Removing lock file: %s" % self._process_lock_file_name) os.unlink(self._process_lock_file_name) def _extract_lock_number(self, lock_file_name): """Return the lock number from lock file.""" prefix_length = len(self._lock_file_path_prefix) return int(lock_file_name[prefix_length:]) def _lock_file_list(self): """Return the list of lock files sequentially.""" lock_list = glob.glob(self._lock_file_path_prefix + '*') lock_list.sort(key=self._extract_lock_number) return lock_list def _next_lock_number(self): """Return the next available lock number.""" lock_list = self._lock_file_list() if not lock_list: return 0 return self._extract_lock_number(lock_list[-1]) + 1 def _curent_lock_pid(self): """Return with the current lock pid. If the lock is not valid it deletes the lock file.""" lock_list = self._lock_file_list() if not lock_list: return try: current_lock_file = open(lock_list[0], 'r') current_pid = current_lock_file.readline() current_lock_file.close() if not (current_pid and self._executive.check_running_pid(int(current_pid))): _log.debug("Removing stuck lock file: %s" % lock_list[0]) os.unlink(lock_list[0]) return except (IOError, OSError): return return int(current_pid) def _create_lock_file(self): """The lock files are used to schedule the running test sessions in first come first served order. The guard lock ensures that the lock numbers are sequential.""" if not os.path.exists(self._lock_path): _log.debug("Lock directory does not exist: %s" % self._lock_path) return False if not self._guard_lock.acquire_lock(): _log.debug("Guard lock timed out!") return False self._process_lock_file_name = (self._lock_file_path_prefix + str(self._next_lock_number())) _log.debug("Creating lock file: %s" % self._process_lock_file_name) lock_file = open(self._process_lock_file_name, 'w') lock_file.write(str(os.getpid())) lock_file.close() self._guard_lock.release_lock() return True def wait_for_httpd_lock(self): """Create a lock file and wait until it's turn comes. If something goes wrong it wont do any locking.""" if not self._create_lock_file(): _log.debug("Warning, http locking failed!") return while self._curent_lock_pid() != os.getpid(): time.sleep(1)