def test_set_iam_policy(self):
        # Setup Expected Response
        version = 351608024
        etag = b'21'
        expected_response = {'version': version, 'etag': etag}
        expected_response = policy_pb2.Policy(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup Request
        resource = client.topic_path('[PROJECT]', '[TOPIC]')
        policy = {}

        response = client.set_iam_policy(resource, policy)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = iam_policy_pb2.SetIamPolicyRequest(
            resource=resource, policy=policy)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemple #2
0
    def test_test_iam_permissions(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = iam_policy_pb2.TestIamPermissionsResponse(
            **expected_response)

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

        # Setup Request
        resource = client.topic_path("[PROJECT]", "[TOPIC]")
        permissions = []

        response = client.test_iam_permissions(resource, permissions)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = iam_policy_pb2.TestIamPermissionsRequest(
            resource=resource, permissions=permissions)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def __init__(self, batch_settings=(), **kwargs):
        # Sanity check: Is our goal to use the emulator?
        # If so, create a grpc insecure channel with the emulator host
        # as the target.
        if os.environ.get('PUBSUB_EMULATOR_HOST'):
            kwargs['channel'] = grpc.insecure_channel(
                target=os.environ.get('PUBSUB_EMULATOR_HOST'),
            )

        # Use a custom channel.
        # We need this in order to set appropriate default message size and
        # keepalive options.
        if 'channel' not in kwargs:
            kwargs['channel'] = grpc_helpers.create_channel(
                credentials=kwargs.pop('credentials', None),
                target=self.target,
                scopes=publisher_client.PublisherClient._DEFAULT_SCOPES,
                options={
                    'grpc.max_send_message_length': -1,
                    'grpc.max_receive_message_length': -1,
                }.items(),
            )

        # Add the metrics headers, and instantiate the underlying GAPIC
        # client.
        self.api = publisher_client.PublisherClient(**kwargs)
        self.batch_settings = types.BatchSettings(*batch_settings)

        # The batches on the publisher client are responsible for holding
        # messages. One batch exists for each topic.
        self._batch_lock = self._batch_class.make_lock()
        self._batches = {}
    def test_list_topics(self):
        # Setup Expected Response
        next_page_token = ''
        topics_element = {}
        topics = [topics_element]
        expected_response = {
            'next_page_token': next_page_token,
            'topics': topics
        }
        expected_response = pubsub_pb2.ListTopicsResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup Request
        project = client.project_path('[PROJECT]')

        paged_list_response = client.list_topics(project)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.topics[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.ListTopicsRequest(project=project)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemple #5
0
def main(args: dict) -> None:
    # batch_settings = pubsub.types.BatchSettings(max_messages=10)
    # publisher = pubsub.PublisherClient(batch_settings=batch_settings)

    kwargs = {}
    emulator = os.environ.get('PUBSUB_EMULATOR_HOST')
    if emulator:
        channel = grpc.insecure_channel(target=emulator)
    else:
        channel = grpc_helpers.create_channel(
            credentials=None,
            target=publisher_client.PublisherClient.SERVICE_ADDRESS,
            scopes=publisher_client.PublisherClient._DEFAULT_SCOPES,
            options=[('grpc.max_send_message_length', -1),
                     ('grpc.max_receive_message_length', -1)])
    transport = publisher_grpc_transport.PublisherGrpcTransport(
        channel=channel)
    api = publisher_client.PublisherClient(transport=transport)

    topic = f'projects/{args["PROJECT"]}/topics/{args["TOPIC"]}'

    for xs in batches(range(int(args['--num'])), int(args['--rate'])):
        messages = []
        for x in xs:
            if x is None:
                break
            # message = PubsubMessage(data=b'foobar', ordering_key='', attributes={})
            message = PubsubMessage(data=b'foobar', attributes={})
            messages.append(message)
            print('.', end='')

        # retries, then raises google.api_core.exceptions.GoogleAPIError
        # response = publisher.api.publish(topic, messages)
        response = api.publish(topic, messages, retry=None, timeout=1)
        print()
    def test_publish(self):
        # Setup Expected Response
        message_ids_element = 'messageIdsElement-744837059'
        message_ids = [message_ids_element]
        expected_response = {'message_ids': message_ids}
        expected_response = pubsub_pb2.PublishResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup Request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')
        data = b'-86'
        messages_element = {'data': data}
        messages = [messages_element]

        response = client.publish(topic, messages)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.PublishRequest(topic=topic,
                                                     messages=messages)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemple #7
0
    def test_get_iam_policy(self):
        # Setup Expected Response
        version = 351608024
        etag = b"21"
        expected_response = {"version": version, "etag": etag}
        expected_response = policy_pb2.Policy(**expected_response)

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

        # Setup Request
        resource = client.topic_path("[PROJECT]", "[TOPIC]")

        response = client.get_iam_policy(resource)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = iam_policy_pb2.GetIamPolicyRequest(
            resource=resource)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemple #8
0
    def test_list_topics(self):
        # Setup Expected Response
        next_page_token = ""
        topics_element = {}
        topics = [topics_element]
        expected_response = {
            "next_page_token": next_page_token,
            "topics": topics
        }
        expected_response = pubsub_pb2.ListTopicsResponse(**expected_response)

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

        # Setup Request
        project = client.project_path("[PROJECT]")

        paged_list_response = client.list_topics(project)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.topics[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.ListTopicsRequest(project=project)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemple #9
0
    def test_update_topic(self):
        # Setup Expected Response
        name = "name3373707"
        expected_response = {"name": name}
        expected_response = pubsub_pb2.Topic(**expected_response)

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

        # Setup Request
        topic = {}
        update_mask = {}

        response = client.update_topic(topic, update_mask)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.UpdateTopicRequest(
            topic=topic, update_mask=update_mask)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemple #10
0
    def test_list_topic_subscriptions(self):
        # Setup Expected Response
        next_page_token = ""
        subscriptions_element = "subscriptionsElement1698708147"
        subscriptions = [subscriptions_element]
        expected_response = {
            "next_page_token": next_page_token,
            "subscriptions": subscriptions,
        }
        expected_response = pubsub_pb2.ListTopicSubscriptionsResponse(
            **expected_response)

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

        # Setup Request
        topic = client.topic_path("[PROJECT]", "[TOPIC]")

        paged_list_response = client.list_topic_subscriptions(topic)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.subscriptions[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.ListTopicSubscriptionsRequest(
            topic=topic)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemple #11
0
    def test_publish(self):
        # Setup Expected Response
        message_ids_element = "messageIdsElement-744837059"
        message_ids = [message_ids_element]
        expected_response = {"message_ids": message_ids}
        expected_response = pubsub_pb2.PublishResponse(**expected_response)

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

        # Setup Request
        topic = client.topic_path("[PROJECT]", "[TOPIC]")
        data = b"-86"
        messages_element = {"data": data}
        messages = [messages_element]

        response = client.publish(topic, messages)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.PublishRequest(topic=topic,
                                                     messages=messages)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_topic_subscriptions(self):
        # Setup Expected Response
        next_page_token = ''
        subscriptions_element = 'subscriptionsElement1698708147'
        subscriptions = [subscriptions_element]
        expected_response = {
            'next_page_token': next_page_token,
            'subscriptions': subscriptions
        }
        expected_response = pubsub_pb2.ListTopicSubscriptionsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup Request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        paged_list_response = client.list_topic_subscriptions(topic)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.subscriptions[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.ListTopicSubscriptionsRequest(
            topic=topic)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_topics_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup request
        project = client.project_path('[PROJECT]')

        paged_list_response = client.list_topics(project)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_get_iam_policy_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup request
        resource = client.topic_path('[PROJECT]', '[TOPIC]')

        with pytest.raises(CustomException):
            client.get_iam_policy(resource)
    def test_create_topic_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup request
        name = client.topic_path('[PROJECT]', '[TOPIC]')

        with pytest.raises(CustomException):
            client.create_topic(name)
    def test_list_topic_subscriptions_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        paged_list_response = client.list_topic_subscriptions(topic)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_update_topic_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup request
        topic = {}
        update_mask = {}

        with pytest.raises(CustomException):
            client.update_topic(topic, update_mask)
    def test_publish_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')
        data = b'-86'
        messages_element = {'data': data}
        messages = [messages_element]

        with pytest.raises(CustomException):
            client.publish(topic, messages)
Exemple #19
0
    def test_detach_subscription_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup request
        subscription = client.subscription_path("[PROJECT]", "[SUBSCRIPTION]")

        with pytest.raises(CustomException):
            client.detach_subscription(subscription)
    def test_list_topics_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup request
        project = client.project_path('[PROJECT]')

        paged_list_response = client.list_topics(project)
        with pytest.raises(CustomException):
            list(paged_list_response)
Exemple #21
0
    def test_list_topic_subscriptions_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup request
        topic = client.topic_path("[PROJECT]", "[TOPIC]")

        paged_list_response = client.list_topic_subscriptions(topic)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_get_iam_policy_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup request
        resource = "resource-341064690"

        with pytest.raises(CustomException):
            client.get_iam_policy(resource)
Exemple #23
0
    def test_create_topic_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup request
        name = client.topic_path("[PROJECT]", "[TOPIC]")

        with pytest.raises(CustomException):
            client.create_topic(name)
    def test_delete_topic(self):
        channel = ChannelStub()
        client = publisher_client.PublisherClient(channel=channel)

        # Setup Request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        client.delete_topic(topic)

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.DeleteTopicRequest(topic=topic)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_get_iam_policy_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup request
        resource = client.topic_path('[PROJECT]', '[TOPIC]')

        with pytest.raises(CustomException):
            client.get_iam_policy(resource)
Exemple #26
0
    def test_update_topic_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup request
        topic = {}
        update_mask = {}

        with pytest.raises(CustomException):
            client.update_topic(topic, update_mask)
Exemple #27
0
    def test_publish_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup request
        topic = client.topic_path("[PROJECT]", "[TOPIC]")
        data = b"-86"
        messages_element = {"data": data}
        messages = [messages_element]

        with pytest.raises(CustomException):
            client.publish(topic, messages)
Exemple #28
0
    def test_delete_topic(self):
        channel = ChannelStub()
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup Request
        topic = client.topic_path("[PROJECT]", "[TOPIC]")

        client.delete_topic(topic)

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.DeleteTopicRequest(topic=topic)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_create_topic(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        expected_response = {'name': name_2}
        expected_response = pubsub_pb2.Topic(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup Request
        name = client.topic_path('[PROJECT]', '[TOPIC]')

        response = client.create_topic(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.Topic(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_get_topic(self):
        # Setup Expected Response
        name = 'name3373707'
        expected_response = {'name': name}
        expected_response = pubsub_pb2.Topic(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup Request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        response = client.get_topic(topic)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.GetTopicRequest(topic=topic)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request