-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_staggered.py
More file actions
151 lines (123 loc) · 4.3 KB
/
test_staggered.py
File metadata and controls
151 lines (123 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import asyncio
import unittest
from asyncio.staggered import staggered_race
from test import support
support.requires_working_socket(module=True)
def tearDownModule():
asyncio.events._set_event_loop_policy(None)
class StaggeredTests(unittest.IsolatedAsyncioTestCase):
async def test_empty(self):
winner, index, excs = await staggered_race(
[],
delay=None,
)
self.assertIs(winner, None)
self.assertIs(index, None)
self.assertEqual(excs, [])
async def test_one_successful(self):
async def coro(index):
return f'Res: {index}'
winner, index, excs = await staggered_race(
[
lambda: coro(0),
lambda: coro(1),
],
delay=None,
)
self.assertEqual(winner, 'Res: 0')
self.assertEqual(index, 0)
self.assertEqual(excs, [None])
async def test_first_error_second_successful(self):
async def coro(index):
if index == 0:
raise ValueError(index)
return f'Res: {index}'
winner, index, excs = await staggered_race(
[
lambda: coro(0),
lambda: coro(1),
],
delay=None,
)
self.assertEqual(winner, 'Res: 1')
self.assertEqual(index, 1)
self.assertEqual(len(excs), 2)
self.assertIsInstance(excs[0], ValueError)
self.assertIs(excs[1], None)
async def test_first_timeout_second_successful(self):
async def coro(index):
if index == 0:
await asyncio.sleep(10) # much bigger than delay
return f'Res: {index}'
winner, index, excs = await staggered_race(
[
lambda: coro(0),
lambda: coro(1),
],
delay=0.1,
)
self.assertEqual(winner, 'Res: 1')
self.assertEqual(index, 1)
self.assertEqual(len(excs), 2)
self.assertIsInstance(excs[0], asyncio.CancelledError)
self.assertIs(excs[1], None)
async def test_none_successful(self):
async def coro(index):
raise ValueError(index)
winner, index, excs = await staggered_race(
[
lambda: coro(0),
lambda: coro(1),
],
delay=None,
)
self.assertIs(winner, None)
self.assertIs(index, None)
self.assertEqual(len(excs), 2)
self.assertIsInstance(excs[0], ValueError)
self.assertIsInstance(excs[1], ValueError)
async def test_multiple_winners(self):
event = asyncio.Event()
async def coro(index):
await event.wait()
return index
async def do_set():
event.set()
await asyncio.Event().wait()
winner, index, excs = await staggered_race(
[
lambda: coro(0),
lambda: coro(1),
do_set,
],
delay=0.1,
)
self.assertIs(winner, 0)
self.assertIs(index, 0)
self.assertEqual(len(excs), 3)
self.assertIsNone(excs[0], None)
self.assertIsInstance(excs[1], asyncio.CancelledError)
self.assertIsInstance(excs[2], asyncio.CancelledError)
async def test_cancelled(self):
log = []
with self.assertRaises(TimeoutError):
async with asyncio.timeout(None) as cs_outer, asyncio.timeout(None) as cs_inner:
async def coro_fn():
cs_inner.reschedule(-1)
await asyncio.sleep(0)
try:
await asyncio.sleep(0)
except asyncio.CancelledError:
log.append("cancelled 1")
cs_outer.reschedule(-1)
await asyncio.sleep(0)
try:
await asyncio.sleep(0)
except asyncio.CancelledError:
log.append("cancelled 2")
try:
await staggered_race([coro_fn], delay=None)
except asyncio.CancelledError:
log.append("cancelled 3")
raise
self.assertListEqual(log, ["cancelled 1", "cancelled 2", "cancelled 3"])