Skip to content

Commit fc28d8a

Browse files
committed
Rm some redundant clean procedures, fix tests
1 parent d6d98d2 commit fc28d8a

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

tests/hubstorage/conftest.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def pytest_configure(config):
7171
my_vcr.before_record_request = lambda request: None
7272

7373

74+
def is_using_real_services(request):
75+
return (request.config.option.update_cassettes or
76+
request.config.option.ignore_cassettes)
77+
78+
7479
@pytest.fixture(scope='session')
7580
def hsclient():
7681
return HubstorageClient(auth=TEST_AUTH, endpoint=TEST_ENDPOINT)
@@ -87,23 +92,20 @@ def hsspiderid(hsproject):
8792
return str(hsproject.ids.spider(TEST_SPIDER_NAME, create=1))
8893

8994

90-
@my_vcr.use_cassette()
9195
@pytest.fixture(scope='session')
92-
def hscollection(hsproject):
96+
def hscollection(hsproject, request):
9397
collection = get_test_collection(hsproject)
94-
clean_collection(collection)
98+
if is_using_real_services(request):
99+
clean_collection(collection)
95100
yield collection
96-
clean_collection(collection)
97101

98102

99-
@my_vcr.use_cassette()
100103
@pytest.fixture(autouse=True, scope='session')
101-
def setup_session(hsclient, hsproject, hscollection):
102-
set_testbotgroup(hsproject)
103-
remove_all_jobs(hsproject)
104+
def setup_session(hsclient, hsproject, hscollection, request):
105+
if is_using_real_services(request):
106+
set_testbotgroup(hsproject)
107+
remove_all_jobs(hsproject)
104108
yield
105-
remove_all_jobs(hsproject)
106-
unset_testbotgroup(hsproject)
107109
hsclient.close()
108110

109111

@@ -116,10 +118,7 @@ def setup_vcrpy(request, hsproject):
116118
request.function.__module__.split('.')[-1],
117119
request.function.__name__
118120
)
119-
# we should clean jobs only when working with real services
120-
# doesn't make sense to clean jobs when working with cassettes
121-
if (request.config.option.update_cassettes or
122-
request.config.option.ignore_cassettes):
121+
if is_using_real_services(request):
123122
remove_all_jobs(hsproject)
124123
with my_vcr.use_cassette(cassette_name):
125124
yield
@@ -145,14 +144,11 @@ def remove_all_jobs(hsproject):
145144
del hsproject.settings[k]
146145
hsproject.settings.save()
147146

148-
# Cleanup JobQ
149-
jobq = hsproject.jobq
150-
for queuename in ('pending', 'running', 'finished'):
151-
info = {'summary': [None]} # poor-guy do...while
152-
while info['summary']:
153-
info = jobq.summary(queuename)
154-
for summary in info['summary']:
155-
_remove_job(hsproject, summary['key'])
147+
# Cleanup JobQ: run 2 times to ensure we covered all jobs
148+
for queuename in ('pending', 'running', 'finished')*2:
149+
info = hsproject.jobq.summary(queuename)
150+
for summary in info['summary']:
151+
_remove_job(hsproject, summary['key'])
156152

157153

158154
def _remove_job(hsproject, jobkey):

tests/hubstorage/test_project.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .conftest import TEST_PROJECT_ID, TEST_SPIDER_NAME
1313
from .conftest import hsspiderid
1414
from .conftest import start_job
15-
from .conftest import set_testbotgroup
15+
from .conftest import set_testbotgroup, unset_testbotgroup
1616
from .testutil import failing_downloader
1717

1818

@@ -157,14 +157,14 @@ def test_broad(hsproject, hsspiderid):
157157

158158

159159
@pytest.fixture
160-
def restore_botgroup(hsproject):
160+
def unset_botgroup(hsproject):
161+
unset_testbotgroup(hsproject)
161162
yield
162163
set_testbotgroup(hsproject)
163164

164165

165-
def test_settings(hsproject, restore_botgroup):
166+
def test_settings(hsproject, unset_botgroup):
166167
settings = dict(hsproject.settings)
167-
settings.pop('botgroups', None) # ignore testsuite botgroups
168168
assert settings == {}
169169
# use some fixed timestamp to represent current time
170170
hsproject.settings['created'] = created = 1476803148638

tests/hubstorage/test_retry.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import re
66

7+
from mock import patch
78
import responses
89
from requests import HTTPError, ConnectionError
910
from scrapinghub import HubstorageClient
@@ -18,13 +19,25 @@
1819
DELETE = responses.DELETE
1920

2021

22+
@patch.multiple('scrapinghub.HubstorageClient',
23+
RETRY_DEFAULT_JITTER_MS=1,
24+
RETRY_DEFAULT_EXPONENTIAL_BACKOFF_MS=1)
2125
def hsclient_with_retries(max_retries=3, max_retry_time=1):
2226
return HubstorageClient(
2327
auth=TEST_AUTH, endpoint=TEST_ENDPOINT,
2428
max_retries=max_retries, max_retry_time=max_retry_time,
2529
)
2630

2731

32+
@patch('scrapinghub.hubstorage.client.Retrying')
33+
def test_hsclient_with_retries_no_wait(retrying_class):
34+
hsclient_with_retries()
35+
assert retrying_class.call_count == 1
36+
call = retrying_class.call_args_list[0]
37+
assert call[1]['wait_jitter_max'] == 1
38+
assert int(call[1]['wait_exponential_multiplier']) == 33
39+
40+
2841
def mock_api(method=GET, callback=None, url_match='/.*'):
2942
"""
3043
Mock an API URL using the responses library.

0 commit comments

Comments
 (0)