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() assert connection.is_connected 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()
async def test_disconnection_when_already_disconnected(): """Test the case when disconnecting a connection already disconnected.""" 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) assert not connection.is_connected await connection.disconnect() assert not connection.is_connected
def setup(self): """Set the test up.""" self.cwd = os.getcwd() self.tmpdir = Path(tempfile.mkdtemp()) d = self.tmpdir / "test_stub" d.mkdir(parents=True) self.input_file_path = d / "input_file.csv" self.output_file_path = d / "output_file.csv" self.connection = _make_stub_connection(self.input_file_path, self.output_file_path) self.multiplexer = Multiplexer([self.connection]) self.multiplexer.connect() os.chdir(self.tmpdir)
def setup_class(cls): """Set the test up.""" cls.cwd = os.getcwd() cls.tmpdir = Path(tempfile.mkdtemp()) d = cls.tmpdir / "test_stub" d.mkdir(parents=True) cls.input_file_path = d / "input_file.csv" cls.output_file_path = d / "output_file.csv" cls.connection = _make_stub_connection(cls.input_file_path, cls.output_file_path) cls.multiplexer = Multiplexer([cls.connection]) cls.multiplexer.connect() os.chdir(cls.tmpdir)
async def test_receiving_returns_none_when_error_occurs(): """Test that when we try to receive an envelope and an error occurs we return None.""" 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) await connection.connect() with mock.patch.object(connection.in_queue, "get", side_effect=Exception): ret = await connection.receive() assert ret is None await connection.disconnect()
async def test_bad_envelope(): """Test bad format envelop.""" 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) await connection.connect() with open(input_file_path, "ab+") as f: f.write(b"1,2,3,4,5,") f.flush() with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(connection.receive(), timeout=0.1) await connection.disconnect()