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

Format with black. Update test suite to work with newer Trio and pytest versions #138

Merged
merged 3 commits into from
Jan 30, 2024
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
14 changes: 7 additions & 7 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ python -m pip --version

python -m pip install .

# See https://github.com/python-trio/trio/issues/334
YAPF_VERSION=0.20.0
BLACK_VERSION=24.1.0

if [ "$CHECK_FORMATTING" = "1" ]; then
pip install yapf==${YAPF_VERSION}
if ! yapf -rpd setup.py trio_asyncio; then
pip install black==${BLACK_VERSION}
if ! black --check setup.py tests trio_asyncio; then
black --diff setup.py tests trio_asyncio
cat <<EOF
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Formatting problems were found (listed above). To fix them, run

pip install yapf==${YAPF_VERSION}
yapf -rpi setup.py trio_asyncio
pip install black==${BLACK_VERSION}
black setup.py tests trio_asyncio

in your local checkout.

Expand All @@ -77,7 +77,7 @@ else
# 'coverage xml' to generate the report that it uses, and that will only
# apply the ignore patterns in the current directory's .coveragerc.
cp ../.coveragerc .
if pytest -ra --junitxml=../test-results.xml --cov=trio_asyncio --verbose ../tests; then
if pytest -ra --junitxml=../test-results.xml --cov=trio_asyncio --verbose --timeout=30 ../tests; then
PASSED=true
else
PASSED=false
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
include_package_data=True,
python_requires=">=3.8",
keywords=["async", "io", "trio", "asyncio", "trio-asyncio"],
setup_requires=['pytest-runner'],
tests_require=['pytest >= 5.4', 'pytest-trio >= 0.6', 'outcome'],
setup_requires=["pytest-runner"],
tests_require=["pytest >= 5.4", "pytest-trio >= 0.6", "outcome"],
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pytest >= 5.4 # for the Node.from_parent() transition
pytest-cov
pytest-trio
outcome
pytest-timeout
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# asyncio.loop logs some things we might need to see.
import logging

logging.basicConfig()
del logging

# asyncio runs many error messages through reprlib,
# which defaults to fairly short strings,
# which is a major PITA.
from reprlib import aRepr

aRepr.maxstring = 9999
aRepr.maxother = 9999
aRepr.maxlong = 9999
Expand Down
1 change: 1 addition & 0 deletions tests/aiotest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time

import asyncio as _asyncio

socketpair = socket.socketpair


Expand Down
11 changes: 6 additions & 5 deletions tests/aiotest/test_add_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
class TestAddReader:
@pytest.mark.trio
async def test_add_reader(self, loop):
result = {'received': None}
result = {"received": None}
rsock, wsock = socketpair()
ready = trio.Event()
try:

def reader():
data = rsock.recv(100)
result['received'] = data
result["received"] = data
loop.remove_reader(rsock)
ready.set()

def writer():
loop.remove_writer(wsock)
loop.call_soon(wsock.send, b'abc')
loop.call_soon(wsock.send, b"abc")

loop.add_reader(rsock, reader)
loop.add_writer(wsock, writer)

await ready.wait()
assert result['received'] == b'abc'
assert result["received"] == b"abc"

finally:
rsock.close()
Expand All @@ -36,12 +36,13 @@ async def check_add_replace(self, event, loop, config):
socket = config.socket

selector = loop._selector
if event == 'reader':
if event == "reader":
add_sock = loop.add_reader
remove_sock = loop.remove_reader

def get_handle(fileobj):
return selector.get_key(fileobj).data[0]

else:
add_sock = loop.add_writer
remove_sock = loop.remove_writer
Expand Down
24 changes: 12 additions & 12 deletions tests/aiotest/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ async def test_call_soon(self, loop):
result = []

def hello_world(loop):
result.append('Hello World')
result.append("Hello World")
loop.stop()

loop.call_soon(hello_world, loop)
await loop.stop().wait()
assert result == ['Hello World']
assert result == ["Hello World"]

@pytest.mark.trio
async def test_call_soon_control(self, loop):
Expand All @@ -33,7 +33,7 @@ def append_result(loop, result, value):
# http://bugs.python.org/issue22875: Ensure that call_soon() does not
# call append_result() immediately, but when control returns to the
# event loop, when func() is done.
assert result == ['[]', 'yes']
assert result == ["[]", "yes"]

