Exemple #1
0
def _find_matching_connections(bus, connection_matcher, process=None):
    """Returns a list of connection names that have passed the
    connection_matcher.

    :param dbus_bus: A DBus bus object to search
        (i.e. SessionBus, SystemBus or BusConnection)
    :param connection_matcher: Callable that takes a connection name and
        returns True if it is what we're looking for, False otherwise.
    :param process: (optional) A process object that we're looking for it's
        dbus connection.
        Used to ensure that the process is in fact still running
        while we're searching for it.

    """
    for _ in Timeout.default():
        _get_child_pids.reset_cache()
        _raise_if_process_has_exited(process)

        connections = bus.list_names()

        valid_connections = [
            c for c in connections if connection_matcher((bus, c))
        ]

        if len(valid_connections) >= 1:
            return _dedupe_connections_on_pid(valid_connections, bus)

    return []
Exemple #2
0
 def test_medium_elapsed_time_increases(self):
     with sleep.mocked():
         last_elapsed = None
         for elapsed in Timeout.default():
             if last_elapsed is not None:
                 self.assertThat(elapsed, GreaterThan(last_elapsed))
             else:
                 self.assertEqual(elapsed, 0.0)
             last_elapsed = elapsed
Exemple #3
0
def _kill_process(process):
    """Kill the process, and return the stdout, stderr and return code."""
    stdout_parts = []
    stderr_parts = []
    _logger.info("waiting for process to exit.")
    _attempt_kill_pid(process.pid)
    for _ in Timeout.default():
        tmp_out, tmp_err = process.communicate()
        if isinstance(tmp_out, bytes):
            tmp_out = tmp_out.decode('utf-8', errors='replace')
        if isinstance(tmp_err, bytes):
            tmp_err = tmp_err.decode('utf-8', errors='replace')
        stdout_parts.append(tmp_out)
        stderr_parts.append(tmp_err)
        if not _is_process_running(process.pid):
            break
    else:
        _logger.info("Killing process group, since it hasn't exited after "
                     "10 seconds.")
        _attempt_kill_pid(process.pid, signal.SIGKILL)
    return ''.join(stdout_parts), ''.join(stderr_parts), process.returncode
Exemple #4
0
    def _open_window(self, app_name, files, locale):
        """Open a new 'app_name' window, returning the window instance or None.

        Raises an AssertionError if this creates more than one window.

        """
        existing_windows = self.get_open_windows_by_application(app_name)

        if locale:
            os.putenv("LC_ALL", locale)
            addCleanup(os.unsetenv, "LC_ALL")
            _logger.info(
                "Starting application '%s' with files %r in locale %s",
                app_name, files, locale)
        else:
            _logger.info("Starting application '%s' with files %r", app_name,
                         files)

        app = self.KNOWN_APPS[app_name]
        self._launch_application(app['desktop-file'], files)
        apps = self.get_running_applications_by_desktop_file(
            app['desktop-file'])

        for _ in Timeout.default():
            try:
                new_windows = []
                [new_windows.extend(a.get_windows()) for a in apps]
                filter_fn = lambda w: w.x_id not in [
                    c.x_id for c in existing_windows
                ]
                new_wins = list(filter(filter_fn, new_windows))
                if new_wins:
                    assert len(new_wins) == 1
                    return new_wins[0]
            except dbus.DBusException:
                pass
        return None
Exemple #5
0
 def test_medium_timeout_final_call(self):
     set_default_timeout_period(0.0)
     self.assertThat(len(list(Timeout.default())), Equals(1))
Exemple #6
0
 def test_medium_sleeps_for_correct_time(self):
     with sleep.mocked() as mocked_sleep:
         for _ in Timeout.default():
             pass
         self.assertEqual(10.0, mocked_sleep.total_time_slept())