Skip to content

Commit 6b80522

Browse files
authored
Fix read/write order in AsyncBytesProvider (#421)
1 parent 67cc099 commit 6b80522

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

packages/smithy-core/src/smithy_core/aio/types.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
import asyncio
44
from asyncio import iscoroutinefunction
5+
from collections import deque
56
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Callable
67
from io import BytesIO
78
from typing import Any, Self, cast
@@ -297,10 +298,9 @@ def __init__(
297298
Calls to ``write`` will block until the number of chunks is less than this
298299
number. Default is 16.
299300
"""
301+
self._data = deque[bytes]()
300302
if intial_data is not None:
301-
self._data = [intial_data]
302-
else:
303-
self._data = []
303+
self._data.append(intial_data)
304304

305305
if max_buffered_chunks < 1:
306306
raise ValueError(
@@ -419,7 +419,7 @@ async def __anext__(self) -> bytes:
419419

420420
# Pop the next chunk of data from the buffer, then notify any waiting
421421
# coroutines, returning immediately after.
422-
result = self._data.pop()
422+
result = self._data.popleft()
423423
self._data_condition.notify()
424424
return result
425425

packages/smithy-core/tests/unit/aio/test_types.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,13 @@ async def test_provider_reads_written_data() -> None:
359359
# Start the read task in the background.
360360
read_task = asyncio.create_task(drain_provider(provider, result))
361361
await provider.write(b"foo")
362+
await provider.write(b"bar")
362363

363364
# Wait for the buffer to drain. At that point all the data should
364365
# be read, but the read task won't actually be complete yet
365366
# because it's still waiting on future data.
366367
await provider.flush()
367-
assert result == [b"foo"]
368+
assert result == [b"foo", b"bar"]
368369
assert not read_task.done()
369370

370371
# Now actually close the provider, which will let the read task
@@ -373,7 +374,7 @@ async def test_provider_reads_written_data() -> None:
373374
await read_task
374375

375376
# The result should not have changed
376-
assert result == [b"foo"]
377+
assert result == [b"foo", b"bar"]
377378

378379

379380
async def test_close_stops_writes() -> None:
@@ -393,7 +394,7 @@ async def test_close_without_flush_deletes_buffered_data() -> None:
393394
# We weren't able to read data, which is what we want. But here we dig into
394395
# the internals to be sure that the buffer is clear and no data is haning
395396
# around.
396-
assert provider._data == [] # type: ignore
397+
assert len(provider._data) == 0 # type: ignore
397398

398399

399400
async def test_only_max_chunks_buffered() -> None:

0 commit comments

Comments
 (0)