1
1
import sys
2
2
import os
3
+ import time
3
4
import asyncio
4
5
import unittest
5
- from unittest .mock import patch
6
+ from unittest .mock import patch , AsyncMock
6
7
7
8
sys .path .append (os .path .abspath ('docs/2. Getting Started with asyncio' ))
8
9
from ex_2_1 import async_sleep_for_one_second as async_sleep_ex_1
9
10
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
10
17
11
18
12
- class TestAsyncSleep (unittest .TestCase ):
19
+ class TestEx1AsyncSleep (unittest .TestCase ):
13
20
@patch ('asyncio.sleep' , return_value = None )
14
21
def test_async_sleep_for_one_second (self , mock_sleep ):
15
22
with patch ('builtins.print' ) as mock_print :
@@ -23,7 +30,7 @@ def test_async_sleep_for_one_second(self, mock_sleep):
23
30
mock_sleep .assert_called_once_with (1 )
24
31
25
32
26
- class TestAsyncSleep (unittest .TestCase ):
33
+ class TestEx2AsyncSleep (unittest .TestCase ):
27
34
@patch ('asyncio.sleep' , return_value = None )
28
35
def test_async_sleep_for_one_second (self , mock_sleep ):
29
36
with patch ('builtins.print' ) as mock_print :
@@ -38,5 +45,118 @@ def test_async_sleep_for_one_second(self, mock_sleep):
38
45
mock_sleep .assert_called_once_with (1 )
39
46
40
47
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
+
41
161
if __name__ == '__main__' :
42
162
unittest .main ()
0 commit comments