Пример #1
0
 def StreamingPull(
     self,
     request_iterator: Iterator[pubsub_pb2.StreamingPullRequest],
     context: grpc.ServicerContext,
 ):  # noqa: D403
     """StreamingPull implementation."""
     for request in request_iterator:
         self.logger.debug("StreamingPull(%.100s)", LazyFormat(request))
         if request.ack_ids:
             self.Acknowledge(
                 pubsub_pb2.AcknowledgeRequest(
                     subscription=request.subscription, ack_ids=request.ack_ids
                 ),
                 context,
             )
         if request.modify_deadline_seconds:
             for ack_id, seconds in zip(
                 request.modify_deadline_ack_ids, request.modify_deadline_seconds
             ):
                 self.ModifyAckDeadline(
                     pubsub_pb2.ModifyAckDeadlineRequest(
                         subscription=request.subscription,
                         ack_ids=[ack_id],
                         seconds=seconds,
                     ),
                     context,
                 )
         yield pubsub_pb2.StreamingPullResponse(
             received_messages=self.Pull(
                 pubsub_pb2.PullRequest(
                     subscription=request.subscription, max_messages=100
                 ),
                 context,
             ).received_messages
         )
    def test_streaming_pull(self):
        # Setup Expected Response
        received_messages_element = {}
        received_messages = [received_messages_element]
        expected_response = {'received_messages': received_messages}
        expected_response = pubsub_pb2.StreamingPullResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[iter([expected_response])])
        client = subscriber_client.SubscriberClient(channel=channel)

        # Setup Request
        subscription = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]')
        stream_ack_deadline_seconds = 1875467245
        request = {
            'subscription': subscription,
            'stream_ack_deadline_seconds': stream_ack_deadline_seconds
        }
        request = pubsub_pb2.StreamingPullRequest(**request)
        requests = [request]

        response = client.streaming_pull(requests)
        resources = list(response)
        assert len(resources) == 1
        assert expected_response == resources[0]

        assert len(channel.requests) == 1
        actual_requests = channel.requests[0][1]
        assert len(actual_requests) == 1
        actual_request = list(actual_requests)[0]
        assert request == actual_request
    def test_streaming_pull(self):
        # Setup Expected Response
        received_messages_element = {}
        received_messages = [received_messages_element]
        expected_response = {"received_messages": received_messages}
        expected_response = pubsub_pb2.StreamingPullResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[iter([expected_response])])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = subscriber_client.SubscriberClient()

        # Setup Request
        subscription = client.subscription_path("[PROJECT]", "[SUBSCRIPTION]")
        stream_ack_deadline_seconds = 1875467245
        request = {
            "subscription": subscription,
            "stream_ack_deadline_seconds": stream_ack_deadline_seconds,
        }
        request = pubsub_pb2.StreamingPullRequest(**request)
        requests = [request]

        response = client.streaming_pull(requests)
        resources = list(response)
        assert len(resources) == 1
        assert expected_response == resources[0]

        assert len(channel.requests) == 1
        actual_requests = channel.requests[0][1]
        assert len(actual_requests) == 1
        actual_request = list(actual_requests)[0]
        assert request == actual_request