Skip to content

Commit

Permalink
Fix more timing issues on Mac and Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Booth committed Feb 4, 2021
1 parent 4268d63 commit f0689fa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
3 changes: 2 additions & 1 deletion aiorpcx/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ async def __aenter__(self):

async def __aexit__(self, exc_type, exc_value, traceback):
await self.transport.close()
assert self.process_messages_task.done()
# Disabled this as loop might not have processed the event, and don't want to sleep here
# assert self.process_messages_task.done()


def serve_ws(session_factory, *args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_curio.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async def test_tg_join_no_arg():

@pytest.mark.asyncio
async def test_tg_cm_no_arg():
tasks = [await spawn(sleep, x/200) for x in range(5, 0, -1)]
tasks = [await spawn(sleep, x) for x in (0.1, 0.01, -1)]
async with TaskGroup(tasks) as t:
pass
assert all(task.done() for task in tasks)
Expand All @@ -154,7 +154,7 @@ async def test_tg_cm_all():

@pytest.mark.asyncio
async def test_tg_cm_any():
tasks = [await spawn(sleep, x/200) for x in (0.1, 0.01, -1)]
tasks = [await spawn(sleep, x) for x in (0.1, 0.05, -1)]
async with TaskGroup(tasks, wait=any) as t:
pass
assert all(task.done() for task in tasks)
Expand Down Expand Up @@ -733,7 +733,7 @@ async def test_ignore_after_no_expire():
async def t1(*values):
return await return_after_sleep(1 + sum(values), 0.001)

assert await ignore_after(0.02, t1, 1) == 2
assert await ignore_after(0.1, t1, 1) == 2
await sleep(0.002)


Expand Down
6 changes: 3 additions & 3 deletions tests/test_jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ async def receive_request():
requests = connection.receive_message(message)
assert not requests

async with timeout_after(0.01):
async with timeout_after(0.2):
async with TaskGroup() as group:
await group.spawn(receive_request)
await group.spawn(send_request)
Expand Down Expand Up @@ -915,7 +915,7 @@ async def receive_request():
for req, request in zip(batch, requests):
assert req == request

async with timeout_after(0.01):
async with timeout_after(0.2):
async with TaskGroup() as group:
await group.spawn(receive_request)
await group.spawn(send_request)
Expand Down Expand Up @@ -988,7 +988,7 @@ async def receive_request():
requests = connection.receive_message(message)
assert requests == [req]

async with timeout_after(0.01):
async with timeout_after(0.2):
async with TaskGroup() as group:
await group.spawn(receive_request)
await group.spawn(send_request)
Expand Down
21 changes: 14 additions & 7 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, *args, **kwargs):

@classmethod
async def current_server(self):
await sleep(0)
await sleep(0.05)
return self.sessions[0]

async def connection_lost(self):
Expand Down Expand Up @@ -190,12 +190,13 @@ async def test_error_base_cost(self, server_port):
server_session = await MyServerSession.current_server()
server_session.error_base_cost = server_session.cost_hard_limit * 1.1
await session._send_message(b'')
await sleep(0.01)
await sleep(0.05)
assert server_session.errors == 1
assert server_session.cost > server_session.cost_hard_limit
# Check next request raises and cuts us off
with pytest.raises(RPCError):
await session.send_request('echo', [23])
await sleep(0.02)
assert session.is_closing()

@pytest.mark.asyncio
Expand All @@ -207,7 +208,7 @@ async def test_RPCError_cost(self, server_port):
with pytest.raises(RPCError):
await session.send_request('costly_error', [1000])
# It can trigger a cost recalc which refunds a tad
epsilon = 0.1
epsilon = 1
assert server_session.cost > server_session.error_base_cost + 1000 - epsilon

@pytest.mark.asyncio
Expand Down Expand Up @@ -381,9 +382,9 @@ async def test_concurrency_decay(self, server_port):
async with connect_rs('localhost', server_port) as session:
session.cost_decay_per_sec = 100
session.cost = 1000
await sleep(0.01)
await sleep(0.1)
session.recalc_concurrency()
assert 990 < session.cost < 999.1
assert 970 < session.cost < 992

@pytest.mark.asyncio
async def test_concurrency_hard_limit_0(self, server_port):
Expand Down Expand Up @@ -442,14 +443,15 @@ async def test_reply_and_disconnect_value(self, server_port):
async with connect_rs('localhost', server_port) as session:
value = 42
assert await session.send_request('disconnect', [value]) == value
await sleep(0)
await sleep(0.01)
assert session.is_closing()

@pytest.mark.asyncio
async def test_reply_and_disconnect_error(self, server_port):
async with connect_rs('localhost', server_port) as session:
with pytest.raises(RPCError) as e:
assert await session.send_request('disconnect')
await sleep(0.01)
exc = e.value
assert exc.code == 1 and exc.message == 'incompatible version'
assert session.is_closing()
Expand Down Expand Up @@ -806,6 +808,7 @@ async def test_errors(self, msg_server_port, caplog):
await session.send_message((b'syntax', b''))
await session.send_message((b'protocol', b''))
await session.send_message((b'cancel', b''))
await sleep(0.01)
assert in_caplog(caplog, 'exception handling')
assert in_caplog(caplog, 'Not allowed')

Expand All @@ -814,6 +817,7 @@ async def test_bad_magic(self, msg_server_port, caplog):
framer = BitcoinFramer(magic=bytes(4))
async with connect_message_session('localhost', msg_server_port, framer=framer) as session:
await session.send_message((b'version', b''))
await sleep(0.01)
assert in_caplog(caplog, 'bad network magic')

@pytest.mark.asyncio
Expand All @@ -822,6 +826,7 @@ async def test_bad_checksum(self, msg_server_port, caplog):
framer._checksum = lambda payload: bytes(32)
async with connect_message_session('localhost', msg_server_port, framer=framer) as session:
await session.send_message((b'version', b''))
await sleep(0.01)
assert in_caplog(caplog, 'checksum mismatch')

@pytest.mark.asyncio
Expand All @@ -832,6 +837,7 @@ async def test_oversized_message(self, msg_server_port, caplog):
assert not in_caplog(caplog, 'oversized payload')
async with connect_message_session('localhost', msg_server_port) as session:
await session.send_message((b'version', bytes(big + 1)))
await sleep(0.01)
assert in_caplog(caplog, 'oversized payload')

@pytest.mark.asyncio
Expand All @@ -855,7 +861,7 @@ async def test_request_over_hard_limit(self, msg_server_port):
server = await MessageServer.current_server()
server.bump_cost(server.cost_hard_limit + 100)
await session.send_message((b'version', b'abc'))
await sleep(0.005)
await sleep(0.05)
assert session.is_closing()

@pytest.mark.asyncio
Expand All @@ -867,6 +873,7 @@ async def test_server_busy(self, msg_server_port, caplog):
await session.send_message((b'sleep', b''))
await sleep(0.02)
assert server.errors == 1
await sleep(0.01)
assert in_caplog(caplog, 'timed out')


Expand Down

0 comments on commit f0689fa

Please sign in to comment.