class XvfbDriver(Driver): def __init__(self, *args, **kwargs): Driver.__init__(self, *args, **kwargs) self._guard_lock = None self._startup_delay_secs = 1.0 def _next_free_display(self): running_pids = self._port.host.executive.run_command(['ps', '-eo', 'comm,command']) reserved_screens = set() for pid in running_pids.split('\n'): match = re.match('(X|Xvfb|Xorg)\s+.*\s:(?P<screen_number>\d+)', pid) if match: reserved_screens.add(int(match.group('screen_number'))) for i in range(99): if i not in reserved_screens: _guard_lock_file = self._port.host.filesystem.join('/tmp', 'WebKitXvfb.lock.%i' % i) self._guard_lock = FileLock(_guard_lock_file) if self._guard_lock.acquire_lock(): return i def _start(self, pixel_tests, per_test_args): # Use even displays for pixel tests and odd ones otherwise. When pixel tests are disabled, # DriverProxy creates two drivers, one for normal and the other for ref tests. Both have # the same worker number, so this prevents them from using the same Xvfb instance. display_id = self._next_free_display() self._lock_file = "/tmp/.X%d-lock" % display_id run_xvfb = ["Xvfb", ":%d" % display_id, "-screen", "0", "800x600x24", "-nolisten", "tcp"] with open(os.devnull, 'w') as devnull: self._xvfb_process = self._port.host.executive.popen(run_xvfb, stderr=devnull) # Crashes intend to occur occasionally in the first few tests that are run through each # worker because the Xvfb display isn't ready yet. Halting execution a bit should avoid that. time.sleep(self._startup_delay_secs) server_name = self._port.driver_name() environment = self._port.setup_environ_for_server(server_name) # We must do this here because the DISPLAY number depends on _worker_number environment['DISPLAY'] = ":%d" % display_id # Drivers should use separate application cache locations environment['XDG_CACHE_HOME'] = self._port.host.filesystem.join(self._port.results_directory(), '%s-appcache-%d' % (server_name, self._worker_number)) self._driver_tempdir = self._port._filesystem.mkdtemp(prefix='%s-' % self._port.driver_name()) environment['DUMPRENDERTREE_TEMP'] = str(self._driver_tempdir) environment['LOCAL_RESOURCE_ROOT'] = self._port.layout_tests_dir() self._crashed_process_name = None self._crashed_pid = None self._server_process = self._port._server_process_constructor(self._port, server_name, self.cmd_line(pixel_tests, per_test_args), environment) self._server_process.start() def stop(self): super(XvfbDriver, self).stop() if self._guard_lock: self._guard_lock.release_lock() self._guard_lock = None if getattr(self, '_xvfb_process', None): self._port.host.executive.kill_process(self._xvfb_process.pid) self._xvfb_process = None if self._port.host.filesystem.exists(self._lock_file): self._port.host.filesystem.remove(self._lock_file)
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 _next_free_display(self): running_pids = self._port.host.executive.run_command( ['ps', '-eo', 'comm,command']) reserved_screens = set() for pid in running_pids.split('\n'): match = re.match('(X|Xvfb|Xorg)\s+.*\s:(?P<screen_number>\d+)', pid) if match: reserved_screens.add(int(match.group('screen_number'))) for i in range(99): if i not in reserved_screens: _guard_lock_file = self._port.host.filesystem.join( '/tmp', 'WebKitXvfb.lock.%i' % i) self._guard_lock = FileLock(_guard_lock_file) if self._guard_lock.acquire_lock(): return i
def __init__(self, lock_path, lock_file_prefix="WebKitHttpd.lock.", guard_lock="WebKit.lock", filesystem=None, executive=None): self._executive = executive or Executive() self._filesystem = filesystem or FileSystem() self._lock_path = lock_path if not self._lock_path: # FIXME: FileSystem should have an accessor for tempdir() self._lock_path = tempfile.gettempdir() self._lock_file_prefix = lock_file_prefix self._lock_file_path_prefix = self._filesystem.join(self._lock_path, self._lock_file_prefix) self._guard_lock_file = self._filesystem.join(self._lock_path, guard_lock) self._guard_lock = FileLock(self._guard_lock_file) self._process_lock_file_name = ""
def _next_free_display(self): running_pids = self._port.host.executive.run_command(['ps', '-eo', 'comm,command']) reserved_screens = set() for pid in running_pids.split('\n'): match = re.match('(X|Xvfb|Xorg)\s+.*\s:(?P<screen_number>\d+)', pid) if match: reserved_screens.add(int(match.group('screen_number'))) for i in range(99): if i not in reserved_screens: _guard_lock_file = self._port.host.filesystem.join('/tmp', 'WebKitXvfb.lock.%i' % i) self._guard_lock = FileLock(_guard_lock_file) if self._guard_lock.acquire_lock(): return i
def setUp(self): self._lock_name = "TestWebKit" + str(os.getpid()) + ".lock" self._lock_path = os.path.join(tempfile.gettempdir(), self._lock_name) self._file_lock1 = FileLock(self._lock_path, 0.1) self._file_lock2 = FileLock(self._lock_path, 0.1)
class FileLockTest(unittest.TestCase): def setUp(self): self._lock_name = "TestWebKit" + str(os.getpid()) + ".lock" self._lock_path = os.path.join(tempfile.gettempdir(), self._lock_name) self._file_lock1 = FileLock(self._lock_path, 0.1) self._file_lock2 = FileLock(self._lock_path, 0.1) def tearDown(self): self._file_lock1.release_lock() self._file_lock2.release_lock() def test_lock_lifecycle(self): # Create the lock. self._file_lock1.acquire_lock() self.assertTrue(os.path.exists(self._lock_path)) # Try to lock again. self.assertFalse(self._file_lock2.acquire_lock()) # Release the lock. self._file_lock1.release_lock() self.assertFalse(os.path.exists(self._lock_path)) def test_stuck_lock(self): open(self._lock_path, 'w').close() self._file_lock1.acquire_lock() self._file_lock1.release_lock()
class XvfbDriver(Driver): def __init__(self, *args, **kwargs): Driver.__init__(self, *args, **kwargs) self._guard_lock = None self._startup_delay_secs = 1.0 def _next_free_display(self): running_pids = self._port.host.executive.run_command( ['ps', '-eo', 'comm,command']) reserved_screens = set() for pid in running_pids.split('\n'): match = re.match('(X|Xvfb|Xorg)\s+.*\s:(?P<screen_number>\d+)', pid) if match: reserved_screens.add(int(match.group('screen_number'))) for i in range(99): if i not in reserved_screens: _guard_lock_file = self._port.host.filesystem.join( '/tmp', 'WebKitXvfb.lock.%i' % i) self._guard_lock = FileLock(_guard_lock_file) if self._guard_lock.acquire_lock(): return i def _start(self, pixel_tests, per_test_args): # Use even displays for pixel tests and odd ones otherwise. When pixel tests are disabled, # DriverProxy creates two drivers, one for normal and the other for ref tests. Both have # the same worker number, so this prevents them from using the same Xvfb instance. display_id = self._next_free_display() self._lock_file = "/tmp/.X%d-lock" % display_id run_xvfb = [ "Xvfb", ":%d" % display_id, "-screen", "0", "800x600x24", "-nolisten", "tcp" ] with open(os.devnull, 'w') as devnull: self._xvfb_process = self._port.host.executive.popen( run_xvfb, stderr=devnull) # Crashes intend to occur occasionally in the first few tests that are run through each # worker because the Xvfb display isn't ready yet. Halting execution a bit should avoid that. time.sleep(self._startup_delay_secs) server_name = self._port.driver_name() environment = self._port.setup_environ_for_server(server_name) # We must do this here because the DISPLAY number depends on _worker_number environment['DISPLAY'] = ":%d" % display_id self._driver_tempdir = self._port._filesystem.mkdtemp( prefix='%s-' % self._port.driver_name()) environment['DUMPRENDERTREE_TEMP'] = str(self._driver_tempdir) environment['LOCAL_RESOURCE_ROOT'] = self._port.layout_tests_dir() # Currently on WebKit2, there is no API for setting the application # cache directory. Each worker should have it's own and it should be # cleaned afterwards, so we set it to inside the temporary folder by # prepending XDG_CACHE_HOME with DUMPRENDERTREE_TEMP. environment['XDG_CACHE_HOME'] = self._port.host.filesystem.join( str(self._driver_tempdir), 'appcache') self._crashed_process_name = None self._crashed_pid = None self._server_process = self._port._server_process_constructor( self._port, server_name, self.cmd_line(pixel_tests, per_test_args), environment) self._server_process.start() def stop(self): super(XvfbDriver, self).stop() if self._guard_lock: self._guard_lock.release_lock() self._guard_lock = None if getattr(self, '_xvfb_process', None): self._port.host.executive.kill_process(self._xvfb_process.pid) self._xvfb_process = None if self._port.host.filesystem.exists(self._lock_file): self._port.host.filesystem.remove(self._lock_file)
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)