Skip to content

Commit c0f439f

Browse files
committed
Fixes flacky tests
1 parent 3e3a38c commit c0f439f

File tree

9 files changed

+54
-22
lines changed

9 files changed

+54
-22
lines changed

algoliasearch/http/hosts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from random import shuffle
22

3-
from typing import List
3+
from typing import List, Optional
44

55

66
class Host(object):
@@ -41,12 +41,12 @@ def __init__(self, hosts):
4141
reverse=True)
4242

4343
def read(self):
44-
# type: () -> HostsCollection
44+
# type: () -> List[Host]
4545

4646
return [host for host in self._hosts if host.accept & CallType.READ]
4747

4848
def write(self):
49-
# type: () -> HostsCollection
49+
# type: () -> List[Host]
5050

5151
return [host for host in self._hosts if host.accept & CallType.WRITE]
5252

algoliasearch/http/transporter.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import time
22

3-
from typing import Optional, Union, Dict, Any
4-
3+
from typing import Optional, Union, Dict, Any, List
54
from algoliasearch.exceptions import (
65
AlgoliaUnreachableHostException,
76
RequestException
@@ -56,7 +55,7 @@ def read(self, verb, path, data, request_options):
5655
return self.request(verb, hosts, path, data, request_options, timeout)
5756

5857
def request(self, verb, hosts, path, data, request_options, timeout):
59-
# type: (str, list, int, str, Optional[Union[dict, list]], RequestOptions, int) -> dict # noqa: E501
58+
# type: (str, List[Host], str, Optional[Union[dict, list]], RequestOptions, int) -> dict # noqa: E501
6059

6160
if isinstance(data, dict):
6261
data.update(request_options.data)
@@ -76,7 +75,7 @@ def request(self, verb, hosts, path, data, request_options, timeout):
7675
return self.retry(hosts, request, relative_url)
7776

7877
def retry(self, hosts, request, relative_url):
79-
# type: (list, Request, str) -> dict
78+
# type: (List[Host], Request, str) -> dict
8079

8180
for host in self._retry_strategy.valid_hosts(hosts):
8281

tests/features/http/test_requester.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ def test_http_error_exception(self):
2020

2121
exception = cm.exception
2222
self.assertEqual(exception.status_code, 400)
23-
self.assertEqual(str(exception),
24-
'Invalid object attributes: zadaz near line:1 column:8')
23+
self.assertEqual(
24+
str(exception),
25+
'Invalid object attributes: zadaz near line:1 column:8'
26+
)

tests/features/test_analytics_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
from algoliasearch.exceptions import RequestException
77
from tests.helpers.factory import Factory as F
8+
from tests.helpers.misc import RetryableClient
89

910

1011
class TestAnalyticsClient(unittest.TestCase):
1112
def setUp(self):
12-
self.client = F.analytics_client()
13+
self.client = RetryableClient(F.analytics_client())
1314
self.index = F.index(self._testMethodName)
1415
self.index2 = F.index('{}2'.format(self._testMethodName))
1516

tests/features/test_search_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ def test_mcm(self):
143143

144144
mcm.assign_user_id(user_id, clusters[0]['clusterName'])
145145

146-
self.assertEqual(
147-
mcm.search_user_ids(user_id)['hits'][0]['userID'],
148-
user_id
146+
users_ids = [user['userID'] for user in
147+
mcm.search_user_ids(user_id)['hits']]
148+
149+
self.assertIn(
150+
user_id,
151+
users_ids
149152
)
150153

151154
users = mcm.list_user_ids()

tests/helpers/factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def rule(data=None, object_id=True):
170170
def decide(client):
171171

172172
if os.environ.get('TEST_TYPE', False) == 'async':
173-
from tests.fixtures.sync_decorator import SyncDecorator
173+
from tests.helpers.misc_async import SyncDecorator
174174

175175
return SyncDecorator(client)
176176

tests/helpers/misc.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import time
2+
3+
from algoliasearch.exceptions import RequestException
4+
5+
6+
class RetryableClient:
7+
8+
def __init__(self, base):
9+
10+
self._base = base
11+
12+
def __getattr__(self, name):
13+
14+
method = getattr(self._base, name)
15+
16+
def closure(*args, **kwargs):
17+
result = None
18+
19+
while result is None:
20+
21+
try:
22+
result = method(*args, **kwargs)
23+
except RequestException as e:
24+
if str(e) != 'Too Many Requests':
25+
raise e
26+
time.sleep(1)
27+
28+
return result
29+
30+
return closure
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
2+
import time
23
import types
34

5+
from algoliasearch.exceptions import RequestException
46
from algoliasearch.iterators import Iterator
57

68

@@ -15,9 +17,11 @@ def __init__(self, base):
1517
self._base = base
1618

1719
def close(self):
20+
1821
return self._base.close()
1922

2023
def __getattr__(self, name):
24+
2125
method = getattr(self._base, name)
2226

2327
if not callable(method):
@@ -57,6 +61,7 @@ def user(self, user_token):
5761
def iterator_to_array(self, iterator):
5862

5963
def closure():
64+
6065
objects = []
6166

6267
while True:

tox.ini

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ envlist =
66
py{34,35,36,37}-async
77
lint
88
type
9-
coverage
109

1110
[testenv]
1211
whitelist_externals = /bin/sh
@@ -28,10 +27,3 @@ description = static analysis checker
2827
basepython = python3.7
2928
commands = mypy --config-file mypy.ini -p algoliasearch --disallow-untyped-calls
3029
deps = -rrequirements/requirements-static-type-checker.txt
31-
32-
[testenv:coverage]
33-
whitelist_externals = /bin/sh
34-
description = coverage checker
35-
basepython = python3.7
36-
commands = coverage: sh tests/run_tests_coverage.sh
37-
deps = -rrequirements/requirements-testing.txt

0 commit comments

Comments
 (0)