|
20 | 20 | import backoff |
21 | 21 | from googleapiclient.errors import HttpError |
22 | 22 | from google.cloud import pubsub |
| 23 | +from google.api_core.exceptions import AlreadyExists |
23 | 24 | from google.api_core.exceptions import NotFound |
24 | 25 | import pytest |
25 | 26 |
|
26 | 27 | # 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 |
28 | 29 | import cloudiot_mqtt_example |
29 | 30 | import manager |
30 | 31 |
|
|
34 | 35 | ca_cert_path = 'resources/roots.pem' |
35 | 36 | rsa_cert_path = 'resources/rsa_cert.pem' |
36 | 37 | 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()) |
38 | 39 |
|
39 | 40 | project_id = os.environ['GCLOUD_PROJECT'] |
40 | 41 | service_account_json = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] |
41 | 42 |
|
42 | 43 | pubsub_topic = 'projects/{}/topics/{}'.format(project_id, topic_id) |
43 | 44 |
|
44 | 45 | # 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())) |
46 | 47 |
|
47 | 48 | mqtt_bridge_hostname = 'mqtt.googleapis.com' |
48 | 49 | mqtt_bridge_port = 443 |
49 | 50 |
|
50 | 51 |
|
51 | 52 | @pytest.fixture(scope='module') |
52 | 53 | 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() |
54 | 63 |
|
55 | 64 | yield topic |
56 | 65 |
|
57 | 66 | pubsub_client = pubsub.PublisherClient() |
58 | 67 | 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() |
60 | 77 |
|
61 | 78 |
|
62 | 79 | @pytest.fixture(scope='module') |
|
0 commit comments