Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python-packages/smithy-core/smithy_core/aio/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ async def flush(self) -> None:
# Unblock writes
self._flushing = False

async def close(self, flush: bool = False) -> None:
async def close(self, flush: bool = True) -> None:
"""Closes the provider.

Pending writing tasks queued after this will fail, so such tasks should be
Expand Down Expand Up @@ -424,4 +424,4 @@ async def __aenter__(self) -> Self:
return self

async def __aexit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
await self.close()
await self.close(flush=True)
32 changes: 29 additions & 3 deletions python-packages/smithy-core/tests/unit/aio/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ async def test_close_stops_writes() -> None:
await provider.write(b"foo")


async def test_close_deletes_buffered_data() -> None:
async def test_close_without_flush_deletes_buffered_data() -> None:
provider = AsyncBytesProvider(b"foo")
await provider.close()
await provider.close(flush=False)
result: list[bytes] = []
await drain_provider(provider, result)
assert result == []
Expand Down Expand Up @@ -400,7 +400,7 @@ async def test_close_stops_queued_writes() -> None:
write_task = asyncio.create_task(provider.write(b"bar"))

# Now close the provider. The write task will raise an error.
await provider.close()
await provider.close(flush=False)

with pytest.raises(SmithyException):
await write_task
Expand Down Expand Up @@ -439,3 +439,29 @@ async def test_close_with_flush() -> None:
assert result == [b"foo"]
with pytest.raises(SmithyException):
await write_task


async def test_aexit_flushes() -> None:
# Initialize a provider, keeping track of it in the top scope just to make
# sure it doesn't get GC'd
provider = AsyncBytesProvider()

# Use the provider in a context manager. When this exits, it should flush
# and close the provider.
async with provider:

# Write some data to the provider.
await provider.write(b"foo")

# Start the task to read data from the provider and exit. Explictly do
# not await it here because the exit function should pass priority while
# it waits for the queue to drain.
result: list[bytes] = []
drain_task = asyncio.create_task(drain_provider(provider, result))

# The queue should have been read by this point.
assert result == [b"foo"]

# The draining task should be able to complete without errors. When next it
# tries to get a chunk, the provider's iterator will exit.
await drain_task
Comment on lines +465 to +467
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything we can assert about the final state here or is this only to validate nothing is raised?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only to validate that it closes successfully and nothing is raised. The function doesn't return anything directly, so there's nothing else to check.

Loading