def convert_to_incoming_packet(reqid, message): """Convert (reqid, message) pair to IncomingPacket object""" raw = OutgoingPacket.create_packet(reqid, message).raw dispatcher = MockDispatcher() dispatcher.prepare_with_raw(raw) packet = IncomingPacket() packet.read(dispatcher) packet.data = packet.data return packet
def get_fail_packet(): """Returns an incomplete IncomingPacket object""" raw = OutgoingPacket.create_packet(REQID.generate(), mock_protobuf.get_mock_pplan()).raw packet = IncomingPacket.create_packet(raw[:4], raw[4:]) packet.is_complete = False return packet
def test_read(self): # complete packets are prepared normal_dispatcher = mock_generator.MockDispatcher() normal_dispatcher.prepare_valid_response() pkt = IncomingPacket() pkt.read(normal_dispatcher) self.assertTrue(pkt.is_header_read) self.assertTrue(pkt.is_complete) # a packet with just a header is prepared header_dispatcher = mock_generator.MockDispatcher() header_dispatcher.prepare_header_only() pkt = IncomingPacket() pkt.read(header_dispatcher) self.assertTrue(pkt.is_header_read) self.assertFalse(pkt.is_complete) self.assertEqual(pkt.data, "") # an incomplete data packet is prepared partial_data_dispatcher = mock_generator.MockDispatcher() partial_data_dispatcher.prepare_partial_data() pkt = IncomingPacket() pkt.read(partial_data_dispatcher) self.assertTrue(pkt.is_header_read) self.assertFalse(pkt.is_complete) self.assertEqual(len(pkt.data), partial_data_dispatcher.PARTIAL_DATA_SIZE) # error test try: eagain_dispatcher = mock_generator.MockDispatcher() eagain_dispatcher.prepare_eagain() pkt = IncomingPacket() pkt.read(eagain_dispatcher) except Exception: self.fail( "Exception raised unexpectedly when testing EAGAIN error") with self.assertRaises(RuntimeError): fatal_dispatcher = mock_generator.MockDispatcher() fatal_dispatcher.prepare_fatal() pkt = IncomingPacket() pkt.read(fatal_dispatcher)
def test_read(self): # complete packets are prepared normal_dispatcher = mock_generator.MockDispatcher() normal_dispatcher.prepare_valid_response() pkt = IncomingPacket() pkt.read(normal_dispatcher) self.assertTrue(pkt.is_header_read) self.assertTrue(pkt.is_complete) # a packet with just a header is prepared header_dispatcher = mock_generator.MockDispatcher() header_dispatcher.prepare_header_only() pkt = IncomingPacket() pkt.read(header_dispatcher) self.assertTrue(pkt.is_header_read) self.assertFalse(pkt.is_complete) self.assertEqual(pkt.data, "") # an incomplete data packet is prepared partial_data_dispatcher = mock_generator.MockDispatcher() partial_data_dispatcher.prepare_partial_data() pkt = IncomingPacket() pkt.read(partial_data_dispatcher) self.assertTrue(pkt.is_header_read) self.assertFalse(pkt.is_complete) self.assertEqual(len(pkt.data), partial_data_dispatcher.PARTIAL_DATA_SIZE) # error test try: eagain_dispatcher = mock_generator.MockDispatcher() eagain_dispatcher.prepare_eagain() pkt = IncomingPacket() pkt.read(eagain_dispatcher) except Exception: self.fail("Exception raised unexpectedly when testing EAGAIN error") with self.assertRaises(RuntimeError): fatal_dispatcher = mock_generator.MockDispatcher() fatal_dispatcher.prepare_fatal() pkt = IncomingPacket() pkt.read(fatal_dispatcher)