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

Inline pytest_tornasync and add downstream tests #30

Merged
merged 2 commits into from
Nov 29, 2022
Merged
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
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ jobs:
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- uses: jupyterlab/maintainer-tools/.github/actions/check-links@v1

jupyter_server_downstream:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1
with:
package_name: jupyter_server

jupyter_client_downstream:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1
with:
package_name: jupyter_client

tests_check: # This job does nothing and is only used for the branch protection
if: always()
needs:
Expand All @@ -130,6 +150,8 @@ jobs:
- test_prereleases
- check_links
- check_release
- jupyter_server_downstream
- jupyter_client_downstream
- test_sdist
runs-on: ubuntu-latest
steps:
Expand Down
6 changes: 0 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ repos:
["flake8-bugbear==22.6.22", "flake8-implicit-str-concat==0.2.0"]
stages: [manual]

- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.28.0
hooks:
- id: eslint
stages: [manual]

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.19.2
hooks:
Expand Down
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,31 @@ to indicate the copyright and license terms:

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.


Pytest Tornasync File License
=============================

The pytest_jupyter/pytest_tornasync.py was originally licensed as:

The MIT License (MIT)

Copyright (c) 2016 eukaryote

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ docs = [
]
client = [
"jupyter_client>=7.4.0",
"ipykernel>=6.14",
"pytest-tornasync>=0.6"
"ipykernel>=6.14"
]
server = [
"jupyter_server>=1.21",
Expand Down Expand Up @@ -94,8 +93,6 @@ timeout_method = "thread"
filterwarnings= [
# Fail on warnings
"error",
# TODO: from jupyter_server
"always:unclosed <socket.socket:ResourceWarning"
]

[tool.coverage.report]
Expand Down
3 changes: 2 additions & 1 deletion pytest_jupyter/jupyter_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"you need. Try: `pip install 'pytest-jupyter[client]'`"
)

# Bring in core plugins.
# Bring in local plugins.
from pytest_jupyter import * # noqa
from pytest_jupyter.pytest_tornasync import * # noqa


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion pytest_jupyter/jupyter_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def jp_asyncio_loop():


@pytest.fixture(autouse=True)
def io_loop(jp_asyncio_loop):
def jp_io_loop(jp_asyncio_loop):
"""Override the io_loop for pytest_tornasync. This is a no-op
if tornado is not installed."""

Expand Down
50 changes: 15 additions & 35 deletions pytest_jupyter/jupyter_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import shutil
import urllib.parse
from binascii import hexlify
from contextlib import closing

import jupyter_core.paths
import pytest
Expand All @@ -25,7 +24,6 @@
from jupyter_server.extension import serverextension
from jupyter_server.serverapp import JUPYTER_SERVICE_HANDLERS, ServerApp
from jupyter_server.utils import url_path_join
from pytest_tornasync.plugin import AsyncHTTPServerClient
from tornado.escape import url_escape
from tornado.httpclient import HTTPClientError
from tornado.websocket import WebSocketHandler
Expand All @@ -44,50 +42,33 @@
)


# Bring in core plugins.
# Bring in local plugins.
from pytest_jupyter import * # noqa
from pytest_jupyter.pytest_tornasync import * # noqa
from pytest_jupyter.utils import mkdir

# List of dependencies needed for this plugin.
pytest_plugins = ["pytest_tornasync"]


# Override some of the fixtures from pytest_tornasync
# The io_loop fixture is overidden in jupyter_core.py so it
# can be shared by other plugins that need it (e.g. jupyter_client.py).


@pytest.fixture
def http_server_client(http_server, io_loop):
"""
Create an asynchronous HTTP client that can fetch from `http_server`.
"""

async def get_client():
return AsyncHTTPServerClient(http_server=http_server)

client = io_loop.run_sync(get_client)
with closing(client) as context:
yield context


@pytest.fixture
def http_server(io_loop, http_server_port, jp_web_app):
def jp_http_server(jp_io_loop, jp_http_server_port, jp_web_app):
"""Start a tornado HTTP server that listens on all available interfaces."""

async def get_server():
server = tornado.httpserver.HTTPServer(jp_web_app)
server.add_socket(http_server_port[0])
server.add_socket(jp_http_server_port[0])
return server

server = io_loop.run_sync(get_server)
server = jp_io_loop.run_sync(get_server)
yield server
server.stop()

if hasattr(server, "close_all_connections"):
io_loop.run_sync(server.close_all_connections)
jp_io_loop.run_sync(server.close_all_connections)

http_server_port[0].close()
jp_http_server_port[0].close()


# End pytest_tornasync overrides
Expand Down Expand Up @@ -126,10 +107,10 @@ def jp_argv():


