Skip to content

Commit

Permalink
Merge pull request #1114 from newrelic/mute-flakey-tests
Browse files Browse the repository at this point in the history
Retry flakey tests
  • Loading branch information
hmstepanek committed Mar 27, 2024
2 parents f233c6f + 2323a71 commit 43e5e25
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
3 changes: 3 additions & 0 deletions tests/agent_features/test_priority_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
override_application_settings,
reset_core_stats_engine,
)
from testing_support.util import retry

from newrelic.api.application import application_instance as application
from newrelic.api.background_task import BackgroundTask
Expand All @@ -29,6 +30,7 @@ def test_priority_used_in_transaction_events(first_transaction_saved):
first_priority = 1 if first_transaction_saved else 0
second_priority = 0 if first_transaction_saved else 1

@retry(attempts=5, wait=2) # This test is flakey so add a retry.
@reset_core_stats_engine()
def _test():
# Stats engine
Expand Down Expand Up @@ -61,6 +63,7 @@ def test_priority_used_in_transaction_error_events(first_transaction_saved):
first_priority = 1 if first_transaction_saved else 0
second_priority = 0 if first_transaction_saved else 1

@retry(attempts=5, wait=2) # This test is flakey so add a retry.
@reset_core_stats_engine()
def _test():
with BackgroundTask(application(), name="T1") as txn:
Expand Down
6 changes: 3 additions & 3 deletions tests/agent_streaming/test_infinite_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import pytest
from testing_support.fixtures import override_generic_settings
from testing_support.util import conditional_decorator
from testing_support.util import conditional_decorator, retry
from testing_support.validators.validate_metric_payload import validate_metric_payload

from newrelic.common.streaming_utils import StreamBuffer
Expand Down Expand Up @@ -281,9 +281,8 @@ def condition(*args, **kwargs):
_create_channel = StreamingRpc.create_channel

def create_channel(self, *args, **kwargs):
ret = _create_channel(self, *args, **kwargs)
_create_channel(self, *args, **kwargs)
connect_event.set()
return ret

monkeypatch.setattr(StreamingRpc, "condition", condition)
monkeypatch.setattr(StreamingRpc, "create_channel", create_channel)
Expand Down Expand Up @@ -356,6 +355,7 @@ def test_no_data_loss_on_reconnect(mock_grpc_server, app, buffer_empty_event, ba

span = Span(intrinsics={}, agent_attributes={}, user_attributes={})

@retry(attempts=5, wait=2) # This test is flakey so add a retry.
@override_generic_settings(
settings,
{
Expand Down
35 changes: 31 additions & 4 deletions tests/testing_support/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

import re
import socket
import time
from functools import wraps


def _to_int(version_str):
m = re.match(r'\d+', version_str)
m = re.match(r"\d+", version_str)
return int(m.group(0)) if m else 0


def version2tuple(version_str):
"""Convert version, even if it contains non-numeric chars.
Expand All @@ -27,32 +31,55 @@ def version2tuple(version_str):
"""

parts = version_str.split('.')[:2]
parts = version_str.split(".")[:2]
return tuple(map(_to_int, parts))


def instance_hostname(hostname):
if hostname == 'localhost' or hostname == "127.0.0.1":
if hostname in ["localhost", "127.0.0.1"]:
hostname = socket.gethostname()
return hostname


def get_open_port():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
port = s.getsockname()[1]
s.close()
return port


def conditional_decorator(condition, decorator):
"""Applies a decorator if the condition is true. Accepts 0 argument callables for the condition."""

def _conditional_decorator(func):
if callable(condition):
condition_eval = condition()
else:
condition_eval = condition

if condition_eval:
return decorator(func)
else:
return func

return _conditional_decorator


def retry(attempts=5, wait=5):
def decorator(test_func):
@wraps(test_func)
def wrapper(*args, **kwargs):
retry_count = 1
while retry_count < attempts:
try:
return test_func(*args, **kwargs)
except AssertionError as assert_error:
time.sleep(wait)
retry_count += 1
# Preserve original traceback in case assertion fails.
return test_func(*args, **kwargs)

return wrapper

return decorator

0 comments on commit 43e5e25

Please sign in to comment.