Skip to content

Commit c56bc52

Browse files
Merge pull request #2 from aligheshlaghi97/feat/test
Feat/test
2 parents d6cde10 + 8c6de4c commit c56bc52

File tree

7 files changed

+378
-36
lines changed

7 files changed

+378
-36
lines changed

docs/3. Handling CPU and IO Bound Tasks/ex_3_1.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
import httpx
44

55

6+
async def fetch_data(url):
7+
async with httpx.AsyncClient() as client:
8+
response = await client.get(url)
9+
return response
10+
11+
612
async def main():
713
t = time.time()
8-
async with httpx.AsyncClient() as client:
9-
response = await client.get('https://www.example.com/')
10-
print(response)
14+
response = await fetch_data('https://www.example.com/')
15+
print(f"response is: {response}")
1116
print(f"it took {time.time() - t} s")
17+
1218
asyncio.run(main())

docs/3. Handling CPU and IO Bound Tasks/ex_3_2.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
import httpx
44

55

6-
async def main():
7-
t = time.time()
6+
async def fetch_data(url):
87
async with httpx.AsyncClient() as client:
9-
task1 = asyncio.create_task(client.get('https://www.example.com/'))
10-
task2 = asyncio.create_task(client.get('https://www.example.com/'))
11-
task3 = asyncio.create_task(client.get('https://www.example.com/'))
8+
task1 = asyncio.create_task(client.get(url))
9+
task2 = asyncio.create_task(client.get(url))
10+
task3 = asyncio.create_task(client.get(url))
1211
response1 = await task1
1312
response2 = await task2
1413
response3 = await task3
15-
print(f'response1: {response1}, response2: {response2}, response3: {response3}')
14+
return response1, response2, response3
15+
16+
17+
async def main():
18+
t = time.time()
19+
response1, response2, response3 = await fetch_data('https://www.example.com/')
20+
print(f'response1: {response1}, response2: {response2}, response3: {response3}')
1621
print(f'It took {time.time() - t} s')
1722

1823
asyncio.run(main())

docs/3. Handling CPU and IO Bound Tasks/ex_3_3.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import httpx
44

55

6-
async def main():
7-
t = time.time()
8-
url = 'https://www.example.com/'
6+
async def fetch_data(url):
97
async with httpx.AsyncClient() as client:
108
tasks = [client.get(url) for _ in range(3)]
119
obj = await asyncio.gather(*tasks)
12-
print(f'obj: {obj}, obj type: {type(obj)}')
10+
return obj
11+
12+
13+
async def main():
14+
t = time.time()
15+
obj = await fetch_data('https://www.example.com/')
16+
print(f'obj: {obj}, obj type: {type(obj)}')
1317
print(f'It took {time.time() - t} s')
1418

1519
asyncio.run(main())

docs/3. Handling CPU and IO Bound Tasks/ex_3_4.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22
import httpx
33

44

5-
url = 'https://www.example.org/'
6-
t = time.time()
7-
response1 = httpx.get(url)
8-
response2 = httpx.get(url)
9-
response3 = httpx.get(url)
10-
print(f'It took {time.time() - t} s')
11-
print(f'response1: {response1}, response2: {response2}, response3: {response3}')
5+
def fetch_data(url):
6+
response1 = httpx.get(url)
7+
response2 = httpx.get(url)
8+
response3 = httpx.get(url)
9+
return response1, response2, response3
10+
11+
12+
def main():
13+
url = 'https://www.example.org/'
14+
t = time.time()
15+
response1, response2, response3 = fetch_data(url)
16+
print(f'It took {time.time() - t} s')
17+
print(f'response1: {response1}, response2: {response2}, response3: {response3}')
18+
19+
20+
if __name__ == '__main__':
21+
main()

docs/3. Handling CPU and IO Bound Tasks/ex_3_5.py

+31-13
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,42 @@
33
from functools import partial
44

55

6-
def cpu_bound_task(a: int, n: int) -> float:
6+
def cpu_bound_task(a: int, n: int) -> int | float:
77
_sum = 0
88
for number in range(n):
99
_sum += a
1010
return _sum
1111

1212

13-
t = time.time()
14-
value = cpu_bound_task(2, 100000000)
15-
print(f'value: {value}')
16-
value = cpu_bound_task(2, 100000000)
17-
print(f'It took without multiprocessing {time.time() - t} s')
18-
print(f'value: {value}')
13+
def without_multiprocessing(step: int, value: int) -> tuple[int, int, float]:
14+
t = time.time()
15+
value1 = cpu_bound_task(step, value)
16+
value2 = cpu_bound_task(step, value)
17+
time_taken = time.time() - t
1918

