| 1 |
import asyncio |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from aioimaplib import Command, CommandTimeout, STOP_WAIT_SERVER_PUSH, AioImapException, Response |
| 6 |
from aioimaplib.imap_testing_server import ImapProtocol |
| 7 |
from tests.server_fixture import with_server, login_user_async, advance_time |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.asyncio() |
| 11 |
async def test_command_timeout(event_loop, with_server, advance_time): |
| 12 |
cmd = Command('CMD', 'tag', loop=event_loop, timeout=1) |
| 13 |
await advance_time(2) |
| 14 |
with pytest.raises(AioImapException): |
| 15 |
await cmd.wait() |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.asyncio() |
| 19 |
async def test_command_close_cancels_timer(event_loop, with_server, advance_time): |
| 20 |
cmd = Command('CMD', 'tag', loop=event_loop, timeout=1) |
| 21 |
cmd.close(b'line', 'OK') |
| 22 |
await advance_time(3) |
| 23 |
|
| 24 |
await cmd.wait() |
| 25 |
assert Response('OK', [b'line']) == cmd.response |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.asyncio() |
| 29 |
async def test_command_begin_literal_data_resets_timer(event_loop, with_server, advance_time): |
| 30 |
cmd = Command('CMD', 'tag', loop=event_loop, timeout=2) |
| 31 |
|
| 32 |
await advance_time(1) |
| 33 |
cmd.begin_literal_data(7, b'literal') |
| 34 |
|
| 35 |
await advance_time(1.9) |
| 36 |
cmd.close(b'line', 'OK') |
| 37 |
|
| 38 |
await cmd.wait() |
| 39 |
assert Response('OK', [b'literal', b'line']) == cmd.response |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.asyncio() |
| 43 |
async def test_command_append_data_resets_timer(event_loop, with_server, advance_time): |
| 44 |
cmd = Command('CMD', 'tag', loop=event_loop, timeout=2) |
| 45 |
cmd.begin_literal_data(4, b'da') |
| 46 |
|
| 47 |
await advance_time(1.9) |
| 48 |
cmd.append_literal_data(b'ta') |
| 49 |
|
| 50 |
await advance_time(1.9) |
| 51 |
cmd.close(b'line', 'OK') |
| 52 |
|
| 53 |
await cmd.wait() |
| 54 |
assert Response('OK', [b'data', b'line']) == cmd.response |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.asyncio() |
| 58 |
async def test_command_append_literal_data_resets_timer(event_loop, with_server, advance_time): |
| 59 |
cmd = Command('CMD', 'tag', loop=event_loop, timeout=2) |
| 60 |
cmd.begin_literal_data(12, b'literal') |
| 61 |
|
| 62 |
await advance_time(1.9) |
| 63 |
cmd.append_literal_data(b' data') |
| 64 |
|
| 65 |
await advance_time(1.9) |
| 66 |
cmd.close(b'line', 'OK') |
| 67 |
|
| 68 |
await cmd.wait() |
| 69 |
assert Response('OK', [b'literal data', b'line']) == cmd.response |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.asyncio() |
| 73 |
async def test_command_append_to_resp_resets_timer(event_loop, with_server, advance_time): |
| 74 |
cmd = Command('CMD', 'tag', loop=event_loop, timeout=2) |
| 75 |
|
| 76 |
await advance_time(1.9) |
| 77 |
cmd.append_to_resp(b'line 1') |
| 78 |
|
| 79 |
await advance_time(1.9) |
| 80 |
cmd.close(b'line 2', 'OK') |
| 81 |
|
| 82 |
await cmd.wait() |
| 83 |
assert Response('OK', [b'line 1', b'line 2']) == cmd.response |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.asyncio() |
| 87 |
async def test_command_timeout_while_receiving_data(event_loop, with_server, advance_time): |
| 88 |
cmd = Command('CMD', 'tag', loop=event_loop, timeout=2) |
| 89 |
|
| 90 |
await advance_time(1) |
| 91 |
cmd.begin_literal_data(12, b'literal') |
| 92 |
|
| 93 |
await advance_time(3) |
| 94 |
with pytest.raises(AioImapException): |
| 95 |
await cmd.wait() |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.asyncio() |
| 99 |
async def test_when_async_commands_timeout__they_should_be_removed_from_protocol_state(event_loop, with_server, advance_time): |
| 100 |
imap_client = await login_user_async('user', 'pass', select=True, loop=event_loop) |
| 101 |
await (imap_client.protocol.execute(Command( |
| 102 |
'DELAY', imap_client.protocol.new_tag(), '3', loop=event_loop))) |
| 103 |
|
| 104 |
noop_task = asyncio.ensure_future(imap_client.protocol.execute( |
| 105 |
Command('NOOP', imap_client.protocol.new_tag(), '', loop=event_loop, timeout=2))) |
| 106 |
await advance_time(1) |
| 107 |
|
| 108 |
assert 1 == len(imap_client.protocol.pending_async_commands) |
| 109 |
await advance_time(1.1) |
| 110 |
|
| 111 |
finished, pending = await asyncio.wait([noop_task]) |
| 112 |
assert noop_task in finished |
| 113 |
assert isinstance(noop_task.exception(), CommandTimeout) |
| 114 |
assert 0 == len(imap_client.protocol.pending_async_commands) |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.asyncio() |
| 118 |
async def test_when_sync_commands_timeout__they_should_be_removed_from_protocol_state(event_loop, with_server, advance_time): |
| 119 |
imap_client = await login_user_async('user', 'pass', select=True, loop=event_loop) |
| 120 |
await (imap_client.protocol.execute(Command( |
| 121 |
'DELAY', imap_client.protocol.new_tag(), '3', loop=event_loop))) |
| 122 |
|
| 123 |
delay_task = asyncio.ensure_future(imap_client.protocol.execute( |
| 124 |
Command('DELAY', imap_client.protocol.new_tag(), '0', loop=event_loop, timeout=2))) |
| 125 |
await advance_time(1) |
| 126 |
assert imap_client.protocol.pending_sync_command is not None |
| 127 |
await advance_time(1.1) |
| 128 |
|
| 129 |
finished, pending = await asyncio.wait([delay_task]) |
| 130 |
assert delay_task in finished |
| 131 |
assert isinstance(delay_task.exception(), CommandTimeout) |
| 132 |
assert imap_client.protocol.pending_sync_command is None |
| 133 |
|
| 134 |
|
| 135 |
# test failing with python 12 |
| 136 |
@pytest.mark.asyncio() |
| 137 |
async def test_idle_start__exits_queue_get_without_timeout_error(event_loop, with_server, advance_time): |
| 138 |
imap_client = await login_user_async('user', 'pass', select=True, loop=event_loop) |
| 139 |
|
| 140 |
idle_timeout = 5 |
| 141 |
await imap_client.idle_start(idle_timeout) |
| 142 |
|
| 143 |
push_task = asyncio.ensure_future(imap_client.wait_server_push(idle_timeout + 2)) |
| 144 |
await advance_time(idle_timeout + 1) |
| 145 |
|
| 146 |
r = await asyncio.wait_for(push_task, 0) |
| 147 |
assert STOP_WAIT_SERVER_PUSH == r |
| 148 |
|
| 149 |
|
| 150 |
@pytest.mark.asyncio() |
| 151 |
async def test_idle_start__exits_queueget_with_keepalive_without_timeout_error(event_loop, with_server, advance_time): |
| 152 |
imap_client = await login_user_async('user', 'pass', select=True, loop=event_loop) |
| 153 |
|
| 154 |
# Idle long enough for the server to issue a keep-alive |
| 155 |
server_idle_timeout = ImapProtocol.IDLE_STILL_HERE_PERIOD_SECONDS |
| 156 |
idle_timeout = server_idle_timeout + 1 |
| 157 |
idle = await imap_client.idle_start(idle_timeout) |
| 158 |
|
| 159 |
push_task = asyncio.ensure_future(imap_client.wait_server_push(server_idle_timeout - 1)) |
| 160 |
|
| 161 |
# Advance time until we've received a keep-alive from server |
| 162 |
await advance_time(server_idle_timeout) |
| 163 |
|
| 164 |
# The original push task timed out |
| 165 |
with pytest.raises(asyncio.TimeoutError): |
| 166 |
await asyncio.wait_for(push_task, 0.1) |
| 167 |
|
| 168 |
# Read the keepalive from the server |
| 169 |
r = await imap_client.wait_server_push(0.1) |
| 170 |
assert [b'OK Still here'] == r |
| 171 |
|
| 172 |
# Advance the clock to the client timeout (idle waiter triggers) |
| 173 |
await advance_time(1) |
| 174 |
imap_client.idle_done() |
| 175 |
|
| 176 |
r = await asyncio.wait_for(idle, 1) |
| 177 |
assert "OK" == r.result |
| 178 |
|
| 179 |
assert not imap_client.protocol._idle_event.is_set() |
| 180 |
|
| 181 |
# Start another idle period |
| 182 |
idle = await imap_client.idle_start(idle_timeout) |
| 183 |
await advance_time(1) |
| 184 |
|
| 185 |
# Read 'stop_wait_server_push' |
| 186 |
push_task = asyncio.ensure_future(imap_client.wait_server_push(0.1)) |
| 187 |
await advance_time(1) |
| 188 |
r = await asyncio.wait_for(push_task, None) |
| 189 |
assert STOP_WAIT_SERVER_PUSH == r |
| 190 |
|
| 191 |
# There shouldn't be anything left in the queue (no '+ idling') |
| 192 |
with pytest.raises(asyncio.TimeoutError): |
| 193 |
push_task = asyncio.ensure_future(imap_client.wait_server_push(0.1)) |
| 194 |
await advance_time(1) |
| 195 |
await asyncio.wait_for(push_task, 0.1) |
| 196 |
|
| 197 |
imap_client.idle_done() |
| 198 |
await asyncio.wait_for(idle, 1) |
| 199 |
|