File: test_locks.py

package info (click to toggle)
python-aiohttp 3.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,612 kB
  • sloc: python: 36,917; ansic: 15,734; makefile: 365; sh: 83
file content (55 lines) | stat: -rw-r--r-- 1,299 bytes parent folder | download
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
"""Tests of custom aiohttp locks implementations"""
import asyncio

import pytest

from aiohttp.locks import EventResultOrError


class TestEventResultOrError:

    async def test_set_exception(self, loop) -> None:
        ev = EventResultOrError(loop=loop)

        async def c():
            try:
                await ev.wait()
            except Exception as e:
                return e
            return 1

        t = loop.create_task(c())
        await asyncio.sleep(0, loop=loop)
        e = Exception()
        ev.set(exc=e)
        assert (await t) == e

    async def test_set(self, loop) -> None:
        ev = EventResultOrError(loop=loop)

        async def c():
            await ev.wait()
            return 1

        t = loop.create_task(c())
        await asyncio.sleep(0, loop=loop)
        ev.set()
        assert (await t) == 1

    async def test_cancel_waiters(self, loop) -> None:
        ev = EventResultOrError(loop=loop)

        async def c():
            await ev.wait()

        t1 = loop.create_task(c())
        t2 = loop.create_task(c())
        await asyncio.sleep(0, loop=loop)
        ev.cancel()
        ev.set()

        with pytest.raises(asyncio.CancelledError):
            await t1

        with pytest.raises(asyncio.CancelledError):
            await t2