Skip to content

Issue using big response object in Entry with async client #225

@Veritogen

Description

@Veritogen

Hey,
firstly: big thanks for the library and the effort you put into it. It really helps us while testing.

While testing some things in our code base, I discovered that mocked async calls never finish if the response object passed to the Entry is above some size.

I wrote some code to be able to reproduce the behaviour:

import asyncio
import datetime
import json

import httpx
from mocket import mocketize
from mocket.mockhttp import Entry


@mocketize(strict_mode=True)
def test_sync_case():

    test_uri = "https://abc.de/testdata/"
    base_timestamp = int(datetime.datetime.now().timestamp())
    response = [
        {"timestamp": base_timestamp + i, "value": 1337 + 42 * i} for i in range(30_000)
    ]
    Entry.single_register(
        method=Entry.POST,
        uri=test_uri,
        body=json.dumps(
            response,
        ),
        headers={"content-type": "application/json"},
    )

    with httpx.Client() as client:
        response = client.post(test_uri)
        print("high number sync: ", len(response.json()))


@mocketize(strict_mode=True)
def test_async_case_low_number():

    test_uri = "https://abc.de/testdata/"
    base_timestamp = int(datetime.datetime.now().timestamp())
    response = [
        {"timestamp": base_timestamp + i, "value": 1337 + 42 * i} for i in range(100)
    ]
    Entry.single_register(
        method=Entry.POST,
        uri=test_uri,
        body=json.dumps(
            response,
        ),
        headers={"content-type": "application/json"},
    )

    async def main():
        async with httpx.AsyncClient() as client:
            response = await client.post(test_uri)

        print("low number async: ", len(response.json()))

    loop = asyncio.new_event_loop()
    loop.run_until_complete(main())


@mocketize(strict_mode=True)
def test_async_case_high_number():

    test_uri = "https://abc.de/testdata/"
    base_timestamp = int(datetime.datetime.now().timestamp())
    response = [
        {"timestamp": base_timestamp + i, "value": 1337 + 42 * i} for i in range(30_000)
    ]
    Entry.single_register(
        method=Entry.POST,
        uri=test_uri,
        body=json.dumps(
            response,
        ),
        headers={"content-type": "application/json"},
    )

    async def main():
        async with httpx.AsyncClient() as client:
            # Awaiting the response never finishes
            response = await client.post(test_uri)

        print("high number async: ", len(response.json()))

    loop = asyncio.new_event_loop()
    loop.run_until_complete(main())


test_sync_case()
test_async_case_low_number()
test_async_case_high_number()

While the functions test_sync_case() and test_async_case_low_number() run through, test_async_case_high_number() never finishes.
The number until the code works for test_async_case_low_number() is response = [{"timestamp": base_timestamp + i, "value": 1337 + 42 * i} for i in range(1_525)]. At 1526 it stops working.

Am I doing something wrong? Or could a bug be hiding here?

We're running Python 3.11 with httpx==0.24.1 and mocket==3.12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions