Skip to content

Commit

Permalink
Merge pull request #334 from mattbennett/retry-check-connections
Browse files Browse the repository at this point in the history
add retry to rabbit connection check
  • Loading branch information
davidszotten committed Aug 15, 2016
2 parents 204fe6e + 5f39de8 commit 13d0d0c
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions nameko/testing/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ def rabbit_manager(request):

@pytest.yield_fixture()
def rabbit_config(request, rabbit_manager):
import itertools
import random
import string
import time
from kombu import pools
from six.moves.urllib.parse import urlparse # pylint: disable=E0401
from nameko.testing.utils import get_rabbit_connections
Expand Down Expand Up @@ -123,8 +125,30 @@ def rabbit_config(request, rabbit_manager):

pools.reset() # close connections in pools

# raise a runtime error if the test leaves any connections lying around
try:
def retry(fn):
""" Barebones retry decorator
"""
def wrapper():
max_retries = 3
delay = 1
exceptions = RuntimeError

counter = itertools.count()
while True:
try:
return fn()
except exceptions:
if next(counter) == max_retries:
raise
time.sleep(delay)
return wrapper

@retry
def check_connections():
""" Raise a runtime error if the test leaves any connections open.
Allow a few retries because the rabbit api is eventually consistent.
"""
connections = get_rabbit_connections(conf['vhost'], rabbit_manager)
open_connections = [
conn for conn in connections if conn['state'] != "closed"
Expand All @@ -134,6 +158,8 @@ def rabbit_config(request, rabbit_manager):
names = ", ".join(conn['name'] for conn in open_connections)
raise RuntimeError(
"{} rabbit connection(s) left open: {}".format(count, names))
try:
check_connections()
finally:
if use_random_vost:
rabbit_manager.delete_vhost(vhost)
Expand Down

0 comments on commit 13d0d0c

Please sign in to comment.