2727import backoff
2828import googleapiclient .discovery
2929import pytest
30+ from googleapiclient .errors import HttpError
3031
3132from custom_metric import create_custom_metric
3233from custom_metric import delete_metric_descriptor
@@ -52,7 +53,6 @@ def client():
5253 return googleapiclient .discovery .build ('monitoring' , 'v3' )
5354
5455
55- @pytest .mark .flaky
5656def test_custom_metric (client ):
5757 PROJECT_RESOURCE = "projects/{}" .format (PROJECT )
5858 # Use a constant seed so psuedo random number is known ahead of time
@@ -69,23 +69,30 @@ def test_custom_metric(client):
6969 client , PROJECT_RESOURCE , METRIC_RESOURCE , METRIC_KIND )
7070
7171 # wait until metric has been created, use the get call to wait until
72- # a response comes back with the new metric
72+ # a response comes back with the new metric with 10 retries.
7373 custom_metric = None
74- while not custom_metric :
74+ retry_count = 0
75+ while not custom_metric and retry_count < 10 :
7576 time .sleep (1 )
77+ retry_count += 1
7678 custom_metric = get_custom_metric (
7779 client , PROJECT_RESOURCE , METRIC_RESOURCE )
80+ # Make sure we get the custom metric
81+ assert custom_metric
7882
7983 write_timeseries_value (client , PROJECT_RESOURCE ,
8084 METRIC_RESOURCE , INSTANCE_ID ,
8185 METRIC_KIND )
8286
8387 # Sometimes on new metric descriptors, writes have a delay in being
8488 # read back. Use eventually_consistent to account for this.
85- @backoff .on_exception (backoff .expo , AssertionError , max_time = 120 )
89+ @backoff .on_exception (
90+ backoff .expo , (AssertionError , HttpError ), max_time = 120 )
8691 def eventually_consistent_test ():
8792 response = read_timeseries (
8893 client , PROJECT_RESOURCE , METRIC_RESOURCE )
94+ # Make sure the value is not empty.
95+ assert 'timeSeries' in response
8996 value = int (
9097 response ['timeSeries' ][0 ]['points' ][0 ]['value' ]['int64Value' ])
9198 # using seed of 1 will create a value of 1
0 commit comments