def setUp(self):
        """
        """
        self.prompts = [">"]
        self.newline = "\n"
        self.callback_result = None
        self._trigger_count = 0
        self._events = []

        self.protocol = CommandResponseInstrumentProtocol(
            self.prompts, self.newline, self.event_callback)

        self.protocol_fsm = ThreadSafeFSM(self.TestState, self.TestEvent,
                                          self.TestEvent.ENTER,
                                          self.TestEvent.EXIT)

        self.protocol_fsm.add_handler(self.TestState.TEST, self.TestEvent.TEST,
                                      lambda x: x)

        self.protocol._add_build_handler(self.TestEvent.TEST,
                                         self._build_simple_command)
        self.protocol._add_response_handler(self.TestEvent.TEST,
                                            self._parse_test_response)
        self.protocol._connection = Mock()
        self.protocol._connection.send = lambda x: self.protocol.add_to_buffer(
            "%s >->" % x)
        self.protocol.get_current_state = Mock(
            return_value=self.TestState.TEST)
        self.protocol._send_wakeup = lambda: self.protocol.add_to_buffer(
            "wakeup response >->")
        self.protocol._wakeup = functools.partial(self.protocol._wakeup,
                                                  delay=0)
Exemplo n.º 2
0
    def test_get_prompts(self):
        """
        ensure prompts are returned sorted by length
        """
        prompts = ['aa', 'bbb', 'c', 'dddd']
        expected = ['dddd', 'bbb', 'aa', 'c']
        class Prompts(BaseEnum):
            A = 'aa'
            B = 'bbb'
            C = 'c'
            D = 'dddd'

        self.protocol = CommandResponseInstrumentProtocol(prompts, '\r\n', self.event_callback)
        self.assertEqual(self.protocol._get_prompts(), expected)

        self.protocol = CommandResponseInstrumentProtocol(Prompts, '\r\n', self.event_callback)
        self.assertEqual(self.protocol._get_prompts(), expected)
    def test_ring_buffer(self):
        """
        verify command thread ring buffer rolls as expected
        """
        prompts = ['aa', 'bbb', 'c', 'dddd']
        self.protocol = CommandResponseInstrumentProtocol(
            prompts, '\r\n', self.event_callback)

        self.protocol._max_buffer_size = Mock(return_value=5)

        self.protocol.add_to_buffer("a")
        self.protocol.add_to_buffer("b")
        self.protocol.add_to_buffer("c")
        self.protocol.add_to_buffer("d")
        self.protocol.add_to_buffer("e")

        self.assertEqual(len(self.protocol._linebuf), 5)
        self.assertEqual(len(self.protocol._promptbuf), 5)

        self.assertEqual(self.protocol._linebuf, "abcde")
        self.assertEqual(self.protocol._promptbuf, "abcde")

        self.protocol.add_to_buffer("f")

        self.assertEqual(len(self.protocol._linebuf), 5)
        self.assertEqual(len(self.protocol._promptbuf), 5)

        self.assertEqual(self.protocol._linebuf, "bcdef")
        self.assertEqual(self.protocol._promptbuf, "bcdef")

        self.protocol.add_to_buffer("gh")

        self.assertEqual(len(self.protocol._linebuf), 5)
        self.assertEqual(len(self.protocol._promptbuf), 5)

        self.assertEqual(self.protocol._linebuf, "defgh")
        self.assertEqual(self.protocol._promptbuf, "defgh")