Skip to content

Commit 6719547

Browse files
committed
Removing async-timeout dependency.
1 parent c87c222 commit 6719547

File tree

4 files changed

+113
-121
lines changed

4 files changed

+113
-121
lines changed

Pipfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pook = "*"
2222
flake8 = "<7"
2323
xxhash = "*"
2424
aiohttp = "*"
25-
async-timeout = "*"
2625
httpx = "*"
2726
pipfile = "*"
2827
build = "*"

README.rst

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ Example:
231231
232232
import aiohttp
233233
import asyncio
234-
import async_timeout
235234
from unittest import TestCase
236235
237236
from mocket.plugins.httpretty import httpretty, httprettified
@@ -248,11 +247,12 @@ Example:
248247
)
249248
250249
async def main(l):
251-
async with aiohttp.ClientSession(loop=l) as session:
252-
async with async_timeout.timeout(3):
253-
async with session.get(url) as get_response:
254-
assert get_response.status == 200
255-
assert await get_response.text() == '{"origin": "127.0.0.1"}'
250+
async with aiohttp.ClientSession(
251+
loop=l, timeout=aiohttp.ClientTimeout(total=3)
252+
) as session:
253+
async with session.get(url) as get_response:
254+
assert get_response.status == 200
255+
assert await get_response.text() == '{"origin": "127.0.0.1"}'
256256
257257
loop = asyncio.new_event_loop()
258258
loop.set_debug(True)
@@ -277,16 +277,16 @@ Example:
277277
Entry.single_register(Entry.POST, url, body=body*2, status=201)
278278
279279
async def main(l):
280-
async with aiohttp.ClientSession(loop=l) as session:
281-
async with async_timeout.timeout(3):
282-
async with session.get(url) as get_response:
283-
assert get_response.status == 404
284-
assert await get_response.text() == body
280+
async with aiohttp.ClientSession(
281+
loop=l, timeout=aiohttp.ClientTimeout(total=3)
282+
) as session:
283+
async with session.get(url) as get_response:
284+
assert get_response.status == 404
285+
assert await get_response.text() == body
285286
286-
async with async_timeout.timeout(3):
287-
async with session.post(url, data=body * 6) as post_response:
288-
assert post_response.status == 201
289-
assert await post_response.text() == body * 2
287+
async with session.post(url, data=body * 6) as post_response:
288+
assert post_response.status == 201
289+
assert await post_response.text() == body * 2
290290
291291
loop = asyncio.new_event_loop()
292292
loop.run_until_complete(main(loop))
@@ -302,18 +302,18 @@ Example:
302302
Entry.single_register(Entry.GET, url, body=body, status=404)
303303
Entry.single_register(Entry.POST, url, body=body * 2, status=201)
304304
305-
async with aiohttp.ClientSession() as session:
306-
async with async_timeout.timeout(3):
307-
async with session.get(url) as get_response:
308-
assert get_response.status == 404
309-
assert await get_response.text() == body
310-
311-
async with async_timeout.timeout(3):
312-
async with session.post(url, data=body * 6) as post_response:
313-
assert post_response.status == 201
314-
assert await post_response.text() == body * 2
315-
assert Mocket.last_request().method == 'POST'
316-
assert Mocket.last_request().body == body * 6
305+
async with aiohttp.ClientSession(
306+
timeout=aiohttp.ClientTimeout(total=3)
307+
) as session:
308+
async with session.get(url) as get_response:
309+
assert get_response.status == 404
310+
assert await get_response.text() == body
311+
312+
async with session.post(url, data=body * 6) as post_response:
313+
assert post_response.status == 201
314+
assert await post_response.text() == body * 2
315+
assert Mocket.last_request().method == 'POST'
316+
assert Mocket.last_request().body == body * 6
317317
318318
319319
Works well with others

tests/main/test_http_aiohttp.py

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,84 @@
11
import asyncio
22
import json
3-
import platform
43
from unittest import TestCase
54

65
import aiohttp
7-
import async_timeout
86

97
from mocket.mocket import Mocket, mocketize
108
from mocket.mockhttp import Entry
119
from mocket.plugins.httpretty import HTTPretty, httprettified
1210

13-
if not platform.python_version().startswith("3.11."):
14-
# Python 3.11 needs async decorators, or aiohttp
15-
# will fail with "Cannot write to closing transport"
16-
class AioHttpEntryTestCase(TestCase):
17-
@mocketize
18-
def test_http_session(self):
19-
url = "http://httpbin.org/ip"
20-
body = "asd" * 100
21-
Entry.single_register(Entry.GET, url, body=body, status=404)
22-
Entry.single_register(Entry.POST, url, body=body * 2, status=201)
2311

