Skip to content

Commit 204ee40

Browse files
committed
Added tests for Zonal writes
Close GCSFile before aaow/mrd
1 parent d2b5b2f commit 204ee40

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

gcsfs/tests/test_zonal_file.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import pytest
77

88
from gcsfs.extended_gcsfs import BucketType
9-
from gcsfs.tests.settings import TEST_BUCKET
9+
from gcsfs.tests.settings import TEST_ZONAL_BUCKET
1010

11-
file_path = f"{TEST_BUCKET}/zonal-file-test"
11+
file_path = f"{TEST_ZONAL_BUCKET}/zonal-file-test"
1212
test_data = b"hello world"
1313

1414
REQUIRED_ENV_VAR = "GCSFS_EXPERIMENTAL_ZB_HNS_SUPPORT"
@@ -150,3 +150,38 @@ def test_zonal_file_not_implemented_methods(
150150
method_to_call = getattr(f, method_name)
151151
with pytest.raises(NotImplementedError):
152152
method_to_call()
153+
154+
155+
@pytest.mark.skipif(
156+
os.environ.get("STORAGE_EMULATOR_HOST") != "https://storage.googleapis.com",
157+
reason="This test class is for real GCS only.",
158+
)
159+
class TestZonalFileRealGCS:
160+
"""
161+
Contains tests for ZonalFile write operations that run only against a
162+
real GCS backend. These tests validate end-to-end write behavior.
163+
"""
164+
165+
def test_simple_upload_overwrite_behavior(self, extended_gcsfs):
166+
"""Tests simple writes to a ZonalFile and verifies the content is overwritten"""
167+
with extended_gcsfs.open(file_path, "wb") as f:
168+
f.write(test_data)
169+
with extended_gcsfs.open(file_path, "wb", content_type="text/plain") as f:
170+
f.write(b"Sample text data.")
171+
assert extended_gcsfs.cat(file_path) == b"Sample text data."
172+
173+
def test_large_upload(self, extended_gcsfs):
174+
"""Tests writing a large chunk of data to a ZonalFile."""
175+
large_data = b"a" * (5 * 1024 * 1024) # 5MB
176+
with extended_gcsfs.open(file_path, "wb") as f:
177+
f.write(large_data)
178+
assert extended_gcsfs.cat(file_path) == large_data
179+
180+
def test_multiple_writes(self, extended_gcsfs):
181+
"""Tests multiple write calls to the same ZonalFile handle."""
182+
data1 = b"first part "
183+
data2 = b"second part"
184+
with extended_gcsfs.open(file_path, "wb") as f:
185+
f.write(data1)
186+
f.write(data2)
187+
assert extended_gcsfs.cat(file_path) == data1 + data2

gcsfs/zonal_file.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,13 @@ def close(self):
160160
Closes the ZonalFile and the underlying AsyncMultiRangeDownloader and AsyncAppendableObjectWriter.
161161
If in write mode, finalizes the write if autocommit is True.
162162
"""
163+
if self.closed:
164+
return
165+
# super is closed before aaow since flush may need aaow
166+
super().close()
163167
if hasattr(self, "mrd") and self.mrd:
164168
asyn.sync(self.gcsfs.loop, self.mrd.close)
165169
if hasattr(self, "aaow") and self.aaow:
166170
asyn.sync(
167171
self.gcsfs.loop, self.aaow.close, finalize_on_close=self.autocommit
168172
)
169-
super().close()

0 commit comments

Comments
 (0)