コード例 #1
0
ファイル: test_ant.py プロジェクト: xxtlyf/ant_nest
async def test_as_completed():
    ant = CliAnt(loop=asyncio.get_event_loop())
    count = 3

    async def cor(i):
        return i

    right_result = 0
    for c in ant.as_completed((cor(i) for i in range(count)), limit=-1):
        await c
        right_result += 1
    assert right_result == count

    async def cor(i):
        await asyncio.sleep(i * 0.1)
        return i

    right_result = 0  # 0, 1, 2
    for c in ant.as_completed((cor(i) for i in reversed(range(count)))):
        result = await c
        assert result == right_result
        right_result += 1
    assert right_result == count
    # with limit
    right_result = 2  # 2, 1, 0
    for c in ant.as_completed((cor(i) for i in reversed(range(count))),
                              limit=1):
        result = await c
        assert result == right_result
        right_result -= 1
    assert right_result == -1

    await ant.close()
コード例 #2
0
ファイル: test_cli.py プロジェクト: strongbugman/ant_nest
def test_cli_shutdown():
    ant = CliAnt()
    cli.shutdown_ant([ant])
    assert ant.pool.closed

    with pytest.raises(SystemExit):
        cli.shutdown_ant([ant])
コード例 #3
0
def test_cli_shutdown():
    ant = CliAnt()
    ant._queue.put_nowait(object())
    cli.shutdown_ant([ant])
    assert ant._is_closed
    assert ant._queue.qsize() == 0

    with pytest.raises(SystemExit):
        cli.shutdown_ant([ant])
コード例 #4
0
ファイル: test_ant.py プロジェクト: xxtlyf/ant_nest
async def test_as_completed_with_async():

    ant = CliAnt()

    async def cor(x):
        if x < 0:
            raise Exception("Test exception")
        return x

    result_sum = 0
    async for result in ant.as_completed_with_async(
        (cor(i) for i in range(5))):
        result_sum += result
    assert result_sum == sum(range(5))

    result_sum = 0
    async for result in ant.as_completed_with_async(
        (cor(i - 2) for i in range(5)), raise_exception=False):
        result_sum += result
    assert result_sum == sum(range(3))

    async for _ in ant.as_completed_with_async([cor(-1)],
                                               raise_exception=False):
        assert _
        raise Exception("This loop should not be entered!")

    with pytest.raises(Exception):
        async for _ in ant.as_completed_with_async([cor(-1)]):
            assert _

    await ant.close()
コード例 #5
0
ファイル: test_ant.py プロジェクト: strongbugman/ant_nest
async def test_with_real_send():
    httpbin_base_url = os.getenv("TEST_HTTPBIN", "http://localhost:8080/")

    ant = CliAnt()
    res = await ant.request(httpbin_base_url)
    assert res.status_code == 200
    # method
    for method in ["GET", "POST", "DELETE", "PUT", "PATCH", "HEAD"]:
        res = await ant.request(httpbin_base_url + "anything", method=method)
        assert res.status_code == 200
        if method != "HEAD":
            assert res.json()["method"] == method
        else:
            assert res.text == ""
    # with stream
    res = await ant.request(httpbin_base_url + "anything", stream=True)
    assert res.status_code == 200
    async for _ in res.aiter_bytes():
        pass
    assert res.is_stream_consumed

    await ant.close()
コード例 #6
0
ファイル: test_ant.py プロジェクト: xxtlyf/ant_nest
async def test_schedule_task():
    count = 0
    max_count = 10

    async def cor():
        nonlocal count
        count += 1

    ant = CliAnt()

    ant.schedule_tasks((cor() for i in range(max_count)))
    await ant.wait_scheduled_tasks()
    assert count == max_count
    # test with limit
    count = 0
    running_count = 0
    max_running_count = -1
    concurrent_limit = 3

    async def cor():
        nonlocal count, running_count, max_running_count
        running_count += 1
        max_running_count = max(running_count, max_running_count)
        await asyncio.sleep(0.1)
        count += 1
        running_count -= 1

    ant.concurrent_limit = concurrent_limit
    ant.schedule_tasks(cor() for i in range(max_count))
    assert ant.is_running
    await ant.wait_scheduled_tasks()
    assert count == max_count
    assert max_running_count <= concurrent_limit
    # test with exception
    count = 0
    max_count = 3

    async def coro():
        nonlocal count
        count += 1
        raise Exception("Test exception")

    ant.schedule_tasks(coro() for i in range(max_count))
    await ant.wait_scheduled_tasks()
    assert count == max_count

    # test with closed ant
    await ant.close()

    x = coro()
    ant.schedule_task(x)  # this coroutine will not be running
    await ant.close()
    assert count == max_count
    with pytest.raises(Exception):
        await x
コード例 #7
0
ファイル: test_ant.py プロジェクト: xxtlyf/ant_nest
async def test_with_real_request():
    httpbin_base_url = os.getenv("TEST_HTTPBIN", "http://*****:*****@localhost:3128")
    ant.request_proxies.append(proxy)
    res = await ant.request("http://httpbin.org/anything")
    assert res.status == 200
    # no proxy anymore
    ant.request_proxies.pop()
    res = await ant.request("http://httpbin.org/anything")
    assert res.status == 200
    # set proxy by request
    res = await ant.request("http://httpbin.org/anything", proxy=proxy)
    assert res.status == 200
    # with stream
    ant.response_in_stream = True
    res = await ant.request(httpbin_base_url + "anything")
    assert res.status == 200
    with pytest.raises(ValueError):
        getattr(res, "simple_text")
    while True:
        chunk = await res.content.read(10)
        if len(chunk) == 0:
            break
    # set streaming by request
    res = await ant.request(httpbin_base_url + "anything",
                            response_in_stream=False)
    assert res.status == 200
    assert res.simple_text is not None

    await ant.close()
コード例 #8
0
ファイル: test_ant.py プロジェクト: ScjMitsui/ant_nest
async def test_with_real_request():
    httpbin_base_url = os.getenv('TEST_HTTPBIN', 'http://*****:*****@localhost:3128')
    ant.request_proxies.append(proxy)
    res = await ant.request('http://httpbin.org/anything')
    assert res.status == 200
    # no proxy anymore
    ant.request_proxies.pop()
    res = await ant.request('http://httpbin.org/anything')
    assert res.status == 200
    # set proxy by request
    res = await ant.request('http://httpbin.org/anything', proxy=proxy)
    assert res.status == 200
    # with stream
    ant.response_in_stream = True
    res = await ant.request(httpbin_base_url + 'anything')
    assert res.status == 200
    with pytest.raises(ValueError):
        res.simple_text
    while True:
        chunk = await res.content.read(10)
        if len(chunk) == 0:
            break
    # set streaming by request
    res = await ant.request(httpbin_base_url + 'anything',
                            response_in_stream=False)
    assert res.status == 200
    assert res.simple_text is not None

    await ant.close()