示例#1
0
    def _do_work(self):
        """Performs transport work and feeding log messages to log_queue.

    Returns:
        bool: always returns True
    """
        command_message = switchboard_process.get_message(self._command_queue,
                                                          timeout=0)
        if command_message:
            self._process_command_message(command_message)
        if self.transport.is_open():
            self._transport_open.set()
            self._transport_write()
            self._transport_read()
        else:
            # Mismatch between self._transport_open and self.transport.is_open
            # means that the transport closed unexpectedly on its own.
            closed_unexpectedly = self._transport_open.is_set()
            can_reopen = self.transport.get_property(props.AUTO_REOPEN, False)
            self._transport_open.clear()

            if closed_unexpectedly:
                self.transport.close()  # Clean up transport resources.
            if closed_unexpectedly and can_reopen:
                self._open_transport()
            else:
                time.sleep(self._read_timeout)
        return True
 def test_004_switchboard_process_get_message_nonempty_queue(self):
     """Test getting message from an empty queue."""
     in_queue = multiprocessing_utils.get_context().Queue()
     in_queue.put(_ECHO_MESSAGE)
     wait_for_queue_writes(in_queue)
     message = switchboard_process.get_message(in_queue, timeout=0)
     self.assertEqual(
         _ECHO_MESSAGE, message,
         "Expected {} from nonempty queue found {}".format(
             _ECHO_MESSAGE, message))
示例#3
0
 def test_get_message_from_queue(self):
     """Test a Switchboard process getting a message from a queue."""
     echo_message = "Message to be echoed"
     in_queue = multiprocessing_utils.get_context().Queue()
     in_queue.put(echo_message)
     switchboard_process.wait_for_queue_writes(in_queue)
     message = switchboard_process.get_message(in_queue, timeout=0)
     self.assertEqual(
         echo_message, message,
         "Expected {} from nonempty queue found {}".format(
             echo_message, message))
示例#4
0
  def _do_work(self):
    """Perform log writing work.

    Returns:
        bool: indicating process should continue running

    Note:
        This implementation always returns True
    """
    command_message = switchboard_process.get_message(
        self._command_queue, timeout=0)
    if command_message:
      self._process_command_message(command_message)
    log_line = switchboard_process.get_message(self._log_queue, timeout=0.01)
    if log_line:
      self._write_log_line(log_line)
      self._do_log_rotation()
    else:
      time.sleep(0.01)

    return True
示例#5
0
    def read(self, size=1, timeout=None):
        """Reads from mock read queue or raises an error if fail_read is True."""
        try:
            self.read_size.value = size
        except IOError:
            # Test probably failed and canceled the manager Event objects
            pass
        if self._fail_read:
            raise Exception(self._failure_message)

        if self._should_read():
            return switchboard_process.get_message(self.reads, timeout=timeout)
        else:
            return None
示例#6
0
    def get_raw_data(self, timeout=None):
        """Returns raw data message from optional raw data queue.

    Args:
        timeout (float): time to wait in seconds for incoming message to
          arrive.

    Raises:
        RuntimeError: if optional expect_queue was not provided when class
                      was initialized

    Returns:
        tuple: A tuple containing the raw_data_id and raw data received from
               the transport device when enabled or None if no data was
               available within timeout specified.
    """
        if self._raw_data_queue is None:
            raise RuntimeError("Device {} can't retrieve raw data. "
                               "No queue provided".format(self.device_name))

        return switchboard_process.get_message(self._raw_data_queue,
                                               timeout=timeout)
示例#7
0
 def _verify_command_split(self, original_command, a_queue):
     count = 0
     command = ""
     while not a_queue.empty():
         count += 1
         partial_command = switchboard_process.get_message(a_queue)
         self.assertIsInstance(partial_command, str)
         partial_command_len = len(partial_command)
         self.assertLessEqual(
             partial_command_len, transport_process._MAX_WRITE_BYTES,
             "Expected enqueued command of length {} found {}".format(
                 transport_process._MAX_WRITE_BYTES, partial_command_len))
         command += partial_command
     expected_count = 1 + (len(original_command) //
                           transport_process._MAX_WRITE_BYTES)
     self.assertEqual(
         expected_count, count,
         "Expected {} enqueued commands found {}".format(
             expected_count, count))
     self.assertEqual(
         original_command, command,
         "Expected {!r} to match original command {!r}".format(
             command, original_command))
示例#8
0
  def _do_work(self):
    """Perform log filtering work.

    Returns:
        bool: indicating process should continue running

    Note:
        This implementation always returns True
    """

    command_message = switchboard_process.get_message(
        self._command_queue, timeout=0)
    if command_message:
      self._process_command_message(command_message)
    if self._has_log_file(self._log_filename):
      if not self._is_open():
        self._open_log_file(self._log_filename)
        if hasattr(self, "_log_file") and self._log_file:
          self._open_event_file()
      self._filter_log_lines()
    else:
      time.sleep(0.001)

    return True
 def test_003_switchboard_process_get_message_empty_queue(self):
     """Test getting message from an empty queue."""
     in_queue = multiprocessing_utils.get_context().Queue()
     message = switchboard_process.get_message(in_queue, timeout=0)
     self.assertIsNone(
         message, "Expected None from empty queue found {}".format(message))
 def test_002_switchboard_process_get_message_bad_queue(self):
     """Test getting message from a bad queue."""
     with self.assertRaisesRegex(ValueError, "Invalid queue of"):
         switchboard_process.get_message(BadQueue())
 def test_001_switchboard_process_get_message_no_queue(self):
     """Test getting message from no queue."""
     with self.assertRaisesRegex(ValueError, "Invalid queue of"):
         switchboard_process.get_message(None)