24-
async def main(_loop):
25-
async with aiohttp.ClientSession(loop=_loop) as session:
26-
async with async_timeout.timeout(3):
27-
async with session.get(url) as get_response:
28-
assert get_response.status == 404
29-
assert await get_response.text() == body
12+
class AioHttpEntryTestCase(TestCase):
13+
timeout = aiohttp.ClientTimeout(total=3)
3014

31-
async with async_timeout.timeout(3):
32-
async with session.post(url, data=body * 6) as post_response:
33-
assert post_response.status == 201
34-
assert await post_response.text() == body * 2
35-
assert Mocket.last_request().method == "POST"
36-
assert Mocket.last_request().body == body * 6
15+
@mocketize
16+
def test_http_session(self):
17+
url = "http://httpbin.org/ip"
18+
body = "asd" * 100
19+
Entry.single_register(Entry.GET, url, body=body, status=404)
20+
Entry.single_register(Entry.POST, url, body=body * 2, status=201)
3721

38-
loop = asyncio.new_event_loop()
39-
loop.set_debug(True)
40-
loop.run_until_complete(main(loop))
41-
self.assertEqual(len(Mocket.request_list()), 2)
22+
async def main(_loop):
23+
async with aiohttp.ClientSession(
24+
loop=_loop, timeout=self.timeout
25+
) as session:
26+
async with session.get(url) as get_response:
27+
assert get_response.status == 404
28+
assert await get_response.text() == body
4229

43-
@mocketize
44-
def test_https_session(self):
45-
url = "https://httpbin.org/ip"
46-
body = "asd" * 100
47-
Entry.single_register(Entry.GET, url, body=body, status=404)
48-
Entry.single_register(Entry.POST, url, body=body * 2, status=201)
30+
async with session.post(url, data=body * 6) as post_response:
31+
assert post_response.status == 201
32+
assert await post_response.text() == body * 2
33+
assert Mocket.last_request().method == "POST"
34+
assert Mocket.last_request().body == body * 6
4935

50-
async def main(_loop):
51-
async with aiohttp.ClientSession(loop=_loop) as session:
52-
async with async_timeout.timeout(3):
53-
async with session.get(url) as get_response:
54-
assert get_response.status == 404
55-
assert await get_response.text() == body
36+
loop = asyncio.new_event_loop()
37+
loop.set_debug(True)
38+
loop.run_until_complete(main(loop))
39+
self.assertEqual(len(Mocket.request_list()), 2)
5640

57-
async with async_timeout.timeout(3):
58-
async with session.post(url, data=body * 6) as post_response:
59-
assert post_response.status == 201
60-
assert await post_response.text() == body * 2
41+
@mocketize
42+
def test_https_session(self):
43+
url = "https://httpbin.org/ip"
44+
body = "asd" * 100
45+
Entry.single_register(Entry.GET, url, body=body, status=404)
46+
Entry.single_register(Entry.POST, url, body=body * 2, status=201)
6147

62-
loop = asyncio.new_event_loop()
63-
loop.set_debug(True)
64-
loop.run_until_complete(main(loop))
65-
self.assertEqual(len(Mocket.request_list()), 2)
48+
async def main(_loop):
49+
async with aiohttp.ClientSession(
50+
loop=_loop, timeout=self.timeout
51+
) as session:
52+
async with session.get(url) as get_response:
53+
assert get_response.status == 404
54+
assert await get_response.text() == body
6655

67-
@httprettified
68-
def test_httprettish_session(self):
69-
url = "https://httpbin.org/ip"
70-
HTTPretty.register_uri(
71-
HTTPretty.GET,
72-
url,
73-
body=json.dumps(dict(origin="127.0.0.1")),
74-
)
56+
async with session.post(url, data=body * 6) as post_response:
57+
assert post_response.status == 201
58+
assert await post_response.text() == body * 2
7559

76-
async def main(_loop):
77-
async with aiohttp.ClientSession(loop=_loop) as session:
78-
async with async_timeout.timeout(3):
79-
async with session.get(url) as get_response:
80-
assert get_response.status == 200
81-
assert (
82-
await get_response.text() == '{"origin": "127.0.0.1"}'
83-
)
60+
loop = asyncio.new_event_loop()
61+
loop.set_debug(True)
62+
loop.run_until_complete(main(loop))
63+
self.assertEqual(len(Mocket.request_list()), 2)
8464

