1
+ import asyncio
1
2
from unittest import mock
2
3
3
4
import pytest
@@ -15,6 +16,13 @@ def stream(loop, protocol):
15
16
return streams .StreamReader (protocol , limit = 1 , loop = loop )
16
17
17
18
19
+ @pytest .fixture
20
+ def buffer (loop , protocol : mock .Mock ) -> streams .FlowControlDataQueue :
21
+ out = streams .FlowControlDataQueue (protocol , limit = 1 , loop = loop )
22
+ out ._allow_pause = True
23
+ return out
24
+
25
+
18
26
class TestFlowControlStreamReader :
19
27
async def test_read (self , stream ) -> None :
20
28
stream .feed_data (b"da" , 2 )
@@ -103,3 +111,72 @@ async def test_read_nowait(self, stream) -> None:
103
111
res = stream .read_nowait (5 )
104
112
assert res == b""
105
113
assert stream ._protocol .resume_reading .call_count == 1 # type: ignore[attr-defined]
114
+
115
+
116
+ async def test_flow_control_data_queue_waiter_cancelled (
117
+ buffer : streams .FlowControlDataQueue ,
118
+ ) -> None :
119
+ """Test that the waiter is cancelled it is cleared."""
120
+ task = asyncio .create_task (buffer .read ())
121
+ await asyncio .sleep (0 )
122
+ assert buffer ._waiter is not None
123
+ buffer ._waiter .cancel ()
124
+
125
+ with pytest .raises (asyncio .CancelledError ):
126
+ await task
127
+ assert buffer ._waiter is None
128
+
129
+
130
+ async def test_flow_control_data_queue_has_buffer (
131
+ buffer : streams .FlowControlDataQueue ,
132
+ ) -> None :
133
+ """Test reading from the buffer."""
134
+ data = object ()
135
+ buffer .feed_data (data , 100 )
136
+ assert buffer ._size == 100
137
+ read_data = await buffer .read ()
138
+ assert read_data is data
139
+ assert buffer ._size == 0
140
+
141
+
142
+ async def test_flow_control_data_queue_read_with_exception (
143
+ buffer : streams .FlowControlDataQueue ,
144
+ ) -> None :
145
+ """Test reading when the buffer is empty and an exception is set."""
146
+ buffer .set_exception (ValueError ("unique_string" ))
147
+ with pytest .raises (ValueError , match = "unique_string" ):
148
+ await buffer .read ()
149
+
150
+
151
+ def test_flow_control_data_queue_feed_pause (
152
+ buffer : streams .FlowControlDataQueue ,
153
+ ) -> None :
154
+ """Test feeding data and pausing the reader."""
155
+ buffer ._protocol ._reading_paused = False
156
+ buffer .feed_data (object (), 100 )
157
+ assert buffer ._protocol .pause_reading .called
158
+
159
+ buffer ._protocol ._reading_paused = True
160
+ buffer ._protocol .pause_reading .reset_mock ()
161
+ buffer .feed_data (object (), 100 )
162
+ assert not buffer ._protocol .pause_reading .called
163
+
164
+
165
+ async def test_flow_control_data_queue_resume_on_read (
166
+ buffer : streams .FlowControlDataQueue ,
167
+ ) -> None :
168
+ """Test that the reader is resumed when reading."""
169
+ buffer .feed_data (object (), 100 )
170
+
171
+ buffer ._protocol ._reading_paused = True
172
+ await buffer .read ()
173
+ assert buffer ._protocol .resume_reading .called
174
+
175
+
176
+ async def test_flow_control_data_queue_read_eof (
177
+ buffer : streams .FlowControlDataQueue ,
178
+ ) -> None :
179
+ """Test that reading after eof raises EofStream."""
180
+ buffer .feed_eof ()
181
+ with pytest .raises (streams .EofStream ):
182
+ await buffer .read ()
0 commit comments