2424import random
2525import time
2626
27- from gcp_devrel .testing import eventually_consistent
28- from flaky import flaky
27+ import backoff
2928import googleapiclient .discovery
3029import pytest
3130
3534from custom_metric import read_timeseries
3635from custom_metric import write_timeseries_value
3736
37+
3838PROJECT = os .environ ['GCLOUD_PROJECT' ]
3939
4040""" Custom metric domain for all custom metrics"""
@@ -52,7 +52,7 @@ def client():
5252 return googleapiclient .discovery .build ('monitoring' , 'v3' )
5353
5454
55- @flaky
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
@@ -64,29 +64,35 @@ def test_custom_metric(client):
6464 INSTANCE_ID = "test_instance"
6565 METRIC_KIND = "GAUGE"
6666
67- custom_metric_descriptor = create_custom_metric (
68- client , PROJECT_RESOURCE , METRIC_RESOURCE , METRIC_KIND )
69-
70- # wait until metric has been created, use the get call to wait until
71- # a response comes back with the new metric
72- custom_metric = None
73- while not custom_metric :
74- time .sleep (1 )
75- custom_metric = get_custom_metric (
76- client , PROJECT_RESOURCE , METRIC_RESOURCE )
77-
78- write_timeseries_value (client , PROJECT_RESOURCE ,
79- METRIC_RESOURCE , INSTANCE_ID ,
80- METRIC_KIND )
81-
82- # Sometimes on new metric descriptors, writes have a delay in being
83- # read back. Use eventually_consistent to account for this.
84- @eventually_consistent .call
85- def _ ():
86- response = read_timeseries (client , PROJECT_RESOURCE , METRIC_RESOURCE )
87- value = int (
88- response ['timeSeries' ][0 ]['points' ][0 ]['value' ]['int64Value' ])
89- # using seed of 1 will create a value of 1
90- assert value == pseudo_random_value
91-
92- delete_metric_descriptor (client , custom_metric_descriptor ['name' ])
67+ try :
68+ custom_metric_descriptor = create_custom_metric (
69+ client , PROJECT_RESOURCE , METRIC_RESOURCE , METRIC_KIND )
70+
71+ # wait until metric has been created, use the get call to wait until
72+ # a response comes back with the new metric
73+ custom_metric = None
74+ while not custom_metric :
75+ time .sleep (1 )
76+ custom_metric = get_custom_metric (
77+ client , PROJECT_RESOURCE , METRIC_RESOURCE )
78+
79+ write_timeseries_value (client , PROJECT_RESOURCE ,
80+ METRIC_RESOURCE , INSTANCE_ID ,
81+ METRIC_KIND )
82+
83+ # Sometimes on new metric descriptors, writes have a delay in being
84+ # read back. Use eventually_consistent to account for this.
85+ @backoff .on_exception (backoff .expo , AssertionError , max_time = 120 )
86+ def eventually_consistent_test ():
87+ response = read_timeseries (
88+ client , PROJECT_RESOURCE , METRIC_RESOURCE )
89+ value = int (
90+ response ['timeSeries' ][0 ]['points' ][0 ]['value' ]['int64Value' ])
91+ # using seed of 1 will create a value of 1
92+ assert value == pseudo_random_value
93+
94+ eventually_consistent_test ()
95+
96+ finally :
97+ # cleanup
98+ delete_metric_descriptor (client , custom_metric_descriptor ['name' ])
0 commit comments