Exemplo n.º 1
0
    def test_240_transport_sleeps_if_not_open(self):
        """Test transport read can retain partial log lines."""
        transport = fake_transport.FakeTransport()
        transport.set_property(transport_properties.OPEN_ON_START, False)
        read_timeout = 0.03
        self.uut = transport_process.TransportProcess(
            "fake_transport",
            self.exception_queue,
            self.command_queue,
            self.log_queue,
            transport,
            call_result_queue=self.call_result_queue,
            read_timeout=read_timeout)

        device_data1 = "partial log line\r"
        transport.reads.put(device_data1)
        wait_for_queue_writes(transport.reads)
        self.uut._pre_run_hook()
        start_time = time.time()
        self.uut._do_work()
        end_time = time.time()
        self.assertFalse(
            self.uut.is_open(),
            "Expected transport process to be closed, found open")
        self.uut._post_run_hook()

        self.assertGreater(
            end_time - start_time, read_timeout,
            "Expected transport to sleep for > {}s, found {}s".format(
                read_timeout, end_time - start_time))
Exemplo n.º 2
0
    def test_300_transport_exceeds_maximum_baudrate_required(self):
        """Test baud rate which exceeds the maximum baud rate of 921600."""

        baudrate = 115200 * 10
        minimum_bytes_per_second = 115200 * 8 / 10
        transport = fake_transport.FakeTransport(baudrate=baudrate,
                                                 generate_lines=True)
        self.uut = transport_process.TransportProcess(
            "fake_transport",
            self.exception_queue,
            self.command_queue,
            self.log_queue,
            transport,
            call_result_queue=self.call_result_queue,
            raw_data_queue=self.raw_data_queue)
        self.uut.toggle_raw_data()
        start_time = time.time()
        end_time = start_time + 5.0
        self.uut._pre_run_hook()
        while time.time() < end_time:
            self.uut._do_work()
        finish_time = time.time()
        self.uut._post_run_hook()
        generated_bytes_per_second = transport.bytes_per_second.value
        self.assertLess(
            minimum_bytes_per_second, generated_bytes_per_second,
            "Expected generator to produce bytes > {} but found {}. "
            "Check load on this lab server.".format(
                minimum_bytes_per_second, generated_bytes_per_second))
        bytes_received = 0
        expected_count = 0
        while not self.raw_data_queue.empty():
            expected_count += 1
            _, raw_data = self.raw_data_queue.get()
            actual_count = int(raw_data[:8])
            self.assertEqual(
                expected_count, actual_count,
                "Expected line count {} found {} in {!r}".format(
                    expected_count, actual_count, raw_data))
            bytes_received += len(raw_data)
        actual_bytes_per_second = bytes_received / (finish_time - start_time)
        self.assertLess(
            minimum_bytes_per_second, actual_bytes_per_second,
            "Expected actual bytes per second > {} but found {}".format(
                minimum_bytes_per_second, actual_bytes_per_second))
Exemplo n.º 3
0
    def test_210_transport_rejects_invalid_command(self):
        """Test transport rejects invalid command."""
        transport = fake_transport.FakeTransport()
        self.uut = transport_process.TransportProcess(
            "fake_transport",
            self.exception_queue,
            self.command_queue,
            self.log_queue,
            transport,
            call_result_queue=self.call_result_queue)

        self.command_queue.put(("invalid cmd", None))
        wait_for_queue_writes(self.command_queue)
        self.uut._pre_run_hook()
        with self.assertRaisesRegex(RuntimeError,
                                    r"received an unknown command"):
            self.uut._do_work()
        self.uut._post_run_hook()
Exemplo n.º 4
0
    def test_233_transport_read_reorders_log_interrupted_partial_lines(self):
        """Test transport read can order partial and full lines correctly.

    See NEP-3223 which is a bug in the interaction of TransportProcess and
    LogFramer.
    """
        transport = fake_transport.FakeTransport()
        response_start = u"response start"
        response_end = u" end"
        response_full = response_start + response_end
        log_line = u"my custom log line\n"
        log_regex = "({})".format(log_line)
        framer = data_framer.InterwovenLogFramer(log_regex)
        uut = transport_process.TransportProcess(
            "fake_transport",
            self.exception_queue,
            self.command_queue,
            self.log_queue,
            transport,
            call_result_queue=self.call_result_queue,
            framer=framer)

        device_data1 = response_start + log_line + response_end
        transport.reads.put(device_data1)
        wait_for_queue_writes(transport.reads)
        uut._pre_run_hook()
        uut._do_work()
        line1 = self.log_queue.get()
        self.assertIn(
            log_line, line1,
            "Expected {!r} in line but found {!r}".format(log_line, line1))
        self.assertTrue(self.log_queue.empty(),
                        "Expected log queue to be empty")
        time.sleep(_LOG_MESSAGE_TIMEOUT)
        uut._do_work()
        line2 = self.log_queue.get()
        self.assertIn(
            response_full, line2,
            "Expected {!r} in line but found {!r}".format(
                response_full, line2))
        self.assertTrue(self.log_queue.empty(),
                        "Expected log queue to be empty")
        uut._post_run_hook()