@pytest.fixture()
def jp_http_port(http_server_port):
def jp_http_port(jp_http_server_port):
"""Returns the port value from the http_server_port fixture."""
yield http_server_port[-1]
http_server_port[0].close()
yield jp_http_server_port[-1]
jp_http_server_port[0].close()


@pytest.fixture
Expand Down Expand Up @@ -186,7 +167,7 @@ def jp_configurable_serverapp(
jp_root_dir,
jp_logging_stream,
jp_asyncio_loop,
io_loop,
jp_io_loop,
):
"""Starts a Jupyter Server instance based on
the provided configuration values.
Expand Down Expand Up @@ -216,7 +197,7 @@ def _configurable_serverapp(
environ=jp_environ,
http_port=jp_http_port,
tmp_path=tmp_path,
io_loop=io_loop,
io_loop=jp_io_loop,
root_dir=jp_root_dir,
**kwargs,
):
Expand Down Expand Up @@ -301,7 +282,7 @@ def jp_base_url():


@pytest.fixture
def jp_fetch(jp_serverapp, http_server_client, jp_auth_header, jp_base_url):
def jp_fetch(jp_serverapp, jp_http_server_client, jp_auth_header, jp_base_url):
"""Sends an (asynchronous) HTTP request to a test server.
The fixture is a factory; it can be called like
a function inside a unit test. Here's a basic
Expand All @@ -328,14 +309,13 @@ def client_fetch(*parts, headers=None, params=None, **kwargs):
for key, value in jp_auth_header.items():
headers.setdefault(key, value)
# Make request.
print(id(http_server_client.io_loop.asyncio_loop))
return http_server_client.fetch(url, headers=headers, request_timeout=20, **kwargs)
return jp_http_server_client.fetch(url, headers=headers, request_timeout=20, **kwargs)

return client_fetch


@pytest.fixture
def jp_ws_fetch(jp_serverapp, http_server_client, jp_auth_header, jp_http_port, jp_base_url):
def jp_ws_fetch(jp_serverapp, jp_http_server_client, jp_auth_header, jp_http_port, jp_base_url):
"""Sends a websocket request to a test server.
The fixture is a factory; it can be called like
a function inside a unit test. Here's a basic
Expand Down
83 changes: 83 additions & 0 deletions pytest_jupyter/pytest_tornasync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Vendored fork of pytest_tornasync from
# https://github.com/eukaryote/pytest-tornasync/blob/9f1bdeec3eb5816e0183f975ca65b5f6f29fbfbb/src/pytest_tornasync/plugin.py

from contextlib import closing
from inspect import iscoroutinefunction

try:
import tornado.ioloop
import tornado.testing
from tornado.simple_httpclient import SimpleAsyncHTTPClient
except ImportError:
SimpleAsyncHTTPClient = object

import pytest


@pytest.hookimpl(tryfirst=True)
def pytest_pycollect_makeitem(collector, name, obj):
if collector.funcnamefilter(name) and iscoroutinefunction(obj):
return list(collector._genfunctions(name, obj))


@pytest.hookimpl(tryfirst=True)
def pytest_pyfunc_call(pyfuncitem):
funcargs = pyfuncitem.funcargs
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}

if not iscoroutinefunction(pyfuncitem.obj):
pyfuncitem.obj(**testargs)
return True

try:
loop = funcargs["jp_io_loop"]
except KeyError:
loop = tornado.ioloop.IOLoop.current()

loop.run_sync(lambda: pyfuncitem.obj(**testargs))
return True


@pytest.fixture
def jp_http_server_port():
"""
Port used by `http_server`.
"""
return tornado.testing.bind_unused_port()


@pytest.fixture
def jp_http_server_client(jp_http_server, jp_io_loop):
"""
Create an asynchronous HTTP client that can fetch from `http_server`.
"""

async def get_client():
return AsyncHTTPServerClient(http_server=jp_http_server)

client = jp_io_loop.run_sync(get_client)
with closing(client) as context:
yield context


class AsyncHTTPServerClient(SimpleAsyncHTTPClient):
def initialize(self, *, http_server=None):
super().initialize()
self._http_server = http_server

def fetch(self, path, **kwargs):
"""
Fetch `path` from test server, passing `kwargs` to the `fetch`
of the underlying `SimpleAsyncHTTPClient`.
"""
return super().fetch(self.get_url(path), **kwargs)

def get_protocol(self):
return "http"

def get_http_port(self):
for sock in self._http_server._sockets.values():
return sock.getsockname()[1]

def get_url(self, path):
return f"{self.get_protocol()}://127.0.0.1:{self.get_http_port()}{path}"