20-
cpu_bound_partial = partial(cpu_bound_task, 2)
19+
return value1, value2, time_taken
2120

22-
with Pool(2) as p:
23-
t = time.time()
24-
value = p.map(cpu_bound_partial, [100000000, 100000000])
25-
print(f'It took with multiprocessing {time.time() - t} s')
26-
print(f'value: {value}')
21+
22+
def with_multiprocessing(step: int, value: int) -> tuple((int, int, float)):
23+
cpu_bound_partial = partial(cpu_bound_task, step)
24+
25+
with Pool(2) as p:
26+
t = time.time()
27+
values = p.map(cpu_bound_partial, [value, value])
28+
time_taken = time.time() - t
29+
30+
return *values, time_taken
31+
32+
33+
def main() -> None:
34+
value1, value2, time_taken_without_mp = without_multiprocessing(step=2, value=100000000)
35+
print(f'Without multiprocessing, value1: {value1}, value2: {value2}')
36+
print(f'time taken: {time_taken_without_mp} s')
37+
print('======================================')
38+
value1, value2, time_taken_with_mp = with_multiprocessing(step=2, value=100000000)
39+
print(f'With multiprocessing, value1: {value1}, value2: {value2}')
40+
print(f'time taken: {time_taken_with_mp} s')
41+
42+
43+
if __name__ == '__main__':
44+
main()

tests/test_ch2.py

+123-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import sys
22
import os
3+
import time
34
import asyncio
45
import unittest
5-
from unittest.mock import patch
6+
from unittest.mock import patch, AsyncMock
67

78
sys.path.append(os.path.abspath('docs/2. Getting Started with asyncio'))
89
from ex_2_1 import async_sleep_for_one_second as async_sleep_ex_1
910
from ex_2_2 import async_sleep_for_one_second as async_sleep_ex_2
11+
from ex_2_3 import nested as nested_ex_3, main as main_ex_3
12+
from ex_2_4 import sleep_coro as sleep_coro_ex_4, main as main_ex_4
13+
from ex_2_5 import sleep_coro as sleep_coro_ex_5, main as main_ex_5
14+
from ex_2_6 import sleep_coro as sleep_coro_ex_6, main as main_ex_6
15+
from ex_2_7 import sleep_coro as sleep_coro_ex_7, main as main_ex_7
16+
from ex_2_8 import sleep_coro as sleep_coro_ex_8, main as main_ex_8
1017

1118

12-
class TestAsyncSleep(unittest.TestCase):
19+
class TestEx1AsyncSleep(unittest.TestCase):
1320
@patch('asyncio.sleep', return_value=None)
1421
def test_async_sleep_for_one_second(self, mock_sleep):
1522
with patch('builtins.print') as mock_print:
@@ -23,7 +30,7 @@ def test_async_sleep_for_one_second(self, mock_sleep):
2330
mock_sleep.assert_called_once_with(1)
2431

2532

26-
class TestAsyncSleep(unittest.TestCase):
33+
class TestEx2AsyncSleep(unittest.TestCase):
2734
@patch('asyncio.sleep', return_value=None)
2835
def test_async_sleep_for_one_second(self, mock_sleep):
2936
with patch('builtins.print') as mock_print:
@@ -38,5 +45,118 @@ def test_async_sleep_for_one_second(self, mock_sleep):
3845
mock_sleep.assert_called_once_with(1)
3946

4047