Exemplo n.º 5
0
 def test_200_transport_opens_and_closes_transport(self):
     """Test transport process calls transport open and close."""
     transport = fake_transport.FakeTransport()
     self.uut = transport_process.TransportProcess(
         "fake_transport",
         self.exception_queue,
         self.command_queue,
         self.log_queue,
         transport,
         call_result_queue=self.call_result_queue)
     self.uut.start()
     self.uut.stop()
     self.assertEqual(
         1, transport.open_count.value,
         "Expected transport.open to be called {} called {}".format(
             1, transport.open_count.value))
     self.assertEqual(
         1, transport.close_count.value,
         "Expected transport.close to be called {} called {}".format(
             1, transport.close_count.value))
Exemplo n.º 6
0
 def test_203_transport_skips_opens_on_start(self):
     """Test transport process calls transport open and close."""
     transport = fake_transport.FakeTransport()
     transport.set_property(transport_properties.OPEN_ON_START, False)
     self.uut = transport_process.TransportProcess(
         "fake_transport",
         self.exception_queue,
         self.command_queue,
         self.log_queue,
         transport,
         call_result_queue=self.call_result_queue)
     self.uut.start()
     self.uut.stop()
     self.assertEqual(
         0, transport.open_count.value,
         "Expected transport.open to be called {} called {}".format(
             0, transport.open_count.value))
     self.assertEqual(
         1, transport.close_count.value,
         "Expected transport.close to be called {} called {}".format(
             1, transport.close_count.value))
Exemplo n.º 7
0
    def test_231_transport_read_error_raises_error(self):
        """Test transport raises error on read exception."""
        transport = fake_transport.FakeTransport(fail_read=True)

        self.uut = transport_process.TransportProcess(
            "fake_transport",
            self.exception_queue,
            self.command_queue,
            self.log_queue,
            transport,
            call_result_queue=self.call_result_queue)
        self.uut.start()
        end_time = time.time() + _EXCEPTION_TIMEOUT
        while self.uut.is_running() and time.time() < end_time:
            time.sleep(0.001)
        self.assertFalse(
            self.uut.is_open(),
            "Expected transport process to be closed, found open")
        self.assertFalse(self.uut.is_running(),
                         "Expected process to end, still running")
        self.uut.stop()

        self.assertEqual(
            1, transport.open_count.value,
            "Expected transport.open to be called {} called {}".format(
                1, transport.open_count.value))
        self.assertEqual(
            1, transport.close_count.value,
            "Expected transport.close to be called {} called {}".format(
                1, transport.close_count.value))
        self.assertIsNotNone(self.exception,
                             "Expected exception to be raised found None")
        self.assertIn(
            fake_transport.EXCEPTION_MESSAGE, self.exception,
            "Expected exception message {} found {!r}".format(
                fake_transport.EXCEPTION_MESSAGE, self.exception))
        # Clear raised exception that was handled
        self.exception = None
Exemplo n.º 8
0
    def test_232_transport_read_retains_partial_lines(self):
        """Test transport read can retain partial log lines."""
        transport = fake_transport.FakeTransport()
        self.uut = transport_process.TransportProcess(
            "fake_transport",
            self.exception_queue,
            self.command_queue,
            self.log_queue,
            transport,
            call_result_queue=self.call_result_queue)

        device_data1 = "partial log line\r"
        transport.reads.put(device_data1)
        wait_for_queue_writes(transport.reads)
        self.uut._pre_run_hook()
        self.uut._do_work()
        self.assertTrue(self.log_queue.empty(),
                        "Expected log queue to be empty")
        time.sleep(_LOG_MESSAGE_TIMEOUT)
        self.uut._do_work()
        self.assertFalse(self.log_queue.empty(),
                         "Expected log queue to not be empty")
        self.uut._post_run_hook()
Exemplo n.º 9
0
    def test_230_transport_invalid_command_raises_error(self):
        """Test transport raises exception on invalid command."""
        transport = fake_transport.FakeTransport()

        self.uut = transport_process.TransportProcess(
            "fake_transport",
            self.exception_queue,
            self.command_queue,
            self.log_queue,
            transport,
            call_result_queue=self.call_result_queue)
        self.command_queue.put(("Invalid command", None))
        wait_for_queue_writes(self.command_queue)
        self.uut.start()
        end_time = time.time() + _EXCEPTION_TIMEOUT
        while self.uut.is_running() and time.time() < end_time:
            time.sleep(0.001)
        self.assertFalse(self.uut.is_running(),
                         "Expected process to end, still running")
        self.uut.stop()

        self.assertEqual(
            1, transport.open_count.value,
            "Expected transport.open to be called {} called {}".format(
                1, transport.open_count.value))
        self.assertEqual(
            1, transport.close_count.value,
            "Expected transport.close to be called {} called {}".format(
                1, transport.close_count.value))
        self.assertIsNotNone(self.exception,
                             "Expected exception to be raised found None")
        self.assertIn(
            "received an unknown command", repr(self.exception),
            "Expected 'received an unknown command' found {!r}".format(
                self.exception))
        # Clear raised exception that was handled
        self.exception = None