def test_decode_little_endian_header(self): byte_stream = MockStream(to_bin('0x0500 0800 cafe babe')) self._msg_stream = MessageStream(byte_stream, self._protocol) decoded = self._msg_stream.get(self.tmp) self.assertEqual(decoded._header.msgId.hex, '0x0005') self.assertEqual(decoded._header.msgId._raw, to_bin('0x0500')) self.assertEqual(decoded.field_1.hex, '0xcafe') self.assertEqual(decoded.field_1._raw, to_bin('0xcafe')) self.assertEqual(decoded.field_2.hex, '0xbabe') self.assertEqual(decoded.field_2._raw, to_bin('0xbabe'))
def setUp(self): self._protocol = Protocol('Test') self._protocol.add(UInt(1, 'id', 1)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-2')) self._msg = MessageTemplate('FooRequest', self._protocol, {'id': '0xaa'}) self._msg.add(UInt(1, 'field_1', None)) self._msg.add(UInt(1, 'field_2', None)) byte_stream = MockStream(to_bin('0xff0004cafe aa0004dead dd0004beef')) self._msg_stream = MessageStream(byte_stream, self._protocol)
class TestMessageStream(TestCase): def setUp(self): self._protocol = Protocol('Test') self._protocol.add(UInt(1, 'id', 1)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-2')) self._msg = MessageTemplate('FooRequest', self._protocol, {'id': '0xaa'}) self._msg.add(UInt(1, 'field_1', None)) self._msg.add(UInt(1, 'field_2', None)) byte_stream = MockStream(to_bin('0xff0004cafe aa0004dead dd0004beef')) self._msg_stream = MessageStream(byte_stream, self._protocol, threading.RLock()) def test_get_message(self): msg = self._msg_stream.get(self._msg, header_filter='id') self.assertEquals(msg.field_1.hex, '0xde') def test_get_message_from_cache(self): _ = self._msg_stream.get(self._msg, header_filter='id') self._msg.header_parameters = {'id': '0xdd'} msg = self._msg_stream.get(self._msg, header_filter='id') self.assertEquals(msg.field_1.hex, '0xbe') def test_empty_message_stream(self): _ = self._msg_stream.get(self._msg, header_filter='id') self._msg_stream.empty() self.assertRaises(socket.timeout, self._msg_stream.get, self._msg, timeout=0.1) def test_filter_by_unset_field_fails(self): self._msg.header_parameters = {} self.assertRaises(AssertionError, self._msg_stream.get, self._msg, header_filter='id') def test_timeout_goes_to_stream(self): self._msg.header_parameters = {'id': '0x00'} self.assertRaises(socket.timeout, self._msg_stream.get, self._msg, timeout=0.1, header_filter='id') def test_get_messages_count_from_cache_two_messages(self): _ = self._msg_stream.get(self._msg, header_filter='id') self._msg.header_parameters = {'id': '0xdd'} count = self._msg_stream.get_messages_count_in_cache() self.assertEquals(count, 2) def test_get_messages_count_from_cache_three_messages(self): count = self._msg_stream.get_messages_count_in_cache() self.assertEquals(count, 3)
class TestLittleEndianProtocol(TestCase): def setUp(self): self._protocol = Protocol('TestProtocol', little_endian=True) self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', '0xcafe')) self.tmp.add(UInt(2, 'field_2', '0xbabe')) def test_encode_little_endian_header(self): encoded = self.tmp.encode({}, {}) self.assertEqual(encoded._header.msgId.hex, '0x0005') self.assertEqual(encoded._header.length.hex, '0x0008') self.assertEqual(encoded._header.msgId._raw, to_bin('0x0500')) self.assertEqual(encoded._header.length._raw, to_bin('0x0800')) def test_decode_little_endian_header(self): byte_stream = MockStream(to_bin('0x0500 0800 cafe babe')) self._msg_stream = MessageStream(byte_stream, self._protocol) decoded = self._msg_stream.get(self.tmp) self.assertEqual(decoded._header.msgId.hex, '0x0005') self.assertEqual(decoded._header.msgId._raw, to_bin('0x0500')) self.assertEqual(decoded.field_1.hex, '0xcafe') self.assertEqual(decoded.field_1._raw, to_bin('0xcafe')) self.assertEqual(decoded.field_2.hex, '0xbabe') self.assertEqual(decoded.field_2._raw, to_bin('0xbabe'))
class TestLittleEndianProtocol(TestCase): def setUp(self): self._protocol = Protocol('TestProtocol', little_endian=True) self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', '0xcafe')) self.tmp.add(UInt(2, 'field_2', '0xbabe')) def test_encode_little_endian_header(self): encoded = self.tmp.encode({}, {}) self.assertEquals(encoded._header.msgId.hex, '0x0005') self.assertEquals(encoded._header.length.hex, '0x0008') self.assertEquals(encoded._header.msgId._raw, to_bin('0x0500')) self.assertEquals(encoded._header.length._raw, to_bin('0x0800')) def test_decode_little_endian_header(self): byte_stream = MockStream(to_bin('0x0500 0800 cafe babe')) self._msg_stream = MessageStream(byte_stream, self._protocol, threading.RLock()) decoded = self._msg_stream.get(self.tmp) self.assertEquals(decoded._header.msgId.hex, '0x0005') self.assertEquals(decoded._header.msgId._raw, to_bin('0x0500')) self.assertEquals(decoded.field_1.hex, '0xcafe') self.assertEquals(decoded.field_1._raw, to_bin('0xcafe')) self.assertEquals(decoded.field_2.hex, '0xbabe') self.assertEquals(decoded.field_2._raw, to_bin('0xbabe'))
def setUp(self): self._protocol = Protocol("Test") self._protocol.add(UInt(1, "id", 1)) self._protocol.add(UInt(2, "length", None)) self._protocol.add(PDU("length-2")) self._msg = MessageTemplate("FooRequest", self._protocol, {"id": "0xaa"}) self._msg.add(UInt(1, "field_1", None)) self._msg.add(UInt(1, "field_2", None)) byte_stream = MockStream(to_bin("0xff0004cafe aa0004dead dd0004beef")) self._msg_stream = MessageStream(byte_stream, self._protocol, threading.RLock())
def test_decode_little_endian_header(self): byte_stream = MockStream(to_bin('0x0500 0800 cafe babe')) self._msg_stream = MessageStream(byte_stream, self._protocol, threading.RLock()) decoded = self._msg_stream.get(self.tmp) self.assertEquals(decoded._header.msgId.hex, '0x0005') self.assertEquals(decoded._header.msgId._raw, to_bin('0x0500')) self.assertEquals(decoded.field_1.hex, '0xcafe') self.assertEquals(decoded.field_1._raw, to_bin('0xcafe')) self.assertEquals(decoded.field_2.hex, '0xbabe') self.assertEquals(decoded.field_2._raw, to_bin('0xbabe'))
class TestMessageStream(TestCase): def setUp(self): self._protocol = Protocol('Test') self._protocol.add(UInt(1, 'id', 1)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-2')) self._msg = MessageTemplate('FooRequest', self._protocol, {'id': '0xaa'}) self._msg.add(UInt(1, 'field_1', None)) self._msg.add(UInt(1, 'field_2', None)) byte_stream = MockStream(to_bin('0xff0004cafe aa0004dead dd0004beef')) self._msg_stream = MessageStream(byte_stream, self._protocol) def test_get_message(self): msg = self._msg_stream.get(self._msg, header_filter='id') self.assertEquals(msg.field_1.hex, '0xde') def test_get_message_from_cache(self): _ = self._msg_stream.get(self._msg, header_filter='id') self._msg.header_parameters = {'id': '0xdd'} msg = self._msg_stream.get(self._msg, header_filter='id') self.assertEquals(msg.field_1.hex, '0xbe') def test_empty_message_stream(self): _ = self._msg_stream.get(self._msg, header_filter='id') self._msg_stream.empty() self.assertRaises(socket.timeout, self._msg_stream.get, self._msg, timeout=0.1) def test_filter_by_unset_field_fails(self): self._msg.header_parameters = {} self.assertRaises(AssertionError, self._msg_stream.get, self._msg, header_filter='id') def test_timeout_goes_to_stream(self): self._msg.header_parameters = {'id': '0x00'} self.assertRaises(socket.timeout, self._msg_stream.get, self._msg, timeout=0.1, header_filter='id')
class TestMessageStream(TestCase): def setUp(self): self._protocol = Protocol("Test") self._protocol.add(UInt(1, "id", 1)) self._protocol.add(UInt(2, "length", None)) self._protocol.add(PDU("length-2")) self._msg = MessageTemplate("FooRequest", self._protocol, {"id": "0xaa"}) self._msg.add(UInt(1, "field_1", None)) self._msg.add(UInt(1, "field_2", None)) byte_stream = MockStream(to_bin("0xff0004cafe aa0004dead dd0004beef")) self._msg_stream = MessageStream(byte_stream, self._protocol, threading.RLock()) def test_get_message(self): msg = self._msg_stream.get(self._msg, header_filter="id") self.assertEquals(msg.field_1.hex, "0xde") def test_get_message_from_cache(self): _ = self._msg_stream.get(self._msg, header_filter="id") self._msg.header_parameters = {"id": "0xdd"} msg = self._msg_stream.get(self._msg, header_filter="id") self.assertEquals(msg.field_1.hex, "0xbe") def test_empty_message_stream(self): _ = self._msg_stream.get(self._msg, header_filter="id") self._msg_stream.empty() self.assertRaises(socket.timeout, self._msg_stream.get, self._msg, timeout=0.1) def test_filter_by_unset_field_fails(self): self._msg.header_parameters = {} self.assertRaises(AssertionError, self._msg_stream.get, self._msg, header_filter="id") def test_timeout_goes_to_stream(self): self._msg.header_parameters = {"id": "0x00"} self.assertRaises(socket.timeout, self._msg_stream.get, self._msg, timeout=0.1, header_filter="id") def test_get_messages_count_from_cache_two_messages(self): _ = self._msg_stream.get(self._msg, header_filter="id") self._msg.header_parameters = {"id": "0xdd"} count = self._msg_stream.get_messages_count_in_cache() self.assertEquals(count, 2) def test_get_messages_count_from_cache_three_messages(self): count = self._msg_stream.get_messages_count_in_cache() self.assertEquals(count, 3)