-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.py
332 lines (235 loc) · 9.63 KB
/
test.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import asyncio
import collections
import unittest
from fifolock import FifoLock
TaskState = collections.namedtuple('TaskState', ['acquired', 'done', 'task'])
def create_lock_tasks(*modes):
async def access(mode, acquired, done):
async with mode:
acquired.set_result(None)
await done
def task(mode):
acquired = asyncio.Future()
done = asyncio.Future()
task = asyncio.ensure_future(access(mode, acquired, done))
return TaskState(acquired=acquired, done=done, task=task)
return [task(mode) for mode in modes]
async def mutate_tasks_in_sequence(task_states, *funcs):
history = []
for func in funcs + (null, ):
await asyncio.sleep(0)
await asyncio.sleep(0)
history.append([state.acquired.done() for state in task_states])
func(task_states)
return history
def cancel(i):
def func(tasks):
tasks[i].task.cancel()
return func
def complete(i):
def func(tasks):
tasks[i].done.set_result(None)
return func
def exception(i, exception):
def func(tasks):
tasks[i].done.set_exception(exception)
return func
def null(_):
pass
def async_test(func):
def wrapper(*args, **kwargs):
future = func(*args, **kwargs)
loop = asyncio.get_event_loop()
loop.run_until_complete(future)
return wrapper
class Mutex(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Mutex]
class Read(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Write]
class Write(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Read] and not holds[Write]
class SemaphoreBase(asyncio.Future):
@classmethod
def is_compatible(cls, holds):
return holds[cls] < cls.size
class TestFifoLock(unittest.TestCase):
@async_test
async def test_mutex_blocks_mutex(self):
lock = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Mutex), lock(Mutex)),
complete(0), complete(1),
)
self.assertEqual(acquisition_history[0], [True, False])
self.assertEqual(acquisition_history[1], [True, True])
@async_test
async def test_mutex_mode_can_be_reused(self):
lock = FifoLock()
mode_instance = lock(Mutex)
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
mode_instance, mode_instance),
complete(0), complete(1),
)
self.assertEqual(acquisition_history[0], [True, False])
self.assertEqual(acquisition_history[1], [True, True])
@async_test
async def test_mutex_are_independant(self):
lock_1 = FifoLock()
lock_2 = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock_1(Mutex), lock_2(Mutex)),
complete(0), complete(1),
)
self.assertEqual(acquisition_history[0], [True, True])
@async_test
async def test_mutex_raising_exception_bubbles_and_allows_later_mutex(self):
lock = FifoLock()
tasks = create_lock_tasks(
lock(Mutex), lock(Mutex))
exp = Exception('Raised exception')
acquisition_history = await mutate_tasks_in_sequence(
tasks,
exception(0, exp), complete(1),
)
self.assertEqual(tasks[0].task.exception(), exp)
self.assertEqual(acquisition_history[0], [True, False])
self.assertEqual(acquisition_history[1], [True, True])
@async_test
async def test_mutex_cancelled_after_it_acquires_allows_later_mutex(self):
lock = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Mutex), lock(Mutex), lock(Mutex)),
complete(0), cancel(1), complete(2),
)
self.assertEqual(acquisition_history[0], [True, False, False])
self.assertEqual(acquisition_history[1], [True, True, False])
self.assertEqual(acquisition_history[2], [True, True, True])
@async_test
async def test_mutex_cancelled_before_it_acquires_allows_later_mutex(self):
lock = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Mutex), lock(Mutex), lock(Mutex)),
cancel(1), complete(0), complete(2),
)
self.assertEqual(acquisition_history[0], [True, False, False])
self.assertEqual(acquisition_history[1], [True, False, False])
self.assertEqual(acquisition_history[2], [True, False, True])
@async_test
async def test_mutex_cancelled_that_cancels_second_task_allows_third(self):
lock = FifoLock()
done = asyncio.Future()
acquired_1 = asyncio.Future()
acquired_3 = asyncio.Future()
async def access_1():
try:
async with lock(Mutex):
acquired_1.set_result(None)
await done
except asyncio.CancelledError:
task_2.cancel()
async def access_2():
async with lock(Mutex):
await done
async def access_3():
async with lock(Mutex):
acquired_3.set_result(None)
task_1 = asyncio.ensure_future(access_1())
task_2 = asyncio.ensure_future(access_2())
asyncio.ensure_future(access_3())
await acquired_1
task_1.cancel()
await acquired_3
@async_test
async def test_mutex_requested_concurrently_can_acquire(self):
lock = FifoLock()
tasks_1 = create_lock_tasks(lock(Mutex))
acquisition_history_1 = await mutate_tasks_in_sequence(tasks_1) # No mutation
tasks_2 = create_lock_tasks(lock(Mutex))
acquisition_history_2 = await mutate_tasks_in_sequence(
tasks_1 + tasks_2,
complete(0), complete(1),
)
self.assertEqual(acquisition_history_2[0], [True, False])
self.assertEqual(acquisition_history_2[1], [True, True])
@async_test
async def test_read_write_lock_write_blocks_write(self):
lock = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Write), lock(Write)),
complete(0), complete(1),
)
self.assertEqual(acquisition_history[0], [True, False])
self.assertEqual(acquisition_history[1], [True, True])
@async_test
async def test_read_write_lock_write_blocks_read(self):
lock = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Write), lock(Read)),
complete(0), complete(1),
)
self.assertEqual(acquisition_history[0], [True, False])
self.assertEqual(acquisition_history[1], [True, True])
@async_test
async def test_read_write_lock_read_allows_read(self):
lock = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Read), lock(Read)),
complete(0), complete(1),
)
self.assertEqual(acquisition_history[0], [True, True])
self.assertEqual(acquisition_history[1], [True, True])
@async_test
async def test_read_write_lock_read_blocks_write(self):
lock = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Read), lock(Write)),
complete(0), complete(1),
)
self.assertEqual(acquisition_history[0], [True, False])
self.assertEqual(acquisition_history[1], [True, True])
@async_test
async def test_read_write_lock_reads_complete_out_of_order_still_block_write(self):
lock = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Read), lock(Read), lock(Write)),
complete(1), complete(0), complete(2),
)
self.assertEqual(acquisition_history[0], [True, True, False])
self.assertEqual(acquisition_history[1], [True, True, False])
self.assertEqual(acquisition_history[2], [True, True, True])
@async_test
async def test_read_write_lock_write_not_given_priority_over_read(self):
lock = FifoLock()
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Write), lock(Read), lock(Write)),
complete(0), complete(1), complete(2),
)
self.assertEqual(acquisition_history[0], [True, False, False])
self.assertEqual(acquisition_history[1], [True, True, False])
self.assertEqual(acquisition_history[2], [True, True, True])
@async_test
async def test_semaphore_complete_in_order(self):
lock = FifoLock()
Semaphore = type('Semaphore', (SemaphoreBase, ), {'size': 2})
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Semaphore), lock(Semaphore), lock(Semaphore)),
complete(0), complete(1), complete(2),
)
self.assertEqual(acquisition_history[0], [True, True, False])
self.assertEqual(acquisition_history[1], [True, True, True])
@async_test
async def test_semaphore_complete_out_of_order(self):
lock = FifoLock()
Semaphore = type('Semaphore', (SemaphoreBase, ), {'size': 2})
acquisition_history = await mutate_tasks_in_sequence(create_lock_tasks(
lock(Semaphore), lock(Semaphore), lock(Semaphore)),
complete(1), complete(0), complete(2),
)
self.assertEqual(acquisition_history[0], [True, True, False])
self.assertEqual(acquisition_history[1], [True, True, True])