Ejemplo n.º 1
0
async def test_compute_message_stats_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.ComputeMessageStatsRequest()
    request.topic = "topic/value"

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

        await client.compute_message_stats(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"]
    def compute_message_stats(
        self,
        request: topic_stats.ComputeMessageStatsRequest = None,
        *,
        retry: retries.Retry = gapic_v1.method.DEFAULT,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = (),
    ) -> topic_stats.ComputeMessageStatsResponse:
        r"""Compute statistics about a range of messages in a
        given topic and partition.

        Args:
            request (google.cloud.pubsublite_v1.types.ComputeMessageStatsRequest):
                The request object. Compute statistics about a range of
                messages in a given topic and 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.ComputeMessageStatsResponse:
                Response containing stats for
                messages in 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.ComputeMessageStatsRequest.
        # There's no risk of modifying the input as we've already verified
        # there are no flattened fields.
        if not isinstance(request, topic_stats.ComputeMessageStatsRequest):
            request = topic_stats.ComputeMessageStatsRequest(request)

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

        # 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.º 3
0
    async def compute_message_stats(
        self,
        request: Union[topic_stats.ComputeMessageStatsRequest, dict] = None,
        *,
        retry: OptionalRetry = gapic_v1.method.DEFAULT,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = (),
    ) -> topic_stats.ComputeMessageStatsResponse:
        r"""Compute statistics about a range of messages in a
        given topic and partition.

        Args:
            request (Union[google.cloud.pubsublite_v1.types.ComputeMessageStatsRequest, dict]):
                The request object. Compute statistics about a range of
                messages in a given topic and 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.ComputeMessageStatsResponse:
                Response containing stats for
                messages in the requested topic and
                partition.

        """
        # Create or coerce a protobuf request object.
        request = topic_stats.ComputeMessageStatsRequest(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_message_stats,
            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.º 4
0
def test_compute_message_stats_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_message_stats),
                           "__call__") as call:
        client.compute_message_stats()
        call.assert_called()
        _, args, _ = call.mock_calls[0]

        assert args[0] == topic_stats.ComputeMessageStatsRequest()
Ejemplo n.º 5
0
async def test_compute_message_stats_async(
        transport: str = "grpc_asyncio",
        request_type=topic_stats.ComputeMessageStatsRequest):
    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_message_stats),
                           "__call__") as call:
        # Designate an appropriate return value for the call.
        call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(
            topic_stats.ComputeMessageStatsResponse(
                message_count=1389,
                message_bytes=1387,
            ))

        response = await client.compute_message_stats(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.ComputeMessageStatsRequest()

    # Establish that the response is the type that we expect.
    assert isinstance(response, topic_stats.ComputeMessageStatsResponse)

    assert response.message_count == 1389

    assert response.message_bytes == 1387
Ejemplo n.º 6
0
    async def compute_message_stats(
        self,
        request: topic_stats.ComputeMessageStatsRequest = None,
        *,
        retry: retries.Retry = gapic_v1.method.DEFAULT,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = (),
    ) -> topic_stats.ComputeMessageStatsResponse:
        r"""Compute statistics about a range of messages in a
        given topic and partition.

        Args:
            request (:class:`google.cloud.pubsublite_v1.types.ComputeMessageStatsRequest`):
                The request object. Compute statistics about a range of
                messages in a given topic and 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.ComputeMessageStatsResponse:
                Response containing stats for
                messages in the requested topic and
                partition.

        """
        # Create or coerce a protobuf request object.

        request = topic_stats.ComputeMessageStatsRequest(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_message_stats,
            default_retry=retries.Retry(
                initial=0.1,
                maximum=60.0,
                multiplier=1.3,
                predicate=retries.if_exception_type(
                    exceptions.Aborted,
                    exceptions.DeadlineExceeded,
                    exceptions.InternalServerError,
                    exceptions.ServiceUnavailable,
                    exceptions.Unknown,
                ),
                deadline=600.0,
            ),
            default_timeout=600.0,
            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