@@ -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' )
7580def 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
158154def _remove_job (hsproject , jobkey ):
0 commit comments