Skip to content

Update google-cloud-pubsub to major version 2. #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
url="https://github.com/testcontainers/testcontainers-python",
install_requires=[
"testcontainers-core",
"google-cloud-pubsub < 2",
"google-cloud-pubsub>=2",
],
python_requires=">=3.7",
)
18 changes: 8 additions & 10 deletions google/testcontainers/google/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
# License for the specific language governing permissions and limitations
# under the License.
from google.cloud import pubsub
import grpc
from typing import Optional
import os
from testcontainers.core.container import DockerContainer
from typing import Type
from unittest.mock import patch


class PubSubContainer(DockerContainer):
Expand Down Expand Up @@ -49,15 +50,12 @@ def __init__(self, image: str = "google/cloud-sdk:emulators", project: str = "te
def get_pubsub_emulator_host(self) -> str:
return f"{self.get_container_host_ip()}:{self.get_exposed_port(self.port)}"

def _get_channel(self, channel: Optional[grpc.Channel] = None) -> grpc.Channel:
if channel is None:
return grpc.insecure_channel(target=self.get_pubsub_emulator_host())
return channel
def _get_client(self, cls: Type, **kwargs) -> dict:
with patch.dict(os.environ, PUBSUB_EMULATOR_HOST=self.get_pubsub_emulator_host()):
return cls(**kwargs)

def get_publisher_client(self, **kwargs) -> pubsub.PublisherClient:
kwargs['channel'] = self._get_channel(kwargs.get('channel'))
return pubsub.PublisherClient(**kwargs)
return self._get_client(pubsub.PublisherClient, **kwargs)

def get_subscriber_client(self, **kwargs) -> pubsub.SubscriberClient:
kwargs['channel'] = self._get_channel(kwargs.get('channel'))
return pubsub.SubscriberClient(**kwargs)
return self._get_client(pubsub.SubscriberClient, **kwargs)
7 changes: 4 additions & 3 deletions google/tests/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@


def test_pubsub_container():
pubsub: PubSubContainer
with PubSubContainer() as pubsub:
wait_for_logs(pubsub, r"Server started, listening on \d+", timeout=10)
wait_for_logs(pubsub, r"Server started, listening on \d+", timeout=60)
# Create a new topic
publisher = pubsub.get_publisher_client()
topic_path = publisher.topic_path(pubsub.project, "my-topic")
publisher.create_topic(topic_path)
publisher.create_topic(name=topic_path)

# Create a subscription
subscriber = pubsub.get_subscriber_client()
subscription_path = subscriber.subscription_path(pubsub.project,
"my-subscription")
subscriber.create_subscription(subscription_path, topic_path)
subscriber.create_subscription(name=subscription_path, topic=topic_path)

# Publish a message
publisher.publish(topic_path, b"Hello world!")
Expand Down
22 changes: 13 additions & 9 deletions requirements/3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,8 @@ charset-normalizer==3.1.0
# via requests
clickhouse-driver==0.2.5
# via testcontainers-clickhouse
# via -r requirements.in
coverage[toml]==7.2.3
# via
# pytest-cov
# via pytest-cov
cryptography==36.0.2
# via
# -r requirements.in
Expand Down Expand Up @@ -159,7 +157,7 @@ google-api-core[grpc]==2.11.0
# via google-cloud-pubsub
google-auth==2.17.2
# via google-api-core
google-cloud-pubsub==1.7.2
google-cloud-pubsub==2.16.0
# via testcontainers-gcp
googleapis-common-protos[grpc]==1.59.0
# via
Expand All @@ -173,11 +171,14 @@ grpc-google-iam-v1==0.12.6
grpcio==1.53.0
# via
# google-api-core
# google-cloud-pubsub
# googleapis-common-protos
# grpc-google-iam-v1
# grpcio-status
grpcio-status==1.48.2
# via google-api-core
grpcio-status==1.53.0
# via
# google-api-core
# google-cloud-pubsub
h11==0.14.0
# via wsproto
idna==3.4
Expand Down Expand Up @@ -226,7 +227,7 @@ opensearch-py==2.2.0
# via testcontainers-opensearch
outcome==1.2.0
# via trio
packaging==23.0
packaging==23.1
# via
# deprecation
# docker
Expand All @@ -242,13 +243,16 @@ pkginfo==1.9.6
# via twine
pluggy==1.0.0
# via pytest
protobuf==3.20.3
proto-plus==1.22.2
# via google-cloud-pubsub
protobuf==4.22.1
# via
# google-api-core
# google-cloud-pubsub
# googleapis-common-protos
# grpc-google-iam-v1
# grpcio-status
# proto-plus
psycopg2-binary==2.9.6
# via testcontainers-postgres
pyasn1==0.4.8
Expand Down Expand Up @@ -332,7 +336,7 @@ requests-toolbelt==0.10.1
# twine
rfc3986==2.0.0
# via twine
rich==13.3.3
rich==13.3.4
# via twine
rsa==4.9
# via
Expand Down
Loading