Coverage for tests/brokers/nats/test_pull_subscriber.py: 97%
176 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1import asyncio
2from contextlib import suppress
3from typing import Any
5import pytest
6from nats.errors import ConnectionClosedError, TimeoutError
7from nats.js.errors import ServiceUnavailableError
9from faststream.nats import JStream, NatsBroker, PullSub
10from faststream.nats.subscriber.usecases import ConcurrentPullStreamSubscriber
13class FakeSubscription:
14 def __init__(self, messages: int = 2) -> None:
15 self.messages = messages
16 self.fetch_requests: list[int] = []
17 self.next_fetch_started = asyncio.Event()
19 async def fetch(self, *, batch: int, timeout: float | None) -> list[object]:
20 self.fetch_requests.append(batch)
22 if len(self.fetch_requests) <= self.messages:
23 return [object()]
25 self.next_fetch_started.set()
26 await asyncio.Event().wait()
27 return []
30def create_subscriber(
31 *, batch_size: int = 2, max_workers: int = 2
32) -> ConcurrentPullStreamSubscriber:
33 broker = NatsBroker()
34 subscriber = broker.subscriber(
35 "test",
36 stream=JStream("test"),
37 pull_sub=PullSub(batch_size=batch_size),
38 max_workers=max_workers,
39 )
40 assert isinstance(subscriber, ConcurrentPullStreamSubscriber)
41 return subscriber
44async def stop_task(task: asyncio.Task[Any]) -> None:
45 task.cancel()
46 with suppress(asyncio.CancelledError):
47 await task
50@pytest.mark.asyncio()
51@pytest.mark.nats()
52@pytest.mark.parametrize(("batch_size", "max_workers"), ((5, 2), (1, 3)))
53async def test_pull_waits_for_worker_capacity(batch_size: int, max_workers: int) -> None:
54 """Fixes https://github.com/ag2ai/faststream/issues/2563."""
55 subscriber = create_subscriber(batch_size=batch_size, max_workers=max_workers)
56 subscription: Any = FakeSubscription(messages=max_workers)
57 subscriber.subscription = subscription
58 subscriber.running = True
60 handlers_started = 0
61 all_handlers_started = asyncio.Event()
62 release_handler = asyncio.Semaphore(0)
64 async def consume(message: object) -> None:
65 nonlocal handlers_started
66 handlers_started += 1
67 if handlers_started == max_workers:
68 all_handlers_started.set()
69 await release_handler.acquire()
71 pull_task = asyncio.create_task(subscriber._consume_pull(cb=consume))
73 try:
74 await asyncio.wait_for(all_handlers_started.wait(), timeout=1)
75 assert subscription.fetch_requests == [1] * max_workers
77 release_handler.release()
78 await asyncio.wait_for(subscription.next_fetch_started.wait(), timeout=1)
79 assert subscription.fetch_requests == [1] * (max_workers + 1)
81 finally:
82 subscriber.running = False
83 for _ in range(max_workers):
84 release_handler.release()
85 await stop_task(pull_task)
87 assert subscriber.limiter.value == subscriber.max_workers
90@pytest.mark.asyncio()
91@pytest.mark.nats()
92@pytest.mark.parametrize(
93 "error", (TimeoutError, ConnectionClosedError, ServiceUnavailableError)
94)
95async def test_pull_releases_capacity_after_fetch_error(error: type[Exception]) -> None:
96 """Fixes https://github.com/ag2ai/faststream/issues/2563."""
97 subscriber = create_subscriber()
98 second_fetch_started = asyncio.Event()
99 fetch_requests: list[int] = []
101 class FailingOnceSubscription:
102 async def fetch(
103 self,
104 *,
105 batch: int,
106 timeout: float | None,
107 ) -> list[object]:
108 fetch_requests.append(batch)
109 if len(fetch_requests) == 1:
110 raise error
112 second_fetch_started.set()
113 await asyncio.Event().wait()
114 return []
116 subscription: Any = FailingOnceSubscription()
117 subscriber.subscription = subscription
118 subscriber.running = True
119 pull_task = asyncio.create_task(subscriber._consume_pull(cb=subscriber.consume))
121 try:
122 await asyncio.wait_for(second_fetch_started.wait(), timeout=1)
123 assert fetch_requests == [1, 1]
124 finally:
125 subscriber.running = False
126 await stop_task(pull_task)
128 assert subscriber.limiter.value == subscriber.max_workers
131@pytest.mark.asyncio()
132@pytest.mark.nats()
133async def test_pull_does_not_fetch_after_stop_while_waiting_for_capacity() -> None:
134 """Fixes https://github.com/ag2ai/faststream/issues/2563."""
135 subscriber = create_subscriber()
136 subscription: Any = FakeSubscription()
137 subscriber.subscription = subscription
138 subscriber.running = True
139 handlers_started = 0
140 both_handlers_started = asyncio.Event()
141 release_handlers = asyncio.Event()
143 async def consume(message: object) -> None:
144 nonlocal handlers_started
145 handlers_started += 1
146 if handlers_started == 2:
147 both_handlers_started.set()
148 await release_handlers.wait()
150 pull_task = asyncio.create_task(subscriber._consume_pull(cb=consume))
151 try:
152 await asyncio.wait_for(both_handlers_started.wait(), timeout=1)
153 subscriber.running = False
154 release_handlers.set()
155 await asyncio.wait_for(pull_task, timeout=1)
156 finally:
157 subscriber.running = False
158 release_handlers.set()
159 await stop_task(pull_task)
161 assert subscription.fetch_requests == [1, 1]
162 assert subscriber.limiter.value == subscriber.max_workers
165@pytest.mark.asyncio()
166@pytest.mark.nats()
167@pytest.mark.parametrize("stop_before_release", (False, True))
168async def test_unexpected_fetch_error_does_not_cancel_active_handler(
169 stop_before_release: bool,
170) -> None:
171 """Fixes https://github.com/ag2ai/faststream/issues/2563."""
172 subscriber = create_subscriber()
173 second_fetch_started = asyncio.Event()
174 fetch_requests: list[int] = []
176 class FailingSubscription:
177 async def fetch(
178 self,
179 *,
180 batch: int,
181 timeout: float | None,
182 ) -> list[object]:
183 fetch_requests.append(batch)
184 if len(fetch_requests) <= 2:
185 return [object()]
187 second_fetch_started.set()
188 msg = "unexpected fetch error"
189 raise RuntimeError(msg)
191 subscription: Any = FailingSubscription()
192 subscriber.subscription = subscription
193 subscriber.running = True
194 handlers_started = 0
195 both_handlers_started = asyncio.Event()
196 release_handler = asyncio.Semaphore(0)
198 async def consume(message: object) -> None:
199 nonlocal handlers_started
200 handlers_started += 1
201 if handlers_started == 2:
202 both_handlers_started.set()
203 await release_handler.acquire()
205 pull_task = asyncio.create_task(subscriber._consume_pull(cb=consume))
206 try:
207 await asyncio.wait_for(both_handlers_started.wait(), timeout=1)
209 release_handler.release()
210 await asyncio.wait_for(second_fetch_started.wait(), timeout=1)
211 await asyncio.sleep(0)
212 assert not pull_task.done()
214 if stop_before_release:
215 subscriber.running = False
217 release_handler.release()
218 if stop_before_release:
219 await pull_task
220 else:
221 with pytest.raises(RuntimeError, match="unexpected fetch error"):
222 await pull_task
223 finally:
224 subscriber.running = False
225 release_handler.release()
226 release_handler.release()
227 if not pull_task.done(): 227 ↛ 228line 227 didn't jump to line 228 because the condition on line 227 was never true
228 await stop_task(pull_task)
230 assert fetch_requests == [1, 1, 1]
231 assert subscriber.limiter.value == subscriber.max_workers
234@pytest.mark.asyncio()
235@pytest.mark.nats()
236@pytest.mark.parametrize("active_handlers", (0, 1))
237async def test_pull_releases_capacity_when_fetch_is_cancelled(
238 active_handlers: int,
239) -> None:
240 """Fixes https://github.com/ag2ai/faststream/issues/2563."""
241 subscriber = create_subscriber()
242 fetch_started = asyncio.Event()
243 release_handler = asyncio.Event()
245 class BlockingSubscription:
246 def __init__(self) -> None:
247 self.requests = 0
249 async def fetch(
250 self,
251 *,
252 batch: int,
253 timeout: float | None,
254 ) -> list[object]:
255 self.requests += 1
256 if self.requests <= active_handlers:
257 return [object()]
258 fetch_started.set()
259 await asyncio.Event().wait()
260 return []
262 subscription: Any = BlockingSubscription()
263 subscriber.subscription = subscription
264 subscriber.running = True
266 async def consume(message: object) -> None:
267 await release_handler.wait()
269 pull_task = asyncio.create_task(subscriber._consume_pull(cb=consume))
271 try:
272 await asyncio.wait_for(fetch_started.wait(), timeout=1)
273 assert subscriber.limiter.value == subscriber.max_workers - active_handlers - 1
274 finally:
275 subscriber.running = False
276 await stop_task(pull_task)
278 assert subscriber.limiter.value == subscriber.max_workers