def test_unpack_method_with_invalid_data_type(self): """Test the unpack static method with an invalid data type.""" packet = Radio.pack(Datum.Integer(id=0, timestamp=0, value=0)) packet[5:6] = bytes([6]) with self.assertRaises(RuntimeError): Radio.unpack(packet)
def test_unpack_method_with_invalid_packet_size(self): """Test the unpack static method with an invalid packet size.""" packet = Radio.pack(Datum.Integer(id=0, timestamp=0, value=0)) packet[0:1] = bytes([18 | 0x80]) with self.assertRaises(RuntimeError): Radio.unpack(packet)
def _recv_packet(sock): # 0x80 | len(packet) : binary data packet # MSB == 0 : no more data packet header = Radio._recvn(sock, 1, MSG_PEEK) if not header[0] & 0x80: return None packet = Radio._recv_packet(sock) return packet
def handle(self): logger = getLogger() def _recv_packet(sock): # 0x80 | len(packet) : binary data packet # MSB == 0 : no more data packet header = Radio._recvn(sock, 1, MSG_PEEK) if not header[0] & 0x80: return None packet = Radio._recv_packet(sock) return packet try: command = self.request.makefile().readline().strip() if command[0] in 'Ww': # write command self.request.sendall(b'OK: Write On\n') while True: packet = _recv_packet(self.request) if not packet: break datum = Radio.unpack(packet) logger.info(datum) else: logger.warning('Command {} not supported'.format(command)) except Exception as e: logger.error(e) raise
def test_receive_method(self): """Test the receive method by actually retrieving the latest data from STS.""" radio = Radio() ids = DatumTest.create_ids() data = radio.receive(ids)
def test_transmit_method(self): """Test the transmit method by actually sending data to STS.""" radio = Radio() data = DatumTest.create_data() radio.transmit(data)
def test_round_trip(self): """Test if data round-trips through the pack and unpack static methods.""" data = DatumTest.create_data() for datum in data: Radio.unpack(Radio.pack(datum))
def test_pack_method_with_invalid_data_type(self): """Test the pack static method with an invalid data type.""" datum = Datum(id=0, format=6) with self.assertRaises(RuntimeError): Radio.pack(datum)
def test_default_constructor(self): """Test the default constructor of the Radio class.""" radio = Radio()