Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: BlobWriter.close() will do nothing if already closed #887

Merged
merged 2 commits into from
Oct 14, 2022
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
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