本文整理汇总了Python中mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol.add_to_buffer方法的典型用法代码示例。如果您正苦于以下问题:Python CommandResponseInstrumentProtocol.add_to_buffer方法的具体用法?Python CommandResponseInstrumentProtocol.add_to_buffer怎么用?Python CommandResponseInstrumentProtocol.add_to_buffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol
的用法示例。
在下文中一共展示了CommandResponseInstrumentProtocol.add_to_buffer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestUnitCommandInstrumentProtocol
# 需要导入模块: from mi.core.instrument.instrument_protocol import CommandResponseInstrumentProtocol [as 别名]
# 或者: from mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol import add_to_buffer [as 别名]
class TestUnitCommandInstrumentProtocol(MiUnitTestCase):
"""
Test cases for instrument protocol class. Functions in this class provide
instrument protocol unit tests and provide a tutorial on use of
the protocol interface.
"""
class TestState(BaseEnum):
"""
Protocol states for SBE37. Cherry picked from DriverProtocolState
enum.
"""
TEST = "TEST"
class TestEvent(BaseEnum):
"""
Protocol events for SBE37. Cherry picked from DriverEvent enum.
"""
ENTER = "ENTER"
EXIT = "EXIT"
TEST = "TEST"
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 >->")
def _build_simple_command(self, cmd):
return "cmd...do it!"
def _parse_test_response(self, resp, prompt):
return "c=%s p=%s" % (resp, prompt)
def event_callback(self, event, value=None):
log.debug("Test event callback: %s" % event)
self._events.append(event)
self._trigger_count += 1
def test_cmd_response(self):
"""
Test getting a response from a command supplied with prompts and regexes.
"""
regex1 = re.compile(r'.*(do it).*')
regex2 = re.compile(r'foobar')
regex3 = re.compile(r'.*(do) (it).*')
regex4 = re.compile(r'.*do it.*')
# Normal case
result = self.protocol._do_cmd_resp(self.TestEvent.TEST)
self.assertEqual(result, self._parse_test_response(self._build_simple_command(None)+" >", ">"))
# expected prompt cases
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, expected_prompt=">")
self.assertEqual(result, self._parse_test_response(self._build_simple_command(None)+" >", ">"))
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, expected_prompt=">-")
self.assertEqual(result, self._parse_test_response(self._build_simple_command(None)+" >-", ">-"))
# Should time out looking for a bad prompt
self.assertRaises(InstrumentTimeoutException,
self.protocol._do_cmd_resp,
self.TestEvent.TEST, expected_prompt="-->", timeout=5)
# regex cases
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, response_regex=regex1)
self.assertEqual(result, self._parse_test_response("do it", ""))
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, response_regex=regex3)
self.assertEqual(result, self._parse_test_response("doit", ""))
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, response_regex=regex4)
self.assertEqual(result, self._parse_test_response("", ""))
# Should time out looking for a bad regex
self.assertRaises(InstrumentTimeoutException,
self.protocol._do_cmd_resp,
self.TestEvent.TEST, response_regex=regex2)
# combo case
self.assertRaises(InstrumentProtocolException,
self.protocol._do_cmd_resp,
self.TestEvent.TEST, expected_prompt=">", response_regex=regex1)
示例2: TestUnitInstrumentProtocol
# 需要导入模块: from mi.core.instrument.instrument_protocol import CommandResponseInstrumentProtocol [as 别名]
# 或者: from mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol import add_to_buffer [as 别名]
#.........这里部分代码省略.........
# Bad parameters raise exceptions event when ALL is specified
with self.assertRaises(InstrumentParameterException):
self.assertEqual(sorted(params), sorted(self.protocol._get_param_list(['noparam', DriverParameter.ALL])))
# An exception is raised when the param is not a list or string
with self.assertRaises(InstrumentParameterException):
self.protocol._get_param_list({'other': 'struct'})
# when a subset is given, the same set is returned.
subset = ['bar', 'baz']
self.assertEqual(sorted(subset), sorted(self.protocol._get_param_list(subset)))
# verify we can accept a tuple
subset = ['bar', 'baz']
self.assertEqual(sorted(subset), sorted(self.protocol._get_param_list(('bar', 'baz'))))
# An exception is raised when the param is not known
with self.assertRaises(InstrumentParameterException):
self.protocol._get_param_list(subset + ['boom'])
# Verify we can send in a single parameter as a string, not ALL
self.assertEqual(['bar'], self.protocol._get_param_list('bar'))
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")
@unittest.skip('Not Written')
def test_publish_raw(self):
"""
Tests to see if raw data is appropriately published back out to
the InstrumentAgent via the event callback.