48+
class TestEx3AsyncNested(unittest.IsolatedAsyncioTestCase):
49+
async def test_nested(self):
50+
result = await nested_ex_3()
51+
self.assertEqual(result, 42)
52+
53+
def test_main(self):
54+
with patch('builtins.print') as mock_print:
55+
asyncio.run(main_ex_3())
56+
mock_print.assert_any_call(42)
57+
58+
59+
class TestEx4AsyncSleep(unittest.IsolatedAsyncioTestCase):
60+
@patch('asyncio.sleep', return_value=None)
61+
async def test_sleep_async_for_ten_second(self, mock_sleep):
62+
with patch('builtins.print') as mock_print:
63+
await sleep_coro_ex_4(10)
64+
65+
mock_print.assert_any_call('Started sleeping for 10 seconds!')
66+
mock_print.assert_any_call('Finished sleeping for 10 seconds!')
67+
68+
mock_sleep.assert_called_once_with(10)
69+
70+
async def test_main_async_sleep_for_one_second(self):
71+
with patch('builtins.print') as mock_print:
72+
await main_ex_4()
73+
mock_print.assert_any_call('Started sleeping for 1 seconds!')
74+
mock_print.assert_any_call('Finished sleeping for 1 seconds!')
75+
76+
77+
class TestEx5AsyncSleep(unittest.IsolatedAsyncioTestCase):
78+
@patch('asyncio.sleep', return_value=None)
79+
async def test_sleep_async_for_five_second(self, mock_sleep):
80+
with patch('builtins.print') as mock_print:
81+
await sleep_coro_ex_5(5)
82+
83+
mock_print.assert_any_call('Started sleeping for 5 seconds!')
84+
mock_print.assert_any_call('Finished sleeping for 5 seconds!')
85+
86+
mock_sleep.assert_called_once_with(5)
87+
88+
async def test_main_async_sleep_two_concurrent_one_sec_sleep_took_about_one_sec(self):
89+
with patch('builtins.print') as mock_print:
90+
start_time = time.time()
91+
await main_ex_5()
92+
self.assertLess(time.time() - start_time, 1.1)
93+
mock_print.assert_any_call('Started sleeping for 1 seconds!')
94+
mock_print.assert_any_call('Finished sleeping for 1 seconds!')
95+
96+
97+
class TestEx6AsyncSleep(unittest.IsolatedAsyncioTestCase):
98+
@patch('asyncio.sleep', return_value=None)
99+
async def test_sleep_async_for_three_second(self, mock_sleep):
100+
with patch('builtins.print') as mock_print:
101+
await sleep_coro_ex_6(3)
102+
103+
mock_print.assert_any_call('Started sleeping for 3 seconds!')
104+
mock_print.assert_any_call('Finished sleeping for 3 seconds!')
105+
106+
mock_sleep.assert_called_once_with(3)
107+
108+
async def test_main_async_sleep_for_one_second(self):
109+
with patch('builtins.print') as mock_print:
110+
await main_ex_6()
111+
mock_print.assert_any_call('Started sleeping for 1 seconds!')
112+
mock_print.assert_any_call('Finished sleeping for 1 seconds!')
113+
114+
115+
class TestEx7AsyncSleep(unittest.IsolatedAsyncioTestCase):
116+
@patch('asyncio.sleep', return_value=None)
117+
async def test_sleep_async_for_seven_second(self, mock_sleep):
118+
with patch('builtins.print') as mock_print:
119+
result = await sleep_coro_ex_7(7)
120+
self.assertEqual(result, 7)
121+
mock_print.assert_any_call('Started sleeping for 7 seconds!')
122+
mock_print.assert_any_call('Finished sleeping for 7 seconds!')
123+
124+
mock_sleep.assert_called_once_with(7)
125+
126+
async def test_main_async_sleep_two_concurrent_two_and_three_sec_sleep_took_about_three_sec(self):
127+
with patch('builtins.print') as mock_print:
128+
start_time = time.time()
129+
await main_ex_7()
130+
self.assertLess(time.time() - start_time, 3.1)
131+
132+
mock_print.assert_any_call('Before running task group!')
133+
134+
mock_print.assert_any_call('Started sleeping for 2 seconds!')
135+
mock_print.assert_any_call('Started sleeping for 3 seconds!')
136+
mock_print.assert_any_call('Finished sleeping for 2 seconds!')
137+
mock_print.assert_any_call('Finished sleeping for 3 seconds!')
138+
139+
mock_print.assert_any_call('After running task group!')
140+
mock_print.assert_any_call('task1: 2, task2: 3')
141+
142+
143+
class TestEx8AsyncSleep(unittest.IsolatedAsyncioTestCase):
144+
@patch('asyncio.sleep', return_value=None)
145+
async def test_sleep_async_for_four_second(self, mock_sleep):
146+
with patch('builtins.print') as mock_print:
147+
await sleep_coro_ex_8(4)
148+
149+
mock_print.assert_any_call('Started sleeping for 4 seconds!')
150+
mock_print.assert_any_call('Finished sleeping for 4 seconds!')
151+
152+
mock_sleep.assert_called_once_with(4)
153+
154+
async def test_main_async_sleep_for_two_second_expect_timeout(self):
155+
with patch('builtins.print') as mock_print:
156+
await main_ex_8()
157+
mock_print.assert_any_call('Started sleeping for 2 seconds!')
158+
mock_print.assert_any_call('timeout!')
159+
160+
41161
if __name__ == '__main__':
42162
unittest.main()

0 commit comments

Comments
 (0)