Commit 018dbb8b authored by di68kap's avatar di68kap

- server.py and test/test_server.py: bug fixes

parent 3e54b254
......@@ -121,7 +121,7 @@ def identify_server():
def json_rpc(func: Callable,
params: Union[List[JSON_Type], Dict[str, JSON_Type]]=[],
params: Union[List[JSON_Type], Dict[str, JSON_Type]]=(),
ID: Optional[int]=None) -> str:
"""Generates a JSON-RPC-call for `func` with parameters `params`"""
return json.dumps({"jsonrpc": "2.0", "method": func.__name__, "params": params, "id": ID})
......@@ -202,7 +202,7 @@ class Server:
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter):
rpc_error = None # type: Optional[Tuple[int, str]]
json_id = 'null' # type: Tuple[int, str]
json_id = 'null' # type: Union[int, str]
obj = {} # type: Dict
result = None # type: JSON_Type
raw = None # type: JSON_Type
......@@ -217,8 +217,7 @@ class Server:
response = RESPONSE_HEADER.format(date=gmt, length=len(encoded_html))
return response.encode() + encoded_html
async def run(method_name: str, method: Callable, params: Union[Dict, Sequence]) \
-> Tuple[JSON_Type, Optional[Tuple[int, str]]]:
async def run(method_name: str, method: Callable, params: Union[Dict, Sequence]):
nonlocal result, rpc_error
try:
# run method either a) directly if it is short running or
......@@ -281,7 +280,7 @@ class Server:
else:
func_name = self.default
argument = data.decode()
err_func = lambda arg: 'Function %() no found!' % func_name
err_func = lambda arg: 'Function %s no found!' % func_name
func = self.rpc_table.get(func_name, err_func)
await run(func_name, func, () if argument is None else (argument,))
if rpc_error is None:
......
......@@ -45,6 +45,9 @@ class TestServer:
# cs = Server(compiler_dummy)
# cs.run_server()
def setup(self):
self.windows = sys.platform.lower().find('win') >= 0
def compiler_dummy(self, src: str) -> str:
return src
......@@ -122,6 +125,8 @@ class TestServer:
"""Test, whether delegation of (long-running) tasks to
processes or threads works."""
sequence = []
SLOW = '0.1' if self.windows else '0.01' # TODO: Test this with more linux systems
FAST = '0.01' if self.windows else '0.001'
async def call_remote(argument):
sequence.append(argument)
......@@ -131,8 +136,8 @@ class TestServer:
writer.close()
async def run_tasks():
await asyncio.gather(call_remote('0.01'),
call_remote('0.001'))
await asyncio.gather(call_remote(SLOW),
call_remote(FAST))
cs = Server(self.long_running,
cpu_bound=frozenset(['long_running']),
......@@ -140,7 +145,7 @@ class TestServer:
try:
cs.spawn_server('127.0.0.1', 8888)
asyncio_run(run_tasks())
assert sequence == ['0.01', '0.001', '0.001', '0.01'], str(sequence)
assert sequence == [SLOW, FAST, FAST, SLOW], str(sequence)
finally:
cs.terminate_server()
......@@ -151,7 +156,7 @@ class TestServer:
sequence = []
cs.spawn_server('127.0.0.1', 8888)
asyncio_run(run_tasks())
assert sequence == ['0.01', '0.001', '0.001', '0.01']
assert sequence == [SLOW, FAST, FAST, SLOW]
finally:
cs.terminate_server()
......@@ -162,7 +167,8 @@ class TestServer:
sequence = []
cs.spawn_server('127.0.0.1', 8888)
asyncio_run(run_tasks())
assert sequence == ['0.01', '0.001', '0.01', '0.001']
# if run asyncronously, order os results is arbitrary
assert sequence.count(SLOW) == 2 and sequence.count(FAST) == 2
finally:
cs.terminate_server()
......
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