Skip to content

Commit

Permalink
tests: remove unused fixtures and local symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
belm0 committed Mar 28, 2024
1 parent 52ce346 commit 6a004bb
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async def test_serve(nursery):
# The server nursery begins with one task (server.listen).
assert len(nursery.child_tasks) == 1
no_clients_nursery_count = len(task.child_nurseries)
async with open_websocket(HOST, port, RESOURCE, use_ssl=False) as conn:
async with open_websocket(HOST, port, RESOURCE, use_ssl=False):
# The server nursery has the same number of tasks, but there is now
# one additional nested nursery.
assert len(nursery.child_tasks) == 1
Expand All @@ -220,25 +220,22 @@ async def test_serve_ssl(nursery):


async def test_serve_handler_nursery(nursery):
task = current_task()
async with trio.open_nursery() as handler_nursery:
serve_with_nursery = partial(serve_websocket, echo_request_handler,
HOST, 0, None, handler_nursery=handler_nursery)
server = await nursery.start(serve_with_nursery)
port = server.port
# The server nursery begins with one task (server.listen).
assert len(nursery.child_tasks) == 1
no_clients_nursery_count = len(task.child_nurseries)
async with open_websocket(HOST, port, RESOURCE, use_ssl=False) as conn:
async with open_websocket(HOST, port, RESOURCE, use_ssl=False):
# The handler nursery should have one task in it
# (conn._reader_task).
assert len(handler_nursery.child_tasks) == 1


async def test_serve_with_zero_listeners(nursery):
task = current_task()
async def test_serve_with_zero_listeners():
with pytest.raises(ValueError):
server = WebSocketServer(echo_request_handler, [])
WebSocketServer(echo_request_handler, [])


async def test_serve_non_tcp_listener(nursery):
Expand Down Expand Up @@ -290,7 +287,7 @@ async def test_client_open_url(path, expected_path, echo_server):

async def test_client_open_invalid_url(echo_server):
with pytest.raises(ValueError):
async with open_websocket_url('http://foo.com/bar') as conn:
async with open_websocket_url('http://foo.com/bar'):
pass


Expand Down Expand Up @@ -358,11 +355,10 @@ async def handler(request):
assert not request.local.is_ssl
assert str(request.remote.address) == HOST
assert not request.remote.is_ssl
conn = await request.accept()
await request.accept()

server = await nursery.start(serve_websocket, handler, HOST, 0, None)
async with open_websocket(HOST, server.port, RESOURCE, use_ssl=False
) as client_ws:
async with open_websocket(HOST, server.port, RESOURCE, use_ssl=False):
pass


Expand Down Expand Up @@ -410,7 +406,7 @@ async def handler(request):
async def test_handshake_server_headers(nursery):
async def handler(request):
headers = [('X-Test-Header', 'My test header')]
server_ws = await request.accept(extra_headers=headers)
await request.accept(extra_headers=headers)

server = await nursery.start(serve_websocket, handler, HOST, 0, None)
async with open_websocket(HOST, server.port, RESOURCE, use_ssl=False
Expand All @@ -433,7 +429,7 @@ async def handler(request):
server = await nursery.start(serve_websocket, handler, HOST, 0,
None)
async with open_websocket(HOST, server.port, RESOURCE,
use_ssl=False) as client_ws:
use_ssl=False):
pass


Expand All @@ -445,8 +441,7 @@ async def handler(request):

server = await nursery.start(serve_websocket, handler, HOST, 0, None)
with pytest.raises(ConnectionRejected) as exc_info:
async with open_websocket(HOST, server.port, RESOURCE, use_ssl=False,
) as client_ws:
async with open_websocket(HOST, server.port, RESOURCE, use_ssl=False):
pass
exc = exc_info.value
assert exc.body == b'My body'
Expand All @@ -467,16 +462,15 @@ async def handler(stream):
port = listeners[0].socket.getsockname()[1]

with pytest.raises(ConnectionRejected) as exc_info:
async with open_websocket(HOST, port, RESOURCE, use_ssl=False,
) as client_ws:
async with open_websocket(HOST, port, RESOURCE, use_ssl=False):
pass
exc = exc_info.value
assert exc.status_code == 100
assert repr(exc) == 'ConnectionRejected<status_code=100>'
assert exc.body is None


async def test_handshake_protocol_error(nursery, echo_server):
async def test_handshake_protocol_error(echo_server):
'''
If a client connects to a trio-websocket server and tries to speak HTTP
instead of WebSocket, the server should reject the connection. (If the
Expand Down Expand Up @@ -604,15 +598,15 @@ async def test_client_open_timeout(nursery, autojump_clock):
'''
async def handler(request):
await trio.sleep(FORCE_TIMEOUT)
server_ws = await request.accept()
await request.accept()
pytest.fail('Should not reach this line.')

server = await nursery.start(
partial(serve_websocket, handler, HOST, 0, ssl_context=None))

with pytest.raises(ConnectionTimeout):
async with open_websocket(HOST, server.port, '/', use_ssl=False,
connect_timeout=TIMEOUT) as client_ws:
connect_timeout=TIMEOUT):
pass


Expand Down Expand Up @@ -649,7 +643,7 @@ async def test_client_connect_networking_error():
connect_websocket_mock:
connect_websocket_mock.side_effect = OSError()
with pytest.raises(HandshakeError):
async with open_websocket(HOST, 0, '/', use_ssl=False) as client_ws:
async with open_websocket(HOST, 0, '/', use_ssl=False):
pass


Expand All @@ -672,7 +666,7 @@ async def handler(request):

old_task_count = len(nursery.child_tasks)
# This stream is not a WebSocket, so it won't send a handshake:
stream = await trio.open_tcp_stream(HOST, server.port)
await trio.open_tcp_stream(HOST, server.port)
# Checkpoint so the server's handler task can spawn:
await trio.sleep(0)
assert len(nursery.child_tasks) == old_task_count + 1, \
Expand Down Expand Up @@ -713,7 +707,7 @@ async def handler(request):
# Spawn client inside an inner nursery so that we can cancel it's reader
# so that it won't do a closing handshake.
async with trio.open_nursery() as inner:
ws = await connect_websocket(inner, HOST, server.port, RESOURCE,
await connect_websocket(inner, HOST, server.port, RESOURCE,
use_ssl=False)
# Checkpoint so the server can spawn a handler task:
await trio.sleep(0)
Expand Down Expand Up @@ -785,7 +779,7 @@ async def handler(stream):

async def test_server_handler_exit(nursery, autojump_clock):
async def handler(request):
server_ws = await request.accept()
await request.accept()
await trio.sleep(1)

server = await nursery.start(
Expand Down

0 comments on commit 6a004bb

Please sign in to comment.