Python backoff.on_exception() Examples

The following are 30 code examples of backoff.on_exception(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module backoff , or try the search function .
Example #1
Source File: label_video_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def test_label_video(
        capsys, annotation_spec_set, instruction, dataset, cleaner):

    @backoff.on_exception(
        backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
    def run_sample():
        # Start labeling.
        return label_video.label_video(
            dataset.name, instruction.name, annotation_spec_set.name)

    response = run_sample()
    cleaner.append(response.operation.name)

    out, _ = capsys.readouterr()
    assert 'Label_video operation name: ' in out

    # Cancels the labeling operation.
    response.cancel()
    assert response.cancelled() is True 
Example #2
Source File: fhir_stores_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def crud_fhir_store_id():
    yield fhir_store_id

    # Clean up
    @backoff.on_exception(backoff.expo, HttpError, max_time=60)
    def clean_up():
        try:
            fhir_stores.delete_fhir_store(
                project_id, cloud_region, dataset_id, fhir_store_id
            )
        except HttpError as err:
            # The API returns 404 when the FHIR store doesn't exist.
            # The API returns 403 when the dataset doesn't exist, so
            # if we try to delete a FHIR store when the parent dataset
            # doesn't exist, the server will return a 403.
            if err.resp.status == 404 or err.resp.status == 403:
                print(
                    "Got exception {} while deleting FHIR store".format(err.resp.status)
                )
            else:
                raise

    clean_up() 
Example #3
Source File: dicom_stores_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def crud_dicom_store_id():
    yield dicom_store_id

    # Clean up
    @backoff.on_exception(backoff.expo, HttpError, max_time=60)
    def clean_up():
        try:
            dicom_stores.delete_dicom_store(
                project_id, cloud_region, dataset_id, dicom_store_id
            )
        except HttpError as err:
            # The API returns 404 when the DICOM store doesn't exist.
            # The API returns 403 when the dataset doesn't exist, so
            # if we try to delete a DICOM store when the parent dataset
            # doesn't exist, the server will return a 403.
            if err.resp.status == 404 or err.resp.status == 403:
                print(
                    "Got exception {} while deleting DICOM store".format(
                        err.resp.status
                    )
                )
            else:
                raise

    clean_up() 
Example #4
Source File: snippets_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def test_update_uptime_config(capsys):
    # create and delete happen in uptime fixture.
    new_display_name = random_name(10)
    new_uptime_check_path = '/' + random_name(10)
    with UptimeFixture() as fixture:

        # We sometimes see the permission error saying the resource
        # may not exist. Weirdly DeadlineExceeded instnace is raised
        # in this case.
        @backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=120)
        def call_sample():
            snippets.update_uptime_check_config(
                fixture.config.name, new_display_name, new_uptime_check_path)

        call_sample()

        out, _ = capsys.readouterr()
        snippets.get_uptime_check_config(fixture.config.name)
        out, _ = capsys.readouterr()
        assert new_display_name in out
        assert new_uptime_check_path in out 
Example #5
Source File: label_text_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def test_label_text(capsys, annotation_spec_set, instruction, dataset, cleaner):

    @backoff.on_exception(
        backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
    def run_sample():
        # Start labeling.
        return label_text.label_text(
            dataset.name, instruction.name, annotation_spec_set.name)

    response = run_sample()
    cleaner.append(response.operation.name)

    out, _ = capsys.readouterr()
    assert 'Label_text operation name: ' in out

    # Cancels the labeling operation.
    response.cancel()
    assert response.cancelled() is True 
Example #6
Source File: hl7v2_stores_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def crud_hl7v2_store_id():
    yield hl7v2_store_id

    # Clean up
    @backoff.on_exception(backoff.expo, HttpError, max_time=60)
    def clean_up():
        try:
            hl7v2_stores.delete_hl7v2_store(
                project_id, cloud_region, dataset_id, hl7v2_store_id
            )
        except HttpError as err:
            # The API returns 403 when the HL7v2 store doesn't exist.
            if err.resp.status == 404 or err.resp.status == 403:
                print(
                    "Got exception {} while deleting HL7v2 store".format(
                        err.resp.status
                    )
                )
            else:
                raise

    clean_up() 
Example #7
Source File: pubsub.py    From iris with MIT License 6 votes vote down vote up
def publish(client, body, topic):
    """Publish a message to a Pub/Sub topic."""
    project = 'projects/{}'.format(utils.get_project_id())
    dest_topic = project + '/topics/' + topic

    @backoff.on_exception(
        backoff.expo, HttpError, max_tries=3, giveup=utils.fatal_code)
    def _do_request():
        client.projects().topics().publish(
            topic=dest_topic, body=body).execute()

    try:
        _do_request()
    except HttpError as e:
        logging.error(e)
        raise PubSubException(e) 
Example #8
Source File: auto_complete_sample_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def test_auto_complete_sample(company_name, capsys):
    @backoff.on_exception(backoff.expo, AssertionError, max_time=120)
    def eventually_consistent_test():
        auto_complete_sample.run_sample(company_name)
        out, _ = capsys.readouterr()
        expected = (
            '.*completionResults.*'
            'suggestion.*Google.*type.*COMPANY_NAME.*\n'
            '.*completionResults.*'
            'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n'
            '.*completionResults.*'
            'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n'
        )
        assert re.search(expected, out)

    eventually_consistent_test() 
Example #9
Source File: translate_v3_list_glossary_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def glossary():
    """Get the ID of a glossary available to session (do not mutate/delete)."""
    glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
    translate_v3_create_glossary.create_glossary(
        PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
    )

    yield glossary_id

    # clean up
    @backoff.on_exception(
        backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
    )
    def delete_glossary():
        try:
            translate_v3_delete_glossary.delete_glossary(
                PROJECT_ID, glossary_id)
        except NotFound as e:
            # Ignoring this case.
            print("Got NotFound, detail: {}".format(str(e)))
    delete_glossary() 
Example #10
Source File: quickstart_searchallresources_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def test_search_all_resources(asset_dataset, capsys):
    scope = "projects/{}".format(PROJECT)
    query = "name:{}".format(DATASET)

    # Dataset creation takes some time to propagate, so the dataset is not
    # immediately searchable. Need some time before the snippet will pass.
    @backoff.on_exception(
        backoff.expo, (AssertionError), max_time=120
    )
    def eventually_consistent_test():
        quickstart_searchallresources.search_all_resources(scope, query=query)
        out, _ = capsys.readouterr()

        assert DATASET in out

    eventually_consistent_test() 
Example #11
Source File: pubsub.py    From iris with MIT License 6 votes vote down vote up
def pull(client, sub, endpoint):
    """Register a listener endpoint."""
    subscription = get_full_subscription_name(utils.get_project_id(), sub)
    body = {'pushConfig': {'pushEndpoint': endpoint}}

    @backoff.on_exception(
        backoff.expo, HttpError, max_tries=3, giveup=utils.fatal_code)
    def _do_request():
        client.projects().subscriptions().modifyPushConfig(
            subscription=subscription, body=body).execute()

    try:
        _do_request()
    except HttpError as e:

        logging.error(e)
        return 'Error', 500
    return 'ok, 204' 
Example #12
Source File: translate_v3_create_glossary_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def test_create_glossary(capsys):
    try:
        glossary_id = "test-{}".format(uuid.uuid4())
        translate_v3_create_glossary.create_glossary(
            PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
        )
        out, _ = capsys.readouterr()
        # assert
        assert "Created:" in out
        assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out
    finally:
        # cleanup
        @backoff.on_exception(
            backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
        )
        def delete_glossary():
            try:
                translate_v3_delete_glossary.delete_glossary(
                    PROJECT_ID, glossary_id)
            except NotFound as e:
                # Ignoring this case.
                print("Got NotFound, detail: {}".format(str(e)))
        delete_glossary() 
Example #13
Source File: location_search_sample_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def test_location_search_sample(company_name, capsys):
    @backoff.on_exception(backoff.expo, AssertionError, max_time=120)
    def eventually_consistent_test():
        location_search_sample.run_sample(company_name)
        out, _ = capsys.readouterr()
        expected = ('.*locationFilters.*\n'
                    '.*locationFilters.*\n'
                    '.*locationFilters.*\n'
                    '.*locationFilters.*\n'
                    '.*locationFilters.*\n')
        assert re.search(expected, out, re.DOTALL)
        expected = ('.*matchingJobs.*\n'
                    '.*matchingJobs.*\n'
                    '.*matchingJobs.*\n'
                    '.*matchingJobs.*\n'
                    '.*matchingJobs.*\n')
        assert re.search(expected, out, re.DOTALL)

    eventually_consistent_test() 
Example #14
Source File: translate_v3_get_glossary_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def glossary():
    """Get the ID of a glossary available to session (do not mutate/delete)."""
    glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
    translate_v3_create_glossary.create_glossary(
        PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
    )

    yield glossary_id

    # cleanup
    @backoff.on_exception(
        backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
    )
    def delete_glossary():
        try:
            translate_v3_delete_glossary.delete_glossary(
                PROJECT_ID, glossary_id)
        except NotFound as e:
            # Ignoring this case.
            print("Got NotFound, detail: {}".format(str(e)))
    delete_glossary() 
Example #15
Source File: translate_v3_batch_translate_text_with_glossary_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def glossary():
    """Get the ID of a glossary available to session (do not mutate/delete)."""
    glossary_id = "test-{}".format(uuid.uuid4())
    translate_v3_create_glossary.create_glossary(
        PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
    )

    yield glossary_id

    # cleanup
    @backoff.on_exception(
        backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
    )
    def delete_glossary():
        try:
            translate_v3_delete_glossary.delete_glossary(
                PROJECT_ID, glossary_id)
        except NotFound as e:
            # Ignoring this case.
            print("Got NotFound, detail: {}".format(str(e)))
    delete_glossary() 
Example #16
Source File: detect_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_detect_web_with_geo_uri(capsys):
    file_name = 'gs://{}/vision/web/city.jpg'.format(ASSET_BUCKET)

    @backoff.on_exception(
        backoff.expo, Exception, max_time=60, giveup=only_sample_error)
    def run_sample():
        detect.web_entities_include_geo_results_uri(file_name)

    run_sample()
    out, _ = capsys.readouterr()
    out = out.lower()
    assert 'description' in out 
Example #17
Source File: manage_dataset_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_delete_dataset(capsys, dataset):
    @backoff.on_exception(
        backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
    def run_sample():
        manage_dataset.delete_dataset(dataset.name)

    run_sample()
    out, _ = capsys.readouterr()
    assert "Dataset deleted." in out 
Example #18
Source File: quickstart_batchgetassetshistory_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_batch_get_assets_history(asset_bucket, capsys):
    bucket_asset_name = '//storage.googleapis.com/{}'.format(BUCKET)
    asset_names = [bucket_asset_name, ]

    @backoff.on_exception(
        backoff.expo, (AssertionError, InvalidArgument), max_time=30
    )
    def eventually_consistent_test():
        quickstart_batchgetassetshistory.batch_get_assets_history(
            PROJECT, asset_names)
        out, _ = capsys.readouterr()

        assert bucket_asset_name in out

    eventually_consistent_test() 
Example #19
Source File: fixtures.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_device_id(test_registry_id):
    device_id = device_id_template.format('RSA256')

    @backoff.on_exception(backoff.expo, HttpError, max_time=60)
    def create_device():
        try:
            manager.create_rs256_device(
                service_account_json, project_id, cloud_region, test_registry_id,
                device_id, rsa_cert_path)
        except AlreadyExists as e:
            # We ignore this case.
            print("The device already exists: detail: {}".format(str(e)))

    create_device()

    yield device_id

    @backoff.on_exception(backoff.expo, HttpError, max_time=60)
    def delete_device():
        try:
            manager.delete_device(
                service_account_json, project_id, cloud_region,
                test_registry_id, device_id)
        except NotFound as e:
            # We ignore this case.
            print("The device doesn't exist: detail: {}".format(str(e)))

    delete_device() 
Example #20
Source File: custom_attribute_sample_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_custom_attribute_sample(create_data, capsys):
    @backoff.on_exception(backoff.expo, AssertionError, max_time=120)
    def eventually_consistent_test():
        custom_attribute_sample.run_sample()
        out, _ = capsys.readouterr()
        expected = ('.*matchingJobs.*job_with_custom_attributes.*\n'
                    '.*matchingJobs.*job_with_custom_attributes.*\n'
                    '.*matchingJobs.*job_with_custom_attributes.*\n')
        assert re.search(expected, out, re.DOTALL)

    eventually_consistent_test() 
Example #21
Source File: email_alert_search_sample_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_email_alert_search_sample(company_name, capsys):

    @backoff.on_exception(retry_delay, AssertionError, max_time=300)
    def eventually_consistent_test():
        email_alert_search_sample.run_sample(company_name)
        out, _ = capsys.readouterr()
        expected = ('.*matchingJobs.*')
        assert re.search(expected, out)

    eventually_consistent_test() 
Example #22
Source File: commute_search_sample_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_commute_search_sample(company_name, capsys):
    @backoff.on_exception(backoff.expo, AssertionError, max_time=120)
    def eventually_consistent_test():
        commute_search_sample.run_sample(company_name)
        out, _ = capsys.readouterr()
        expected = ('.*matchingJobs.*1600 Amphitheatre Pkwy.*')
        assert re.search(expected, out)

    eventually_consistent_test() 
Example #23
Source File: histogram_sample_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_histogram_sample(company_name, capsys):
    @backoff.on_exception(backoff.expo, AssertionError, max_time=120)
    def eventually_consistent_test():
        histogram_sample.run_sample(company_name)
        out, _ = capsys.readouterr()
        assert re.search('COMPANY_ID', out)
        assert re.search('someFieldName1', out)

    eventually_consistent_test() 
Example #24
Source File: hl7v2_messages_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_CRUD_hl7v2_message(test_dataset, test_hl7v2_store, capsys):
    hl7v2_messages.create_hl7v2_message(
        project_id, cloud_region, dataset_id, hl7v2_store_id, hl7v2_message_file
    )

    @backoff.on_exception(backoff.expo, AssertionError, max_time=60)
    def run_eventually_consistent_test():
        hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages(
            project_id, cloud_region, dataset_id, hl7v2_store_id
        )

        assert len(hl7v2_messages_list) > 0
        hl7v2_message_name = hl7v2_messages_list[0].get("name")
        elms = hl7v2_message_name.split("/", 9)
        assert len(elms) >= 10
        hl7v2_message_id = elms[9]
        return hl7v2_message_id

    hl7v2_message_id = run_eventually_consistent_test()

    hl7v2_messages.get_hl7v2_message(
        project_id, cloud_region, dataset_id, hl7v2_store_id, hl7v2_message_id
    )

    hl7v2_messages.delete_hl7v2_message(
        project_id, cloud_region, dataset_id, hl7v2_store_id, hl7v2_message_id
    )

    out, _ = capsys.readouterr()

    # Check that create/get/list/delete worked
    assert "Created HL7v2 message" in out
    assert "Name" in out
    assert "Deleted HL7v2 message" in out 
Example #25
Source File: fhir_stores_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_dataset():
    @backoff.on_exception(backoff.expo, HttpError, max_time=60)
    def create():
        try:
            datasets.create_dataset(project_id, cloud_region, dataset_id)
        except HttpError as err:
            # We ignore 409 conflict here, because we know it's most
            # likely the first request failed on the client side, but
            # the creation suceeded on the server side.
            if err.resp.status == 409:
                print("Got exception {} while creating dataset".format(err.resp.status))
            else:
                raise

    create()

    yield

    # Clean up
    @backoff.on_exception(backoff.expo, HttpError, max_time=60)
    def clean_up():
        try:
            datasets.delete_dataset(project_id, cloud_region, dataset_id)
        except HttpError as err:
            # The API returns 403 when the dataset doesn't exist.
            if err.resp.status == 404 or err.resp.status == 403:
                print("Got exception {} while deleting dataset".format(err.resp.status))
            else:
                raise

    clean_up() 
Example #26
Source File: fhir_resources_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_patient():
    patient_response = fhir_resources.create_patient(
        base_url, project_id, cloud_region, dataset_id, fhir_store_id,
    )
    patient_resource = patient_response.json()
    patient_resource_id = patient_resource["id"]

    yield patient_resource_id

    @backoff.on_exception(backoff.expo, HttpError, max_time=60)
    # Clean up
    def clean_up():
        try:
            fhir_resources.delete_resource(
                base_url,
                project_id,
                cloud_region,
                dataset_id,
                fhir_store_id,
                resource_type,
                patient_resource_id,
            )

        except HttpError as err:
            # The API returns 200 whether the resource exists or was
            # successfully deleted or not.
            if err.resp.status > 200:
                print(
                    "Got exception {} while deleting FHIR store".format(err.resp.status)
                )
            else:
                raise

    clean_up() 
Example #27
Source File: manage_dataset_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_create_dataset(cleaner, capsys):

    @backoff.on_exception(
        backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
    def run_sample():
        return manage_dataset.create_dataset(PROJECT_ID)

    response = run_sample()
    cleaner.append(response.name)

    out, _ = capsys.readouterr()
    assert "The dataset resource name:" in out 
Example #28
Source File: create_annotation_spec_set_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_create_annotation_spec_set(cleaner, capsys):

    @backoff.on_exception(
        backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
    def run_sample():
        return create_annotation_spec_set.create_annotation_spec_set(PROJECT_ID)

    response = run_sample()

    # For cleanup
    cleaner.append(response.name)

    out, _ = capsys.readouterr()
    assert 'The annotation_spec_set resource name:' in out 
Example #29
Source File: manage_dataset_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_get_dataset(capsys, dataset):
    @backoff.on_exception(
        backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
    def run_sample():
        manage_dataset.get_dataset(dataset.name)

    run_sample()
    out, _ = capsys.readouterr()
    assert "The dataset resource name:" in out 
Example #30
Source File: create_instruction_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_create_instruction(cleaner, capsys):

    @backoff.on_exception(
        backoff.expo, ServerError, max_time=testing_lib.RETRY_DEADLINE)
    def run_sample():
        return create_instruction.create_instruction(
            PROJECT_ID, 'IMAGE', INSTRUCTION_GCS_URI)

    instruction = run_sample()
    cleaner.append(instruction.name)

    out, _ = capsys.readouterr()
    assert 'The instruction resource name: ' in out