| 1 | import asyncio |
| 2 | import itertools |
| 3 | import pathlib |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | import aioftp |
| 8 | |
| 9 | |
| 10 | @pytest.mark.asyncio |
| 11 | async def test_abort_stor(pair_factory): |
| 12 | async with pair_factory() as pair: |
| 13 | stream = await pair.client.upload_stream("test.txt") |
| 14 | with pytest.raises((ConnectionResetError, BrokenPipeError)): |
| 15 | while True: |
| 16 | await stream.write(b"-" * aioftp.DEFAULT_BLOCK_SIZE) |
| 17 | await pair.client.abort() |
| 18 | |
| 19 | |
| 20 | class SlowReadMemoryPathIO(aioftp.MemoryPathIO): |
| 21 | async def read(self, *args, **kwargs): |
| 22 | await asyncio.sleep(0.01) |
| 23 | return await super().read(*args, **kwargs) |
| 24 | |
| 25 | |
| 26 | @pytest.mark.asyncio |
| 27 | async def test_abort_retr(pair_factory, Server): |
| 28 | s = Server(path_io_factory=SlowReadMemoryPathIO) |
| 29 | async with pair_factory(None, s) as pair: |
| 30 | await pair.make_server_files("test.txt") |
| 31 | stream = await pair.client.download_stream("test.txt") |
| 32 | for i in itertools.count(): |
| 33 | data = await stream.read(aioftp.DEFAULT_BLOCK_SIZE) |
| 34 | if not data: |
| 35 | stream.close() |
| 36 | break |
| 37 | if i == 0: |
| 38 | await pair.client.abort() |
| 39 | |
| 40 | |
| 41 | @pytest.mark.asyncio |
| 42 | async def test_abort_retr_no_wait( |
| 43 | pair_factory, |
| 44 | Server, |
| 45 | expect_codes_in_exception, |
| 46 | ): |
| 47 | s = Server(path_io_factory=SlowReadMemoryPathIO) |
| 48 | async with pair_factory(None, s) as pair: |
| 49 | await pair.make_server_files("test.txt") |
| 50 | stream = await pair.client.download_stream("test.txt") |
| 51 | with expect_codes_in_exception("426"): |
| 52 | for i in itertools.count(): |
| 53 | data = await stream.read(aioftp.DEFAULT_BLOCK_SIZE) |
| 54 | if not data: |
| 55 | await stream.finish() |
| 56 | break |
| 57 | if i == 0: |
| 58 | await pair.client.abort(wait=False) |
| 59 | |
| 60 | |
| 61 | @pytest.mark.asyncio |
| 62 | async def test_nothing_to_abort(pair_factory): |
| 63 | async with pair_factory() as pair: |
| 64 | await pair.client.abort() |
| 65 | |
| 66 | |
| 67 | class SlowListMemoryPathIO(aioftp.MemoryPathIO): |
| 68 | async def is_file(self, *a, **kw): |
| 69 | return True |
| 70 | |
| 71 | def list(self, *args, **kwargs): |
| 72 | class Lister(aioftp.AbstractAsyncLister): |
| 73 | async def __anext__(cls): |
| 74 | await asyncio.sleep(0.01) |
| 75 | return pathlib.PurePath("/test.txt") |
| 76 | |
| 77 | return Lister() |
| 78 | |
| 79 | async def stat(self, *a, **kw): |
| 80 | class Stat: |
| 81 | st_size = 0 |
| 82 | st_mtime = 0 |
| 83 | st_ctime = 0 |
| 84 | |
| 85 | return Stat |
| 86 | |
| 87 | |
| 88 | @pytest.mark.asyncio |
| 89 | async def test_mlsd_abort(pair_factory, Server): |
| 90 | s = Server(path_io_factory=SlowListMemoryPathIO) |
| 91 | async with pair_factory(None, s) as pair: |
| 92 | cwd = await pair.client.get_current_directory() |
| 93 | assert cwd == pathlib.PurePosixPath("/") |
| 94 | async for path, info in pair.client.list(): |
| 95 | await pair.client.abort() |
| 96 | break |