def test_init_w_unicode_api_endpoint(): client_options = {"api_endpoint": u"testendpoint.google.com"} client = subscriber.Client(client_options=client_options) assert isinstance(client.api, subscriber_client.SubscriberClient) assert (client.api.transport._channel._channel.target() ).decode("utf-8") == "testendpoint.google.com"
def test_init_w_empty_client_options(): client = subscriber.Client(client_options={}) assert isinstance(client.api, subscriber_client.SubscriberClient) assert (client.api._transport.grpc_channel._channel.target()).decode( "utf-8" ) == subscriber_client.SubscriberClient.SERVICE_ADDRESS
def test_subscribe_with_callback(): client = subscriber.Client() callback = mock.Mock() with mock.patch.object(thread.Policy, 'open') as open_: subscription = client.subscribe('sub_name_b', callback) open_.assert_called_once_with(callback) assert isinstance(subscription, thread.Policy)
def test_close(): mock_transport = mock.NonCallableMock() client = subscriber.Client(transport=mock_transport) client.close() mock_transport.channel.close.assert_called()
def test_subscribe_with_failed_callback(): creds = mock.Mock(spec=credentials.Credentials) client = subscriber.Client(credentials=creds) callback = 'abcdefg' with pytest.raises(TypeError) as exc_info: subscription = client.subscribe('sub_name_b', callback) assert callback in str(exc_info.value)
def test_closes_channel_as_context_manager(): mock_transport = mock.NonCallableMock() client = subscriber.Client(transport=mock_transport) with client: pass mock_transport.channel.close.assert_called()
def Start(self, request, context): self.message_size = request.message_size self.batch_size = request.publish_batch_size subscription = "projects/" + request.project + "/subscriptions/" + request.pubsub_options.subscription self.client = subscriber.Client() self.client.subscribe(subscription, lambda msg: self.ProcessMessage(msg)) return loadtest_pb2.StartResponse()
def test_close(): client = subscriber.Client() patcher = mock.patch.object(client.api._transport.grpc_channel, "close") with patcher as patched_close: client.close() patched_close.assert_called()
def test_init_w_api_endpoint(creds): client_options = {"api_endpoint": "testendpoint.google.com"} client = subscriber.Client(client_options=client_options, credentials=creds) assert isinstance(client.api, subscriber_client.SubscriberClient) assert (client.api._transport.grpc_channel._channel.target() ).decode("utf-8") == "testendpoint.google.com:443"
def create_and_open_policy(callback, **kwargs): creds = mock.create_autospec(credentials.Credentials, instance=True) client = subscriber.Client(credentials=creds) policy = thread.Policy(client, 'sub_name_c', **kwargs) with mock.patch('threading.Thread', autospec=True): policy.open(callback) return policy
def test_subscribe_experimental(manager_open): creds = mock.Mock(spec=credentials.Credentials) client = subscriber.Client(credentials=creds) future = client.subscribe_experimental('sub_name_a', callback=mock.sentinel.callback) assert isinstance(future, futures.StreamingPullFuture) manager_open.assert_called_once_with(mock.ANY, mock.sentinel.callback)
def test_streaming_pull_gapic_monkeypatch(): client = subscriber.Client() with mock.patch("google.api_core.gapic_v1.method.wrap_method"): client.streaming_pull(requests=iter([])) transport = client.api._transport assert hasattr(transport.streaming_pull, "_prefetch_first_result_") assert not transport.streaming_pull._prefetch_first_result_
def test_closes_channel_as_context_manager(): client = subscriber.Client() patcher = mock.patch.object(client.api._transport.grpc_channel, "close") with patcher as patched_close: with client: pass patched_close.assert_called()
def test_subscribe(manager_open): creds = mock.Mock(spec=credentials.Credentials) client = subscriber.Client(credentials=creds) future = client.subscribe("sub_name_a", callback=mock.sentinel.callback) assert isinstance(future, futures.StreamingPullFuture) assert future._manager._subscription == "sub_name_a" manager_open.assert_called_once_with(mock.ANY, mock.sentinel.callback)
def test_streaming_pull_gapic_monkeypatch(): transport = mock.NonCallableMock(spec=["streaming_pull"]) transport.streaming_pull = mock.Mock(spec=[]) client = subscriber.Client(transport=transport) client.streaming_pull(requests=iter([])) assert client.api.transport is transport assert hasattr(transport.streaming_pull, "_prefetch_first_result_") assert not transport.streaming_pull._prefetch_first_result_
def test_init_emulator(monkeypatch): monkeypatch.setenv('PUBSUB_EMULATOR_HOST', '/baz/bacon/') client = subscriber.Client() # Establish that a gRPC request would attempt to hit the emulator host. # # Sadly, there seems to be no good way to do this without poking at # the private API of gRPC. channel = client.api.subscriber_stub.Pull._channel assert channel.target().decode('utf8') == '/baz/bacon/'
def test_subscribe(manager_open, creds): client = subscriber.Client(credentials=creds) future = client.subscribe("sub_name_a", callback=mock.sentinel.callback) assert isinstance(future, futures.StreamingPullFuture) assert future._manager._subscription == "sub_name_a" manager_open.assert_called_once_with( mock.ANY, callback=mock.sentinel.callback, on_callback_error=future.set_exception, )
def test_init_emulator(monkeypatch): monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/baz/bacon/") # NOTE: When the emulator host is set, a custom channel will be used, so # no credentials (mock ot otherwise) can be passed in. client = subscriber.Client() # Establish that a gRPC request would attempt to hit the emulator host. # # Sadly, there seems to be no good way to do this without poking at # the private API of gRPC. channel = client.api.transport.pull._channel assert channel.target().decode("utf8") == "/baz/bacon/"
def test_init_default_client_info(creds): client = subscriber.Client(credentials=creds) installed_version = subscriber.client.__version__ expected_client_info = f"gccl/{installed_version}" for wrapped_method in client.api.transport._wrapped_methods.values(): user_agent = next( (header_value for header, header_value in wrapped_method._metadata if header == METRICS_METADATA_KEY), None, # pragma: NO COVER ) assert user_agent is not None assert expected_client_info in user_agent
def test_sync_pull_warning_if_return_immediately(creds): client = subscriber.Client(credentials=creds) subscription_path = "projects/foo/subscriptions/bar" with mock.patch.object(client.api._transport, "_wrapped_methods"), warnings.catch_warnings( record=True) as warned: client.pull(subscription=subscription_path, return_immediately=True) # Setting the deprecated return_immediately flag to True should emit a warning. assert len(warned) == 1 assert issubclass(warned[0].category, DeprecationWarning) warning_msg = str(warned[0].message) assert "return_immediately" in warning_msg assert "deprecated" in warning_msg
def test_start_consuming(): creds = mock.Mock(spec=credentials.Credentials) client = subscriber.Client(credentials=creds) policy = client.subscribe('sub_name_e') consumer = _consumer.Consumer() with mock.patch.object(threading, 'Thread', autospec=True) as Thread: consumer.start_consuming(policy) assert consumer._stopped.is_set() is False Thread.assert_called_once_with( name=_consumer._BIDIRECTIONAL_CONSUMER_NAME, target=consumer._blocking_consume, args=(policy, ), ) assert consumer._consumer_thread is Thread.return_value
def test_init_client_options_pass_through(): def init(self, *args, **kwargs): self.kwargs = kwargs with mock.patch.object(subscriber_client.SubscriberClient, "__init__", init): client = subscriber.Client( client_options={ "quota_project_id": "42", "scopes": [], "credentials_file": "file.json", }) client_options = client._api.kwargs["client_options"] assert client_options.get("quota_project_id") == "42" assert client_options.get("scopes") == [] assert client_options.get("credentials_file") == "file.json"
def test_subscribe_options(manager_open): creds = mock.Mock(spec=credentials.Credentials) client = subscriber.Client(credentials=creds) flow_control = types.FlowControl(max_bytes=42) scheduler = mock.sentinel.scheduler future = client.subscribe('sub_name_a', callback=mock.sentinel.callback, flow_control=flow_control, scheduler=scheduler) assert isinstance(future, futures.StreamingPullFuture) assert future._manager._subscription == 'sub_name_a' assert future._manager.flow_control == flow_control assert future._manager._scheduler == scheduler manager_open.assert_called_once_with(mock.ANY, mock.sentinel.callback)
def test_request_generator_thread(): consumer = _consumer.Consumer() creds = mock.Mock(spec=credentials.Credentials) client = subscriber.Client(credentials=creds) policy = client.subscribe('sub_name_e') generator = consumer._request_generator_thread(policy) # The first request that comes from the request generator thread # should always be the initial request. initial_request = next(generator) assert initial_request.subscription == 'sub_name_e' assert initial_request.stream_ack_deadline_seconds == 10 # Subsequent requests correspond to items placed in the request queue. consumer.send_request(types.StreamingPullRequest(ack_ids=['i'])) request = next(generator) assert request.ack_ids == ['i'] # The poison pill should stop the loop. consumer.send_request(_helper_threads.STOP) with pytest.raises(StopIteration): next(generator)
def test_subscribe_options(manager_open, creds): client = subscriber.Client(credentials=creds) flow_control = types.FlowControl(max_bytes=42) scheduler = mock.sentinel.scheduler future = client.subscribe( "sub_name_a", callback=mock.sentinel.callback, flow_control=flow_control, scheduler=scheduler, await_callbacks_on_shutdown=mock.sentinel.await_callbacks, ) assert isinstance(future, futures.StreamingPullFuture) assert future._manager._subscription == "sub_name_a" assert future._manager.flow_control == flow_control assert future._manager._scheduler == scheduler assert future._manager._await_callbacks_on_shutdown is mock.sentinel.await_callbacks manager_open.assert_called_once_with( mock.ANY, callback=mock.sentinel.callback, on_callback_error=future.set_exception, )
def test_init_client_options_pass_through(): mock_ssl_creds = grpc.ssl_channel_credentials() def init(self, *args, **kwargs): self.kwargs = kwargs self._transport = mock.Mock() self._transport._host = "testendpoint.google.com" self._transport._ssl_channel_credentials = mock_ssl_creds with mock.patch.object(subscriber_client.SubscriberClient, "__init__", init): client = subscriber.Client( client_options={ "quota_project_id": "42", "scopes": [], "credentials_file": "file.json", } ) client_options = client._api.kwargs["client_options"] assert client_options.get("quota_project_id") == "42" assert client_options.get("scopes") == [] assert client_options.get("credentials_file") == "file.json" assert client.target == "testendpoint.google.com" assert client.api.transport._ssl_channel_credentials == mock_ssl_creds
def test_init_w_custom_transport(): transport = object() client = subscriber.Client(transport=transport) assert isinstance(client.api, subscriber_client.SubscriberClient) assert client.api.transport is transport
def test_init(): creds = mock.Mock(spec=credentials.Credentials) client = subscriber.Client(credentials=creds) assert isinstance(client.api, subscriber_client.SubscriberClient)
def create_policy(flow_control=types.FlowControl()): creds = mock.Mock(spec=credentials.Credentials) client = subscriber.Client(credentials=creds) return thread.Policy(client, 'sub_name_d', flow_control=flow_control)
def create_policy(**kwargs): creds = mock.Mock(spec=credentials.Credentials) client = subscriber.Client(credentials=creds) return thread.Policy(client, 'sub_name_c', **kwargs)