Commit bf11f707 authored by eckhart's avatar eckhart

- test_server.py: python3.5 compatibility

parent 15901cf6
...@@ -138,16 +138,20 @@ def maybe_int(s: str) -> Union[int, str]: ...@@ -138,16 +138,20 @@ def maybe_int(s: str) -> Union[int, str]:
return s return s
def asyncio_run(coroutine: Coroutine) -> Any: def asyncio_run(coroutine: Coroutine, loop=None) -> Any:
"""Backward compatible version of Pyhon 3.7's `asyncio.run()`""" """Backward compatible version of Pyhon 3.7's `asyncio.run()`"""
if sys.version_info >= (3, 7): if sys.version_info >= (3, 7):
return asyncio.run(coroutine) return asyncio.run(coroutine)
else: else:
loop = asyncio.new_event_loop() if loop is None:
asyncio.set_event_loop(loop) myloop = asyncio.new_event_loop()
result = loop.run_until_complete(coroutine) asyncio.set_event_loop(loop)
asyncio.set_event_loop(None) else:
loop.close() myloop = loop
result = myloop.run_until_complete(coroutine)
if loop is None:
asyncio.set_event_loop(None)
myloop.close()
return result return result
...@@ -315,7 +319,7 @@ class Server: ...@@ -315,7 +319,7 @@ class Server:
if rpc_error is None: if rpc_error is None:
try: try:
raw = json.loads(data) raw = json.loads(data.decode())
except json.decoder.JSONDecodeError as e: except json.decoder.JSONDecodeError as e:
rpc_error = -32700, "JSONDecodeError: " + str(e) + str(data) rpc_error = -32700, "JSONDecodeError: " + str(e) + str(data)
...@@ -383,7 +387,7 @@ class Server: ...@@ -383,7 +387,7 @@ class Server:
self.server_messages.put(SERVER_ONLINE) self.server_messages.put(SERVER_ONLINE)
await self.server.serve_forever() await self.server.serve_forever()
def serve_py35(self, host: str = USE_DEFAULT_HOST, port: int = USE_DEFAULT_PORT): def serve_py35(self, host: str = USE_DEFAULT_HOST, port: int = USE_DEFAULT_PORT, loop=None):
if host == USE_DEFAULT_HOST: if host == USE_DEFAULT_HOST:
host = get_config_value('server_default_host') host = get_config_value('server_default_host')
if port == USE_DEFAULT_PORT: if port == USE_DEFAULT_PORT:
...@@ -395,8 +399,11 @@ class Server: ...@@ -395,8 +399,11 @@ class Server:
self.stop_response = "DHParser server at {}:{} stopped!".format(host, port) self.stop_response = "DHParser server at {}:{} stopped!".format(host, port)
self.host.value = host.encode() self.host.value = host.encode()
self.port.value = port self.port.value = port
self.loop = asyncio.new_event_loop() if loop is None:
asyncio.set_event_loop(self.loop) self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
else:
self.loop = loop
self.server = cast( self.server = cast(
asyncio.base_events.Server, asyncio.base_events.Server,
self.loop.run_until_complete( self.loop.run_until_complete(
...@@ -406,7 +413,9 @@ class Server: ...@@ -406,7 +413,9 @@ class Server:
self.server_messages.put(SERVER_ONLINE) self.server_messages.put(SERVER_ONLINE)
self.loop.run_forever() self.loop.run_forever()
finally: finally:
asyncio.set_event_loop(None) if loop is None:
asyncio.set_event_loop(None)
self.loop.close()
self.server.close() self.server.close()
asyncio_run(self.server.wait_closed()) asyncio_run(self.server.wait_closed())
...@@ -414,7 +423,7 @@ class Server: ...@@ -414,7 +423,7 @@ class Server:
while not self.server_messages.empty(): while not self.server_messages.empty():
self.server_messages.get() self.server_messages.get()
def run_server(self, host: str = USE_DEFAULT_HOST, port: int = USE_DEFAULT_PORT): def run_server(self, host: str = USE_DEFAULT_HOST, port: int = USE_DEFAULT_PORT, loop=None):
""" """
Starts a DHParser-Server. This function will not return until the Starts a DHParser-Server. This function will not return until the
DHParser-Server ist stopped by sending a STOP_SERVER_REQUEST. DHParser-Server ist stopped by sending a STOP_SERVER_REQUEST.
...@@ -426,7 +435,7 @@ class Server: ...@@ -426,7 +435,7 @@ class Server:
if sys.version_info >= (3, 7): if sys.version_info >= (3, 7):
asyncio.run(self.serve(host, port)) asyncio.run(self.serve(host, port))
else: else:
self.serve_py35(host, port) self.serve_py35(host, port, loop)
except CancelledError: except CancelledError:
pass pass
self.pp_executor = None self.pp_executor = None
......
...@@ -35,10 +35,13 @@ def run_doctests(module): ...@@ -35,10 +35,13 @@ def run_doctests(module):
if __name__ == "__main__": if __name__ == "__main__":
interpreters = ['python3 ' if os.system('python3 -V') == 0 else 'python '] interpreters = ['python3 ' if os.system('python3 -V') == 0 else 'python ']
if os.system('python3.5 -V') == 0:
interpreters.append('python3.5 ')
if os.system('pypy3 -V') == 0: if os.system('pypy3 -V') == 0:
interpreters.append('pypy3 ') interpreters.append('pypy3 ')
elif os.system('pypy -V') == 0: elif os.system('pypy -V') == 0:
interpreters.append('pypy ') interpreters.append('pypy ')
print('Interpreters found: ' + ''.join(interpreters))
cwd = os.getcwd() cwd = os.getcwd()
os.chdir(os.path.join(scriptdir, '..')) os.chdir(os.path.join(scriptdir, '..'))
......
...@@ -126,8 +126,10 @@ class TestServer: ...@@ -126,8 +126,10 @@ class TestServer:
"""Test, whether delegation of (long-running) tasks to """Test, whether delegation of (long-running) tasks to
processes or threads works.""" processes or threads works."""
sequence = [] sequence = []
SLOW = '0.1' if self.windows else '0.01' # TODO: Test this with more linux systems if self.windows:
FAST = '0.01' if self.windows else '0.001' SLOW, FAST = '0.1', '0.01'
else:
SLOW, FAST = '0.01', '0.001'
async def call_remote(argument): async def call_remote(argument):
sequence.append(argument) sequence.append(argument)
...@@ -140,26 +142,27 @@ class TestServer: ...@@ -140,26 +142,27 @@ class TestServer:
await asyncio.gather(call_remote(SLOW), await asyncio.gather(call_remote(SLOW),
call_remote(FAST)) call_remote(FAST))
cs = Server(self.long_running, if sys.version_info >= (3, 6):
cpu_bound=frozenset(['long_running']), cs = Server(self.long_running,
blocking=frozenset()) cpu_bound=frozenset(['long_running']),
try: blocking=frozenset())
cs.spawn_server('127.0.0.1', 8888) try:
asyncio_run(run_tasks()) cs.spawn_server('127.0.0.1', 8888)
assert sequence == [SLOW, FAST, FAST, SLOW], str(sequence) asyncio_run(run_tasks())
finally: assert sequence == [SLOW, FAST, FAST, SLOW], str(sequence)
cs.terminate_server() finally:
cs.terminate_server()
cs = Server(self.long_running,
cpu_bound=frozenset(), cs = Server(self.long_running,
blocking=frozenset(['long_running'])) cpu_bound=frozenset(),
try: blocking=frozenset(['long_running']))
sequence = [] try:
cs.spawn_server('127.0.0.1', 8888) sequence = []
asyncio_run(run_tasks()) cs.spawn_server('127.0.0.1', 8888)
assert sequence == [SLOW, FAST, FAST, SLOW] asyncio_run(run_tasks())
finally: assert sequence == [SLOW, FAST, FAST, SLOW]
cs.terminate_server() finally:
cs.terminate_server()
cs = Server(self.long_running, cs = Server(self.long_running,
cpu_bound=frozenset(), cpu_bound=frozenset(),
......
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