ยปCore Development>Code coverage>Lib/test/test_asyncio/test_locks.py

Python code coverage for Lib/test/test_asyncio/test_locks.py

#countcontent
1n/a"""Tests for lock.py"""
2n/a
3n/aimport unittest
4n/afrom unittest import mock
5n/aimport re
6n/a
7n/aimport asyncio
8n/afrom asyncio import test_utils
9n/a
10n/aSTR_RGX_REPR = (
11n/a r'^<(?P<class>.*?) object at (?P<address>.*?)'
12n/a r'\[(?P<extras>'
13n/a r'(set|unset|locked|unlocked)(,value:\d)?(,waiters:\d+)?'
14n/a r')\]>\Z'
15n/a)
16n/aRGX_REPR = re.compile(STR_RGX_REPR)
17n/a
18n/a
19n/aclass LockTests(test_utils.TestCase):
20n/a
21n/a def setUp(self):
22n/a super().setUp()
23n/a self.loop = self.new_test_loop()
24n/a
25n/a def test_ctor_loop(self):
26n/a loop = mock.Mock()
27n/a lock = asyncio.Lock(loop=loop)
28n/a self.assertIs(lock._loop, loop)
29n/a
30n/a lock = asyncio.Lock(loop=self.loop)
31n/a self.assertIs(lock._loop, self.loop)
32n/a
33n/a def test_ctor_noloop(self):
34n/a asyncio.set_event_loop(self.loop)
35n/a lock = asyncio.Lock()
36n/a self.assertIs(lock._loop, self.loop)
37n/a
38n/a def test_repr(self):
39n/a lock = asyncio.Lock(loop=self.loop)
40n/a self.assertTrue(repr(lock).endswith('[unlocked]>'))
41n/a self.assertTrue(RGX_REPR.match(repr(lock)))
42n/a
43n/a @asyncio.coroutine
44n/a def acquire_lock():
45n/a yield from lock
46n/a
47n/a self.loop.run_until_complete(acquire_lock())
48n/a self.assertTrue(repr(lock).endswith('[locked]>'))
49n/a self.assertTrue(RGX_REPR.match(repr(lock)))
50n/a
51n/a def test_lock(self):
52n/a lock = asyncio.Lock(loop=self.loop)
53n/a
54n/a @asyncio.coroutine
55n/a def acquire_lock():
56n/a return (yield from lock)
57n/a
58n/a res = self.loop.run_until_complete(acquire_lock())
59n/a
60n/a self.assertTrue(res)
61n/a self.assertTrue(lock.locked())
62n/a
63n/a lock.release()
64n/a self.assertFalse(lock.locked())
65n/a
66n/a def test_acquire(self):
67n/a lock = asyncio.Lock(loop=self.loop)
68n/a result = []
69n/a
70n/a self.assertTrue(self.loop.run_until_complete(lock.acquire()))
71n/a
72n/a @asyncio.coroutine
73n/a def c1(result):
74n/a if (yield from lock.acquire()):
75n/a result.append(1)
76n/a return True
77n/a
78n/a @asyncio.coroutine
79n/a def c2(result):
80n/a if (yield from lock.acquire()):
81n/a result.append(2)
82n/a return True
83n/a
84n/a @asyncio.coroutine
85n/a def c3(result):
86n/a if (yield from lock.acquire()):
87n/a result.append(3)
88n/a return True
89n/a
90n/a t1 = asyncio.Task(c1(result), loop=self.loop)
91n/a t2 = asyncio.Task(c2(result), loop=self.loop)
92n/a
93n/a test_utils.run_briefly(self.loop)
94n/a self.assertEqual([], result)
95n/a
96n/a lock.release()
97n/a test_utils.run_briefly(self.loop)
98n/a self.assertEqual([1], result)
99n/a
100n/a test_utils.run_briefly(self.loop)
101n/a self.assertEqual([1], result)
102n/a
103n/a t3 = asyncio.Task(c3(result), loop=self.loop)
104n/a
105n/a lock.release()
106n/a test_utils.run_briefly(self.loop)
107n/a self.assertEqual([1, 2], result)
108n/a
109n/a lock.release()
110n/a test_utils.run_briefly(self.loop)
111n/a self.assertEqual([1, 2, 3], result)
112n/a
113n/a self.assertTrue(t1.done())
114n/a self.assertTrue(t1.result())
115n/a self.assertTrue(t2.done())
116n/a self.assertTrue(t2.result())
117n/a self.assertTrue(t3.done())
118n/a self.assertTrue(t3.result())
119n/a
120n/a def test_acquire_cancel(self):
121n/a lock = asyncio.Lock(loop=self.loop)
122n/a self.assertTrue(self.loop.run_until_complete(lock.acquire()))
123n/a
124n/a task = asyncio.Task(lock.acquire(), loop=self.loop)
125n/a self.loop.call_soon(task.cancel)
126n/a self.assertRaises(
127n/a asyncio.CancelledError,
128n/a self.loop.run_until_complete, task)
129n/a self.assertFalse(lock._waiters)
130n/a
131n/a def test_cancel_race(self):
132n/a # Several tasks:
133n/a # - A acquires the lock
134n/a # - B is blocked in acquire()
135n/a # - C is blocked in acquire()
136n/a #
137n/a # Now, concurrently:
138n/a # - B is cancelled
139n/a # - A releases the lock
140n/a #
141n/a # If B's waiter is marked cancelled but not yet removed from
142n/a # _waiters, A's release() call will crash when trying to set
143n/a # B's waiter; instead, it should move on to C's waiter.
144n/a
145n/a # Setup: A has the lock, b and c are waiting.
146n/a lock = asyncio.Lock(loop=self.loop)
147n/a
148n/a @asyncio.coroutine
149n/a def lockit(name, blocker):
150n/a yield from lock.acquire()
151n/a try:
152n/a if blocker is not None:
153n/a yield from blocker
154n/a finally:
155n/a lock.release()
156n/a
157n/a fa = asyncio.Future(loop=self.loop)
158n/a ta = asyncio.Task(lockit('A', fa), loop=self.loop)
159n/a test_utils.run_briefly(self.loop)
160n/a self.assertTrue(lock.locked())
161n/a tb = asyncio.Task(lockit('B', None), loop=self.loop)
162n/a test_utils.run_briefly(self.loop)
163n/a self.assertEqual(len(lock._waiters), 1)
164n/a tc = asyncio.Task(lockit('C', None), loop=self.loop)
165n/a test_utils.run_briefly(self.loop)
166n/a self.assertEqual(len(lock._waiters), 2)
167n/a
168n/a # Create the race and check.
169n/a # Without the fix this failed at the last assert.
170n/a fa.set_result(None)
171n/a tb.cancel()
172n/a self.assertTrue(lock._waiters[0].cancelled())
173n/a test_utils.run_briefly(self.loop)
174n/a self.assertFalse(lock.locked())
175n/a self.assertTrue(ta.done())
176n/a self.assertTrue(tb.cancelled())
177n/a self.assertTrue(tc.done())
178n/a
179n/a def test_release_not_acquired(self):
180n/a lock = asyncio.Lock(loop=self.loop)
181n/a
182n/a self.assertRaises(RuntimeError, lock.release)
183n/a
184n/a def test_release_no_waiters(self):
185n/a lock = asyncio.Lock(loop=self.loop)
186n/a self.loop.run_until_complete(lock.acquire())
187n/a self.assertTrue(lock.locked())
188n/a
189n/a lock.release()
190n/a self.assertFalse(lock.locked())
191n/a
192n/a def test_context_manager(self):
193n/a lock = asyncio.Lock(loop=self.loop)
194n/a
195n/a @asyncio.coroutine
196n/a def acquire_lock():
197n/a return (yield from lock)
198n/a
199n/a with self.loop.run_until_complete(acquire_lock()):
200n/a self.assertTrue(lock.locked())
201n/a
202n/a self.assertFalse(lock.locked())
203n/a
204n/a def test_context_manager_cant_reuse(self):
205n/a lock = asyncio.Lock(loop=self.loop)
206n/a
207n/a @asyncio.coroutine
208n/a def acquire_lock():
209n/a return (yield from lock)
210n/a
211n/a # This spells "yield from lock" outside a generator.
212n/a cm = self.loop.run_until_complete(acquire_lock())
213n/a with cm:
214n/a self.assertTrue(lock.locked())
215n/a
216n/a self.assertFalse(lock.locked())
217n/a
218n/a with self.assertRaises(AttributeError):
219n/a with cm:
220n/a pass
221n/a
222n/a def test_context_manager_no_yield(self):
223n/a lock = asyncio.Lock(loop=self.loop)
224n/a
225n/a try:
226n/a with lock:
227n/a self.fail('RuntimeError is not raised in with expression')
228n/a except RuntimeError as err:
229n/a self.assertEqual(
230n/a str(err),
231n/a '"yield from" should be used as context manager expression')
232n/a
233n/a self.assertFalse(lock.locked())
234n/a
235n/a
236n/aclass EventTests(test_utils.TestCase):
237n/a
238n/a def setUp(self):
239n/a super().setUp()
240n/a self.loop = self.new_test_loop()
241n/a
242n/a def test_ctor_loop(self):
243n/a loop = mock.Mock()
244n/a ev = asyncio.Event(loop=loop)
245n/a self.assertIs(ev._loop, loop)
246n/a
247n/a ev = asyncio.Event(loop=self.loop)
248n/a self.assertIs(ev._loop, self.loop)
249n/a
250n/a def test_ctor_noloop(self):
251n/a asyncio.set_event_loop(self.loop)
252n/a ev = asyncio.Event()
253n/a self.assertIs(ev._loop, self.loop)
254n/a
255n/a def test_repr(self):
256n/a ev = asyncio.Event(loop=self.loop)
257n/a self.assertTrue(repr(ev).endswith('[unset]>'))
258n/a match = RGX_REPR.match(repr(ev))
259n/a self.assertEqual(match.group('extras'), 'unset')
260n/a
261n/a ev.set()
262n/a self.assertTrue(repr(ev).endswith('[set]>'))
263n/a self.assertTrue(RGX_REPR.match(repr(ev)))
264n/a
265n/a ev._waiters.append(mock.Mock())
266n/a self.assertTrue('waiters:1' in repr(ev))
267n/a self.assertTrue(RGX_REPR.match(repr(ev)))
268n/a
269n/a def test_wait(self):
270n/a ev = asyncio.Event(loop=self.loop)
271n/a self.assertFalse(ev.is_set())
272n/a
273n/a result = []
274n/a
275n/a @asyncio.coroutine
276n/a def c1(result):
277n/a if (yield from ev.wait()):
278n/a result.append(1)
279n/a
280n/a @asyncio.coroutine
281n/a def c2(result):
282n/a if (yield from ev.wait()):
283n/a result.append(2)
284n/a
285n/a @asyncio.coroutine
286n/a def c3(result):
287n/a if (yield from ev.wait()):
288n/a result.append(3)
289n/a
290n/a t1 = asyncio.Task(c1(result), loop=self.loop)
291n/a t2 = asyncio.Task(c2(result), loop=self.loop)
292n/a
293n/a test_utils.run_briefly(self.loop)
294n/a self.assertEqual([], result)
295n/a
296n/a t3 = asyncio.Task(c3(result), loop=self.loop)
297n/a
298n/a ev.set()
299n/a test_utils.run_briefly(self.loop)
300n/a self.assertEqual([3, 1, 2], result)
301n/a
302n/a self.assertTrue(t1.done())
303n/a self.assertIsNone(t1.result())
304n/a self.assertTrue(t2.done())
305n/a self.assertIsNone(t2.result())
306n/a self.assertTrue(t3.done())
307n/a self.assertIsNone(t3.result())
308n/a
309n/a def test_wait_on_set(self):
310n/a ev = asyncio.Event(loop=self.loop)
311n/a ev.set()
312n/a
313n/a res = self.loop.run_until_complete(ev.wait())
314n/a self.assertTrue(res)
315n/a
316n/a def test_wait_cancel(self):
317n/a ev = asyncio.Event(loop=self.loop)
318n/a
319n/a wait = asyncio.Task(ev.wait(), loop=self.loop)
320n/a self.loop.call_soon(wait.cancel)
321n/a self.assertRaises(
322n/a asyncio.CancelledError,
323n/a self.loop.run_until_complete, wait)
324n/a self.assertFalse(ev._waiters)
325n/a
326n/a def test_clear(self):
327n/a ev = asyncio.Event(loop=self.loop)
328n/a self.assertFalse(ev.is_set())
329n/a
330n/a ev.set()
331n/a self.assertTrue(ev.is_set())
332n/a
333n/a ev.clear()
334n/a self.assertFalse(ev.is_set())
335n/a
336n/a def test_clear_with_waiters(self):
337n/a ev = asyncio.Event(loop=self.loop)
338n/a result = []
339n/a
340n/a @asyncio.coroutine
341n/a def c1(result):
342n/a if (yield from ev.wait()):
343n/a result.append(1)
344n/a return True
345n/a
346n/a t = asyncio.Task(c1(result), loop=self.loop)
347n/a test_utils.run_briefly(self.loop)
348n/a self.assertEqual([], result)
349n/a
350n/a ev.set()
351n/a ev.clear()
352n/a self.assertFalse(ev.is_set())
353n/a
354n/a ev.set()
355n/a ev.set()
356n/a self.assertEqual(1, len(ev._waiters))
357n/a
358n/a test_utils.run_briefly(self.loop)
359n/a self.assertEqual([1], result)
360n/a self.assertEqual(0, len(ev._waiters))
361n/a
362n/a self.assertTrue(t.done())
363n/a self.assertTrue(t.result())
364n/a
365n/a
366n/aclass ConditionTests(test_utils.TestCase):
367n/a
368n/a def setUp(self):
369n/a super().setUp()
370n/a self.loop = self.new_test_loop()
371n/a
372n/a def test_ctor_loop(self):
373n/a loop = mock.Mock()
374n/a cond = asyncio.Condition(loop=loop)
375n/a self.assertIs(cond._loop, loop)
376n/a
377n/a cond = asyncio.Condition(loop=self.loop)
378n/a self.assertIs(cond._loop, self.loop)
379n/a
380n/a def test_ctor_noloop(self):
381n/a asyncio.set_event_loop(self.loop)
382n/a cond = asyncio.Condition()
383n/a self.assertIs(cond._loop, self.loop)
384n/a
385n/a def test_wait(self):
386n/a cond = asyncio.Condition(loop=self.loop)
387n/a result = []
388n/a
389n/a @asyncio.coroutine
390n/a def c1(result):
391n/a yield from cond.acquire()
392n/a if (yield from cond.wait()):
393n/a result.append(1)
394n/a return True
395n/a
396n/a @asyncio.coroutine
397n/a def c2(result):
398n/a yield from cond.acquire()
399n/a if (yield from cond.wait()):
400n/a result.append(2)
401n/a return True
402n/a
403n/a @asyncio.coroutine
404n/a def c3(result):
405n/a yield from cond.acquire()
406n/a if (yield from cond.wait()):
407n/a result.append(3)
408n/a return True
409n/a
410n/a t1 = asyncio.Task(c1(result), loop=self.loop)
411n/a t2 = asyncio.Task(c2(result), loop=self.loop)
412n/a t3 = asyncio.Task(c3(result), loop=self.loop)
413n/a
414n/a test_utils.run_briefly(self.loop)
415n/a self.assertEqual([], result)
416n/a self.assertFalse(cond.locked())
417n/a
418n/a self.assertTrue(self.loop.run_until_complete(cond.acquire()))
419n/a cond.notify()
420n/a test_utils.run_briefly(self.loop)
421n/a self.assertEqual([], result)
422n/a self.assertTrue(cond.locked())
423n/a
424n/a cond.release()
425n/a test_utils.run_briefly(self.loop)
426n/a self.assertEqual([1], result)
427n/a self.assertTrue(cond.locked())
428n/a
429n/a cond.notify(2)
430n/a test_utils.run_briefly(self.loop)
431n/a self.assertEqual([1], result)
432n/a self.assertTrue(cond.locked())
433n/a
434n/a cond.release()
435n/a test_utils.run_briefly(self.loop)
436n/a self.assertEqual([1, 2], result)
437n/a self.assertTrue(cond.locked())
438n/a
439n/a cond.release()
440n/a test_utils.run_briefly(self.loop)
441n/a self.assertEqual([1, 2, 3], result)
442n/a self.assertTrue(cond.locked())
443n/a
444n/a self.assertTrue(t1.done())
445n/a self.assertTrue(t1.result())
446n/a self.assertTrue(t2.done())
447n/a self.assertTrue(t2.result())
448n/a self.assertTrue(t3.done())
449n/a self.assertTrue(t3.result())
450n/a
451n/a def test_wait_cancel(self):
452n/a cond = asyncio.Condition(loop=self.loop)
453n/a self.loop.run_until_complete(cond.acquire())
454n/a
455n/a wait = asyncio.Task(cond.wait(), loop=self.loop)
456n/a self.loop.call_soon(wait.cancel)
457n/a self.assertRaises(
458n/a asyncio.CancelledError,
459n/a self.loop.run_until_complete, wait)
460n/a self.assertFalse(cond._waiters)
461n/a self.assertTrue(cond.locked())
462n/a
463n/a def test_wait_cancel_contested(self):
464n/a cond = asyncio.Condition(loop=self.loop)
465n/a
466n/a self.loop.run_until_complete(cond.acquire())
467n/a self.assertTrue(cond.locked())
468n/a
469n/a wait_task = asyncio.Task(cond.wait(), loop=self.loop)
470n/a test_utils.run_briefly(self.loop)
471n/a self.assertFalse(cond.locked())
472n/a
473n/a # Notify, but contest the lock before cancelling
474n/a self.loop.run_until_complete(cond.acquire())
475n/a self.assertTrue(cond.locked())
476n/a cond.notify()
477n/a self.loop.call_soon(wait_task.cancel)
478n/a self.loop.call_soon(cond.release)
479n/a
480n/a try:
481n/a self.loop.run_until_complete(wait_task)
482n/a except asyncio.CancelledError:
483n/a # Should not happen, since no cancellation points
484n/a pass
485n/a
486n/a self.assertTrue(cond.locked())
487n/a
488n/a def test_wait_unacquired(self):
489n/a cond = asyncio.Condition(loop=self.loop)
490n/a self.assertRaises(
491n/a RuntimeError,
492n/a self.loop.run_until_complete, cond.wait())
493n/a
494n/a def test_wait_for(self):
495n/a cond = asyncio.Condition(loop=self.loop)
496n/a presult = False
497n/a
498n/a def predicate():
499n/a return presult
500n/a
501n/a result = []
502n/a
503n/a @asyncio.coroutine
504n/a def c1(result):
505n/a yield from cond.acquire()
506n/a if (yield from cond.wait_for(predicate)):
507n/a result.append(1)
508n/a cond.release()
509n/a return True
510n/a
511n/a t = asyncio.Task(c1(result), loop=self.loop)
512n/a
513n/a test_utils.run_briefly(self.loop)
514n/a self.assertEqual([], result)
515n/a
516n/a self.loop.run_until_complete(cond.acquire())
517n/a cond.notify()
518n/a cond.release()
519n/a test_utils.run_briefly(self.loop)
520n/a self.assertEqual([], result)
521n/a
522n/a presult = True
523n/a self.loop.run_until_complete(cond.acquire())
524n/a cond.notify()
525n/a cond.release()
526n/a test_utils.run_briefly(self.loop)
527n/a self.assertEqual([1], result)
528n/a
529n/a self.assertTrue(t.done())
530n/a self.assertTrue(t.result())
531n/a
532n/a def test_wait_for_unacquired(self):
533n/a cond = asyncio.Condition(loop=self.loop)
534n/a
535n/a # predicate can return true immediately
536n/a res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3]))
537n/a self.assertEqual([1, 2, 3], res)
538n/a
539n/a self.assertRaises(
540n/a RuntimeError,
541n/a self.loop.run_until_complete,
542n/a cond.wait_for(lambda: False))
543n/a
544n/a def test_notify(self):
545n/a cond = asyncio.Condition(loop=self.loop)
546n/a result = []
547n/a
548n/a @asyncio.coroutine
549n/a def c1(result):
550n/a yield from cond.acquire()
551n/a if (yield from cond.wait()):
552n/a result.append(1)
553n/a cond.release()
554n/a return True
555n/a
556n/a @asyncio.coroutine
557n/a def c2(result):
558n/a yield from cond.acquire()
559n/a if (yield from cond.wait()):
560n/a result.append(2)
561n/a cond.release()
562n/a return True
563n/a
564n/a @asyncio.coroutine
565n/a def c3(result):
566n/a yield from cond.acquire()
567n/a if (yield from cond.wait()):
568n/a result.append(3)
569n/a cond.release()
570n/a return True
571n/a
572n/a t1 = asyncio.Task(c1(result), loop=self.loop)
573n/a t2 = asyncio.Task(c2(result), loop=self.loop)
574n/a t3 = asyncio.Task(c3(result), loop=self.loop)
575n/a
576n/a test_utils.run_briefly(self.loop)
577n/a self.assertEqual([], result)
578n/a
579n/a self.loop.run_until_complete(cond.acquire())
580n/a cond.notify(1)
581n/a cond.release()
582n/a test_utils.run_briefly(self.loop)
583n/a self.assertEqual([1], result)
584n/a
585n/a self.loop.run_until_complete(cond.acquire())
586n/a cond.notify(1)
587n/a cond.notify(2048)
588n/a cond.release()
589n/a test_utils.run_briefly(self.loop)
590n/a self.assertEqual([1, 2, 3], result)
591n/a
592n/a self.assertTrue(t1.done())
593n/a self.assertTrue(t1.result())
594n/a self.assertTrue(t2.done())
595n/a self.assertTrue(t2.result())
596n/a self.assertTrue(t3.done())
597n/a self.assertTrue(t3.result())
598n/a
599n/a def test_notify_all(self):
600n/a cond = asyncio.Condition(loop=self.loop)
601n/a
602n/a result = []
603n/a
604n/a @asyncio.coroutine
605n/a def c1(result):
606n/a yield from cond.acquire()
607n/a if (yield from cond.wait()):
608n/a result.append(1)
609n/a cond.release()
610n/a return True
611n/a
612n/a @asyncio.coroutine
613n/a def c2(result):
614n/a yield from cond.acquire()
615n/a if (yield from cond.wait()):
616n/a result.append(2)
617n/a cond.release()
618n/a return True
619n/a
620n/a t1 = asyncio.Task(c1(result), loop=self.loop)
621n/a t2 = asyncio.Task(c2(result), loop=self.loop)
622n/a
623n/a test_utils.run_briefly(self.loop)
624n/a self.assertEqual([], result)
625n/a
626n/a self.loop.run_until_complete(cond.acquire())
627n/a cond.notify_all()
628n/a cond.release()
629n/a test_utils.run_briefly(self.loop)
630n/a self.assertEqual([1, 2], result)
631n/a
632n/a self.assertTrue(t1.done())
633n/a self.assertTrue(t1.result())
634n/a self.assertTrue(t2.done())
635n/a self.assertTrue(t2.result())
636n/a
637n/a def test_notify_unacquired(self):
638n/a cond = asyncio.Condition(loop=self.loop)
639n/a self.assertRaises(RuntimeError, cond.notify)
640n/a
641n/a def test_notify_all_unacquired(self):
642n/a cond = asyncio.Condition(loop=self.loop)
643n/a self.assertRaises(RuntimeError, cond.notify_all)
644n/a
645n/a def test_repr(self):
646n/a cond = asyncio.Condition(loop=self.loop)
647n/a self.assertTrue('unlocked' in repr(cond))
648n/a self.assertTrue(RGX_REPR.match(repr(cond)))
649n/a
650n/a self.loop.run_until_complete(cond.acquire())
651n/a self.assertTrue('locked' in repr(cond))
652n/a
653n/a cond._waiters.append(mock.Mock())
654n/a self.assertTrue('waiters:1' in repr(cond))
655n/a self.assertTrue(RGX_REPR.match(repr(cond)))
656n/a
657n/a cond._waiters.append(mock.Mock())
658n/a self.assertTrue('waiters:2' in repr(cond))
659n/a self.assertTrue(RGX_REPR.match(repr(cond)))
660n/a
661n/a def test_context_manager(self):
662n/a cond = asyncio.Condition(loop=self.loop)
663n/a
664n/a @asyncio.coroutine
665n/a def acquire_cond():
666n/a return (yield from cond)
667n/a
668n/a with self.loop.run_until_complete(acquire_cond()):
669n/a self.assertTrue(cond.locked())
670n/a
671n/a self.assertFalse(cond.locked())
672n/a
673n/a def test_context_manager_no_yield(self):
674n/a cond = asyncio.Condition(loop=self.loop)
675n/a
676n/a try:
677n/a with cond:
678n/a self.fail('RuntimeError is not raised in with expression')
679n/a except RuntimeError as err:
680n/a self.assertEqual(
681n/a str(err),
682n/a '"yield from" should be used as context manager expression')
683n/a
684n/a self.assertFalse(cond.locked())
685n/a
686n/a def test_explicit_lock(self):
687n/a lock = asyncio.Lock(loop=self.loop)
688n/a cond = asyncio.Condition(lock, loop=self.loop)
689n/a
690n/a self.assertIs(cond._lock, lock)
691n/a self.assertIs(cond._loop, lock._loop)
692n/a
693n/a def test_ambiguous_loops(self):
694n/a loop = self.new_test_loop()
695n/a self.addCleanup(loop.close)
696n/a
697n/a lock = asyncio.Lock(loop=self.loop)
698n/a with self.assertRaises(ValueError):
699n/a asyncio.Condition(lock, loop=loop)
700n/a
701n/a
702n/aclass SemaphoreTests(test_utils.TestCase):
703n/a
704n/a def setUp(self):
705n/a super().setUp()
706n/a self.loop = self.new_test_loop()
707n/a
708n/a def test_ctor_loop(self):
709n/a loop = mock.Mock()
710n/a sem = asyncio.Semaphore(loop=loop)
711n/a self.assertIs(sem._loop, loop)
712n/a
713n/a sem = asyncio.Semaphore(loop=self.loop)
714n/a self.assertIs(sem._loop, self.loop)
715n/a
716n/a def test_ctor_noloop(self):
717n/a asyncio.set_event_loop(self.loop)
718n/a sem = asyncio.Semaphore()
719n/a self.assertIs(sem._loop, self.loop)
720n/a
721n/a def test_initial_value_zero(self):
722n/a sem = asyncio.Semaphore(0, loop=self.loop)
723n/a self.assertTrue(sem.locked())
724n/a
725n/a def test_repr(self):
726n/a sem = asyncio.Semaphore(loop=self.loop)
727n/a self.assertTrue(repr(sem).endswith('[unlocked,value:1]>'))
728n/a self.assertTrue(RGX_REPR.match(repr(sem)))
729n/a
730n/a self.loop.run_until_complete(sem.acquire())
731n/a self.assertTrue(repr(sem).endswith('[locked]>'))
732n/a self.assertTrue('waiters' not in repr(sem))
733n/a self.assertTrue(RGX_REPR.match(repr(sem)))
734n/a
735n/a sem._waiters.append(mock.Mock())
736n/a self.assertTrue('waiters:1' in repr(sem))
737n/a self.assertTrue(RGX_REPR.match(repr(sem)))
738n/a
739n/a sem._waiters.append(mock.Mock())
740n/a self.assertTrue('waiters:2' in repr(sem))
741n/a self.assertTrue(RGX_REPR.match(repr(sem)))
742n/a
743n/a def test_semaphore(self):
744n/a sem = asyncio.Semaphore(loop=self.loop)
745n/a self.assertEqual(1, sem._value)
746n/a
747n/a @asyncio.coroutine
748n/a def acquire_lock():
749n/a return (yield from sem)
750n/a
751n/a res = self.loop.run_until_complete(acquire_lock())
752n/a
753n/a self.assertTrue(res)
754n/a self.assertTrue(sem.locked())
755n/a self.assertEqual(0, sem._value)
756n/a
757n/a sem.release()
758n/a self.assertFalse(sem.locked())
759n/a self.assertEqual(1, sem._value)
760n/a
761n/a def test_semaphore_value(self):
762n/a self.assertRaises(ValueError, asyncio.Semaphore, -1)
763n/a
764n/a def test_acquire(self):
765n/a sem = asyncio.Semaphore(3, loop=self.loop)
766n/a result = []
767n/a
768n/a self.assertTrue(self.loop.run_until_complete(sem.acquire()))
769n/a self.assertTrue(self.loop.run_until_complete(sem.acquire()))
770n/a self.assertFalse(sem.locked())
771n/a
772n/a @asyncio.coroutine
773n/a def c1(result):
774n/a yield from sem.acquire()
775n/a result.append(1)
776n/a return True
777n/a
778n/a @asyncio.coroutine
779n/a def c2(result):
780n/a yield from sem.acquire()
781n/a result.append(2)
782n/a return True
783n/a
784n/a @asyncio.coroutine
785n/a def c3(result):
786n/a yield from sem.acquire()
787n/a result.append(3)
788n/a return True
789n/a
790n/a @asyncio.coroutine
791n/a def c4(result):
792n/a yield from sem.acquire()
793n/a result.append(4)
794n/a return True
795n/a
796n/a t1 = asyncio.Task(c1(result), loop=self.loop)
797n/a t2 = asyncio.Task(c2(result), loop=self.loop)
798n/a t3 = asyncio.Task(c3(result), loop=self.loop)
799n/a
800n/a test_utils.run_briefly(self.loop)
801n/a self.assertEqual([1], result)
802n/a self.assertTrue(sem.locked())
803n/a self.assertEqual(2, len(sem._waiters))
804n/a self.assertEqual(0, sem._value)
805n/a
806n/a t4 = asyncio.Task(c4(result), loop=self.loop)
807n/a
808n/a sem.release()
809n/a sem.release()
810n/a self.assertEqual(2, sem._value)
811n/a
812n/a test_utils.run_briefly(self.loop)
813n/a self.assertEqual(0, sem._value)
814n/a self.assertEqual(3, len(result))
815n/a self.assertTrue(sem.locked())
816n/a self.assertEqual(1, len(sem._waiters))
817n/a self.assertEqual(0, sem._value)
818n/a
819n/a self.assertTrue(t1.done())
820n/a self.assertTrue(t1.result())
821n/a race_tasks = [t2, t3, t4]
822n/a done_tasks = [t for t in race_tasks if t.done() and t.result()]
823n/a self.assertTrue(2, len(done_tasks))
824n/a
825n/a # cleanup locked semaphore
826n/a sem.release()
827n/a self.loop.run_until_complete(asyncio.gather(*race_tasks))
828n/a
829n/a def test_acquire_cancel(self):
830n/a sem = asyncio.Semaphore(loop=self.loop)
831n/a self.loop.run_until_complete(sem.acquire())
832n/a
833n/a acquire = asyncio.Task(sem.acquire(), loop=self.loop)
834n/a self.loop.call_soon(acquire.cancel)
835n/a self.assertRaises(
836n/a asyncio.CancelledError,
837n/a self.loop.run_until_complete, acquire)
838n/a self.assertTrue((not sem._waiters) or
839n/a all(waiter.done() for waiter in sem._waiters))
840n/a
841n/a def test_acquire_cancel_before_awoken(self):
842n/a sem = asyncio.Semaphore(value=0, loop=self.loop)
843n/a
844n/a t1 = asyncio.Task(sem.acquire(), loop=self.loop)
845n/a t2 = asyncio.Task(sem.acquire(), loop=self.loop)
846n/a t3 = asyncio.Task(sem.acquire(), loop=self.loop)
847n/a t4 = asyncio.Task(sem.acquire(), loop=self.loop)
848n/a
849n/a test_utils.run_briefly(self.loop)
850n/a
851n/a sem.release()
852n/a t1.cancel()
853n/a t2.cancel()
854n/a
855n/a test_utils.run_briefly(self.loop)
856n/a num_done = sum(t.done() for t in [t3, t4])
857n/a self.assertEqual(num_done, 1)
858n/a
859n/a t3.cancel()
860n/a t4.cancel()
861n/a test_utils.run_briefly(self.loop)
862n/a
863n/a def test_acquire_hang(self):
864n/a sem = asyncio.Semaphore(value=0, loop=self.loop)
865n/a
866n/a t1 = asyncio.Task(sem.acquire(), loop=self.loop)
867n/a t2 = asyncio.Task(sem.acquire(), loop=self.loop)
868n/a
869n/a test_utils.run_briefly(self.loop)
870n/a
871n/a sem.release()
872n/a t1.cancel()
873n/a
874n/a test_utils.run_briefly(self.loop)
875n/a self.assertTrue(sem.locked())
876n/a
877n/a def test_release_not_acquired(self):
878n/a sem = asyncio.BoundedSemaphore(loop=self.loop)
879n/a
880n/a self.assertRaises(ValueError, sem.release)
881n/a
882n/a def test_release_no_waiters(self):
883n/a sem = asyncio.Semaphore(loop=self.loop)
884n/a self.loop.run_until_complete(sem.acquire())
885n/a self.assertTrue(sem.locked())
886n/a
887n/a sem.release()
888n/a self.assertFalse(sem.locked())
889n/a
890n/a def test_context_manager(self):
891n/a sem = asyncio.Semaphore(2, loop=self.loop)
892n/a
893n/a @asyncio.coroutine
894n/a def acquire_lock():
895n/a return (yield from sem)
896n/a
897n/a with self.loop.run_until_complete(acquire_lock()):
898n/a self.assertFalse(sem.locked())
899n/a self.assertEqual(1, sem._value)
900n/a
901n/a with self.loop.run_until_complete(acquire_lock()):
902n/a self.assertTrue(sem.locked())
903n/a
904n/a self.assertEqual(2, sem._value)
905n/a
906n/a def test_context_manager_no_yield(self):
907n/a sem = asyncio.Semaphore(2, loop=self.loop)
908n/a
909n/a try:
910n/a with sem:
911n/a self.fail('RuntimeError is not raised in with expression')
912n/a except RuntimeError as err:
913n/a self.assertEqual(
914n/a str(err),
915n/a '"yield from" should be used as context manager expression')
916n/a
917n/a self.assertEqual(2, sem._value)
918n/a
919n/a
920n/aif __name__ == '__main__':
921n/a unittest.main()