| 1 |
import asyncio |
| 2 |
from functools import reduce |
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import aioftp |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.asyncio |
| 11 |
async def test_patched_sleep(skip_sleep): |
| 12 |
await asyncio.sleep(10) |
| 13 |
assert skip_sleep.is_close(10) |
| 14 |
|
| 15 |
|
| 16 |
SIZE = 3 * 100 * 1024 |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.parametrize("times", [10, 20, 30]) |
| 20 |
@pytest.mark.parametrize("type", ["read", "write"]) |
| 21 |
@pytest.mark.parametrize("direction", ["download", "upload"]) |
| 22 |
@pytest.mark.asyncio |
| 23 |
async def test_client_side_throttle( |
| 24 |
pair_factory, |
| 25 |
skip_sleep, |
| 26 |
times, |
| 27 |
type, |
| 28 |
direction, |
| 29 |
): |
| 30 |
async with pair_factory() as pair: |
| 31 |
await pair.make_server_files("foo", size=SIZE) |
| 32 |
await pair.make_client_files("foo", size=SIZE) |
| 33 |
getattr(pair.client.throttle, type).limit = SIZE / times |
| 34 |
await getattr(pair.client, direction)("foo") |
| 35 |
if (type, direction) in {("read", "download"), ("write", "upload")}: |
| 36 |
assert skip_sleep.is_close(times) |
| 37 |
else: |
| 38 |
assert skip_sleep.is_close(0) |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.parametrize("times", [10, 20, 30]) |
| 42 |
@pytest.mark.parametrize("users", [1, 2, 3]) |
| 43 |
@pytest.mark.parametrize("throttle_direction", ["read", "write"]) |
| 44 |
@pytest.mark.parametrize("data_direction", ["download", "upload"]) |
| 45 |
@pytest.mark.parametrize( |
| 46 |
"throttle_level", |
| 47 |
[ |
| 48 |
"throttle", |
| 49 |
"throttle_per_connection", |
| 50 |
], |
| 51 |
) |
| 52 |
@pytest.mark.asyncio |
| 53 |
async def test_server_side_throttle( |
| 54 |
pair_factory, |
| 55 |
skip_sleep, |
| 56 |
times, |
| 57 |
users, |
| 58 |
throttle_direction, |
| 59 |
data_direction, |
| 60 |
throttle_level, |
| 61 |
): |
| 62 |
async with pair_factory() as pair: |
| 63 |
names = [] |
| 64 |
for i in range(users): |
| 65 |
name = f"foo{i}" |
| 66 |
names.append(name) |
| 67 |
await pair.make_server_files(name, size=SIZE) |
| 68 |
throttle = reduce( |
| 69 |
getattr, |
| 70 |
[throttle_level, throttle_direction], |
| 71 |
pair.server, |
| 72 |
) |
| 73 |
throttle.limit = SIZE / times |
| 74 |
clients = [] |
| 75 |
for name in names: |
| 76 |
c = aioftp.Client(path_io_factory=aioftp.MemoryPathIO) |
| 77 |
async with c.path_io.open(Path(name), "wb") as f: |
| 78 |
await f.write(b"-" * SIZE) |
| 79 |
await c.connect(pair.server.server_host, pair.server.server_port) |
| 80 |
await c.login() |
| 81 |
clients.append(c) |
| 82 |
coros = [getattr(c, data_direction)(n) for c, n in zip(clients, names)] |
| 83 |
await asyncio.gather(*coros) |
| 84 |
await asyncio.gather(*[c.quit() for c in clients]) |
| 85 |
throttled = {("read", "upload"), ("write", "download")} |
| 86 |
if (throttle_direction, data_direction) not in throttled: |
| 87 |
assert skip_sleep.is_close(0) |
| 88 |
else: |
| 89 |
t = times |
| 90 |
if throttle_level == "throttle": |
| 91 |
t *= users |
| 92 |
assert skip_sleep.is_close(t) |