diff options
| author | Andrei Belov <defan@nginx.com> | 2020-10-08 19:19:31 +0300 |
|---|---|---|
| committer | Andrei Belov <defan@nginx.com> | 2020-10-08 19:19:31 +0300 |
| commit | d586ac9fdc4a86c142b06a75dde4cdacad5b52f6 (patch) | |
| tree | 9817282396f9d2cf5333050e4b5bf807d3617e40 /test/test_asgi_lifespan.py | |
| parent | 9be35d9b7418c041e5177f273c20f0fd2d3f00ad (diff) | |
| parent | ad516735a65fe109773b60e26214a071411f1734 (diff) | |
| download | unit-1.20.0-1.tar.gz unit-1.20.0-1.tar.bz2 | |
Merged with the default branch.1.20.0-1
Diffstat (limited to 'test/test_asgi_lifespan.py')
| -rw-r--r-- | test/test_asgi_lifespan.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/test/test_asgi_lifespan.py b/test/test_asgi_lifespan.py new file mode 100644 index 00000000..c37a1aae --- /dev/null +++ b/test/test_asgi_lifespan.py @@ -0,0 +1,80 @@ +import os +from distutils.version import LooseVersion + +import pytest + +from conftest import option +from conftest import public_dir +from unit.applications.lang.python import TestApplicationPython + + +class TestASGILifespan(TestApplicationPython): + prerequisites = { + 'modules': {'python': lambda v: LooseVersion(v) >= LooseVersion('3.5')} + } + load_module = 'asgi' + + def test_asgi_lifespan(self): + self.load('lifespan/empty') + + startup_path = option.test_dir + '/python/lifespan/empty/startup' + shutdown_path = option.test_dir + '/python/lifespan/empty/shutdown' + version_path = option.test_dir + '/python/lifespan/empty/version' + + os.chmod(option.test_dir + '/python/lifespan/empty', 0o777) + + open(startup_path, 'a').close() + os.chmod(startup_path, 0o777) + + open(shutdown_path, 'a').close() + os.chmod(shutdown_path, 0o777) + + open(version_path, 'a').close() + os.chmod(version_path, 0o777) + + assert self.get()['status'] == 204 + + self.stop() + + is_startup = os.path.isfile(startup_path) + is_shutdown = os.path.isfile(shutdown_path) + + if is_startup: + os.remove(startup_path) + + if is_shutdown: + os.remove(shutdown_path) + + with open(version_path, 'r') as f: + version = f.read() + + os.remove(version_path) + + assert not is_startup, 'startup' + assert not is_shutdown, 'shutdown' + assert version == '3.0 2.0', 'version' + + def test_asgi_lifespan_failed(self): + self.load('lifespan/failed') + + assert self.get()['status'] == 503 + + assert ( + self.wait_for_record(r'\[error\].*Application startup failed') + is not None + ), 'error message' + assert self.wait_for_record(r'Exception blah') is not None, 'exception' + + def test_asgi_lifespan_error(self): + self.load('lifespan/error') + + self.get() + + assert self.wait_for_record(r'Exception blah') is not None, 'exception' + + def test_asgi_lifespan_error_auto(self): + self.load('lifespan/error_auto') + + self.get() + + assert self.wait_for_record(r'AssertionError') is not None, 'assertion' |
