Commit 261b1758 authored by eckhart's avatar eckhart

- sync commit

parent 3d55f49e
......@@ -35,6 +35,7 @@ of module `server`, i.e. the compilation-modules, to decide.
import asyncio
from multiprocessing import Value, Queue
from typing import Callable, Any
from DHParser.preprocess import BEGIN_TOKEN, END_TOKEN, TOKEN_DELIMITER
......@@ -47,10 +48,18 @@ SERVER_ERROR = "COMPILER-SERVER-ERROR"
CompileFunc = Callable[[str, str], Any] # compiler_src(source: str, log_dir: str) -> Any
SERVER_OFFLINE = 0
SERVER_STARTING = 1
SERVER_ONLINE = 2
class CompilerServer:
def __init__(self, compiler: CompileFunc):
self.compiler = compiler
self.max_source_size = get_config_value('max_source_size')
self.stage = Value('b', SERVER_OFFLINE)
self.server_messages = Queue()
async def handle_compilation_request(self,
reader: asyncio.StreamReader,
......@@ -68,7 +77,16 @@ class CompilerServer:
async def serve(self, address: str='127.0.0.1', port: int=8888):
server = await asyncio.start_server(self.handle_compilation_request, address, port)
async with server:
self.stage.value = SERVER_ONLINE
self.server_messages.put(SERVER_ONLINE)
await server.serve_forever()
def run_server(self, address: str='127.0.0.1', port: int=8888):
self.stage.value = SERVER_STARTING
asyncio.run(self.serve(address, port))
def wait_until_server_online(self):
if self.server_messages.get() != SERVER_ONLINE:
raise AssertionError('could not start server!?')
assert self.stage.value == SERVER_ONLINE
......@@ -41,6 +41,8 @@ class TestServer:
cs = CompilerServer(compiler_dummy)
p = Process(target=cs.run_server)
p.start()
cs.wait_until_server_online()
async def compile(src, log_dir):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write(src.encode())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment