Skip to content

Commit

Permalink
Fix: BlobWriter.close() will do nothing if already closed (#887)
Browse files Browse the repository at this point in the history
* Fix: BlobWriter.close() will do nothing if already closed

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
andrewsg and gcf-owl-bot[bot] authored Oct 14, 2022
1 parent 3d84972 commit 7707220
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 2 additions & 3 deletions google/cloud/storage/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,8 @@ def flush(self):
)

def close(self):
self._checkClosed() # Raises ValueError if closed.

self._upload_chunks_from_buffer(1)
if not self._buffer.closed:
self._upload_chunks_from_buffer(1)
self._buffer.close()

def _checkClosed(self):
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,23 @@ def test_write(self, mock_warn):
stacklevel=2,
)

def test_close_errors(self):
blob = mock.Mock(chunk_size=None)

upload = mock.Mock()
transport = mock.Mock()

blob._initiate_resumable_upload.return_value = (upload, transport)

writer = self._make_blob_writer(blob)

writer.close()
# Close a second time to verify it successfully does nothing.
writer.close()
# Try to write to closed file.
with self.assertRaises(ValueError):
writer.write(TEST_BINARY_DATA)

def test_flush_fails(self):
blob = mock.Mock(chunk_size=None)
writer = self._make_blob_writer(blob)
Expand Down

0 comments on commit 7707220

Please sign in to comment.