Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent autover from failing when used alongside async frameworks #59

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion autover/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def run_cmd(args, cwd=None):
cwd=cwd)
output, error = (str(s.decode()).strip() for s in proc.communicate())

if proc.returncode != 0:
if proc.returncode != 0 or len(error) > 0:
raise Exception(proc.returncode, error)
return output

Expand Down
1 change: 1 addition & 0 deletions conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ test:
source_files:
- tests
commands:
- conda install -y -c conda-forge gunicorn uvicorn starlette
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to declare these dependencies rather than running a command explicitly: https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html#test-requirements

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I first thought about that. Unfortunately, apparently the test requirements cannot refer to a library that is not in the same conda channel as the project; and given that uvicorn is not available in the channel, it had no way of satisfying the requirement unless I explicitly conda installed it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha ha, you can't win, sorry. No need to change then.

@jbednar @jlstevens can I change this project to use conda-forge?

- nosetests -vv --nologcapture

about:
Expand Down
2 changes: 1 addition & 1 deletion examples/PkgBundle/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def run_cmd(args, cwd=None):
cwd=cwd)
output, error = (str(s.decode()).strip() for s in proc.communicate())

if proc.returncode != 0:
if proc.returncode != 0 or len(error) > 0:
raise Exception(proc.returncode, error)
return output

Expand Down
2 changes: 1 addition & 1 deletion examples/pkg_bundle/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def run_cmd(args, cwd=None):
cwd=cwd)
output, error = (str(s.decode()).strip() for s in proc.communicate())

if proc.returncode != 0:
if proc.returncode != 0 or len(error) > 0:
raise Exception(proc.returncode, error)
return output

Expand Down
24 changes: 24 additions & 0 deletions tests/test_asgi_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import autover
from starlette.applications import Starlette

# A simple ASGI server application
# that tries to make use of autover.
#
# Given that autover makes use of
# subprocess.Popen and the program
# workflow depends on that, there
# were some corner cases where
# autover would just crash if we
# tried to get its __version__,
# therefore crashing the whole
# ASGI application.
#
# This test application is used
# with Gunicorn in the unit tests
# (testversion.py), just to check
# if the ASGI application crashes
# or not.

_ = autover.__version__

app = Starlette()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file a "test support file" rather than an actual test? If so, maybe rename it so it's clearly not a file containing tests, and then no need to exclude specially from nose? (Unless there's some reason that's not possible?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it that way just to make sure that the file got bundled when creating the package (as everything that is named test* inside the tests folder does), but I can just modify setup.py to force the file inclusion there after renaming. I don't know which one is more elegant...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as everything that is named test* inside the tests folder does

Do you happen to know if that is a limitation we have put in somewhere here in autover? I wasn't aware of this as a general thing (in setup tools, tox, etc), and I'm only on my phone just now...

31 changes: 31 additions & 0 deletions tests/testversion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"""
Unit test for autover.Version
"""
import os
import unittest
import subprocess
import sys
import time
import pkg_resources._vendor.packaging.version as packaging_version
from autover import Version

Expand Down Expand Up @@ -75,6 +79,7 @@ def pep440(version):


class TestVersion(unittest.TestCase):
maxDiff=None


def git_describe_check(self, describe_tests, index):
Expand Down Expand Up @@ -160,6 +165,32 @@ def test_git_describe_4(self):
def test_git_describe_5(self):
self.git_describe_check(describe_tests, 5)

#=======================================#
# Compatibility with ASGI applications #
#=======================================#

def test_run_asgi_server(self):
"""test_run_asgi_server (An ASGI app that uses autover and is launched with Gunicorn should not crash)"""
if sys.version[0] == "3":
serverfile_dir = os.path.abspath(os.path.join(__file__, os.pardir))
logfile_path = os.path.abspath(os.path.join(__file__, os.pardir, "stderr_gunicorn.log"))
subprocess.Popen(["gunicorn", "-D", "--log-level", "ERROR",
"--chdir", serverfile_dir,
"-k", "uvicorn.workers.UvicornWorker",
"--error-logfile", logfile_path,
"--bind", "unix:/tmp/gunicorn.sock",
"test_asgi_server:app"])
time.sleep(5)
with open(logfile_path, "r") as gunicorn_log_file:
stderr_log = gunicorn_log_file.read()

subprocess.Popen(["pkill", "-f", "gunicorn"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would be better to kill exactly the pid that was created, in case there are other things running. But we might be able to create an entirely simpler test (we'll see once I can reproduce), so let's wait and see.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess you are right. Fortunately, Gunicorn can output the PID that launches, so can be retreived by Python and be killed.

time.sleep(5)
os.remove(logfile_path)
else:
stderr_log = ""
self.assertEqual(stderr_log, "")

if __name__ == "__main__":
import nose
nose.runmodule()
8 changes: 6 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ envlist = py36,py27

[testenv]
passenv = GIT_VERSION
deps = nose
commands = nosetests -vv --nologcapture --with-doctest --with-coverage --cover-package=autover
deps =
nose
gunicorn;python_version>"3.4"
uvicorn;python_version>"3.4"
starlette;python_version>"3.4"
commands = nosetests -vv --nologcapture --with-doctest --with-coverage --cover-package=autover --ignore-files=test_asgi_server.py

[testenv:lint_checks]
deps = flake8
Expand Down