85-
loop = asyncio.new_event_loop()
86-
loop.set_debug(True)
87-
loop.run_until_complete(main(loop))
65+
@httprettified
66+
def test_httprettish_session(self):
67+
url = "https://httpbin.org/ip"
68+
HTTPretty.register_uri(
69+
HTTPretty.GET,
70+
url,
71+
body=json.dumps(dict(origin="127.0.0.1")),
72+
)
73+
74+
async def main(_loop):
75+
async with aiohttp.ClientSession(
76+
loop=_loop, timeout=self.timeout
77+
) as session:
78+
async with session.get(url) as get_response:
79+
assert get_response.status == 200
80+
assert await get_response.text() == '{"origin": "127.0.0.1"}'
81+
82+
loop = asyncio.new_event_loop()
83+
loop.set_debug(True)
84+
loop.run_until_complete(main(loop))

tests/tests38/test_http_aiohttp.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from unittest import IsolatedAsyncioTestCase
33

44
import aiohttp
5-
import async_timeout
65

76
from mocket.async_mocket import async_mocketize
87
from mocket.mocket import Mocket
@@ -11,25 +10,25 @@
1110

1211

1312
class AioHttpEntryTestCase(IsolatedAsyncioTestCase):
13+
timeout = aiohttp.ClientTimeout(total=3)
14+
1415
@async_mocketize
1516
async def test_http_session(self):
1617
url = "http://httpbin.org/ip"
1718
body = "asd" * 100
1819
Entry.single_register(Entry.GET, url, body=body, status=404)
1920
Entry.single_register(Entry.POST, url, body=body * 2, status=201)
2021

21-
async with aiohttp.ClientSession() as session:
22-
async with async_timeout.timeout(3):
23-
async with session.get(url) as get_response:
24-
assert get_response.status == 404
25-
assert await get_response.text() == body
22+
async with aiohttp.ClientSession(timeout=self.timeout) as session:
23+
async with session.get(url) as get_response:
24+
assert get_response.status == 404
25+
assert await get_response.text() == body
2626

27-
async with async_timeout.timeout(3):
28-
async with session.post(url, data=body * 6) as post_response:
29-
assert post_response.status == 201
30-
assert await post_response.text() == body * 2
31-
assert Mocket.last_request().method == "POST"
32-
assert Mocket.last_request().body == body * 6
27+
async with session.post(url, data=body * 6) as post_response:
28+
assert post_response.status == 201
29+
assert await post_response.text() == body * 2
30+
assert Mocket.last_request().method == "POST"
31+
assert Mocket.last_request().body == body * 6
3332

3433
self.assertEqual(len(Mocket.request_list()), 2)
3534

@@ -40,16 +39,14 @@ async def test_https_session(self):
4039
Entry.single_register(Entry.GET, url, body=body, status=404)
4140
Entry.single_register(Entry.POST, url, body=body * 2, status=201)
4241

43-
async with aiohttp.ClientSession() as session:
44-
async with async_timeout.timeout(3):
45-
async with session.get(url) as get_response:
46-
assert get_response.status == 404
47-
assert await get_response.text() == body
42+
async with aiohttp.ClientSession(timeout=self.timeout) as session:
43+
async with session.get(url) as get_response:
44+
assert get_response.status == 404
45+
assert await get_response.text() == body
4846

49-
async with async_timeout.timeout(3):
50-
async with session.post(url, data=body * 6) as post_response:
51-
assert post_response.status == 201
52-
assert await post_response.text() == body * 2
47+
async with session.post(url, data=body * 6) as post_response:
48+
assert post_response.status == 201
49+
assert await post_response.text() == body * 2
5350

5451
self.assertEqual(len(Mocket.request_list()), 2)
5552

@@ -62,8 +59,7 @@ async def test_httprettish_session(self):
6259
body=json.dumps(dict(origin="127.0.0.1")),
6360
)
6461

65-
async with aiohttp.ClientSession() as session:
66-
async with async_timeout.timeout(3):
67-
async with session.get(url) as get_response:
68-
assert get_response.status == 200
69-
assert await get_response.text() == '{"origin": "127.0.0.1"}'
62+
async with aiohttp.ClientSession(timeout=self.timeout) as session:
63+
async with session.get(url) as get_response:
64+
assert get_response.status == 200
65+
assert await get_response.text() == '{"origin": "127.0.0.1"}'

0 commit comments

Comments
 (0)