Skip to content

Closing AsyncClient in all tests (#871) - #1219

Merged
cdeler merged 1 commit into
encode:masterfrom
cdeler:close-clients-in-tests
Aug 31, 2020
Merged

Closing AsyncClient in all tests (#871)#1219
cdeler merged 1 commit into
encode:masterfrom
cdeler:close-clients-in-tests

Conversation

@cdeler

Copy link
Copy Markdown
Contributor

As we discussed in #1197 (comment) , it was decided to move the test refactoring out from #1197

In this PR I added closing to all unclosed AsyncClient in the tests

@lovelydinosaur

lovelydinosaur commented Aug 26, 2020

Copy link
Copy Markdown
Contributor

Great!

At the moment you're using async with in some cases here, and .aclose() in others.

I'm wondering if we should instead just always be using async with?
Maybe it doesn't really mater either way? What do other @encode/maintainers think?

Might be a good signal to users digging through our test cases, about the proper usage?

@cdeler

cdeler commented Aug 26, 2020

Copy link
Copy Markdown
ContributorAuthor

@tomchristie
Some tests, for example

@pytest.mark.asyncio
asyncdeftest_auth_property() ->None:
client=AsyncClient(transport=AsyncMockTransport())
assertclient.authisNone
client.auth= ("tomchristie", "password123") # type: ignore
assertisinstance(client.auth, BasicAuth)
url="https://example.org/"
response=awaitclient.get(url)
assertresponse.status_code==200
assertresponse.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}
awaitclient.aclose()

are long. In such cases I was adding await client.aclose() to prevent adding new indentation level (but I can change all test to make them use identical AsyncClient closing method, e.g. async with)

PS moveover, these tests are not about closing. They might be provided with AsyncClient using fixture, but it would make test suite more complicated

@florimondmanca

florimondmanca commented Aug 26, 2020

Copy link
Copy Markdown
Contributor

I'm wondering if we should instead just always be using async with?

In such cases I was adding await client.aclose() to prevent adding new indentation level (but I can change all test to make them use identical AsyncClient closing method, e.g. async with)

Yeah, imo, we should probably stick to async with everywhere, even if it increases the diff size here a bit (we can ease review by enabling "ignore whitespace" in the GitHub review UI). At least we shouldn't be using .aclose() without a try/finally so we don't hint at wrong usage, and so by that point we need extra indentation anyway. :-)

@florimondmancaflorimondmanca added the refactor Issues and PRs related to code refactoring label Aug 26, 2020
@lovelydinosaur

Copy link
Copy Markdown
Contributor

Yup, fair call. I've also noticed that there seem to be a bunch of places in the test cases where we're using AsyncClient, but that I think we should probably just be using Client instead.

For example https://github.com/encode/httpx/blob/master/tests/client/test_headers.py where it looks to me like it'd be a whole bunch neater if it was just testing against a plain client.

Wondering if we should do the following in order?

  • Tackle any cleanup in test cases, and always prefer just using httpx.Client unless there's a good reason.
  • Make sure we're using context management in any remaining places where we are still using httpx.AsyncClient.
  • Update Issue warning on unclosed AsyncClient. #1197 against the master branch at that point.

(Also @cdeler thanks for being so patient, there's been a whole bunch of extra stuff we've had to work through in order to approach this as carefully and throughly as this.)

@lovelydinosaur

Copy link
Copy Markdown
Contributor

See #1222.

@cdeler

Copy link
Copy Markdown
ContributorAuthor

I'm to work on this ticket in 2 days

@lovelydinosaur

Copy link
Copy Markdown
Contributor

Sure thing - don't put yourself under pressure, or feel that you need to let us know when you will or won't be working on stuff. Thanks again for all your work on this. 😄

@cdeler
cdelerforce-pushed the close-clients-in-tests branch from cfda19e to a92fb80CompareAugust 31, 2020 13:33
All over the AsyncClient invocation is made using context manager or with try-finally block
@cdeler
cdelerforce-pushed the close-clients-in-tests branch from a92fb80 to ad5f651CompareAugust 31, 2020 13:37
@cdeler

Copy link
Copy Markdown
ContributorAuthor

@tomchristie

I did what you asked:

  1. for now all AsyncClient usage is gone by async with block (except thetest_async_proxy_close, where it's done in finally block)
  2. I changed all AsyncClient references to httpx.AsyncClient

@lovelydinosaur

Copy link
Copy Markdown
Contributor

Great! It looks like there's still some work we could be doing on preferring the use of httpx.Client over httpx.AsyncClient wherever possible in the tests, but we can probably just treat that separately to the rest of this now.

I'm happy enough for this to go in whenever. Good work. 👍

@cdeler
cdeler merged commit fa7661b into encode:masterAug 31, 2020
@cdeler
cdeler deleted the close-clients-in-tests branch August 31, 2020 15:02
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactorIssues and PRs related to code refactoring

Development

Successfully merging this pull request may close these issues.

3 participants

@cdeler@lovelydinosaur@florimondmanca