forked from googleapis/python-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_zonal.py
More file actions
362 lines (306 loc) · 12.3 KB
/
test_zonal.py
File metadata and controls
362 lines (306 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# py standard imports
import os
import uuid
from io import BytesIO
# python additional imports
import google_crc32c
import pytest
import gc
# current library imports
from google.cloud.storage._experimental.asyncio.async_grpc_client import AsyncGrpcClient
from google.cloud.storage._experimental.asyncio.async_appendable_object_writer import (
AsyncAppendableObjectWriter,
_DEFAULT_FLUSH_INTERVAL_BYTES,
)
from google.cloud.storage._experimental.asyncio.async_multi_range_downloader import (
AsyncMultiRangeDownloader,
)
pytestmark = pytest.mark.skipif(
os.getenv("RUN_ZONAL_SYSTEM_TESTS") != "True",
reason="Zonal system tests need to be explicitly enabled. This helps scheduling tests in Kokoro and Cloud Build.",
)
# TODO: replace this with a fixture once zonal bucket creation / deletion
# is supported in grpc client or json client client.
_ZONAL_BUCKET = os.getenv("ZONAL_BUCKET")
_BYTES_TO_UPLOAD = b"dummy_bytes_to_write_read_and_delete_appendable_object"
def _get_equal_dist(a: int, b: int) -> tuple[int, int]:
step = (b - a) // 3
return a + step, a + 2 * step
@pytest.mark.asyncio
@pytest.mark.parametrize(
"object_size",
[
256, # less than _chunk size
10 * 1024 * 1024, # less than _MAX_BUFFER_SIZE_BYTES
20 * 1024 * 1024, # greater than _MAX_BUFFER_SIZE
],
)
@pytest.mark.parametrize(
"attempt_direct_path",
[True, False],
)
async def test_basic_wrd(
storage_client, blobs_to_delete, attempt_direct_path, object_size
):
object_name = f"test_basic_wrd-{str(uuid.uuid4())}"
# Client instantiation; it cannot be part of fixture because.
# grpc_client's event loop and event loop of coroutine running it
# (i.e. this test) must be same.
# Note:
# 1. @pytest.mark.asyncio ensures new event loop for each test.
# 2. we can keep the same event loop for entire module but that may
# create issues if tests are run in parallel and one test hogs the event
# loop slowing down other tests.
object_data = os.urandom(object_size)
object_checksum = google_crc32c.value(object_data)
grpc_client = AsyncGrpcClient(attempt_direct_path=attempt_direct_path).grpc_client
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
await writer.append(object_data)
object_metadata = await writer.close(finalize_on_close=True)
assert object_metadata.size == object_size
assert int(object_metadata.checksums.crc32c) == object_checksum
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
buffer = BytesIO()
await mrd.open()
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
await mrd.close()
assert buffer.getvalue() == object_data
assert mrd.persisted_size == object_size
# Clean up; use json client (i.e. `storage_client` fixture) to delete.
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del mrd
gc.collect()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"object_size",
[
10, # less than _chunk size,
10 * 1024 * 1024, # less than _MAX_BUFFER_SIZE_BYTES
20 * 1024 * 1024, # greater than _MAX_BUFFER_SIZE_BYTES
],
)
async def test_basic_wrd_in_slices(storage_client, blobs_to_delete, object_size):
object_name = f"test_basic_wrd-{str(uuid.uuid4())}"
# Client instantiation; it cannot be part of fixture because.
# grpc_client's event loop and event loop of coroutine running it
# (i.e. this test) must be same.
# Note:
# 1. @pytest.mark.asyncio ensures new event loop for each test.
# 2. we can keep the same event loop for entire module but that may
# create issues if tests are run in parallel and one test hogs the event
# loop slowing down other tests.
object_data = os.urandom(object_size)
object_checksum = google_crc32c.value(object_data)
grpc_client = AsyncGrpcClient().grpc_client
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
mark1, mark2 = _get_equal_dist(0, object_size)
await writer.append(object_data[0:mark1])
await writer.append(object_data[mark1:mark2])
await writer.append(object_data[mark2:])
object_metadata = await writer.close(finalize_on_close=True)
assert object_metadata.size == object_size
assert int(object_metadata.checksums.crc32c) == object_checksum
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
buffer = BytesIO()
await mrd.open()
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
await mrd.close()
assert buffer.getvalue() == object_data
assert mrd.persisted_size == object_size
# Clean up; use json client (i.e. `storage_client` fixture) to delete.
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del mrd
gc.collect()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"flush_interval",
[
2 * 1024 * 1024,
4 * 1024 * 1024,
8 * 1024 * 1024,
_DEFAULT_FLUSH_INTERVAL_BYTES,
],
)
async def test_wrd_with_non_default_flush_interval(
storage_client,
blobs_to_delete,
flush_interval,
):
object_name = f"test_basic_wrd-{str(uuid.uuid4())}"
object_size = 9 * 1024 * 1024
# Client instantiation; it cannot be part of fixture because.
# grpc_client's event loop and event loop of coroutine running it
# (i.e. this test) must be same.
# Note:
# 1. @pytest.mark.asyncio ensures new event loop for each test.
# 2. we can keep the same event loop for entire module but that may
# create issues if tests are run in parallel and one test hogs the event
# loop slowing down other tests.
object_data = os.urandom(object_size)
object_checksum = google_crc32c.value(object_data)
grpc_client = AsyncGrpcClient().grpc_client
writer = AsyncAppendableObjectWriter(
grpc_client,
_ZONAL_BUCKET,
object_name,
writer_options={"FLUSH_INTERVAL_BYTES": flush_interval},
)
await writer.open()
mark1, mark2 = _get_equal_dist(0, object_size)
await writer.append(object_data[0:mark1])
await writer.append(object_data[mark1:mark2])
await writer.append(object_data[mark2:])
object_metadata = await writer.close(finalize_on_close=True)
assert object_metadata.size == object_size
assert int(object_metadata.checksums.crc32c) == object_checksum
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
buffer = BytesIO()
await mrd.open()
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
await mrd.close()
assert buffer.getvalue() == object_data
assert mrd.persisted_size == object_size
# Clean up; use json client (i.e. `storage_client` fixture) to delete.
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del mrd
gc.collect()
@pytest.mark.asyncio
async def test_read_unfinalized_appendable_object(storage_client, blobs_to_delete):
object_name = f"read_unfinalized_appendable_object-{str(uuid.uuid4())[:4]}"
grpc_client = AsyncGrpcClient(attempt_direct_path=True).grpc_client
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
await writer.append(_BYTES_TO_UPLOAD)
await writer.flush()
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
buffer = BytesIO()
await mrd.open()
assert mrd.persisted_size == len(_BYTES_TO_UPLOAD)
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
await mrd.close()
assert buffer.getvalue() == _BYTES_TO_UPLOAD
# Clean up; use json client (i.e. `storage_client` fixture) to delete.
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del mrd
gc.collect()
@pytest.mark.asyncio
async def test_mrd_open_with_read_handle():
grpc_client = AsyncGrpcClient().grpc_client
object_name = f"test_read_handl-{str(uuid.uuid4())[:4]}"
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
await writer.append(_BYTES_TO_UPLOAD)
await writer.close()
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
await mrd.open()
read_handle = mrd.read_handle
await mrd.close()
# Open a new MRD using the `read_handle` obtained above
new_mrd = AsyncMultiRangeDownloader(
grpc_client, _ZONAL_BUCKET, object_name, read_handle=read_handle
)
await new_mrd.open()
# persisted_size not set when opened with read_handle
assert new_mrd.persisted_size is None
buffer = BytesIO()
await new_mrd.download_ranges([(0, 0, buffer)])
await new_mrd.close()
assert buffer.getvalue() == _BYTES_TO_UPLOAD
del mrd
del new_mrd
gc.collect()
@pytest.mark.asyncio
async def test_read_unfinalized_appendable_object_with_generation(
storage_client, blobs_to_delete
):
object_name = f"read_unfinalized_appendable_object-{str(uuid.uuid4())[:4]}"
grpc_client = AsyncGrpcClient(attempt_direct_path=True).grpc_client
async def _read_and_verify(expected_content, generation=None):
"""Helper to read object content and verify against expected."""
mrd = AsyncMultiRangeDownloader(
grpc_client, _ZONAL_BUCKET, object_name, generation
)
buffer = BytesIO()
await mrd.open()
try:
assert mrd.persisted_size == len(expected_content)
await mrd.download_ranges([(0, 0, buffer)])
assert buffer.getvalue() == expected_content
finally:
await mrd.close()
return mrd
# First write
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
await writer.append(_BYTES_TO_UPLOAD)
await writer.flush()
generation = writer.generation
# First read
mrd = await _read_and_verify(_BYTES_TO_UPLOAD)
# Second write, using generation from the first write.
writer_2 = AsyncAppendableObjectWriter(
grpc_client, _ZONAL_BUCKET, object_name, generation=generation
)
await writer_2.open()
await writer_2.append(_BYTES_TO_UPLOAD)
await writer_2.flush()
# Second read
mrd_2 = await _read_and_verify(_BYTES_TO_UPLOAD + _BYTES_TO_UPLOAD, generation)
# Clean up
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del writer_2
del mrd
del mrd_2
gc.collect()
@pytest.mark.asyncio
async def test_append_flushes_and_state_lookup(storage_client, blobs_to_delete):
"""
System test for AsyncAppendableObjectWriter, verifying flushing behavior
for both small and large appends.
"""
object_name = f"test-append-flush-varied-size-{uuid.uuid4()}"
grpc_client = AsyncGrpcClient().grpc_client
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
# Schedule for cleanup
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
# --- Part 1: Test with small data ---
small_data = b"small data"
await writer.open()
assert writer._is_stream_open
await writer.append(small_data)
persisted_size = await writer.state_lookup()
assert persisted_size == len(small_data)
# --- Part 2: Test with large data ---
large_data = os.urandom(38 * 1024 * 1024)
# Append data larger than the default flush interval (16 MiB).
# This should trigger the interval-based flushing logic.
await writer.append(large_data)
# Verify the total data has been persisted.
total_size = len(small_data) + len(large_data)
persisted_size = await writer.state_lookup()
assert persisted_size == total_size
# --- Part 3: Finalize and verify ---
final_object = await writer.close(finalize_on_close=True)
assert not writer._is_stream_open
assert final_object.size == total_size
# Verify the full content of the object.
full_data = small_data + large_data
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
buffer = BytesIO()
await mrd.open()
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
await mrd.close()
content = buffer.getvalue()
assert content == full_data