Skip to content

Commit b8dd570

Browse files
test: add extra test cases for 0 limit CapacityLimiter
1 parent 2f4c529 commit b8dd570

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/trio/_tests/test_sync.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,41 @@ async def test_CapacityLimiter_memleak_548() -> None:
185185
assert len(limiter._pending_borrowers) == 0
186186

187187

188+
async def test_CapacityLimiter_zero_limit_tokens() -> None:
189+
c = CapacityLimiter(5)
190+
191+
assert c.total_tokens == 5
192+
193+
async with _core.open_nursery() as nursery:
194+
c.total_tokens = 0
195+
196+
for i in range(6):
197+
nursery.start_soon(c.acquire_on_behalf_of, i)
198+
await wait_all_tasks_blocked()
199+
200+
assert set(c.statistics().borrowers) == set()
201+
202+
c.total_tokens = 5
203+
204+
assert set(c.statistics().borrowers) == {0, 1, 2, 3, 4}
205+
206+
for i in range(6):
207+
c.release_on_behalf_of(i)
208+
209+
# making sure that zero limit capacity limiter doesn't let any tasks through
210+
211+
c.total_tokens = 0
212+
213+
with pytest.raises(_core.WouldBlock):
214+
c.acquire_nowait()
215+
216+
nursery.cancel_scope.cancel()
217+
218+
assert c.total_tokens == 0
219+
assert c.statistics().borrowers == []
220+
assert c._pending_borrowers == {}
221+
222+
188223
async def test_Semaphore() -> None:
189224
with pytest.raises(TypeError):
190225
Semaphore(1.0) # type: ignore[arg-type]

0 commit comments

Comments
 (0)