Skip to content

Commit

Permalink
fix: pass client options to publisher and subscriber clients (#166) (#…
Browse files Browse the repository at this point in the history
…190)

Co-authored-by: Peter Lamut <plamut@users.noreply.github.com>
  • Loading branch information
cguardia and plamut authored Sep 14, 2020
1 parent afd7323 commit 049ae7d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion google/cloud/pubsub_v1/publisher/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __init__(self, batch_settings=(), publisher_options=(), **kwargs):
target=os.environ.get("PUBSUB_EMULATOR_HOST")
)

client_options = kwargs.pop("client_options", None)
client_options = kwargs.get("client_options", None)
if (
client_options
and "api_endpoint" in client_options
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/pubsub_v1/subscriber/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(self, **kwargs):
)

# api_endpoint wont be applied if 'transport' is passed in.
client_options = kwargs.pop("client_options", None)
client_options = kwargs.get("client_options", None)
if (
client_options
and "api_endpoint" in client_options
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/pubsub_v1/publisher/test_publisher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ def test_init_w_empty_client_options():
) == publisher_client.PublisherClient.SERVICE_ADDRESS


def test_init_client_options_pass_through():
def init(self, *args, **kwargs):
self.kwargs = kwargs

with mock.patch.object(publisher_client.PublisherClient, "__init__", init):
client = publisher.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_init_emulator(monkeypatch):
monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/foo/bar/")
# NOTE: When the emulator host is set, a custom channel will be used, so
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/pubsub_v1/subscriber/test_subscriber_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ def test_init_w_empty_client_options():
) == subscriber_client.SubscriberClient.SERVICE_ADDRESS


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_init_emulator(monkeypatch):
monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/baz/bacon/")
# NOTE: When the emulator host is set, a custom channel will be used, so
Expand Down

0 comments on commit 049ae7d

Please sign in to comment.