diff --git a/tests/test_core/conftest.py b/tests/test_core/conftest.py index 8e0691392..39f04877b 100644 --- a/tests/test_core/conftest.py +++ b/tests/test_core/conftest.py @@ -70,7 +70,7 @@ def close(self, *argv, **kwarg): @pytest_asyncio.fixture async def p9(request, tmpdir): ctx = AsyncPlan9Context() - await ctx.ensure_client() + await ctx.ensure_session() yield ctx await ctx.close() diff --git a/tests/test_core/pr2test/plan9.py b/tests/test_core/pr2test/plan9.py index 3974f4cb7..087951fd8 100644 --- a/tests/test_core/pr2test/plan9.py +++ b/tests/test_core/pr2test/plan9.py @@ -1,45 +1,7 @@ -import asyncio -import json -import threading -from functools import partial - -from pyroute2.plan9 import Plan9Exit +from socket import socketpair from pyroute2.plan9.client import Plan9ClientSocket from pyroute2.plan9.server import Plan9ServerSocket -address = ('127.0.0.1', 8149) - - -def stop_server(context): - if 'task' in context: - context['task'].cancel() - raise Plan9Exit( - '{"status": 200, "message": "shutting down the server"}' - ) - return '{"status": 503, "message": "server is not ready, try again later"}' - - -async def server(address, server_started, sample_data): - p9 = Plan9ServerSocket(address=address) - context = {} - # data file - i_data = p9.filesystem.create('data') - i_data.data.write(sample_data) - # shutdown file - i_stop = p9.filesystem.create('stop') - i_stop.publish_function(partial(stop_server, context), loader=lambda x: {}) - # save the server task - context['task'] = await p9.async_run() - server_started.set() - try: - await context['task'] - except asyncio.exceptions.CancelledError: - pass - - -def server_thread(address, server_started, sample_data): - asyncio.run(server(address, server_started, sample_data)) - class AsyncPlan9Context: @@ -49,24 +11,15 @@ class AsyncPlan9Context: sample_data = b'Pi6raTaXuzohdu7n' def __init__(self): - self.server_started = threading.Event() - self.server = threading.Thread( - target=server_thread, - args=(address, self.server_started, self.sample_data), - ) - self.server.start() - self.server_started.wait() - - async def ensure_client(self): - self.client = Plan9ClientSocket(address=address) + self.server_sock, self.client_sock = socketpair() + self.server = Plan9ServerSocket(use_socket=self.server_sock) + self.client = Plan9ClientSocket(use_socket=self.client_sock) + inode = self.server.filesystem.create('test_file') + inode.data.write(self.sample_data) + + async def ensure_session(self): + self.task = await self.server.async_run() await self.client.start_session() async def close(self): - shutdown_file = await self.client.fid('stop') - await self.client.write(shutdown_file, '') - try: - await self.client.read(shutdown_file) - except Exception as e: - assert json.loads(e.args[0])['status'] == 200 - self.shutdown_response = e.args[0] - self.server.join() + self.task.cancel() diff --git a/tests/test_core/test_plan9/test_basic.py b/tests/test_core/test_plan9/test_basic.py index 3ea6f998e..f06de01cb 100644 --- a/tests/test_core/test_plan9/test_basic.py +++ b/tests/test_core/test_plan9/test_basic.py @@ -3,7 +3,7 @@ @pytest.mark.asyncio async def test_server_data_read(p9): - fid = await p9.client.fid('data') + fid = await p9.client.fid('test_file') response = await p9.client.read(fid) assert response['data'] == p9.sample_data @@ -11,7 +11,7 @@ async def test_server_data_read(p9): @pytest.mark.asyncio async def test_server_data_write(p9): new_sample = b'aevei3PhaeGeiseh' - fid = await p9.client.fid('data') + fid = await p9.client.fid('test_file') await p9.client.write(fid, new_sample) response = await p9.client.read(fid) assert response['data'] == new_sample