Debian Codegraph @

src/trio/_core/_tests/test_unbounded_queue.py

raw
1 from __future__ import annotations
2
3 import itertools
4
5 import pytest
6
7 from ... import _core
8 from ...testing import assert_checkpoints, wait_all_tasks_blocked
9
10 pytestmark = pytest.mark.filterwarnings(
11 "ignore:.*UnboundedQueue:trio.TrioDeprecationWarning",
12 )
13
14
15 async def test_UnboundedQueue_basic() -> None:
16 q: _core.UnboundedQueue[str | int | None] = _core.UnboundedQueue()
17 q.put_nowait("hi")
18 assert await q.get_batch() == ["hi"]
19 with pytest.raises(_core.WouldBlock):
20 q.get_batch_nowait()
21 q.put_nowait(1)
22 q.put_nowait(2)
23 q.put_nowait(3)
24 assert q.get_batch_nowait() == [1, 2, 3]
25
26 assert q.empty()
27 assert q.qsize() == 0
28 q.put_nowait(None)
29 assert not q.empty()
30 assert q.qsize() == 1
31
32 stats = q.statistics()
33 assert stats.qsize == 1
34 assert stats.tasks_waiting == 0
35
36 # smoke test
37 repr(q)
38
39
40 async def test_UnboundedQueue_blocking() -> None:
41 record = []
42 q = _core.UnboundedQueue[int]()
43
44 async def get_batch_consumer() -> None:
45 while True:
46 batch = await q.get_batch()
47 assert batch
48 record.append(batch)
49
50 async def aiter_consumer() -> None:
51 async for batch in q:
52 assert batch
53 record.append(batch)
54
55 for consumer in (get_batch_consumer, aiter_consumer):
56 record.clear()
57 async with _core.open_nursery() as nursery:
58 nursery.start_soon(consumer)
59 await _core.wait_all_tasks_blocked()
60 stats = q.statistics()
61 assert stats.qsize == 0
62 assert stats.tasks_waiting == 1
63 q.put_nowait(10)
64 q.put_nowait(11)
65 await _core.wait_all_tasks_blocked()
66 q.put_nowait(12)
67 await _core.wait_all_tasks_blocked()
68 assert record == [[10, 11], [12]]
69 nursery.cancel_scope.cancel()
70
71
72 async def test_UnboundedQueue_fairness() -> None:
73 q = _core.UnboundedQueue[int]()
74
75 # If there's no-one else around, we can put stuff in and take it out
76 # again, no problem
77 q.put_nowait(1)
78 q.put_nowait(2)
79 assert q.get_batch_nowait() == [1, 2]
80
81 result = None
82
83 async def get_batch(q: _core.UnboundedQueue[int]) -> None:
84 nonlocal result
85 result = await q.get_batch()
86
87 # But if someone else is waiting to read, then they get dibs
88 async with _core.open_nursery() as nursery:
89 nursery.start_soon(get_batch, q)
90 await _core.wait_all_tasks_blocked()
91 q.put_nowait(3)
92 q.put_nowait(4)
93 with pytest.raises(_core.WouldBlock):
94 q.get_batch_nowait()
95 assert result == [3, 4]
96
97 # If two tasks are trying to read, they alternate
98 record = []
99
100 async def reader(name: str) -> None:
101 while True:
102 record.append((name, await q.get_batch()))
103
104 async with _core.open_nursery() as nursery:
105 nursery.start_soon(reader, "a")
106 await _core.wait_all_tasks_blocked()
107 nursery.start_soon(reader, "b")
108 await _core.wait_all_tasks_blocked()
109
110 for i in range(20):
111 q.put_nowait(i)
112 await _core.wait_all_tasks_blocked()
113
114 nursery.cancel_scope.cancel()
115
116 assert record == list(zip(itertools.cycle("ab"), [[i] for i in range(20)]))
117
118
119 async def test_UnboundedQueue_trivial_yields() -> None:
120 q = _core.UnboundedQueue[None]()
121
122 q.put_nowait(None)
123 with assert_checkpoints():
124 await q.get_batch()
125
126 q.put_nowait(None)
127 with assert_checkpoints():
128 async for _ in q: # pragma: no branch
129 break
130
131
132 async def test_UnboundedQueue_no_spurious_wakeups() -> None:
133 # If we have two tasks waiting, and put two items into the queue... then
134 # only one task wakes up
135 record = []
136
137 async def getter(q: _core.UnboundedQueue[int], i: int) -> None:
138 got = await q.get_batch()
139 record.append((i, got))
140
141 async with _core.open_nursery() as nursery:
142 q = _core.UnboundedQueue[int]()
143 nursery.start_soon(getter, q, 1)
144 await wait_all_tasks_blocked()
145 nursery.start_soon(getter, q, 2)
146 await wait_all_tasks_blocked()
147
148 for i in range(10):
149 q.put_nowait(i)
150 await wait_all_tasks_blocked()
151
152 assert record == [(1, list(range(10)))]
153
154 nursery.cancel_scope.cancel()