Content-Length: 417140 | pFad | http://github.com/dhermes/google-cloud-python/commit/012fd2c43ea2a1c6eb5dfb3327f192d24d33735a

98 Attempt retry hook for flaky regression test cases. · dhermes/google-cloud-python@012fd2c · GitHub
Skip to content

Commit 012fd2c

Browse files
committed
Attempt retry hook for flaky regression test cases.
Fixes googleapis#531. To "test" that this works, feel free to add a test case like: x = 0 def test_retry(self): # Feel free to vary 3 higher and higher, should always be # NUM_RETRIES in the final error message. if self.x < 3: self.x += 1 self.assertEqual(self.x, object()) # Fails else: self.assertTrue(True)
1 parent edfd5e2 commit 012fd2c

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

regression/datastore.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# This assumes the command is being run via tox hence the
2121
# repository root is the current directory.
2222
from regression import populate_datastore
23+
from regression.regression_utils import RetryTestsMetaclass
2324

2425

2526
datastore._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID'
@@ -28,6 +29,8 @@
2829

2930
class TestDatastore(unittest2.TestCase):
3031

32+
__metaclass__ = RetryTestsMetaclass
33+
3134
def setUp(self):
3235
self.case_entities_to_delete = []
3336

regression/regression_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from __future__ import print_function
1616
import os
1717
import sys
18+
import types
1819

1920
from gcloud import storage
2021

@@ -31,6 +32,38 @@
3132
"""
3233

3334

35+
class RetryTestsMetaclass(type):
36+
37+
NUM_RETRIES = 2
38+
FLAKY_ERROR_CLASSES = (AssertionError,)
39+
40+
@staticmethod
41+
def _wrap_class_attr(class_attr):
42+
if not (isinstance(class_attr, types.FunctionType) and
43+
class_attr.__name__.startswith('test_')):
44+
return class_attr
45+
46+
def retry_function(self):
47+
num_attempts = 0
48+
while num_attempts < RetryTestsMetaclass.NUM_RETRIES:
49+
try:
50+
return class_attr(self)
51+
except RetryTestsMetaclass.FLAKY_ERROR_CLASSES:
52+
num_attempts += 1
53+
if num_attempts == RetryTestsMetaclass.NUM_RETRIES:
54+
raise
55+
56+
return retry_function
57+
58+
def __new__(mcs, name, bases, attrs):
59+
new_attrs = {}
60+
for attr_name, value in attrs.items():
61+
new_attrs[attr_name] = mcs._wrap_class_attr(value)
62+
63+
return super(RetryTestsMetaclass, mcs).__new__(
64+
mcs, name, bases, new_attrs)
65+
66+
3467
def get_environ(require_datastore=False, require_storage=False):
3568
if require_datastore:
3669
if DATASET_ID is None or not os.path.isfile(CREDENTIALS):

regression/storage.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def tearDownModule():
4747

4848
class TestStorage(unittest2.TestCase):
4949

50+
__metaclass__ = regression_utils.RetryTestsMetaclass
51+
5052
@classmethod
5153
def setUpClass(cls):
5254
cls.connection = regression_utils.get_storage_connection()

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/dhermes/google-cloud-python/commit/012fd2c43ea2a1c6eb5dfb3327f192d24d33735a

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy