Skip to content

Commit

Permalink
ci/core: update plan9 tests and simplify fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
svinota committed Jan 20, 2025
1 parent 35d254e commit 96046cb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 60 deletions.
2 changes: 1 addition & 1 deletion tests/test_core/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
67 changes: 10 additions & 57 deletions tests/test_core/pr2test/plan9.py
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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()
4 changes: 2 additions & 2 deletions tests/test_core/test_plan9/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

@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


@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
Expand Down

0 comments on commit 96046cb

Please sign in to comment.