def test_envelope_from_bytes_bad_format(): """Test envelope_from_bytes, when the input has a bad format we raise ValueError.""" test_error_message = "Something bad happened." _bytes = b"" with patch("aea.helpers.file_io._decode", side_effect=ValueError(test_error_message)): with patch.object(aea.helpers.file_io._default_logger, "error") as mock_error: envelope_from_bytes(_bytes) mock_error.assert_called_with( f"Bad formatted input: {_bytes}. {test_error_message}")
def test_envelope_serialization(): """Test envelope serialization/deserialization with files.""" envelope = Envelope( to="to", sender="sender", protocol_specification_id=PublicId("author", "name", "0.1.0"), message=b"", ) with tempfile.TemporaryDirectory() as temp_dir: output_file = Path(os.path.join(temp_dir, "output_file")) with output_file.open(mode="wb") as fout: write_envelope(envelope, fout) actual_envelope = envelope_from_bytes(output_file.read_bytes()) assert envelope == actual_envelope
async def read_envelopes(self) -> None: """Read envelopes from inptut file, decode and put into in_queue.""" self._ensure_connected() if self.in_queue is None: # pragma: nocover raise ValueError("Input queue not initialized.") self.logger.debug("Read messages!") async for data in self._file_read_and_trunc(delay=self.read_delay): lines = self._split_messages(data) for line in lines: envelope = envelope_from_bytes(line, SEPARATOR, self.logger) if envelope is None: continue self.logger.debug(f"Add envelope {envelope}") await self.in_queue.put(envelope)