Skip to content

Commit f2c71b3

Browse files
author
Takashi Matsuo
committed
avoid topic name conflict
1 parent 3567574 commit f2c71b3

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

iot/api-client/mqtt_example/cloudiot_mqtt_example_test.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
import backoff
2121
from googleapiclient.errors import HttpError
2222
from google.cloud import pubsub
23+
from google.api_core.exceptions import AlreadyExists
2324
from google.api_core.exceptions import NotFound
2425
import pytest
2526

2627
# Add manager for bootstrapping device registry / device for testing
27-
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'manager')) # noqa
28+
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'manager')) # noqa
2829
import cloudiot_mqtt_example
2930
import manager
3031

@@ -34,29 +35,45 @@
3435
ca_cert_path = 'resources/roots.pem'
3536
rsa_cert_path = 'resources/rsa_cert.pem'
3637
rsa_private_path = 'resources/rsa_private.pem'
37-
topic_id = 'test-device-events-{}'.format(int(time.time()))
38+
topic_id = 'test-device-events-{}'.format(uuid.uuid4())
3839

3940
project_id = os.environ['GCLOUD_PROJECT']
4041
service_account_json = os.environ['GOOGLE_APPLICATION_CREDENTIALS']
4142

4243
pubsub_topic = 'projects/{}/topics/{}'.format(project_id, topic_id)
4344

4445
# This format is used in the `../manager.py::clean_up_registries()`.
45-
registry_id = 'test-registry-{}-{}'.format(uuid.uuid1(), int(time.time()))
46+
registry_id = 'test-registry-{}-{}'.format(uuid.uuid4(), int(time.time()))
4647

4748
mqtt_bridge_hostname = 'mqtt.googleapis.com'
4849
mqtt_bridge_port = 443
4950

5051

5152
@pytest.fixture(scope='module')
5253
def test_topic():
53-
topic = manager.create_iot_topic(project_id, topic_id)
54+
@backoff.on_exception(backoff.expo, HttpError, max_time=60)
55+
def create_topic():
56+
try:
57+
return manager.create_iot_topic(project_id, topic_id)
58+
except AlreadyExists as e:
59+
# We ignore this case.
60+
print("The topic already exists, detail: {}".format(str(e)))
61+
62+
topic = create_topic()
5463

5564
yield topic
5665

5766
pubsub_client = pubsub.PublisherClient()
5867
topic_path = pubsub_client.topic_path(project_id, topic_id)
59-
pubsub_client.delete_topic(topic_path)
68+
@backoff.on_exception(backoff.expo, HttpError, max_time=60)
69+
def delete_topic():
70+
try:
71+
pubsub_client.delete_topic(topic_path)
72+
except NotFound as e:
73+
# We ignore this case.
74+
print("The topic doesn't exist: detail: {}".format(str(e)))
75+
76+
delete_topic()
6077

6178

6279
@pytest.fixture(scope='module')

0 commit comments

Comments
 (0)