Ejemplo n.º 1
0
    def compute_head_cursor(
        self,
        request: topic_stats.ComputeHeadCursorRequest = None,
        *,
        retry: retries.Retry = gapic_v1.method.DEFAULT,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = (),
    ) -> topic_stats.ComputeHeadCursorResponse:
        r"""Compute the head cursor for the partition.
        The head cursor's offset is guaranteed to be less than
        or equal to all messages which have not yet been
        acknowledged as published, and greater than the offset
        of any message whose publish has already been
        acknowledged. It is zero if there have never been
        messages in the partition.

        Args:
            request (google.cloud.pubsublite_v1.types.ComputeHeadCursorRequest):
                The request object. Compute the current head cursor for
                a partition.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            google.cloud.pubsublite_v1.types.ComputeHeadCursorResponse:
                Response containing the head cursor
                for the requested topic and partition.

        """
        # Create or coerce a protobuf request object.
        # Minor optimization to avoid making a copy if the user passes
        # in a topic_stats.ComputeHeadCursorRequest.
        # There's no risk of modifying the input as we've already verified
        # there are no flattened fields.
        if not isinstance(request, topic_stats.ComputeHeadCursorRequest):
            request = topic_stats.ComputeHeadCursorRequest(request)

        # Wrap the RPC method; this adds retry and timeout information,
        # and friendly error handling.
        rpc = self._transport._wrapped_methods[
            self._transport.compute_head_cursor]

        # Certain fields should be provided within the metadata header;
        # add these here.
        metadata = tuple(metadata) + (gapic_v1.routing_header.to_grpc_metadata(
            (("topic", request.topic), )), )

        # Send the request.
        response = rpc(
            request,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )

        # Done; return the response.
        return response
Ejemplo n.º 2
0
async def test_compute_head_cursor_field_headers_async():
    client = TopicStatsServiceAsyncClient(
        credentials=credentials.AnonymousCredentials(), )

    # Any value that is part of the HTTP/1.1 URI should be sent as
    # a field header. Set these to a non-empty value.
    request = topic_stats.ComputeHeadCursorRequest()
    request.topic = "topic/value"

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client.transport.compute_head_cursor),
                           "__call__") as call:
        call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(
            topic_stats.ComputeHeadCursorResponse())

        await client.compute_head_cursor(request)

        # Establish that the underlying gRPC stub method was called.
        assert len(call.mock_calls)
        _, args, _ = call.mock_calls[0]
        assert args[0] == request

    # Establish that the field header was sent.
    _, _, kw = call.mock_calls[0]
    assert (
        "x-goog-request-params",
        "topic=topic/value",
    ) in kw["metadata"]
Ejemplo n.º 3
0
async def test_compute_head_cursor_async(
        transport: str = "grpc_asyncio",
        request_type=topic_stats.ComputeHeadCursorRequest):
    client = TopicStatsServiceAsyncClient(
        credentials=credentials.AnonymousCredentials(),
        transport=transport,
    )

    # Everything is optional in proto3 as far as the runtime is concerned,
    # and we are mocking out the actual API, so just send an empty request.
    request = request_type()

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client.transport.compute_head_cursor),
                           "__call__") as call:
        # Designate an appropriate return value for the call.
        call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(
            topic_stats.ComputeHeadCursorResponse())

        response = await client.compute_head_cursor(request)

        # Establish that the underlying gRPC stub method was called.
        assert len(call.mock_calls)
        _, args, _ = call.mock_calls[0]

        assert args[0] == topic_stats.ComputeHeadCursorRequest()

    # Establish that the response is the type that we expect.
    assert isinstance(response, topic_stats.ComputeHeadCursorResponse)
Ejemplo n.º 4
0
    async def compute_head_cursor(
        self,
        request: Union[topic_stats.ComputeHeadCursorRequest, dict] = None,
        *,
        retry: OptionalRetry = gapic_v1.method.DEFAULT,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = (),
    ) -> topic_stats.ComputeHeadCursorResponse:
        r"""Compute the head cursor for the partition.
        The head cursor's offset is guaranteed to be less than
        or equal to all messages which have not yet been
        acknowledged as published, and greater than the offset
        of any message whose publish has already been
        acknowledged. It is zero if there have never been
        messages in the partition.

        Args:
            request (Union[google.cloud.pubsublite_v1.types.ComputeHeadCursorRequest, dict]):
                The request object. Compute the current head cursor for
                a partition.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            google.cloud.pubsublite_v1.types.ComputeHeadCursorResponse:
                Response containing the head cursor
                for the requested topic and partition.

        """
        # Create or coerce a protobuf request object.
        request = topic_stats.ComputeHeadCursorRequest(request)

        # Wrap the RPC method; this adds retry and timeout information,
        # and friendly error handling.
        rpc = gapic_v1.method_async.wrap_method(
            self._client._transport.compute_head_cursor,
            default_timeout=None,
            client_info=DEFAULT_CLIENT_INFO,
        )

        # Certain fields should be provided within the metadata header;
        # add these here.
        metadata = tuple(metadata) + (gapic_v1.routing_header.to_grpc_metadata(
            (("topic", request.topic), )), )

        # Send the request.
        response = await rpc(
            request,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )

        # Done; return the response.
        return response
Ejemplo n.º 5
0
def test_compute_head_cursor_empty_call():
    # This test is a coverage failsafe to make sure that totally empty calls,
    # i.e. request == None and no flattened fields passed, work.
    client = TopicStatsServiceClient(
        credentials=credentials.AnonymousCredentials(),
        transport="grpc",
    )

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client.transport.compute_head_cursor),
                           "__call__") as call:
        client.compute_head_cursor()
        call.assert_called()
        _, args, _ = call.mock_calls[0]

        assert args[0] == topic_stats.ComputeHeadCursorRequest()