async def test_multiple_envelopes(): """Test many envelopes received.""" tmpdir = Path(tempfile.mkdtemp()) d = tmpdir / "test_stub" d.mkdir(parents=True) input_file_path = d / "input_file.csv" output_file_path = d / "output_file.csv" connection = _make_stub_connection(input_file_path, output_file_path) num_envelopes = 5 await connection.connect() async def wait_num(num): for _ in range(num): assert await connection.receive() task = asyncio.get_event_loop().create_task(wait_num(num_envelopes)) with open(input_file_path, "ab+") as f: for _ in range(num_envelopes): write_envelope(make_test_envelope(), f) await asyncio.sleep(0.01) # spin asyncio loop await asyncio.wait_for(task, timeout=3) await connection.disconnect()
def test_reception_a(self): """Test that the connection receives what has been enqueued in the input file.""" msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) msg.counterparty = "any" expected_envelope = Envelope( to="any", sender="any", protocol_id=DefaultMessage.protocol_id, message=msg, ) with open(self.input_file_path, "ab+") as f: write_envelope(expected_envelope, f) actual_envelope = self.multiplexer.get(block=True, timeout=3.0) assert expected_envelope.to == actual_envelope.to assert expected_envelope.sender == actual_envelope.sender assert expected_envelope.protocol_id == actual_envelope.protocol_id msg = DefaultMessage.serializer.decode(actual_envelope.message) msg.counterparty = actual_envelope.to assert expected_envelope.message == msg
def write_envelope_to_file(envelope: Envelope, file_path: str) -> None: """ Write an envelope to a file. :param envelope: Envelope. :param file_path: the file path :return: None """ with open(Path(file_path), "ab+") as f: write_envelope(envelope, f)