@pytest.mark.trio
async def test_close(self, loop, config):
Expand All @@ -50,23 +50,23 @@ async def test():
func = lambda: False
coro = test()
try:
with pytest.raises(RuntimeError, match='not a sync loop'):
with pytest.raises(RuntimeError, match="not a sync loop"):
loop.run_until_complete(None)
with pytest.raises(RuntimeError):
loop.run_forever()
with pytest.raises(RuntimeError, match='Event loop is closed'):
with pytest.raises(RuntimeError, match="Event loop is closed"):
loop.call_soon(func)
with pytest.raises(RuntimeError, match='Event loop is closed'):
with pytest.raises(RuntimeError, match="Event loop is closed"):
loop.call_soon_threadsafe(func)
with pytest.raises(RuntimeError, match='Event loop is closed'):
with pytest.raises(RuntimeError, match="Event loop is closed"):
loop.call_later(1.0, func)
with pytest.raises(RuntimeError, match='Event loop is closed'):
loop.call_at(loop.time() + .0, func)
with pytest.raises(RuntimeError, match='Event loop is closed'):
with pytest.raises(RuntimeError, match="Event loop is closed"):
loop.call_at(loop.time() + 0.0, func)
with pytest.raises(RuntimeError, match="Event loop is closed"):
loop.run_in_executor(None, func)
with pytest.raises(RuntimeError, match='Event loop is closed'):
with pytest.raises(RuntimeError, match="Event loop is closed"):
await loop.run_aio_coroutine(coro)
with pytest.raises(RuntimeError, match='Event loop is closed'):
with pytest.raises(RuntimeError, match="Event loop is closed"):
loop.add_signal_handler(signal.SIGTERM, func)
finally:
coro.close()
6 changes: 3 additions & 3 deletions tests/aiotest/test_coroutine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async def hello_world(asyncio, result, delay, loop):
result.append("Hello")
# retrieve the event loop from the policy
await asyncio.sleep(delay)
result.append('World')
result.append("World")
return "."


Expand All @@ -17,7 +17,7 @@ async def test_hello_world(self, loop, config):
result = []
coro = hello_world(config.asyncio, result, 0.001, loop)
await loop.run_aio_coroutine(config.asyncio.ensure_future(coro))
assert result == ['Hello', 'World']
assert result == ["Hello", "World"]

@pytest.mark.trio
async def test_waiter(self, loop, config):
Expand All @@ -33,4 +33,4 @@ async def waiter(asyncio, hello_world, result):

result = []
await trio_asyncio.aio_as_trio(waiter)(config.asyncio, hello_world, result)
assert result == ['Future', 'Hello', 'World', '.']
assert result == ["Future", "Hello", "World", "."]
16 changes: 8 additions & 8 deletions tests/aiotest/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class TcpEchoClientProtocol(asyncio.Protocol):
def __init__(self, message, loop):
self.message = message
self.loop = loop
self.state = 'new'
self.state = "new"
self.received = None

def connection_made(self, transport):
self.state = 'ping'
self.state = "ping"
transport.write(self.message)

def data_received(self, data):
self.state = 'pong'
self.state = "pong"
self.received = data

def connection_lost(self, exc):
self.state = 'closed'
self.state = "closed"
self.loop.stop()

class TcpServer(threading.Thread):
Expand Down Expand Up @@ -67,8 +67,8 @@ class TestNetwork(aiotest.TestCase):
async def test_tcp_hello(self, loop, config):
return
port = 8888
host = '127.0.0.1'
message = b'Hello World!'
host = "127.0.0.1"
message = b"Hello World!"

event = config.threading.Event()
TcpEchoClientProtocol, TcpServer = create_classes(config)
Expand All @@ -80,8 +80,8 @@ async def test_tcp_hello(self, loop, config):
proto = TcpEchoClientProtocol(message, loop)
coro = loop.create_connection(lambda: proto, host, port)
await loop.run_aio_coroutine(coro)
assert proto.state != 'new'
assert proto.state != "new"

await loop.stop().wait()()
assert proto.state == 'closed'
assert proto.state == "closed"
assert proto.received == message
14 changes: 7 additions & 7 deletions tests/aiotest/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ async def test_ident(self, loop, config):
except AttributeError:
get_ident = threading._get_ident # Python 2

result = {'ident': None}
result = {"ident": None}

def work():
result['ident'] = get_ident()
result["ident"] = get_ident()

fut = loop.run_in_executor(None, work)
await loop.run_aio_coroutine(fut)

# ensure that work() was executed in a different thread
work_ident = result['ident']
work_ident = result["ident"]
assert work_ident is not None
assert work_ident != get_ident()

Expand All @@ -45,18 +45,18 @@ def work():
@pytest.mark.trio
async def test_policy(self, loop, config):
asyncio = config.asyncio
result = {'loop': 'not set'} # sentinel, different than None
result = {"loop": "not set"} # sentinel, different than None

def work():
try:
result['loop'] = asyncio.get_event_loop()
result["loop"] = asyncio.get_event_loop()
except Exception as exc:
result['loop'] = exc
result["loop"] = exc

# get_event_loop() must return None in a different thread
fut = loop.run_in_executor(None, work)
await loop.run_aio_future(fut)
assert isinstance(result['loop'], (AssertionError, RuntimeError))
assert isinstance(result["loop"], (AssertionError, RuntimeError))

@pytest.mark.trio
async def test_run_in_thread(self, config):
Expand Down
Loading
Loading