def test_target_up_nothing_to_read(self):
     with patch("socket.socket.recv", return_value='') as mock_recv:
         callback = Mock(return_value="should not be called")
         self.assertFalse(
             py_dlt_client_main_loop(self.client, callback=callback))
         self.assertEqual(mock_recv.call_count, 1)
         self.assertFalse(callback.called)
 def test_target_up_nothing_to_read(self):
     with patch.object(self.client._connected_socket,
                       'recv',
                       return_value=b"") as mock_recv:
         callback = Mock(return_value="should not be called")
         self.assertFalse(
             py_dlt_client_main_loop(self.client, callback=callback))
         self.assertEqual(mock_recv.call_count, 1)
         self.assertFalse(callback.called)
 def test_exit_if_callback_returns_false(self, *ignored):
     with patch("socket.socket.recv", return_value='X'):
         # setup dlt_receiver_receive_socket to return a partial message
         replacement = functools.partial(mock_dlt_receiver_receive_socket,
                                         partial=True)
         with patch('dlt.dlt.dltlib.dlt_receiver_receive_socket',
                    new=replacement):
             self.assertFalse(
                 py_dlt_client_main_loop(self.client,
                                         callback=lambda msg: False))
    def test_read_message(self, *ignored):
        dumpfile = StringIO()

        stream_one.seek(0)
        expected = stream_one.read()

        with patch.object(self.client._connected_socket,
                          'recv',
                          return_value=b'X'):
            # setup dlt_receiver_receive to return a complete message
            replacement = functools.partial(mock_dlt_receiver_receive_socket)
            callback = Mock(side_effect=[True, False, False])
            with patch('dlt.dlt.dltlib.dlt_receiver_receive', new=replacement):
                self.assertTrue(
                    py_dlt_client_main_loop(self.client,
                                            dumpfile=dumpfile,
                                            callback=callback))
                self.assertEqual(
                    dumpfile.getvalue()[ctypes.sizeof(cDltStorageHeader):],
                    expected)
    def run(self):
        """DLTMessageHandler worker method"""
        if self._filename is not None:
            logger.info("Opening the DLT trace file '%s'", self._filename)
            self.tracefile = open(self._filename, mode="ab", buffering=False)

        while not self.mp_stop_flag.is_set():
            exception_occured = False
            if not self._client_connect():
                # keep trying to reconnect, until we either successfully
                # connect or the stop_flag is set
                continue
            try:
                res = py_dlt_client_main_loop(self._client,
                                              verbose=0,
                                              callback=self.handle,
                                              dumpfile=self.tracefile)
                if res is False and not self.mp_stop_flag.is_set(
                ):  # main loop returned False
                    logger.error("DLT connection lost. Restarting DLT client")
                    exception_occured = True
            except KeyboardInterrupt:
                exception_occured = True
                logger.debug("main loop manually interrupted")
                break
            except socket.timeout as exc:
                exception_occured = True
                logger.error("socket timeout error")
                logger.debug(exc)
            except Exception:  # pylint: disable=broad-except
                exception_occured = True
                logger.exception("Exception during the DLT message receive")

            finally:
                if exception_occured:
                    logger.debug("Closing open socket connections.")
                    self._client.disconnect()

        self.message_queue.close()
        self._client.disconnect()
        logger.info("DLTMessageHandler worker execution complete")
    def test_target_down(self):
        with patch.object(self.client._connected_socket,
                          'recv',
                          side_effect=socket.timeout):
            callback = Mock(return_value="should not be called")

            if six.PY3:
                with self.assertLogs(logger=logger) as dlt_logger:
                    return_value = py_dlt_client_main_loop(self.client,
                                                           callback=callback)
                    self.assertFalse(return_value)

                    log_output = dlt_logger.output
                    self.assertEqual(len(log_output), 1)
                    self.assertEqual(
                        log_output[0],
                        'ERROR:dlt.dlt:[]: DLTLib closed connected socket')
            else:
                self.assertRaises(socket.timeout,
                                  py_dlt_client_main_loop,
                                  self.client,
                                  callback=callback)

            self.assertFalse(callback.called)