2020-06-03 14:45:07 +01:00
|
|
|
import os
|
|
|
|
import secrets
|
|
|
|
import sys
|
|
|
|
|
2020-06-28 12:45:52 +01:00
|
|
|
from contextlib import suppress
|
2020-06-28 06:43:12 +01:00
|
|
|
from subprocess import PIPE, Popen, TimeoutExpired
|
2020-06-03 14:45:07 +01:00
|
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from textwrap import dedent
|
|
|
|
from threading import Timer
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
# We need to interrupt the autoreloader without killing it, so that the server gets terminated
|
|
|
|
# https://stefan.sofa-rockers.org/2013/08/15/handling-sub-process-hierarchies-python-linux-os-x/
|
|
|
|
|
|
|
|
try:
|
|
|
|
from signal import CTRL_BREAK_EVENT
|
|
|
|
from subprocess import CREATE_NEW_PROCESS_GROUP
|
|
|
|
|
|
|
|
flags = CREATE_NEW_PROCESS_GROUP
|
|
|
|
except ImportError:
|
|
|
|
flags = 0
|
|
|
|
|
2021-06-18 09:39:09 +01:00
|
|
|
TIMER_DELAY = 2
|
|
|
|
|
2020-06-28 12:45:52 +01:00
|
|
|
|
2020-06-03 14:45:07 +01:00
|
|
|
def terminate(proc):
|
|
|
|
if flags:
|
|
|
|
proc.send_signal(CTRL_BREAK_EVENT)
|
|
|
|
else:
|
|
|
|
proc.terminate()
|
|
|
|
|
2020-06-28 12:45:52 +01:00
|
|
|
|
2020-06-03 14:45:07 +01:00
|
|
|
def write_app(filename, **runargs):
|
|
|
|
text = secrets.token_urlsafe()
|
|
|
|
with open(filename, "w") as f:
|
2020-06-28 12:45:52 +01:00
|
|
|
f.write(
|
|
|
|
dedent(
|
|
|
|
f"""\
|
2020-06-03 14:45:07 +01:00
|
|
|
import os
|
|
|
|
from sanic import Sanic
|
|
|
|
|
|
|
|
app = Sanic(__name__)
|
|
|
|
|
2021-02-08 10:18:29 +00:00
|
|
|
app.route("/")(lambda x: x)
|
|
|
|
|
2020-06-03 14:45:07 +01:00
|
|
|
@app.listener("after_server_start")
|
|
|
|
def complete(*args):
|
|
|
|
print("complete", os.getpid(), {text!r})
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(**{runargs!r})
|
|
|
|
"""
|
2020-06-28 12:45:52 +01:00
|
|
|
)
|
|
|
|
)
|
2020-06-03 14:45:07 +01:00
|
|
|
return text
|
|
|
|
|
2020-06-28 12:45:52 +01:00
|
|
|
|
2021-06-18 09:39:09 +01:00
|
|
|
def write_json_config_app(filename, jsonfile, **runargs):
|
|
|
|
with open(filename, "w") as f:
|
|
|
|
f.write(
|
|
|
|
dedent(
|
|
|
|
f"""\
|
|
|
|
import os
|
|
|
|
from sanic import Sanic
|
|
|
|
import json
|
|
|
|
|
|
|
|
app = Sanic(__name__)
|
|
|
|
with open("{jsonfile}", "r") as f:
|
|
|
|
config = json.load(f)
|
|
|
|
app.config.update_config(config)
|
|
|
|
|
|
|
|
app.route("/")(lambda x: x)
|
|
|
|
|
|
|
|
@app.listener("after_server_start")
|
|
|
|
def complete(*args):
|
|
|
|
print("complete", os.getpid(), app.config.FOO)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(**{runargs!r})
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def write_file(filename):
|
|
|
|
text = secrets.token_urlsafe()
|
|
|
|
with open(filename, "w") as f:
|
|
|
|
f.write(f"""{{"FOO": "{text}"}}""")
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
2020-06-03 14:45:07 +01:00
|
|
|
def scanner(proc):
|
|
|
|
for line in proc.stdout:
|
|
|
|
line = line.decode().strip()
|
|
|
|
if line.startswith("complete"):
|
|
|
|
yield line
|
|
|
|
|
|
|
|
|
|
|
|
argv = dict(
|
|
|
|
script=[sys.executable, "reloader.py"],
|
|
|
|
module=[sys.executable, "-m", "reloader"],
|
2020-06-28 12:45:52 +01:00
|
|
|
sanic=[
|
|
|
|
sys.executable,
|
|
|
|
"-m",
|
|
|
|
"sanic",
|
|
|
|
"--port",
|
2021-12-21 20:56:12 +00:00
|
|
|
"42204",
|
2020-06-28 12:45:52 +01:00
|
|
|
"--debug",
|
|
|
|
"reloader.app",
|
|
|
|
],
|
2020-06-03 14:45:07 +01:00
|
|
|
)
|
|
|
|
|
2020-06-28 12:45:52 +01:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"runargs, mode",
|
|
|
|
[
|
2021-12-13 17:48:30 +00:00
|
|
|
(dict(port=42202, auto_reload=True), "script"),
|
|
|
|
(dict(port=42203, debug=True), "module"),
|
2021-03-05 08:26:03 +00:00
|
|
|
({}, "sanic"),
|
2020-06-28 12:45:52 +01:00
|
|
|
],
|
|
|
|
)
|
2021-12-21 20:56:12 +00:00
|
|
|
@pytest.mark.xfail
|
2020-06-03 14:45:07 +01:00
|
|
|
async def test_reloader_live(runargs, mode):
|
|
|
|
with TemporaryDirectory() as tmpdir:
|
|
|
|
filename = os.path.join(tmpdir, "reloader.py")
|
|
|
|
text = write_app(filename, **runargs)
|
2021-06-18 09:39:09 +01:00
|
|
|
command = argv[mode]
|
|
|
|
proc = Popen(command, cwd=tmpdir, stdout=PIPE, creationflags=flags)
|
2020-06-03 14:45:07 +01:00
|
|
|
try:
|
2021-06-18 09:39:09 +01:00
|
|
|
timeout = Timer(TIMER_DELAY, terminate, [proc])
|
2020-06-03 14:45:07 +01:00
|
|
|
timeout.start()
|
|
|
|
# Python apparently keeps using the old source sometimes if
|
|
|
|
# we don't sleep before rewrite (pycache timestamp problem?)
|
|
|
|
sleep(1)
|
|
|
|
line = scanner(proc)
|
|
|
|
assert text in next(line)
|
|
|
|
# Edit source code and try again
|
|
|
|
text = write_app(filename, **runargs)
|
|
|
|
assert text in next(line)
|
|
|
|
finally:
|
|
|
|
timeout.cancel()
|
|
|
|
terminate(proc)
|
2020-06-28 06:43:12 +01:00
|
|
|
with suppress(TimeoutExpired):
|
|
|
|
proc.wait(timeout=3)
|
2021-06-18 09:39:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"runargs, mode",
|
|
|
|
[
|
2021-12-13 17:48:30 +00:00
|
|
|
(dict(port=42302, auto_reload=True), "script"),
|
|
|
|
(dict(port=42303, debug=True), "module"),
|
2021-06-18 09:39:09 +01:00
|
|
|
({}, "sanic"),
|
|
|
|
],
|
|
|
|
)
|
2021-12-21 20:56:12 +00:00
|
|
|
@pytest.mark.xfail
|
2021-06-18 09:39:09 +01:00
|
|
|
async def test_reloader_live_with_dir(runargs, mode):
|
|
|
|
with TemporaryDirectory() as tmpdir:
|
|
|
|
filename = os.path.join(tmpdir, "reloader.py")
|
|
|
|
config_file = os.path.join(tmpdir, "config.json")
|
|
|
|
runargs["reload_dir"] = tmpdir
|
|
|
|
write_json_config_app(filename, config_file, **runargs)
|
|
|
|
text = write_file(config_file)
|
|
|
|
command = argv[mode]
|
|
|
|
if mode == "sanic":
|
|
|
|
command += ["--reload-dir", tmpdir]
|
|
|
|
proc = Popen(command, cwd=tmpdir, stdout=PIPE, creationflags=flags)
|
|
|
|
try:
|
|
|
|
timeout = Timer(TIMER_DELAY, terminate, [proc])
|
|
|
|
timeout.start()
|
|
|
|
# Python apparently keeps using the old source sometimes if
|
|
|
|
# we don't sleep before rewrite (pycache timestamp problem?)
|
|
|
|
sleep(1)
|
|
|
|
line = scanner(proc)
|
|
|
|
assert text in next(line)
|
|
|
|
# Edit source code and try again
|
|
|
|
text = write_file(config_file)
|
|
|
|
assert text in next(line)
|
|
|
|
finally:
|
|
|
|
timeout.cancel()
|
|
|
|
terminate(proc)
|
|
|
|
with suppress(TimeoutExpired):
|
|
|
|
proc.wait(timeout=3)
|