Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
from typing import List, Optional, Tuple

from google.api_core.bidi_async import AsyncBidiRpc
Expand Down Expand Up @@ -79,6 +80,9 @@ def __init__(
self.socket_like_rpc: Optional[AsyncBidiRpc] = None
self._is_stream_open: bool = False
self.persisted_size: Optional[int] = None
self.is_finalized: bool = False
self.full_obj_server_crc32c: Optional[int] = None
Comment thread
chandra-siri marked this conversation as resolved.
self.object_metadata: Optional[_storage_v2.Object] = None

async def open(self, metadata: Optional[List[Tuple[str, str]]] = None) -> None:
"""Opens the bidi-gRPC connection to read from the object.
Expand Down Expand Up @@ -132,6 +136,21 @@ async def open(self, metadata: Optional[List[Tuple[str, str]]] = None) -> None:
self.generation_number = response.metadata.generation
# update persisted size
self.persisted_size = response.metadata.size
self.object_metadata = response.metadata
# Since full object checksum validation is only required for finalized objects,
# check finalize_time (which is DatetimeWithNanoseconds/datetime in production, or mocked in tests).
finalize_time = getattr(response.metadata, "finalize_time", None)
if finalize_time:
is_finalized_val = False
if isinstance(finalize_time, datetime.datetime):
is_finalized_val = True
elif hasattr(finalize_time, "seconds") and finalize_time.seconds > 0:
is_finalized_val = True

if is_finalized_val:
self.is_finalized = True
if hasattr(response.metadata, "checksums") and response.metadata.checksums:
self.full_obj_server_crc32c = response.metadata.checksums.crc32c
Comment thread
chandra-siri marked this conversation as resolved.
Outdated

if response and response.read_handle:
self.read_handle = response.read_handle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ async def instantiate_read_obj_stream(mock_client, mock_cls_async_bidi_rpc, open
socket_like_rpc.open = AsyncMock()

recv_response = mock.MagicMock(spec=_storage_v2.BidiReadObjectResponse)
recv_response.metadata = mock.MagicMock(spec=_storage_v2.Object)
recv_response.metadata = mock.MagicMock()
recv_response.metadata.generation = _TEST_GENERATION_NUMBER
recv_response.metadata.size = _TEST_OBJECT_SIZE
recv_response.metadata.finalize_time.seconds = 12345
recv_response.metadata.checksums.crc32c = 98765
Comment thread
chandra-siri marked this conversation as resolved.
Outdated
recv_response.read_handle = _TEST_READ_HANDLE
socket_like_rpc.recv = AsyncMock(return_value=recv_response)

Expand Down Expand Up @@ -130,6 +132,8 @@ async def test_open(mock_client, mock_cls_async_bidi_rpc):
assert read_obj_stream.generation_number == _TEST_GENERATION_NUMBER
assert read_obj_stream.read_handle == _TEST_READ_HANDLE
assert read_obj_stream.persisted_size == _TEST_OBJECT_SIZE
assert read_obj_stream.is_finalized is True
assert read_obj_stream.full_obj_server_crc32c == 98765
assert read_obj_stream.is_stream_open


Expand Down Expand Up @@ -381,3 +385,34 @@ async def test_recv_updates_read_handle_on_refresh(

await stream.recv()
assert stream.read_handle == refreshed_handle


@mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc")
@mock.patch(
"google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_open_unfinalized_object_skips_checksum(mock_client, mock_cls_async_bidi_rpc):
socket_like_rpc = AsyncMock()
mock_cls_async_bidi_rpc.return_value = socket_like_rpc
socket_like_rpc.open = AsyncMock()

recv_response = mock.MagicMock(spec=_storage_v2.BidiReadObjectResponse)
recv_response.metadata = mock.MagicMock()
recv_response.metadata.generation = _TEST_GENERATION_NUMBER
recv_response.metadata.size = _TEST_OBJECT_SIZE
recv_response.metadata.finalize_time.seconds = 0 # NOT finalized!
Comment thread
chandra-siri marked this conversation as resolved.
Outdated
recv_response.metadata.checksums.crc32c = 98765
recv_response.read_handle = _TEST_READ_HANDLE
socket_like_rpc.recv = AsyncMock(return_value=recv_response)

read_obj_stream = _AsyncReadObjectStream(
client=mock_client,
bucket_name=_TEST_BUCKET_NAME,
object_name=_TEST_OBJECT_NAME,
)

await read_obj_stream.open()

assert read_obj_stream.is_finalized is False
assert read_obj_stream.full_obj_server_crc32c is None
Loading