11# py standard imports
2- import asyncio
32import os
43import uuid
54from io import BytesIO
87import google_crc32c
98
109import pytest
10+ import gc
1111
1212# current library imports
1313from google .cloud .storage ._experimental .asyncio .async_grpc_client import AsyncGrpcClient
1919 AsyncMultiRangeDownloader ,
2020)
2121
22+
2223pytestmark = pytest .mark .skipif (
2324 os .getenv ("RUN_ZONAL_SYSTEM_TESTS" ) != "True" ,
2425 reason = "Zonal system tests need to be explicitly enabled. This helps scheduling tests in Kokoro and Cloud Build." ,
@@ -36,36 +37,6 @@ def _get_equal_dist(a: int, b: int) -> tuple[int, int]:
3637 return a + step , a + 2 * step
3738
3839
39- async def write_one_appendable_object (
40- bucket_name : str ,
41- object_name : str ,
42- data : bytes ,
43- ) -> None :
44- """Helper to write an appendable object."""
45- grpc_client = AsyncGrpcClient (attempt_direct_path = True ).grpc_client
46- writer = AsyncAppendableObjectWriter (grpc_client , bucket_name , object_name )
47- await writer .open ()
48- await writer .append (data )
49- await writer .close ()
50-
51-
52- @pytest .fixture (scope = "function" )
53- def appendable_object (storage_client , blobs_to_delete ):
54- """Fixture to create and cleanup an appendable object."""
55- object_name = f"appendable_obj_for_mrd-{ str (uuid .uuid4 ())[:4 ]} "
56- asyncio .run (
57- write_one_appendable_object (
58- _ZONAL_BUCKET ,
59- object_name ,
60- _BYTES_TO_UPLOAD ,
61- )
62- )
63- yield object_name
64-
65- # Clean up; use json client (i.e. `storage_client` fixture) to delete.
66- blobs_to_delete .append (storage_client .bucket (_ZONAL_BUCKET ).blob (object_name ))
67-
68-
6940@pytest .mark .asyncio
7041@pytest .mark .parametrize (
7142 "object_size" ,
@@ -114,6 +85,9 @@ async def test_basic_wrd(
11485
11586 # Clean up; use json client (i.e. `storage_client` fixture) to delete.
11687 blobs_to_delete .append (storage_client .bucket (_ZONAL_BUCKET ).blob (object_name ))
88+ del writer
89+ del mrd
90+ gc .collect ()
11791
11892
11993@pytest .mark .asyncio
@@ -161,12 +135,20 @@ async def test_basic_wrd_in_slices(storage_client, blobs_to_delete, object_size)
161135
162136 # Clean up; use json client (i.e. `storage_client` fixture) to delete.
163137 blobs_to_delete .append (storage_client .bucket (_ZONAL_BUCKET ).blob (object_name ))
138+ del writer
139+ del mrd
140+ gc .collect ()
164141
165142
166143@pytest .mark .asyncio
167144@pytest .mark .parametrize (
168145 "flush_interval" ,
169- [2 * 1024 * 1024 , 4 * 1024 * 1024 , 8 * 1024 * 1024 , _DEFAULT_FLUSH_INTERVAL_BYTES ],
146+ [
147+ 2 * 1024 * 1024 ,
148+ 4 * 1024 * 1024 ,
149+ 8 * 1024 * 1024 ,
150+ _DEFAULT_FLUSH_INTERVAL_BYTES ,
151+ ],
170152)
171153async def test_wrd_with_non_default_flush_interval (
172154 storage_client ,
@@ -214,6 +196,9 @@ async def test_wrd_with_non_default_flush_interval(
214196
215197 # Clean up; use json client (i.e. `storage_client` fixture) to delete.
216198 blobs_to_delete .append (storage_client .bucket (_ZONAL_BUCKET ).blob (object_name ))
199+ del writer
200+ del mrd
201+ gc .collect ()
217202
218203
219204@pytest .mark .asyncio
@@ -237,20 +222,28 @@ async def test_read_unfinalized_appendable_object(storage_client, blobs_to_delet
237222
238223 # Clean up; use json client (i.e. `storage_client` fixture) to delete.
239224 blobs_to_delete .append (storage_client .bucket (_ZONAL_BUCKET ).blob (object_name ))
225+ del writer
226+ del mrd
227+ gc .collect ()
240228
241229
242230@pytest .mark .asyncio
243- async def test_mrd_open_with_read_handle (appendable_object ):
244- grpc_client = AsyncGrpcClient (attempt_direct_path = True ).grpc_client
231+ async def test_mrd_open_with_read_handle ():
232+ grpc_client = AsyncGrpcClient ().grpc_client
233+ object_name = f"test_read_handl-{ str (uuid .uuid4 ())[:4 ]} "
234+ writer = AsyncAppendableObjectWriter (grpc_client , _ZONAL_BUCKET , object_name )
235+ await writer .open ()
236+ await writer .append (_BYTES_TO_UPLOAD )
237+ await writer .close ()
245238
246- mrd = AsyncMultiRangeDownloader (grpc_client , _ZONAL_BUCKET , appendable_object )
239+ mrd = AsyncMultiRangeDownloader (grpc_client , _ZONAL_BUCKET , object_name )
247240 await mrd .open ()
248241 read_handle = mrd .read_handle
249242 await mrd .close ()
250243
251244 # Open a new MRD using the `read_handle` obtained above
252245 new_mrd = AsyncMultiRangeDownloader (
253- grpc_client , _ZONAL_BUCKET , appendable_object , read_handle = read_handle
246+ grpc_client , _ZONAL_BUCKET , object_name , read_handle = read_handle
254247 )
255248 await new_mrd .open ()
256249 # persisted_size not set when opened with read_handle
@@ -259,3 +252,6 @@ async def test_mrd_open_with_read_handle(appendable_object):
259252 await new_mrd .download_ranges ([(0 , 0 , buffer )])
260253 await new_mrd .close ()
261254 assert buffer .getvalue () == _BYTES_TO_UPLOAD
255+ del mrd
256+ del new_mrd
257+ gc .collect ()
0 commit comments