Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt retry hook for flaky regression test cases. #535

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions regression/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# This assumes the command is being run via tox hence the
# repository root is the current directory.
from regression import populate_datastore
from regression.regression_utils import RetryTestsMetaclass


datastore._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID'
Expand All @@ -28,6 +29,8 @@

class TestDatastore(unittest2.TestCase):

__metaclass__ = RetryTestsMetaclass

def setUp(self):
self.case_entities_to_delete = []

Expand Down Expand Up @@ -77,7 +80,8 @@ def _get_post(self, id_or_name=None, post_content=None):

def _generic_test_post(self, name=None, key_id=None):
entity = self._get_post(id_or_name=(name or key_id))
datastore.put([entity])
with datastore.Transaction():
datastore.put([entity])

# Register entity to be deleted.
self.case_entities_to_delete.append(entity)
Expand Down Expand Up @@ -146,7 +150,8 @@ def test_save_key_self_reference(self):
entity['fullName'] = u'Full name'
entity['linkedTo'] = key # Self reference.

datastore.put([entity])
with datastore.Transaction():
datastore.put([entity])
self.case_entities_to_delete.append(entity)

query = datastore.Query(kind='Person')
Expand Down
34 changes: 34 additions & 0 deletions regression/regression_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
from __future__ import print_function
import os
import sys
import types

from gcloud import storage
from gcloud.storage.exceptions import NotFound


# From shell environ. May be None.
Expand All @@ -31,6 +33,38 @@
"""


class RetryTestsMetaclass(type):

NUM_RETRIES = 2
FLAKY_ERROR_CLASSES = (NotFound,)

@staticmethod
def _wrap_class_attr(class_attr):
if not (isinstance(class_attr, types.FunctionType) and
class_attr.__name__.startswith('test_')):
return class_attr

def retry_function(self):
num_attempts = 0
while num_attempts < RetryTestsMetaclass.NUM_RETRIES:
try:
return class_attr(self)
except RetryTestsMetaclass.FLAKY_ERROR_CLASSES:
num_attempts += 1
if num_attempts == RetryTestsMetaclass.NUM_RETRIES:
raise

return retry_function

def __new__(mcs, name, bases, attrs):
new_attrs = {}
for attr_name, value in attrs.items():
new_attrs[attr_name] = mcs._wrap_class_attr(value)

return super(RetryTestsMetaclass, mcs).__new__(
mcs, name, bases, new_attrs)


def get_environ(require_datastore=False, require_storage=False):
if require_datastore:
if DATASET_ID is None or not os.path.isfile(CREDENTIALS):
Expand Down
2 changes: 2 additions & 0 deletions regression/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def tearDownModule():

class TestStorage(unittest2.TestCase):

__metaclass__ = regression_utils.RetryTestsMetaclass

@classmethod
def setUpClass(cls):
cls.connection = regression_utils.get_storage_connection()
Expand Down