Skip to content

Conversation

@ShamansCoding
Copy link

Motivation:

The SanicASGITestClient class provides only a client that lifts a new instance of the test application for each request.

This is very expensive and time-consuming. The application I'm trying to migrate to the current version of sanic-testing has 1500+ tests that pass on the old testing engine in 7 minutes. With the new SanicASGITestClient, passing 30 percent of the tests takes about 8 minutes.

In addition to the increased passing time, at 31 percent of the tests, there is a recursion error somewhere deep in ast.py. I couldn't figure out the cause of this behavior, and with the current version of the client, it's simply impossible to run the tests to completion.

Proposal to solve the problem:

The ability to use the SanicASGITestClient as a context manager or manually control its execution through the run() and stop() methods has been added to the class.

This allows for the repeated use of the running test server for multiple requests to the test server. With the right configuration of the test environment, the test server can be used within the scope="session."

@pytest.fixture(scope="session", autouse=True)
def event_loop():
    """Create an instance of the default event loop for each test case."""
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()
    
@pytest.fixture(scope="session", autouse=True)
async def test_cli(event_loop):
    asgi_client = SanicASGITestClient(real_app)

    await asgi_client.run()

    yield asgi_client

    await asgi_client.stop()

The running server also executes all lifecycle hooks of the provided real application instance, allowing you to start the server and test some parts of the application more isolated, while still having the ability to access the real context of the application and everything initialized in that context in the tests.

When running 100 consecutive API requests in one test using the old client in my application, the speed is about 44 seconds. With the use of the new client, the speed increases to 14 seconds.

Full backward compatibility is maintained; the client can be used as before, preserving the old API and behavior completely.

Copy link
Member

@ahopkins ahopkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed review. This looks pretty nice thanks for the efforts here. I want to take a closer look on my local machine and run it, but looks great 😎

@LanderMoerkerke
Copy link

@ahopkins hi, what is blocking this PR? If needed, I would be able to pick up any changes since merging this will benefit my implementation.

@ShamansCoding
Copy link
Author

Hi! Any updates on merging this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants